import 'package:fluwx/fluwx.dart' as fluwx; // import 'package:flutter_alipay/flutter_alipay.dart'; // import 'package:stripe_payment/stripe_payment.dart'; import 'package:flutter_paydemo/models/payment.dart'; class PayService { PayService._(); /// 发起微信支付 Future createWechatPayment({ required String productId, String? source, }) async { return sendPostRequest( '/v1/payment/wechatpay/', (resp) { return WechatPaymentCreatedResponse.fromJson(resp.data); }, formData: { 'product_id': productId, 'source': source, }, ); } void wechatPay(PaymentProduct product) async { bool isInstalled = await fluwx.isWeChatAppInstalled(); try { final created = await APIServer().createWechatPayment( productId: product.id, source: paymentSource(), ); paymentId = created.paymentId; if (PlatformTool.isAndroid() || PlatformTool.isIOS()) { await fluwx.payWithWeChat( appId: created.appId!, partnerId: created.partnerId!, prepayId: created.prepayId!, packageValue: created.package!, nonceStr: created.noncestr!, timeStamp: int.parse(created.timestamp!), sign: created.sign!, ); } else { openDialog( // ignore: use_build_context_synchronously context, builder: Builder(builder: (context) { return Container( alignment: Alignment.center, height: 250, width: 220, margin: const EdgeInsets.only(top: 20), child: Column( children: [ ClipRRect( borderRadius: BorderRadius.circular(8), child: QrImageView( data: created.codeUrl!, version: QrVersions.auto, size: 200, backgroundColor: Colors.white, ), ), const SizedBox(height: 10), const Text( '请使用微信扫码支付', style: TextStyle( fontSize: 14, ), ), ], ), ); }), onSubmit: () { _startPaymentLoading(); APIServer().queryPaymentStatus(created.paymentId).then((resp) { if (resp.success) { showSuccessMessage(resp.note ?? '支付成功'); _closePaymentLoading(); } else { // 支付失败,延迟 5s 再次查询支付状态 Future.delayed(const Duration(seconds: 5), () async { try { final value = await APIServer().queryPaymentStatus(created.paymentId); if (value.success) { showSuccessMessage(value.note ?? '支付成功'); } else { showErrorMessage('支付未完成,我们接收到的状态为:${value.note}'); } } catch (e) { // ignore: use_build_context_synchronously showErrorMessage(resolveError(context, e)); } finally { _closePaymentLoading(); } }); } }); return true; }, confirmText: '已完成支付', barrierDismissible: false, ); } } on Exception catch (e) { // ignore: use_build_context_synchronously showErrorMessageEnhanced(context, e); } finally { _closePaymentLoading(); } } void alipay() async { String orderString = 'your_order_string'; // 从服务器获取 // var result = await FlutterAlipay.pay(orderString); } void stripe() async { // var token = await StripePayment.paymentRequestWithCardForm( // CardFormPaymentRequest()); // 发送 token 到服务器进行支付处理 } Future sendPostRequest( String s, WechatPaymentCreatedResponse Function(dynamic resp) param1, {required Map formData}) {} }