pay_page.dart 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'dart:math' as math;
  5. import 'package:bbyyy/beans/alipay_result_bean_entity.dart';
  6. import 'package:bbyyy/beans/my_coupon_bean_entity.dart';
  7. import 'package:bbyyy/beans/pay_by_alipay_bean_entity.dart';
  8. import 'package:bbyyy/beans/pay_by_we_chat_bean_entity.dart';
  9. import 'package:bbyyy/beans/smart_order_bean_entity.dart';
  10. import 'package:bbyyy/beans/user_balance_entity.dart';
  11. import 'package:bbyyy/https/MyDio.dart';
  12. import 'package:bbyyy/https/my_request.dart';
  13. import 'package:bbyyy/https/url.dart';
  14. import 'package:bbyyy/my_tools/const.dart';
  15. import 'package:bbyyy/my_tools/dims.dart';
  16. import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
  17. import 'package:bbyyy/my_tools/event_bus.dart';
  18. import 'package:bbyyy/my_tools/my_colors.dart';
  19. import 'package:bbyyy/my_tools/my_tools.dart';
  20. import 'package:bbyyy/my_tools/my_views.dart';
  21. import 'package:bbyyy/paegs/mine_page/wallet_page/wallet_page.dart';
  22. import 'package:bbyyy/paegs/root_page/root_page.dart';
  23. import 'package:bbyyy/paegs/root_page/root_page_view.dart';
  24. import 'package:bbyyy/pay/pay_tools.dart';
  25. import 'package:flustars/flustars.dart';
  26. import 'package:flutter/cupertino.dart';
  27. import 'package:flutter/material.dart';
  28. import 'package:flutter_svg/flutter_svg.dart';
  29. import 'package:fluwx/fluwx.dart';
  30. import 'package:tobias/tobias.dart';
  31. class PayPage extends StatefulWidget {
  32. int orderUID;
  33. bool cancelOrder;
  34. double amount;
  35. int payWay;
  36. MyCouponBeanDataData couponData;
  37. PayPage(this.orderUID, this.cancelOrder, this.amount, this.payWay,
  38. this.couponData);
  39. @override
  40. _PayPageState createState() => _PayPageState();
  41. }
  42. class _PayPageState extends State<PayPage> with WidgetsBindingObserver {
  43. var titles = ['正在支付', '支付成功', '支付失败'];
  44. var icons = ['images/svg/等待.svg', 'images/svg/成功.svg', 'images/svg/失败.svg'];
  45. var content = ['正在支付,请稍后…', '支付成功,即将自动跳转…', '支付失败'];
  46. int status = 0;
  47. SmartOrderBeanDataData order;
  48. @override
  49. void initState() {
  50. super.initState();
  51. WidgetsBinding.instance.addObserver(this);
  52. Future.delayed(Duration(seconds: 1), () {
  53. pay();
  54. });
  55. }
  56. @override
  57. void dispose() {
  58. // TODO: implement dispose
  59. super.dispose();
  60. checkUnpaidThirdPartyOrders();
  61. }
  62. @override
  63. Widget build(BuildContext context) {
  64. return WillPopScope(
  65. onWillPop: () {
  66. Navigator.pop(context, status);
  67. return Future.value(false);
  68. },
  69. child: Scaffold(
  70. backgroundColor: Colors.white,
  71. body: Column(
  72. children: [
  73. Container(
  74. color: Colors.white,
  75. child: SafeArea(
  76. bottom: false,
  77. child: Container(
  78. height: 45,
  79. width: double.infinity,
  80. color: Colors.white,
  81. child: Stack(
  82. children: [
  83. Text(
  84. titles[status],
  85. style: TextStyle(color: MyColors.c333333, fontSize: 16),
  86. ),
  87. Positioned(
  88. left: 0,
  89. child: GestureDetector(
  90. onTap: () {
  91. Navigator.pop(context, status);
  92. },
  93. behavior: HitTestBehavior.translucent,
  94. child: Transform.rotate(
  95. angle: math.pi,
  96. child: Padding(
  97. padding: EdgeInsets.only(
  98. left: 16, right: 16, top: 8, bottom: 8),
  99. child: SvgPicture.asset(
  100. 'images/svg/箭头.svg',
  101. color: MyColors.c333333,
  102. height: 14,
  103. ),
  104. ),
  105. ),
  106. ),
  107. ),
  108. ],
  109. alignment: Alignment.center,
  110. ),
  111. ),
  112. ),
  113. ),
  114. Container(
  115. height: 10,
  116. color: MyColors.cF7F7F7,
  117. ),
  118. Expanded(
  119. child: status == 1
  120. ? Column(
  121. children: [
  122. Expanded(
  123. child: Column(
  124. children: [
  125. Container(
  126. child: Text(
  127. '支付金额',
  128. style: TextStyle(
  129. color: MyColors.c333333,
  130. fontSize: 16,
  131. fontWeight: FontWeight.bold),
  132. ),
  133. margin: EdgeInsets.only(top: 25, bottom: 5),
  134. ),
  135. Text(
  136. widget.couponData == null
  137. ? order.amount.toStringAsFixed(2)
  138. : NumUtil.subtract(order.amount,
  139. widget.couponData.amount) <
  140. 0
  141. ? 0.0.toStringAsFixed(2)
  142. : NumUtil.subtract(order.amount,
  143. widget.couponData.amount)
  144. .toStringAsFixed(2),
  145. style: TextStyle(
  146. color: MyColors.c333333,
  147. fontSize: 26,
  148. fontWeight: FontWeight.bold),
  149. ),
  150. Container(
  151. margin: EdgeInsets.only(
  152. top: 5,
  153. ),
  154. child: MyViews().myText('支付时间:${order.payTime}',
  155. MyColors.c333333, 12),
  156. ),
  157. Container(
  158. height: 0.5,
  159. color: MyColors.cE7E7E7,
  160. margin: EdgeInsets.only(
  161. left: 21, right: 21, top: 25, bottom: 10),
  162. ),
  163. Container(
  164. margin: EdgeInsets.symmetric(horizontal: 21),
  165. child: Row(
  166. children: [
  167. Container(
  168. decoration: BoxDecoration(
  169. color: MyColors.cFF4233,
  170. borderRadius: BorderRadius.circular(2),
  171. ),
  172. height: 16,
  173. width: 38,
  174. alignment: Alignment.center,
  175. child: MyViews()
  176. .myText('收款方', Colors.white, 10),
  177. ),
  178. Container(
  179. margin:
  180. EdgeInsets.only(left: 7, right: 5),
  181. child: ClipRRect(
  182. borderRadius:
  183. BorderRadius.circular(12.5),
  184. child: order.sellerUid == 0
  185. ? Image.asset(
  186. 'images/app_logo.png',
  187. height: 25,
  188. width: 25,
  189. )
  190. : MyViews().netImg(
  191. imgURL(order.sellerPic),
  192. 25,
  193. 25),
  194. ),
  195. ),
  196. MyViews().myText(
  197. order.sellerUid == 0
  198. ? '平台'
  199. : '${order.sellerName.length > 12 ? order.sellerName.substring(0, 12) + '...' : order.sellerName}(${order.sellerUid})',
  200. MyColors.c333333,
  201. 14),
  202. ],
  203. mainAxisAlignment: MainAxisAlignment.start,
  204. ),
  205. ),
  206. Container(
  207. margin: EdgeInsets.symmetric(
  208. horizontal: 21, vertical: 10),
  209. child: Row(
  210. children: [
  211. Container(
  212. decoration: BoxDecoration(
  213. color: MyColors.cFF4233,
  214. borderRadius: BorderRadius.circular(2),
  215. ),
  216. height: 16,
  217. width: 38,
  218. alignment: Alignment.center,
  219. child: MyViews()
  220. .myText('付款方', Colors.white, 10),
  221. ),
  222. Container(
  223. margin:
  224. EdgeInsets.only(left: 7, right: 5),
  225. child: ClipRRect(
  226. borderRadius:
  227. BorderRadius.circular(12.5),
  228. child: MyViews().netImg(
  229. imgURL(order.payerPic), 25, 25),
  230. ),
  231. ),
  232. MyViews().myText(
  233. '${order.payerName.length > 12 ? order.payerName.substring(0, 12) + '...' : order.payerName}(${order.payerUid})',
  234. MyColors.c333333,
  235. 14),
  236. ],
  237. mainAxisAlignment: MainAxisAlignment.start,
  238. ),
  239. )
  240. ],
  241. ),
  242. ),
  243. SafeArea(
  244. child: Container(
  245. color: Colors.white,
  246. child: Column(
  247. children: [
  248. Container(
  249. height: 5,
  250. color: MyColors.cF7F7F7,
  251. ),
  252. Container(
  253. color: Colors.white,
  254. child: SafeArea(
  255. top: false,
  256. child: Container(
  257. padding:
  258. EdgeInsets.symmetric(horizontal: 37),
  259. color: Colors.white,
  260. width: double.infinity,
  261. height: 60,
  262. child: Row(
  263. children: [
  264. Expanded(
  265. child: GestureDetector(
  266. onTap: () {
  267. MyTools().toPage(
  268. context, WalletPage(),
  269. (then) {
  270. Navigator.pop(
  271. context, status);
  272. });
  273. },
  274. behavior:
  275. HitTestBehavior.translucent,
  276. child: Container(
  277. decoration: BoxDecoration(
  278. borderRadius:
  279. BorderRadius.only(
  280. topLeft:
  281. Radius.circular(20),
  282. bottomLeft:
  283. Radius.circular(20),
  284. ),
  285. border: Border.all(
  286. color:
  287. MyColors.cFF4233,
  288. width: 1)),
  289. height: 40,
  290. child: MyViews().myText(
  291. '查看流水',
  292. MyColors.cFF4233,
  293. 14),
  294. alignment: Alignment.center),
  295. ),
  296. ),
  297. Expanded(
  298. child: GestureDetector(
  299. onTap: () {
  300. RootPageView().bNIndex = 0;
  301. MyTools().toPage(context, RootPage(), (then){});
  302. },
  303. behavior:
  304. HitTestBehavior.translucent,
  305. child: Container(
  306. decoration: BoxDecoration(
  307. borderRadius:
  308. BorderRadius.only(
  309. topRight:
  310. Radius.circular(20),
  311. bottomRight:
  312. Radius.circular(20),
  313. ),
  314. color: MyColors.cFF4233),
  315. height: 40,
  316. child: MyViews().myText(
  317. '返回首页', Colors.white, 14),
  318. alignment: Alignment.center),
  319. ),
  320. )
  321. ],
  322. ),
  323. ),
  324. ),
  325. )
  326. ],
  327. ),
  328. ),
  329. top: false,
  330. ),
  331. ],
  332. )
  333. : Center(
  334. child: Column(
  335. children: [
  336. Expanded(
  337. child: Column(
  338. children: [
  339. SvgPicture.asset(
  340. icons[status],
  341. height: 94,
  342. width: 94,
  343. ),
  344. Container(
  345. margin: EdgeInsets.only(top: 30),
  346. child: Text(
  347. content[status],
  348. style: TextStyle(
  349. color: MyColors.c666666, fontSize: 15),
  350. ),
  351. ),
  352. ],
  353. mainAxisAlignment: MainAxisAlignment.center,
  354. ),
  355. ),
  356. Expanded(child: Container()),
  357. ],
  358. ),
  359. ),
  360. ),
  361. ],
  362. ),
  363. ),
  364. );
  365. }
  366. pay() {
  367. getUserWalletBalance((re, hE) {
  368. if (!hE) {
  369. UserBalanceEntity balance =
  370. UserBalanceEntity().fromJson(json.decode(re.data.toString()));
  371. if (widget.payWay == payWayWallet) {
  372. if (balance.data >= widget.amount) {
  373. payWithWallet();
  374. } else {
  375. if (widget.couponData != null) {
  376. if (balance.data + widget.couponData.amount >= widget.amount) {
  377. payWithWallet();
  378. } else {
  379. showToast('积分不足,请选择其他支付方式');
  380. status = 2;
  381. setState(() {});
  382. }
  383. } else {
  384. showToast('积分不足,请选择其他支付方式');
  385. status = 2;
  386. setState(() {});
  387. }
  388. }
  389. } else if (widget.payWay == payWayAliPay) {
  390. payWithAliPay();
  391. }else if(widget.payWay == payWayWeChat){
  392. payByWeChat();
  393. }
  394. }
  395. }, (e) {}, context);
  396. }
  397. @override
  398. void didChangeAppLifecycleState(AppLifecycleState state) {
  399. super.didChangeAppLifecycleState(state);
  400. if (state == AppLifecycleState.paused) {
  401. print('AlipayPage went to Background');
  402. count = 0;
  403. status = 0;
  404. setState(() {});
  405. }
  406. if (state == AppLifecycleState.resumed) {
  407. print('AlipayPage came back to Foreground');
  408. if (Platform.isIOS) {
  409. count = 10;
  410. checkIfTheOrderIsPaid();
  411. }
  412. }
  413. }
  414. int count = 0;
  415. void checkIfTheOrderIsPaid() {
  416. MyDio().query({
  417. "key": "order",
  418. "filters": {
  419. "conditions": ["uid == ${widget.orderUID}"]
  420. },
  421. "dims": orderDims,
  422. "paging": [1, 1]
  423. }, (response, hasError) {
  424. if (!hasError) {
  425. SmartOrderBeanEntity entity = SmartOrderBeanEntity()
  426. .fromJson(json.decode(response.data.toString()));
  427. if (entity.data.data[0].state == orderStatePaid ||
  428. entity.data.data[0].state == orderStateOtherPaid) {
  429. order = entity.data.data[0];
  430. status = 1;
  431. setState(() {});
  432. // Timer(Duration(seconds: 1), () {
  433. // Navigator.pop(context, '支付成功');
  434. // });
  435. } else {
  436. if (count != 0) {
  437. Timer(Duration(seconds: 1), () {
  438. count--;
  439. checkIfTheOrderIsPaid();
  440. });
  441. } else {
  442. count = 10;
  443. status = 2;
  444. if (widget.cancelOrder) {
  445. revokePayOrder(widget.orderUID);
  446. } else {
  447. resetOrder(widget.orderUID);
  448. }
  449. setState(() {});
  450. }
  451. }
  452. }
  453. }, (error) {});
  454. }
  455. void payWithWallet() {
  456. payOrder(widget.couponData, payWayWallet, widget.orderUID, (re, hE) {
  457. if (!hE) {
  458. showToast('支付成功');
  459. count = 30;
  460. checkIfTheOrderIsPaid();
  461. }
  462. }, (e) {}, context);
  463. }
  464. void payWithAliPay() {
  465. payOrder(widget.couponData, payWayAliPay, widget.orderUID, (re, hE) {
  466. if (!hE) {
  467. PayByAlipayBeanEntity entity =
  468. PayByAlipayBeanEntity().fromJson(json.decode(re.data.toString()));
  469. EventBus().on('alipayPaymentCallback', (arg) {
  470. EventBus().off('alipayPaymentCallback');
  471. EasyLoading.dismiss();
  472. showToast('支付成功');
  473. EventBus().emit('reNoPayOrder');
  474. // Navigator.pop(context);
  475. count = 30;
  476. checkIfTheOrderIsPaid();
  477. });
  478. print(entity.data);
  479. EasyLoading.instance
  480. ..contentPadding = EdgeInsets.symmetric(horizontal: 20, vertical: 12)
  481. ..alignment = Alignment.center
  482. ..loadingStyle = EasyLoadingStyle.light
  483. ..contentMargin = EdgeInsets.all(20);
  484. aliPay(entity.data).then((value) {
  485. print(json.encode(value));
  486. AlipayResultBeanEntity e = AlipayResultBeanEntity()
  487. .fromJson(json.decode(json.encode(value)));
  488. try {
  489. int r = int.parse(e.resultStatus);
  490. if (r == 9000) {
  491. count = 30;
  492. showToast('支付成功');
  493. checkIfTheOrderIsPaid();
  494. } else {
  495. showToast(aliPayResultStatus(r));
  496. if (widget.cancelOrder) {
  497. revokePayOrder(widget.orderUID);
  498. } else {
  499. resetOrder(widget.orderUID);
  500. }
  501. setState(() {
  502. status = 2;
  503. });
  504. }
  505. } catch (e) {
  506. EasyLoading.dismiss();
  507. }
  508. });
  509. }
  510. }, (e) {}, context);
  511. }
  512. void payByWeChat() {
  513. payOrder(widget.couponData, widget.payWay, widget.orderUID, (re,hE){
  514. if(!hE){
  515. PayByWeChatBeanEntity entity = PayByWeChatBeanEntity().fromJson(json.decode(re.toString()));
  516. EventBus().on('WeChatPaymentResponse', (arg) {
  517. if(arg){
  518. count = 30;
  519. showToast('支付成功');
  520. checkIfTheOrderIsPaid();
  521. }else if(!arg){
  522. showToast('支付失败');
  523. if (widget.cancelOrder) {
  524. revokePayOrder(widget.orderUID);
  525. } else {
  526. resetOrder(widget.orderUID);
  527. }
  528. status = 2;
  529. setState(() {});
  530. }
  531. EventBus().off('WeChatPaymentResponse');
  532. });
  533. payWithWeChat(
  534. appId: entity.data.appid,
  535. partnerId: entity.data.partnerid,
  536. prepayId: entity.data.prepayid,
  537. packageValue: entity.data.package,
  538. nonceStr:entity.data.noncestr,
  539. timeStamp: int.parse(entity.data.timestamp),
  540. sign: entity.data.sign,
  541. );
  542. }
  543. }, (e){}, context);
  544. }
  545. }