pay_page.dart 15 KB

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