pay_page.dart 24 KB

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