pay_page.dart 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. payWithAliPay();
  424. } else if (widget.payWay == payWayWeChat) {
  425. payByWeChat();
  426. }
  427. }
  428. }, (e) {}, context);
  429. }
  430. @override
  431. void didChangeAppLifecycleState(AppLifecycleState state) {
  432. super.didChangeAppLifecycleState(state);
  433. if (state == AppLifecycleState.paused) {
  434. print('AlipayPage went to Background');
  435. count = 0;
  436. status = 0;
  437. setState(() {});
  438. }
  439. if (state == AppLifecycleState.resumed) {
  440. print('AlipayPage came back to Foreground');
  441. if (Platform.isIOS) {
  442. count = 10;
  443. checkIfTheOrderIsPaid();
  444. }
  445. }
  446. }
  447. int count = 0;
  448. void checkIfTheOrderIsPaid() {
  449. MyDio().query({
  450. "key": "order",
  451. "filters": {
  452. "conditions": ["uid == ${widget.orderUID}"]
  453. },
  454. "dims": orderDims,
  455. "paging": [1, 1]
  456. }, (response, hasError) {
  457. if (!hasError) {
  458. SmartOrderBeanEntity entity = SmartOrderBeanEntity()
  459. .fromJson(json.decode(response.data.toString()));
  460. if (entity.data.data[0].state == orderStatePaid ||
  461. entity.data.data[0].state == orderStateOtherPaid) {
  462. order = entity.data.data[0];
  463. status = 1;
  464. setState(() {});
  465. // Timer(Duration(seconds: 1), () {
  466. // Navigator.pop(context, '支付成功');
  467. // });
  468. } else {
  469. if (count != 0) {
  470. Timer(Duration(seconds: 1), () {
  471. count--;
  472. checkIfTheOrderIsPaid();
  473. });
  474. } else {
  475. count = 10;
  476. status = 2;
  477. if (widget.cancelOrder) {
  478. revokePayOrder(widget.orderUID);
  479. } else {
  480. resetOrder(widget.orderUID);
  481. }
  482. setState(() {});
  483. }
  484. }
  485. }
  486. }, (error) {});
  487. }
  488. void payWithWallet() {
  489. payOrder(widget.couponData, payWayWallet, widget.orderUID, (re, hE) {
  490. if (!hE) {
  491. showToast('支付成功');
  492. count = 30;
  493. checkIfTheOrderIsPaid();
  494. }
  495. }, (e) {}, context);
  496. }
  497. void payWithAliPay() {
  498. payOrder(widget.couponData, payWayAliPay, widget.orderUID, (re, hE) {
  499. if (!hE) {
  500. PayByAlipayBeanEntity entity =
  501. PayByAlipayBeanEntity().fromJson(json.decode(re.data.toString()));
  502. EventBus().on('alipayPaymentCallback', (arg) {
  503. EventBus().off('alipayPaymentCallback');
  504. EasyLoading.dismiss();
  505. showToast('支付成功');
  506. EventBus().emit('reNoPayOrder');
  507. // Navigator.pop(context);
  508. count = 30;
  509. checkIfTheOrderIsPaid();
  510. });
  511. print(entity.data);
  512. EasyLoading.instance
  513. ..contentPadding = EdgeInsets.symmetric(horizontal: 20, vertical: 12)
  514. ..alignment = Alignment.center
  515. ..loadingStyle = EasyLoadingStyle.light
  516. ..contentMargin = EdgeInsets.all(20);
  517. aliPay(entity.data).then((value) {
  518. print(json.encode(value));
  519. AlipayResultBeanEntity e = AlipayResultBeanEntity()
  520. .fromJson(json.decode(json.encode(value)));
  521. try {
  522. int r = int.parse(e.resultStatus);
  523. if (r == 9000) {
  524. count = 30;
  525. showToast('支付成功');
  526. checkIfTheOrderIsPaid();
  527. } else {
  528. showToast(aliPayResultStatus(r));
  529. if (widget.cancelOrder) {
  530. revokePayOrder(widget.orderUID);
  531. } else {
  532. resetOrder(widget.orderUID);
  533. }
  534. setState(() {
  535. status = 2;
  536. });
  537. }
  538. } catch (e) {
  539. EasyLoading.dismiss();
  540. }
  541. });
  542. }
  543. }, (e) {}, context);
  544. }
  545. void payByWeChat() {
  546. payOrder(widget.couponData, widget.payWay, widget.orderUID, (re, hE) {
  547. if (!hE) {
  548. PayByWeChatBeanEntity entity =
  549. PayByWeChatBeanEntity().fromJson(json.decode(re.toString()));
  550. EventBus().on('WeChatPaymentResponse', (arg) {
  551. if (arg) {
  552. count = 30;
  553. showToast('支付成功');
  554. checkIfTheOrderIsPaid();
  555. } else if (!arg) {
  556. showToast('支付失败');
  557. if (widget.cancelOrder) {
  558. revokePayOrder(widget.orderUID);
  559. } else {
  560. resetOrder(widget.orderUID);
  561. }
  562. status = 2;
  563. setState(() {});
  564. }
  565. EventBus().off('WeChatPaymentResponse');
  566. });
  567. payWithWeChat(
  568. appId: entity.data.appid,
  569. partnerId: entity.data.partnerid,
  570. prepayId: entity.data.prepayid,
  571. packageValue: entity.data.package,
  572. nonceStr: entity.data.noncestr,
  573. timeStamp: int.parse(entity.data.timestamp),
  574. sign: entity.data.sign,
  575. );
  576. }
  577. }, (e) {}, context);
  578. }
  579. }