pay_page.dart 22 KB

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