| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'dart:math' as math;
- import 'package:bbyyy/beans/alipay_result_bean_entity.dart';
- import 'package:bbyyy/beans/my_coupon_bean_entity.dart';
- import 'package:bbyyy/beans/pay_by_alipay_bean_entity.dart';
- import 'package:bbyyy/beans/pay_by_we_chat_bean_entity.dart';
- import 'package:bbyyy/beans/smart_order_bean_entity.dart';
- import 'package:bbyyy/beans/user_balance_entity.dart';
- import 'package:bbyyy/https/MyDio.dart';
- import 'package:bbyyy/https/my_request.dart';
- import 'package:bbyyy/https/url.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/paegs/mine_page/wallet_page/wallet_page.dart';
- import 'package:bbyyy/pay/pay_tools.dart';
- import 'package:flustars/flustars.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_svg/flutter_svg.dart';
- import 'package:fluwx/fluwx.dart';
- import 'package:tobias/tobias.dart';
- class PayPage extends StatefulWidget {
- int orderUID;
- bool cancelOrder;
- double amount;
- int payWay;
- MyCouponBeanDataData couponData;
- double discountAmount;
- PayPage(this.orderUID, this.cancelOrder, this.amount, this.payWay,
- this.couponData, this.discountAmount);
- @override
- _PayPageState createState() => _PayPageState();
- }
- class _PayPageState extends State<PayPage> with WidgetsBindingObserver {
- var titles = ['正在支付', '支付成功', '支付失败'];
- var icons = ['images/svg/等待.svg', 'images/svg/成功.svg', 'images/svg/失败.svg'];
- var content = ['正在支付,请稍后…', '支付成功,即将自动跳转…', '支付失败'];
- int status = 0;
- SmartOrderBeanDataData order;
- bool showCancel = false;
- @override
- void initState() {
- super.initState();
- WidgetsBinding.instance.addObserver(this);
- Future.delayed(Duration(seconds: 1), () {
- pay();
- });
- Future.delayed(Duration(seconds: 3), () {
- showCancel = true;
- setState(() {
- });
- });
- }
- @override
- void dispose() {
- // TODO: implement dispose
- super.dispose();
- checkUnpaidThirdPartyOrders();
- }
- @override
- Widget build(BuildContext context) {
- return WillPopScope(
- onWillPop: () {
- if(status!=1){
- resetOrder(widget.orderUID);
- }
- Navigator.pop(context, status);
- return Future.value(false);
- },
- child: Scaffold(
- backgroundColor: Colors.white,
- body: Column(
- children: [
- Container(
- color: Colors.white,
- child: SafeArea(
- bottom: false,
- child: Container(
- height: 45,
- width: double.infinity,
- color: Colors.white,
- child: Stack(
- children: [
- Text(
- titles[status],
- style: TextStyle(color: MyColors.c333333, fontSize: 16),
- ),
- Positioned(
- left: 0,
- child: GestureDetector(
- onTap: () {
- if(status!=1){
- resetOrder(widget.orderUID);
- }
- Navigator.pop(context, status);
- },
- behavior: HitTestBehavior.translucent,
- child: Transform.rotate(
- angle: math.pi,
- child: Padding(
- padding: EdgeInsets.only(
- left: 16, right: 16, top: 8, bottom: 8),
- child: SvgPicture.asset(
- 'images/svg/箭头.svg',
- color: MyColors.c333333,
- height: 14,
- ),
- ),
- ),
- ),
- ),
- ],
- alignment: Alignment.center,
- ),
- ),
- ),
- ),
- Container(
- height: 10,
- color: MyColors.cF7F7F7,
- ),
- Expanded(
- child: status == 1
- ? Column(
- children: [
- Expanded(
- child: Column(
- children: [
- Container(
- child: Text(
- '支付金额',
- style: TextStyle(
- color: MyColors.c333333,
- fontSize: 16,
- fontWeight: FontWeight.bold),
- ),
- margin: EdgeInsets.only(top: 25, bottom: 5),
- ),
- Text(
- widget.couponData == null
- ? order.amount.toStringAsFixed(2)
- : NumUtil.subtract(order.amount,
- widget.couponData.amount) <
- 0
- ? 0.0.toStringAsFixed(2)
- : NumUtil.subtract(order.amount,
- widget.couponData.amount)
- .toStringAsFixed(2),
- style: TextStyle(
- color: MyColors.c333333,
- fontSize: 26,
- fontWeight: FontWeight.bold),
- ),
- Container(
- margin: EdgeInsets.only(
- top: 5,
- ),
- child: MyViews().myText('支付时间:${order.payTime}',
- MyColors.c333333, 12),
- ),
- Container(
- height: 0.5,
- color: MyColors.cE7E7E7,
- margin: EdgeInsets.only(
- left: 21, right: 21, top: 25, bottom: 10),
- ),
- Container(
- margin: EdgeInsets.symmetric(horizontal: 21),
- child: Row(
- children: [
- Container(
- decoration: BoxDecoration(
- color: MyColors.cFF4233,
- borderRadius: BorderRadius.circular(2),
- ),
- height: 16,
- width: 38,
- alignment: Alignment.center,
- child: MyViews()
- .myText('收款方', Colors.white, 10),
- ),
- Container(
- margin:
- EdgeInsets.only(left: 7, right: 5),
- child: ClipRRect(
- borderRadius:
- BorderRadius.circular(12.5),
- child: order.sellerUid == 0
- ? Image.asset(
- 'images/app_logo.png',
- height: 25,
- width: 25,
- )
- : MyViews().netImg(
- imgURL(order.sellerPic),
- 25,
- 25),
- ),
- ),
- MyViews().myText(
- order.sellerUid == 0
- ? '平台'
- : '${order.sellerName.length > 12 ? order.sellerName.substring(0, 12) + '...' : order.sellerName}(${order.sellerUid})',
- MyColors.c333333,
- 14),
- ],
- mainAxisAlignment: MainAxisAlignment.start,
- ),
- ),
- Container(
- margin: EdgeInsets.symmetric(
- horizontal: 21, vertical: 10),
- child: Row(
- children: [
- Container(
- decoration: BoxDecoration(
- color: MyColors.cFF4233,
- borderRadius: BorderRadius.circular(2),
- ),
- height: 16,
- width: 38,
- alignment: Alignment.center,
- child: MyViews()
- .myText('付款方', Colors.white, 10),
- ),
- Container(
- margin:
- EdgeInsets.only(left: 7, right: 5),
- child: ClipRRect(
- borderRadius:
- BorderRadius.circular(12.5),
- child: MyViews().netImg(
- imgURL(order.payerPic), 25, 25),
- ),
- ),
- MyViews().myText(
- '${order.payerName.length > 12 ? order.payerName.substring(0, 12) + '...' : order.payerName}(${order.payerUid})',
- MyColors.c333333,
- 14),
- ],
- mainAxisAlignment: MainAxisAlignment.start,
- ),
- )
- ],
- ),
- ),
- SafeArea(
- child: Container(
- color: Colors.white,
- child: Column(
- children: [
- Container(
- height: 5,
- color: MyColors.cF7F7F7,
- ),
- Container(
- color: Colors.white,
- child: SafeArea(
- top: false,
- child: Container(
- padding:
- EdgeInsets.symmetric(horizontal: 37),
- color: Colors.white,
- width: double.infinity,
- height: 60,
- child: Row(
- children: [
- Expanded(
- child: GestureDetector(
- onTap: () {
- MyTools().toPage(
- context, WalletPage(),
- (then) {
- Navigator.pop(
- context, status);
- });
- },
- behavior:
- HitTestBehavior.translucent,
- child: Container(
- decoration: BoxDecoration(
- borderRadius:
- BorderRadius.only(
- topLeft:
- Radius.circular(20),
- bottomLeft:
- Radius.circular(20),
- ),
- border: Border.all(
- color:
- MyColors.cFF4233,
- width: 1)),
- height: 40,
- child: MyViews().myText(
- '查看流水',
- MyColors.cFF4233,
- 14),
- alignment: Alignment.center),
- ),
- ),
- Expanded(
- child: GestureDetector(
- onTap: () {
- // RootPageView().bNIndex = 0;
- // MyTools().toPage(context, RootPage(), (then){});
- Navigator.pop(context, status);
- },
- behavior:
- HitTestBehavior.translucent,
- child: Container(
- decoration: BoxDecoration(
- borderRadius:
- BorderRadius.only(
- topRight:
- Radius.circular(20),
- bottomRight:
- Radius.circular(20),
- ),
- color: MyColors.cFF4233),
- height: 40,
- child: MyViews().myText(
- '返回首页', Colors.white, 14),
- alignment: Alignment.center),
- ),
- )
- ],
- ),
- ),
- ),
- )
- ],
- ),
- ),
- top: false,
- ),
- ],
- )
- : Center(
- child: Column(
- children: [
- 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()),
- ],
- ),
- ),
- ),
- if (showCancel && status != 1)
- GestureDetector(
- onTap: (){
- resetOrder(widget.orderUID);
- Navigator.pop(context,2);
- },
- child: Container(
- margin: EdgeInsets.symmetric(horizontal: 50,vertical: 50),
- decoration: BoxDecoration(
- color: MyColors.cFF4233,
- borderRadius: BorderRadius.circular(20),
- ),
- height: 40,
- child: Text('取消支付',style: TextStyle(color: Colors.white,fontSize: 16),),
- alignment: Alignment.center,
- ),
- )
- ],
- ),
- ),
- );
- }
- pay() {
- EasyLoading.show();
- getUserWalletBalance((re, hE) {
- if (!hE) {
- UserBalanceEntity balance =
- UserBalanceEntity().fromJson(json.decode(re.data.toString()));
- MyCookie().balance = balance.data;
- double balanceAm = balanceDisplay(balance.data);
- if (widget.payWay == payWayWallet) {
- if (balanceAm >= widget.amount) {
- payWithWallet();
- } else {
- if (widget.couponData != null) {
- if (balanceAm + widget.couponData.amount >= widget.amount) {
- payWithWallet();
- } else {
- showToast('积分不足,请选择其他支付方式');
- status = 2;
- setState(() {});
- }
- } else {
- showToast('积分不足,请选择其他支付方式');
- status = 2;
- setState(() {});
- }
- }
- } else if (widget.payWay == payWayAliPay) {
- payWithAliPay();
- } else if (widget.payWay == payWayWeChat) {
- payByWeChat();
- }
- }
- }, (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 ||
- entity.data.data[0].state == orderStateOtherPaid) {
- order = entity.data.data[0];
- 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;
- if (widget.cancelOrder) {
- revokePayOrder(widget.orderUID);
- } else {
- resetOrder(widget.orderUID);
- }
- setState(() {});
- }
- }
- }
- }, (error) {});
- }
- void payWithWallet() {
- payOrder(widget.couponData, payWayWallet, widget.orderUID, (re, hE) {
- if (!hE) {
- showToast('支付成功');
- count = 30;
- checkIfTheOrderIsPaid();
- }
- }, (e) {}, context);
- }
- void payWithAliPay() {
- payOrder(widget.couponData, 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);
- count = 30;
- checkIfTheOrderIsPaid();
- });
- print(entity.data);
- EasyLoading.instance
- ..contentPadding = EdgeInsets.symmetric(horizontal: 20, vertical: 12)
- ..alignment = Alignment.center
- ..loadingStyle = EasyLoadingStyle.light
- ..contentMargin = EdgeInsets.all(20);
- 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) {
- count = 30;
- showToast('支付成功');
- checkIfTheOrderIsPaid();
- } else {
- showToast(aliPayResultStatus(r));
- if (widget.cancelOrder) {
- revokePayOrder(widget.orderUID);
- } else {
- resetOrder(widget.orderUID);
- }
- setState(() {
- status = 2;
- });
- }
- } catch (e) {
- EasyLoading.dismiss();
- }
- });
- }
- }, (e) {}, context);
- }
- void payByWeChat() {
- payOrder(widget.couponData, widget.payWay, widget.orderUID, (re, hE) {
- if (!hE) {
- PayByWeChatBeanEntity entity =
- PayByWeChatBeanEntity().fromJson(json.decode(re.toString()));
- EventBus().on('WeChatPaymentResponse', (arg) {
- if (arg) {
- count = 30;
- showToast('支付成功');
- checkIfTheOrderIsPaid();
- } else if (!arg) {
- showToast('支付失败');
- if (widget.cancelOrder) {
- revokePayOrder(widget.orderUID);
- } else {
- resetOrder(widget.orderUID);
- }
- status = 2;
- setState(() {});
- }
- EventBus().off('WeChatPaymentResponse');
- });
- payWithWeChat(
- appId: entity.data.appid,
- partnerId: entity.data.partnerid,
- prepayId: entity.data.prepayid,
- packageValue: entity.data.package,
- nonceStr: entity.data.noncestr,
- timeStamp: int.parse(entity.data.timestamp),
- sign: entity.data.sign,
- );
- }
- }, (e) {}, context);
- }
- }
|