pay_page.dart 23 KB

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