pay_service.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import 'package:fluwx/fluwx.dart' as fluwx;
  2. // import 'package:flutter_alipay/flutter_alipay.dart';
  3. // import 'package:stripe_payment/stripe_payment.dart';
  4. import 'package:flutter_paydemo/models/payment.dart';
  5. class PayService {
  6. PayService._();
  7. /// 发起微信支付
  8. Future<WechatPaymentCreatedResponse> createWechatPayment({
  9. required String productId,
  10. String? source,
  11. }) async {
  12. return sendPostRequest(
  13. '/v1/payment/wechatpay/',
  14. (resp) {
  15. return WechatPaymentCreatedResponse.fromJson(resp.data);
  16. },
  17. formData: {
  18. 'product_id': productId,
  19. 'source': source,
  20. },
  21. );
  22. }
  23. void wechatPay(PaymentProduct product) async {
  24. bool isInstalled = await fluwx.isWeChatAppInstalled();
  25. try {
  26. final created = await APIServer().createWechatPayment(
  27. productId: product.id,
  28. source: paymentSource(),
  29. );
  30. paymentId = created.paymentId;
  31. if (PlatformTool.isAndroid() || PlatformTool.isIOS()) {
  32. await fluwx.payWithWeChat(
  33. appId: created.appId!,
  34. partnerId: created.partnerId!,
  35. prepayId: created.prepayId!,
  36. packageValue: created.package!,
  37. nonceStr: created.noncestr!,
  38. timeStamp: int.parse(created.timestamp!),
  39. sign: created.sign!,
  40. );
  41. } else {
  42. openDialog(
  43. // ignore: use_build_context_synchronously
  44. context,
  45. builder: Builder(builder: (context) {
  46. return Container(
  47. alignment: Alignment.center,
  48. height: 250,
  49. width: 220,
  50. margin: const EdgeInsets.only(top: 20),
  51. child: Column(
  52. children: [
  53. ClipRRect(
  54. borderRadius: BorderRadius.circular(8),
  55. child: QrImageView(
  56. data: created.codeUrl!,
  57. version: QrVersions.auto,
  58. size: 200,
  59. backgroundColor: Colors.white,
  60. ),
  61. ),
  62. const SizedBox(height: 10),
  63. const Text(
  64. '请使用微信扫码支付',
  65. style: TextStyle(
  66. fontSize: 14,
  67. ),
  68. ),
  69. ],
  70. ),
  71. );
  72. }),
  73. onSubmit: () {
  74. _startPaymentLoading();
  75. APIServer().queryPaymentStatus(created.paymentId).then((resp) {
  76. if (resp.success) {
  77. showSuccessMessage(resp.note ?? '支付成功');
  78. _closePaymentLoading();
  79. } else {
  80. // 支付失败,延迟 5s 再次查询支付状态
  81. Future.delayed(const Duration(seconds: 5), () async {
  82. try {
  83. final value =
  84. await APIServer().queryPaymentStatus(created.paymentId);
  85. if (value.success) {
  86. showSuccessMessage(value.note ?? '支付成功');
  87. } else {
  88. showErrorMessage('支付未完成,我们接收到的状态为:${value.note}');
  89. }
  90. } catch (e) {
  91. // ignore: use_build_context_synchronously
  92. showErrorMessage(resolveError(context, e));
  93. } finally {
  94. _closePaymentLoading();
  95. }
  96. });
  97. }
  98. });
  99. return true;
  100. },
  101. confirmText: '已完成支付',
  102. barrierDismissible: false,
  103. );
  104. }
  105. } on Exception catch (e) {
  106. // ignore: use_build_context_synchronously
  107. showErrorMessageEnhanced(context, e);
  108. } finally {
  109. _closePaymentLoading();
  110. }
  111. }
  112. void alipay() async {
  113. String orderString = 'your_order_string'; // 从服务器获取
  114. // var result = await FlutterAlipay.pay(orderString);
  115. }
  116. void stripe() async {
  117. // var token = await StripePayment.paymentRequestWithCardForm(
  118. // CardFormPaymentRequest());
  119. // 发送 token 到服务器进行支付处理
  120. }
  121. Future<WechatPaymentCreatedResponse> sendPostRequest(
  122. String s, WechatPaymentCreatedResponse Function(dynamic resp) param1,
  123. {required Map<String, String?> formData}) {}
  124. }