pay_page.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:bbyyy/beans/alipay_result_bean_entity.dart';
  5. import 'package:bbyyy/beans/pay_by_alipay_bean_entity.dart';
  6. import 'package:bbyyy/beans/smart_order_bean_entity.dart';
  7. import 'package:bbyyy/beans/user_balance_entity.dart';
  8. import 'package:bbyyy/https/MyDio.dart';
  9. import 'package:bbyyy/https/my_request.dart';
  10. import 'package:bbyyy/https/url.dart';
  11. import 'package:bbyyy/my_tools/const.dart';
  12. import 'package:bbyyy/my_tools/dims.dart';
  13. import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
  14. import 'package:bbyyy/my_tools/event_bus.dart';
  15. import 'package:bbyyy/my_tools/my_colors.dart';
  16. import 'package:bbyyy/my_tools/my_tools.dart';
  17. import 'package:bbyyy/my_tools/my_views.dart';
  18. import 'package:bbyyy/pay/pay_tools.dart';
  19. import 'package:flutter/cupertino.dart';
  20. import 'package:flutter/material.dart';
  21. import 'package:flutter_svg/flutter_svg.dart';
  22. import 'package:tobias/tobias.dart';
  23. class PayPage extends StatefulWidget {
  24. int orderUID;
  25. bool cancelOrder;
  26. double amount;
  27. PayPage(this.orderUID, this.cancelOrder, this.amount);
  28. @override
  29. _PayPageState createState() => _PayPageState();
  30. }
  31. class _PayPageState extends State<PayPage> with WidgetsBindingObserver {
  32. var titles = ['正在支付', '支付成功', '支付失败'];
  33. var icons = ['images/svg/等待.svg', 'images/svg/成功.svg', 'images/svg/失败.svg'];
  34. var content = ['正在支付,请稍后…', '支付成功,即将自动跳转…', '支付失败'];
  35. int status = 0;
  36. SmartOrderBeanDataData order;
  37. @override
  38. void initState() {
  39. super.initState();
  40. WidgetsBinding.instance.addObserver(this);
  41. Future.delayed(Duration(seconds: 1), () {
  42. pay();
  43. });
  44. }
  45. @override
  46. Widget build(BuildContext context) {
  47. return Scaffold(
  48. backgroundColor: Colors.white,
  49. body: Column(
  50. children: [
  51. MyViews().myAppBar(titles[status], context, []),
  52. Container(
  53. height: 10,
  54. color: MyColors.cF7F7F7,
  55. ),
  56. Expanded(
  57. child: status == 1
  58. ? Column(
  59. children: [
  60. Container(child: Text('支付金额',style: TextStyle(color: MyColors.c333333,fontSize: 16,fontWeight: FontWeight.bold),),margin: EdgeInsets.only(top: 25,bottom: 5),),
  61. Text(order.amount.toStringAsFixed(2),style: TextStyle(color: MyColors.c333333,fontSize: 26,fontWeight: FontWeight.bold),),
  62. Container(
  63. margin: EdgeInsets.only(top: 5,),
  64. child: MyViews().myText(
  65. '支付时间:${order.payTime}', MyColors.c333333, 12),
  66. ),
  67. Container(
  68. height: 0.5,
  69. color: MyColors.cE7E7E7,
  70. margin: EdgeInsets.only(
  71. left: 21, right: 21, top: 25, bottom: 10),
  72. ),
  73. Container(
  74. margin: EdgeInsets.symmetric(horizontal: 21),
  75. child: Row(
  76. children: [
  77. Container(
  78. decoration: BoxDecoration(
  79. color: MyColors.cFF4233,
  80. borderRadius: BorderRadius.circular(2),
  81. ),
  82. height: 16,
  83. width: 38, alignment: Alignment.center,
  84. child: MyViews().myText('收款方', Colors.white, 10),
  85. ),
  86. Container(
  87. margin: EdgeInsets.only(left: 7,right: 5),
  88. child: ClipRRect(
  89. borderRadius: BorderRadius.circular(12.5),
  90. child: MyViews()
  91. .netImg(imgURL(order.sellerPic), 25, 25),
  92. ),
  93. ),
  94. MyViews().myText(
  95. '${order.sellerName.length>12?order.sellerName.substring(0,12)+'...':order.buyerName}(${order.sellerUid})',
  96. MyColors.c333333,
  97. 14),
  98. ],mainAxisAlignment: MainAxisAlignment.start,
  99. ),
  100. ),
  101. Container(
  102. margin: EdgeInsets.symmetric(horizontal: 21,vertical: 10),
  103. child: Row(
  104. children: [
  105. Container(
  106. decoration: BoxDecoration(
  107. color: MyColors.cFF4233,
  108. borderRadius: BorderRadius.circular(2),
  109. ),
  110. height: 16,
  111. width: 38,
  112. alignment: Alignment.center,
  113. child: MyViews().myText('付款方', Colors.white, 10),
  114. ),
  115. Container(
  116. margin: EdgeInsets.only(left: 7,right: 5),
  117. child: ClipRRect(
  118. borderRadius: BorderRadius.circular(12.5),
  119. child: MyViews()
  120. .netImg(imgURL(order.buyerPic), 25, 25),
  121. ),
  122. ),
  123. MyViews().myText(
  124. '${order.buyerName.length>12?order.buyerName.substring(0,12)+'...':order.buyerName}(${order.buyerUid})',
  125. MyColors.c333333,
  126. 14),
  127. ],mainAxisAlignment: MainAxisAlignment.start,
  128. ),
  129. )
  130. ],
  131. )
  132. : Center(
  133. child: Column(
  134. children: [
  135. Expanded(
  136. child: Column(
  137. children: [
  138. SvgPicture.asset(
  139. icons[status],
  140. height: 94,
  141. width: 94,
  142. ),
  143. Container(
  144. margin: EdgeInsets.only(top: 30),
  145. child: Text(
  146. content[status],
  147. style: TextStyle(
  148. color: MyColors.c666666, fontSize: 15),
  149. ),
  150. ),
  151. ],
  152. mainAxisAlignment: MainAxisAlignment.center,
  153. ),
  154. ),
  155. Expanded(child: Container()),
  156. ],
  157. ),
  158. ),
  159. ),
  160. ],
  161. ),
  162. );
  163. }
  164. pay() {
  165. getUserWalletBalance((re, hE) {
  166. if (!hE) {
  167. UserBalanceEntity balance =
  168. UserBalanceEntity().fromJson(json.decode(re.data.toString()));
  169. if (balance.data >= widget.amount) {
  170. payOrder(payWayWallet, widget.orderUID, (re, hE) {
  171. if (!hE) {
  172. showToast('支付成功');
  173. count = 30;
  174. checkIfTheOrderIsPaid();
  175. }
  176. }, (e) {}, context);
  177. } else {
  178. payOrder(payWayAliPay, widget.orderUID, (re, hE) {
  179. if (!hE) {
  180. PayByAlipayBeanEntity entity = PayByAlipayBeanEntity()
  181. .fromJson(json.decode(re.data.toString()));
  182. EventBus().on('alipayPaymentCallback', (arg) {
  183. EventBus().off('alipayPaymentCallback');
  184. EasyLoading.dismiss();
  185. showToast('支付成功');
  186. EventBus().emit('reNoPayOrder');
  187. // Navigator.pop(context);
  188. count = 30;
  189. checkIfTheOrderIsPaid();
  190. });
  191. print(entity.data);
  192. EasyLoading.instance
  193. ..contentPadding =
  194. EdgeInsets.symmetric(horizontal: 20, vertical: 12)
  195. ..alignment = Alignment.center
  196. ..loadingStyle = EasyLoadingStyle.light
  197. ..contentMargin = EdgeInsets.all(20);
  198. aliPay(entity.data).then((value) {
  199. print(json.encode(value));
  200. AlipayResultBeanEntity e = AlipayResultBeanEntity()
  201. .fromJson(json.decode(json.encode(value)));
  202. try {
  203. int r = int.parse(e.resultStatus);
  204. if (r == 9000) {
  205. count = 30;
  206. showToast('支付成功');
  207. checkIfTheOrderIsPaid();
  208. } else {
  209. showToast(aliPayResultStatus(r));
  210. if (widget.cancelOrder) {
  211. revokePayOrder(widget.orderUID);
  212. } else {
  213. resetOrder(widget.orderUID);
  214. }
  215. setState(() {
  216. status = 2;
  217. });
  218. }
  219. } catch (e) {
  220. EasyLoading.dismiss();
  221. }
  222. });
  223. }
  224. }, (e) {}, context);
  225. }
  226. }
  227. }, (e) {}, context);
  228. }
  229. @override
  230. void didChangeAppLifecycleState(AppLifecycleState state) {
  231. super.didChangeAppLifecycleState(state);
  232. if (state == AppLifecycleState.paused) {
  233. print('AlipayPage went to Background');
  234. count = 0;
  235. status = 0;
  236. setState(() {});
  237. }
  238. if (state == AppLifecycleState.resumed) {
  239. print('AlipayPage came back to Foreground');
  240. if (Platform.isIOS) {
  241. count = 10;
  242. checkIfTheOrderIsPaid();
  243. }
  244. }
  245. }
  246. int count = 0;
  247. void checkIfTheOrderIsPaid() {
  248. MyDio().query({
  249. "key": "order",
  250. "filters": {
  251. "conditions": ["uid == ${widget.orderUID}"]
  252. },
  253. "dims": orderDims,
  254. "paging": [1, 1]
  255. }, (response, hasError) {
  256. if (!hasError) {
  257. SmartOrderBeanEntity entity = SmartOrderBeanEntity()
  258. .fromJson(json.decode(response.data.toString()));
  259. if (entity.data.data[0].state == orderStatePaid) {
  260. order = entity.data.data[0];
  261. status = 1;
  262. setState(() {});
  263. // Timer(Duration(seconds: 1), () {
  264. // Navigator.pop(context, '支付成功');
  265. // });
  266. } else {
  267. if (count != 0) {
  268. Timer(Duration(seconds: 1), () {
  269. count--;
  270. checkIfTheOrderIsPaid();
  271. });
  272. } else {
  273. count = 10;
  274. status = 2;
  275. if (widget.cancelOrder) {
  276. revokePayOrder(widget.orderUID);
  277. } else {
  278. resetOrder(widget.orderUID);
  279. }
  280. setState(() {});
  281. }
  282. }
  283. }
  284. }, (error) {});
  285. }
  286. }