pay_page.dart 22 KB

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