pay_page.dart 24 KB

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