123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- 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<WechatPaymentCreatedResponse> 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<WechatPaymentCreatedResponse> sendPostRequest(
- String s, WechatPaymentCreatedResponse Function(dynamic resp) param1,
- {required Map<String, String?> formData}) {}
- }
|