alipay_page.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. aliPay(entity.data).then((value) {
  103. print(json.encode(value));
  104. AlipayResultBeanEntity e = AlipayResultBeanEntity()
  105. .fromJson(json.decode(json.encode(value)));
  106. try {
  107. int r = int.parse(e.resultStatus);
  108. if (r == 9000) {
  109. showToast('支付成功');
  110. // Navigator.pop(context);
  111. setState(() {
  112. status = 1;
  113. });
  114. } else {
  115. showToast(aliPayResultStatus(r));
  116. revokePayOrder(widget.orderUID);
  117. setState(() {
  118. status = 2;
  119. });
  120. }
  121. } catch (e) {
  122. EasyLoading.dismiss();
  123. }
  124. });
  125. }
  126. }, (e) {}, context);
  127. }
  128. @override
  129. void didChangeAppLifecycleState(AppLifecycleState state) {
  130. super.didChangeAppLifecycleState(state);
  131. if (state == AppLifecycleState.paused) {
  132. print('AlipayPage went to Background');
  133. count = 0;
  134. status = 0;
  135. setState(() {});
  136. }
  137. if (state == AppLifecycleState.resumed) {
  138. print('AlipayPage came back to Foreground');
  139. if(Platform.isIOS){
  140. count = 10;
  141. checkIfTheOrderIsPaid();
  142. }
  143. }
  144. }
  145. int count = 0;
  146. void checkIfTheOrderIsPaid() {
  147. MyDio().query({
  148. "key": "order",
  149. "filters": {
  150. "conditions": ["uid == ${widget.orderUID}"]
  151. },
  152. "dims": orderDims,
  153. "paging": [1, 1]
  154. }, (response, hasError) {
  155. if (!hasError) {
  156. SmartOrderBeanEntity entity = SmartOrderBeanEntity()
  157. .fromJson(json.decode(response.data.toString()));
  158. if (entity.data.data[0].state == orderStatePaid) {
  159. status = 1;
  160. setState(() {});
  161. // Timer(Duration(seconds: 1), () {
  162. // Navigator.pop(context, '支付成功');
  163. // });
  164. } else {
  165. if (count != 0) {
  166. Timer(Duration(seconds: 1), () {
  167. count--;
  168. checkIfTheOrderIsPaid();
  169. });
  170. } else {
  171. count = 10;
  172. status = 2;
  173. setState(() {});
  174. }
  175. }
  176. }
  177. }, (error) {});
  178. }
  179. }