payment.dart 5.8 KB

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