payment.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import 'dart:async';
  2. import 'package:bot_toast/bot_toast.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:twong/utils/pay.dart';
  5. // import 'package:wechat_kit/wechat_kit.dart';
  6. import 'package:alipay_kit/alipay_kit.dart';
  7. import 'package:twong/api/index.dart';
  8. import 'package:twong/utils/index.dart';
  9. import 'package:twong/router/index.dart';
  10. import 'package:twong/models/index.dart';
  11. import 'package:twong/config/pay_config.dart';
  12. class PaymentInfo {
  13. int addressId;
  14. num integral;
  15. String orderId;
  16. String orderKey;
  17. String price;
  18. num rechargeId;
  19. PaymentInfo.id(this.orderId);
  20. PaymentInfo.recharge(this.rechargeId, this.price);
  21. PaymentInfo(this.orderKey, this.addressId, this.integral);
  22. }
  23. class Payment extends StatefulWidget {
  24. final PaymentInfo info;
  25. Payment(this.info);
  26. @override
  27. State<StatefulWidget> createState() {
  28. return _PaymentState();
  29. }
  30. }
  31. class _PaymentState extends State<Payment> {
  32. String _orderId = "";
  33. @override
  34. void initState() {
  35. super.initState();
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. return SafeArea(
  40. child: Container(
  41. height: 260.px,
  42. child: Column(
  43. children: [
  44. Stack(
  45. children: [
  46. Container(
  47. width: double.infinity,
  48. margin: EdgeInsets.only(top: 6.px, bottom: 6.px),
  49. child: Text("选择付款方式", style: TextStyle(fontSize: 16.px), textAlign: TextAlign.center)
  50. ),
  51. Positioned(
  52. top: 8.px,
  53. right: 12.px,
  54. child: InkWell(
  55. onTap: () => Navigator.pop(context),
  56. child: Icon(Icons.close, color: Colors.grey, size: 18.px),
  57. ))
  58. ],
  59. ),
  60. _buildPaymentButtons()
  61. ],
  62. ),
  63. ),
  64. );
  65. }
  66. Widget _buildPaymentButtons() {
  67. List<Widget> widgets = List<Widget>();
  68. var configs = PayConfig.payTypes;
  69. if(widget.info.rechargeId != null) {
  70. configs.removeWhere((element) => element.key == "yue");
  71. }
  72. for(var conf in configs) {
  73. widgets.add(Divider());
  74. widgets.add(InkWell(
  75. onTap: () => _onPay(conf),
  76. child: Container(
  77. height: 40.px,
  78. margin: EdgeInsets.only(left: 12.px, right: 12.px),
  79. child: Row(
  80. children: [
  81. Container(
  82. margin: EdgeInsets.only(right: 16.px, left: 6.px),
  83. child: conf.icon,
  84. ),
  85. Expanded(child: Column(
  86. crossAxisAlignment: CrossAxisAlignment.start,
  87. children: [
  88. Text("${conf.name}支付", style: TextStyle(fontSize: 14.px)),
  89. conf.type == PayType.yue ? RichText(text: TextSpan(
  90. text: conf.desc,
  91. style: TextStyle(color: Colors.grey),
  92. children: [TextSpan(
  93. text: Utils.formatRMB(Cache.user.now_money, show: true),
  94. style: TextStyle(color: Colors.redAccent, fontSize: 12.px)
  95. )]
  96. )) : Text(conf.desc, style: TextStyle(color: Colors.grey, fontSize: 12.px))
  97. ],
  98. )),
  99. Icon(Icons.chevron_right, color: Colors.grey)
  100. ],
  101. ),
  102. ),
  103. ));
  104. }
  105. widgets.add(Divider());
  106. return Container(
  107. child: Column(children: widgets),
  108. );
  109. }
  110. void _onPay(PayInfo conf) async {
  111. Utils.loading();
  112. OrderInfo info;
  113. try {
  114. if (widget.info.orderId != null) {
  115. info = await Network.inst.payOrder(payType: conf.key,
  116. orderId: widget.info.orderId);
  117. _orderId = widget.info.orderId;
  118. } else if(widget.info.orderKey != null) {
  119. info = await Network.inst.createOrder(payType: conf.key,
  120. address: widget.info.addressId,
  121. orderKey: widget.info.orderKey,
  122. integral: widget.info.addressId);
  123. _orderId = info.result["orderId"];
  124. } else if(widget.info.price != null) {
  125. info = await Network.inst.recharge(type: conf.key,
  126. price: widget.info.price,
  127. id: widget.info.rechargeId);
  128. }
  129. Log.d(info.toJson());
  130. } catch (err, stack) {
  131. // Log.d(err);
  132. // Log.e(stack);
  133. Utils.closeLoading();
  134. Navigator.pop(context);
  135. return;
  136. }
  137. switch(conf.type) {
  138. case PayType.alipay:
  139. aliPay(info);
  140. break;
  141. case PayType.wechat:
  142. wechatPay(info);
  143. break;
  144. case PayType.yue:
  145. _payOver();
  146. break;
  147. default:
  148. Log.e("Unknown pay type: ${conf.key}");
  149. break;
  150. }
  151. }
  152. void aliPay(OrderInfo info) {
  153. var payConf = info.result["jsConfig"];
  154. Pay.aliPay(payConf, callback: (success) {
  155. _payOver();
  156. });
  157. }
  158. void wechatPay(OrderInfo info) {
  159. var payConf = info.result["jsConfig"];
  160. Pay.wechatPay(payConf, callback: (success) {
  161. _payOver();
  162. });
  163. }
  164. void _listenAliPay(AlipayResp resp) {
  165. Log.d('Alipay : ${resp.resultStatus} - ${resp.result} ${resp.memo}');
  166. Utils.closeLoading();
  167. }
  168. // void _listenWechatPay(WechatPayResp resp) {
  169. // Log.d('WechatPay : ${resp.returnKey} - ${resp.errorCode}: ${resp.errorMsg}');
  170. // Utils.closeLoading();
  171. // }
  172. void _payOver() {
  173. Utils.closeLoading();
  174. Navigator.pushNamedAndRemoveUntil(context, RouteNames.orderDetails,
  175. ModalRoute.withName('/'), arguments: _orderId);
  176. }
  177. }