pay_page.dart 23 KB

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