alipay_page.dart 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/https/MyDio.dart';
  8. import 'package:bbyyy/https/my_request.dart';
  9. import 'package:bbyyy/my_tools/const.dart';
  10. import 'package:bbyyy/my_tools/dims.dart';
  11. import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
  12. import 'package:bbyyy/my_tools/event_bus.dart';
  13. import 'package:bbyyy/my_tools/my_colors.dart';
  14. import 'package:bbyyy/my_tools/my_cookie.dart';
  15. import 'package:bbyyy/my_tools/my_tools.dart';
  16. import 'package:bbyyy/my_tools/my_views.dart';
  17. import 'package:bbyyy/pay/pay_tools.dart';
  18. import 'package:flutter/material.dart';
  19. import 'package:flutter_svg/flutter_svg.dart';
  20. import 'package:tobias/tobias.dart';
  21. class AlipayPage extends StatefulWidget {
  22. int orderUID;
  23. AlipayPage(this.orderUID);
  24. @override
  25. _AlipayPageState createState() => _AlipayPageState();
  26. }
  27. class _AlipayPageState extends State<AlipayPage> with WidgetsBindingObserver {
  28. var titles = ['正在支付', '支付成功', '支付失败'];
  29. var icons = ['images/svg/等待.svg', 'images/svg/成功.svg', 'images/svg/失败.svg'];
  30. var content = ['正在支付,请稍后…', '支付成功,即将自动跳转…', '支付失败'];
  31. int status = 0;
  32. @override
  33. void initState() {
  34. super.initState();
  35. WidgetsBinding.instance.addObserver(this);
  36. Future.delayed(Duration(seconds: 1), () {
  37. pay();
  38. });
  39. }
  40. @override
  41. Widget build(BuildContext context) {
  42. return Scaffold(
  43. backgroundColor: Colors.white,
  44. body: Column(
  45. children: [
  46. MyViews().myAppBar(titles[status], context, []),
  47. Expanded(
  48. child: Center(
  49. child: Column(
  50. children: [
  51. Container(
  52. height: 10,
  53. color: MyColors.cF7F7F7,
  54. ),
  55. Expanded(
  56. child: Column(
  57. children: [
  58. SvgPicture.asset(
  59. icons[status],
  60. height: 94,
  61. width: 94,
  62. ),
  63. Container(
  64. margin: EdgeInsets.only(top: 30),
  65. child: Text(
  66. content[status],
  67. style: TextStyle(
  68. color: MyColors.c666666, fontSize: 15),
  69. ),
  70. ),
  71. ],
  72. mainAxisAlignment: MainAxisAlignment.center,
  73. ),
  74. ),
  75. Expanded(child: Container()),
  76. ],
  77. ),
  78. ),
  79. ),
  80. ],
  81. ),
  82. );
  83. }
  84. pay() {
  85. payOrder(payWayAliPay, widget.orderUID, (re, hE) {
  86. if (!hE) {
  87. PayByAlipayBeanEntity entity =
  88. PayByAlipayBeanEntity().fromJson(json.decode(re.data.toString()));
  89. EventBus().on('alipayPaymentCallback', (arg) {
  90. EventBus().off('alipayPaymentCallback');
  91. EasyLoading.dismiss();
  92. showToast('支付成功');
  93. EventBus().emit('reNoPayOrder');
  94. // Navigator.pop(context);
  95. });
  96. print(entity.data);
  97. EasyLoading.instance
  98. ..contentPadding = EdgeInsets.symmetric(horizontal: 20, vertical: 12)
  99. ..alignment = Alignment.center
  100. ..loadingStyle = EasyLoadingStyle.light
  101. ..contentMargin = EdgeInsets.all(20);
  102. EasyLoading.show();
  103. aliPay(entity.data).then((value) {
  104. print(json.encode(value));
  105. AlipayResultBeanEntity e = AlipayResultBeanEntity()
  106. .fromJson(json.decode(json.encode(value)));
  107. try {
  108. int r = int.parse(e.resultStatus);
  109. if (r == 9000) {
  110. showToast('支付成功');
  111. // Navigator.pop(context);
  112. setState(() {
  113. status = 1;
  114. });
  115. } else {
  116. showToast(aliPayResultStatus(r));
  117. revokePayOrder(widget.orderUID);
  118. setState(() {
  119. status = 2;
  120. });
  121. }
  122. } catch (e) {
  123. EasyLoading.dismiss();
  124. }
  125. });
  126. }
  127. }, (e) {}, context);
  128. }
  129. @override
  130. void didChangeAppLifecycleState(AppLifecycleState state) {
  131. super.didChangeAppLifecycleState(state);
  132. if (state == AppLifecycleState.paused) {
  133. print('AlipayPage went to Background');
  134. count = 0;
  135. status = 0;
  136. setState(() {});
  137. }
  138. if (state == AppLifecycleState.resumed) {
  139. print('AlipayPage came back to Foreground');
  140. if(Platform.isIOS){
  141. count = 10;
  142. checkIfTheOrderIsPaid();
  143. }
  144. }
  145. }
  146. int count = 0;
  147. void checkIfTheOrderIsPaid() {
  148. MyDio().query({
  149. "key": "order",
  150. "filters": {
  151. "conditions": ["uid == ${widget.orderUID}"]
  152. },
  153. "dims": orderDims,
  154. "paging": [1, 1]
  155. }, (response, hasError) {
  156. if (!hasError) {
  157. SmartOrderBeanEntity entity = SmartOrderBeanEntity()
  158. .fromJson(json.decode(response.data.toString()));
  159. if (entity.data.data[0].state == orderStatePaid) {
  160. status = 1;
  161. setState(() {});
  162. // Timer(Duration(seconds: 1), () {
  163. // Navigator.pop(context, '支付成功');
  164. // });
  165. } else {
  166. if (count != 0) {
  167. Timer(Duration(seconds: 1), () {
  168. count--;
  169. checkIfTheOrderIsPaid();
  170. });
  171. } else {
  172. count = 10;
  173. status = 2;
  174. setState(() {});
  175. }
  176. }
  177. }
  178. }, (error) {});
  179. }
  180. }