import 'package:get/get.dart'; import 'package:fluwx/fluwx.dart'; import '../../modules/login/controllers/weixinLogin.dart'; /// 支付结果回调 typedef PaymentResultCallback = void Function(bool success, String? message); /// 支付服务 class PayService extends GetxService { late FlWX _flwx; @override void onInit() { super.onInit(); _flwx = FlWX(); } /// 微信支付 /// /// [paymentData] 支付参数,包含以下字段: /// - appId: 应用ID /// - partnerId: 商户号 /// - prepayId: 预支付交易会话ID /// - packageValue: 扩展字段,固定值"Sign=WXPay" /// - nonceStr: 随机字符串 /// - timestamp: 时间戳 /// - sign: 签名 /// /// [onResult] 支付结果回调 Future wechatPay({ required Map paymentData, PaymentResultCallback? onResult, }) async { try { // 检查微信是否安装 if (!await _flwx.isInstalled) { onResult?.call(false, '微信未安装'); return false; } // 将支付数据转换为Payment对象 final payment = paymentData.toPayment(); if (payment == null) { onResult?.call(false, '支付参数格式错误'); return false; } // 调用微信支付 final result = await _flwx.pay( payment, onSuccess: () { onResult?.call(true, '支付成功'); }, onResponse: (response) { if (response.errCode == 0) { onResult?.call(true, '支付成功'); } else if (response.errCode == -2) { onResult?.call(false, '用户取消支付'); } else { onResult?.call(false, '支付失败: ${response.errStr}'); } }, ); return result; } catch (e) { onResult?.call(false, '支付异常: $e'); return false; } } /// 创建微信支付订单 /// /// [orderInfo] 订单信息,包含: /// - orderId: 订单ID /// - amount: 支付金额(分) /// - description: 商品描述 /// - userId: 用户ID /// /// 返回支付参数,用于调用 [wechatPay] 方法 Future?> createWechatPayOrder({ required String orderId, required int amount, required String description, String? userId, }) async { try { // TODO: 这里需要调用后端API创建支付订单 // 示例代码,实际需要根据后端接口调整 // final response = await Api.createPayOrder({ // 'orderId': orderId, // 'amount': amount, // 'description': description, // 'userId': userId, // 'payType': 'wechat', // }); // if (response['success'] == true) { // return response['data'] as Map; // } // 临时返回示例数据结构,实际使用时需要删除 return { 'appId': 'your_app_id', 'partnerId': 'your_partner_id', 'prepayId': 'prepay_id_from_server', 'packageValue': 'Sign=WXPay', 'nonceStr': 'random_string', 'timestamp': DateTime.now().millisecondsSinceEpoch ~/ 1000, 'sign': 'signature_from_server', }; } catch (e) { print('创建支付订单失败: $e'); return null; } } /// 查询支付结果 /// /// [orderId] 订单ID /// 返回支付状态:success, failed, pending Future queryPaymentStatus(String orderId) async { try { // TODO: 调用后端API查询支付状态 // final response = await Api.queryPaymentStatus({'orderId': orderId}); // return response['status'] as String?; // 临时返回,实际使用时需要删除 return 'pending'; } catch (e) { print('查询支付状态失败: $e'); return null; } } /// 完整的支付流程 /// /// [orderId] 订单ID /// [amount] 支付金额(分) /// [description] 商品描述 /// [userId] 用户ID /// [onResult] 支付结果回调 Future processWechatPayment({ required String orderId, required int amount, required String description, String? userId, PaymentResultCallback? onResult, }) async { try { // 1. 创建支付订单 final paymentData = await createWechatPayOrder( orderId: orderId, amount: amount, description: description, userId: userId, ); if (paymentData == null) { onResult?.call(false, '创建支付订单失败'); return false; } // 2. 调用微信支付 return await wechatPay( paymentData: paymentData, onResult: onResult, ); } catch (e) { onResult?.call(false, '支付流程异常: $e'); return false; } } }