import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:bbyyy/beans/alipay_result_bean_entity.dart'; import 'package:bbyyy/beans/pay_by_alipay_bean_entity.dart'; import 'package:bbyyy/beans/smart_order_bean_entity.dart'; import 'package:bbyyy/https/MyDio.dart'; import 'package:bbyyy/https/my_request.dart'; import 'package:bbyyy/my_tools/const.dart'; import 'package:bbyyy/my_tools/dims.dart'; import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart'; import 'package:bbyyy/my_tools/event_bus.dart'; import 'package:bbyyy/my_tools/my_colors.dart'; import 'package:bbyyy/my_tools/my_cookie.dart'; import 'package:bbyyy/my_tools/my_tools.dart'; import 'package:bbyyy/my_tools/my_views.dart'; import 'package:bbyyy/pay/pay_tools.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:tobias/tobias.dart'; class AlipayPage extends StatefulWidget { int orderUID; AlipayPage(this.orderUID); @override _AlipayPageState createState() => _AlipayPageState(); } class _AlipayPageState extends State with WidgetsBindingObserver { var titles = ['正在支付', '支付成功', '支付失败']; var icons = ['images/svg/等待.svg', 'images/svg/成功.svg', 'images/svg/失败.svg']; var content = ['正在支付,请稍后…', '支付成功,即将自动跳转…', '支付失败']; int status = 0; @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); Future.delayed(Duration(seconds: 1), () { pay(); }); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, body: Column( children: [ MyViews().myAppBar(titles[status], context, []), Expanded( child: Center( child: Column( children: [ Container( height: 10, color: MyColors.cF7F7F7, ), Expanded( child: Column( children: [ SvgPicture.asset( icons[status], height: 94, width: 94, ), Container( margin: EdgeInsets.only(top: 30), child: Text( content[status], style: TextStyle( color: MyColors.c666666, fontSize: 15), ), ), ], mainAxisAlignment: MainAxisAlignment.center, ), ), Expanded(child: Container()), ], ), ), ), ], ), ); } pay() { payOrder(payWayAliPay, widget.orderUID, (re, hE) { if (!hE) { PayByAlipayBeanEntity entity = PayByAlipayBeanEntity().fromJson(json.decode(re.data.toString())); EventBus().on('alipayPaymentCallback', (arg) { EventBus().off('alipayPaymentCallback'); EasyLoading.dismiss(); showToast('支付成功'); EventBus().emit('reNoPayOrder'); // Navigator.pop(context); }); print(entity.data); EasyLoading.instance ..contentPadding = EdgeInsets.symmetric(horizontal: 20, vertical: 12) ..alignment = Alignment.center ..loadingStyle = EasyLoadingStyle.light ..contentMargin = EdgeInsets.all(20); EasyLoading.show(); aliPay(entity.data).then((value) { print(json.encode(value)); AlipayResultBeanEntity e = AlipayResultBeanEntity() .fromJson(json.decode(json.encode(value))); try { int r = int.parse(e.resultStatus); if (r == 9000) { showToast('支付成功'); // Navigator.pop(context); setState(() { status = 1; }); } else { showToast(aliPayResultStatus(r)); revokePayOrder(widget.orderUID); setState(() { status = 2; }); } } catch (e) { EasyLoading.dismiss(); } }); } }, (e) {}, context); } @override void didChangeAppLifecycleState(AppLifecycleState state) { super.didChangeAppLifecycleState(state); if (state == AppLifecycleState.paused) { print('AlipayPage went to Background'); count = 0; status = 0; setState(() {}); } if (state == AppLifecycleState.resumed) { print('AlipayPage came back to Foreground'); if(Platform.isIOS){ count = 10; checkIfTheOrderIsPaid(); } } } int count = 0; void checkIfTheOrderIsPaid() { MyDio().query({ "key": "order", "filters": { "conditions": ["uid == ${widget.orderUID}"] }, "dims": orderDims, "paging": [1, 1] }, (response, hasError) { if (!hasError) { SmartOrderBeanEntity entity = SmartOrderBeanEntity() .fromJson(json.decode(response.data.toString())); if (entity.data.data[0].state == orderStatePaid) { status = 1; setState(() {}); // Timer(Duration(seconds: 1), () { // Navigator.pop(context, '支付成功'); // }); } else { if (count != 0) { Timer(Duration(seconds: 1), () { count--; checkIfTheOrderIsPaid(); }); } else { count = 10; status = 2; setState(() {}); } } } }, (error) {}); } }