|
|
|
@ -1,9 +1,9 @@ |
|
|
|
import 'package:get/get.dart'; |
|
|
|
import 'package:fluwx/fluwx.dart'; |
|
|
|
import 'package:voitrans/data/services/network/api.dart'; |
|
|
|
|
|
|
|
import '../../modules/login/controllers/weixinLogin.dart'; |
|
|
|
|
|
|
|
/// 支付结果回调 |
|
|
|
typedef PaymentResultCallback = void Function(bool success, String? message); |
|
|
|
typedef PaymentResultCallback = void Function(bool success, String message); |
|
|
|
|
|
|
|
/// 支付服务 |
|
|
|
class PayService extends GetxService { |
|
|
|
@ -12,6 +12,15 @@ class PayService extends GetxService { |
|
|
|
@override |
|
|
|
void onInit() { |
|
|
|
super.onInit(); |
|
|
|
//微信 |
|
|
|
FlWX().register( |
|
|
|
appSecret: '9ceb39df312b3ba95a229b2aa0d15496', |
|
|
|
appId: 'wx4b5ec8957ad5e425', |
|
|
|
partnerId: '1719983673', |
|
|
|
universalLink: 'https://voitrans.net/app/', |
|
|
|
params: FlWXCallbackParams(onHttp: (url) async { |
|
|
|
return ''; |
|
|
|
})); |
|
|
|
_flwx = FlWX(); |
|
|
|
} |
|
|
|
|
|
|
|
@ -71,47 +80,58 @@ class PayService extends GetxService { |
|
|
|
|
|
|
|
/// 创建微信支付订单 |
|
|
|
/// |
|
|
|
/// [orderInfo] 订单信息,包含: |
|
|
|
/// - orderId: 订单ID |
|
|
|
/// - amount: 支付金额(分) |
|
|
|
/// - description: 商品描述 |
|
|
|
/// - userId: 用户ID |
|
|
|
/// [productId] 商品ID |
|
|
|
/// [productName] 商品名称 |
|
|
|
/// [productDesc] 商品描述 |
|
|
|
/// [amount] 支付金额(分) |
|
|
|
/// [paymentMethod] 支付方式(1=微信支付) |
|
|
|
/// [clientIp] 客户端IP(可选) |
|
|
|
/// [notifyUrl] 通知URL(可选) |
|
|
|
/// [extraData] 扩展数据(可选) |
|
|
|
/// |
|
|
|
/// 返回支付参数,用于调用 [wechatPay] 方法 |
|
|
|
Future<Map<String, dynamic>?> createWechatPayOrder({ |
|
|
|
required String orderId, |
|
|
|
required String productId, |
|
|
|
required String productName, |
|
|
|
String? productDesc, |
|
|
|
required int amount, |
|
|
|
required String description, |
|
|
|
String? userId, |
|
|
|
int paymentMethod = 4, // 1表示微信支付 |
|
|
|
String? clientIp, |
|
|
|
String? notifyUrl, |
|
|
|
String? extraData, |
|
|
|
}) 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<String, dynamic>; |
|
|
|
// } |
|
|
|
|
|
|
|
// 临时返回示例数据结构,实际使用时需要删除 |
|
|
|
// 调用后端API创建支付订单 |
|
|
|
final response = await Api.paycreateorder({ |
|
|
|
'product_id': productId, |
|
|
|
'product_name': productName, |
|
|
|
'product_desc': productDesc ?? '', |
|
|
|
'amount': amount, |
|
|
|
'payment_method': paymentMethod, |
|
|
|
'client_ip': clientIp ?? '', |
|
|
|
'notify_url': notifyUrl ?? '', |
|
|
|
'extra_data': extraData ?? '', |
|
|
|
}); |
|
|
|
print('创建支付订单: $response'); |
|
|
|
// 检查响应是否成功 |
|
|
|
if (response != null) { |
|
|
|
final pay_params = response["pay_params"] as Map<String, dynamic>; |
|
|
|
// 返回微信支付所需的参数 |
|
|
|
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', |
|
|
|
'appId': pay_params['appid'], |
|
|
|
'partnerId': pay_params['partnerid'], |
|
|
|
'prepayId': pay_params['prepayid'], |
|
|
|
'packageValue': pay_params['package'] ?? 'Sign=WXPay', |
|
|
|
'nonceStr': pay_params['noncestr'], |
|
|
|
'timestamp': pay_params['timestamp'], |
|
|
|
'sign': pay_params['sign'], |
|
|
|
}; |
|
|
|
} else { |
|
|
|
print('创建支付订单失败: ${response?['message'] ?? '未知错误'}'); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
print('创建支付订单失败: $e'); |
|
|
|
print('创建支付订单异常: $e'); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -151,10 +171,12 @@ class PayService extends GetxService { |
|
|
|
try { |
|
|
|
// 1. 创建支付订单 |
|
|
|
final paymentData = await createWechatPayOrder( |
|
|
|
orderId: orderId, |
|
|
|
amount: amount, |
|
|
|
description: description, |
|
|
|
userId: userId, |
|
|
|
productId: 'prod_001', |
|
|
|
productName: '会员充值', |
|
|
|
productDesc: '1个月会员服务', |
|
|
|
amount: 1, // 29.99元,单位:分 |
|
|
|
paymentMethod: 4, // 微信支付 |
|
|
|
notifyUrl: 'https://api.ideapsound.com/api/home/pay_wechatnotify', |
|
|
|
); |
|
|
|
|
|
|
|
if (paymentData == null) { |
|
|
|
|