pay_page.dart 23 KB

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