Browse Source

上传支付

newdev_shunjiawei
liwei1dao 1 year ago
parent
commit
3fce297ff4
  1. 4
      lib/core/bindings/initial_binding.dart
  2. 9
      lib/data/services/network/api.dart
  3. 120
      lib/data/services/pay_service.dart
  4. 9
      lib/modules/login/controllers/weixinLogin.dart

4
lib/core/bindings/initial_binding.dart

@ -1,3 +1,4 @@
import 'package:voitrans/data/services/pay_service.dart';
import 'package:voitrans/data/services/qq_music.dart'; import 'package:voitrans/data/services/qq_music.dart';
import '../../../data/services/user_portrait.dart'; import '../../../data/services/user_portrait.dart';
@ -87,6 +88,9 @@ class InitialBinding extends Bindings {
// OTA检查服务 // OTA检查服务
Get.lazyPut<OtaCheckService>(() => OtaCheckService(), fenix: true); Get.lazyPut<OtaCheckService>(() => OtaCheckService(), fenix: true);
// 支付服务
Get.lazyPut<PayService>(() => PayService(), fenix: true);
// 杰理蓝牙服务 // 杰理蓝牙服务
// // 语言管理器(需要最先初始化) // // 语言管理器(需要最先初始化)

9
lib/data/services/network/api.dart

@ -171,4 +171,13 @@ class Api {
params: params, params: params,
); );
} }
//创建支付订单
static paycreateorder([params]) {
return DioManager().request(
NWMethod.post,
'/api/home/pay_createorder',
params: params,
);
}
} }

120
lib/data/services/pay_service.dart

@ -1,9 +1,9 @@
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:fluwx/fluwx.dart'; import 'package:voitrans/data/services/network/api.dart';
import '../../modules/login/controllers/weixinLogin.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 { class PayService extends GetxService {
@ -12,11 +12,20 @@ class PayService extends GetxService {
@override @override
void onInit() { void onInit() {
super.onInit(); super.onInit();
//微信
FlWX().register(
appSecret: '9ceb39df312b3ba95a229b2aa0d15496',
appId: 'wx4b5ec8957ad5e425',
partnerId: '1719983673',
universalLink: 'https://voitrans.net/app/',
params: FlWXCallbackParams(onHttp: (url) async {
return '';
}));
_flwx = FlWX(); _flwx = FlWX();
} }
/// 微信支付 /// 微信支付
/// ///
/// [paymentData] 支付参数,包含以下字段: /// [paymentData] 支付参数,包含以下字段:
/// - appId: 应用ID /// - appId: 应用ID
/// - partnerId: 商户号 /// - partnerId: 商户号
@ -25,7 +34,7 @@ class PayService extends GetxService {
/// - nonceStr: 随机字符串 /// - nonceStr: 随机字符串
/// - timestamp: 时间戳 /// - timestamp: 时间戳
/// - sign: 签名 /// - sign: 签名
/// ///
/// [onResult] 支付结果回调 /// [onResult] 支付结果回调
Future<bool> wechatPay({ Future<bool> wechatPay({
required Map<String, dynamic> paymentData, required Map<String, dynamic> paymentData,
@ -70,54 +79,65 @@ class PayService extends GetxService {
} }
/// 创建微信支付订单 /// 创建微信支付订单
/// ///
/// [orderInfo] 订单信息,包含: /// [productId] 商品ID
/// - orderId: 订单ID /// [productName] 商品名称
/// - amount: 支付金额(分) /// [productDesc] 商品描述
/// - description: 商品描述 /// [amount] 支付金额(分)
/// - userId: 用户ID /// [paymentMethod] 支付方式(1=微信支付)
/// /// [clientIp] 客户端IP(可选)
/// [notifyUrl] 通知URL(可选)
/// [extraData] 扩展数据(可选)
///
/// 返回支付参数,用于调用 [wechatPay] 方法 /// 返回支付参数,用于调用 [wechatPay] 方法
Future<Map<String, dynamic>?> createWechatPayOrder({ Future<Map<String, dynamic>?> createWechatPayOrder({
required String orderId, required String productId,
required String productName,
String? productDesc,
required int amount, required int amount,
required String description, int paymentMethod = 4, // 1表示微信支付
String? userId, String? clientIp,
String? notifyUrl,
String? extraData,
}) async { }) async {
try { try {
// TODO: 这里需要调用后端API创建支付订单 // 调用后端API创建支付订单
// 示例代码,实际需要根据后端接口调整 final response = await Api.paycreateorder({
'product_id': productId,
// final response = await Api.createPayOrder({ 'product_name': productName,
// 'orderId': orderId, 'product_desc': productDesc ?? '',
// 'amount': amount, 'amount': amount,
// 'description': description, 'payment_method': paymentMethod,
// 'userId': userId, 'client_ip': clientIp ?? '',
// 'payType': 'wechat', 'notify_url': notifyUrl ?? '',
// }); 'extra_data': extraData ?? '',
});
// if (response['success'] == true) { print('创建支付订单: $response');
// return response['data'] as Map<String, dynamic>; // 检查响应是否成功
// } if (response != null) {
final pay_params = response["pay_params"] as Map<String, dynamic>;
// 临时返回示例数据结构,实际使用时需要删除 // 返回微信支付所需的参数
return { return {
'appId': 'your_app_id', 'appId': pay_params['appid'],
'partnerId': 'your_partner_id', 'partnerId': pay_params['partnerid'],
'prepayId': 'prepay_id_from_server', 'prepayId': pay_params['prepayid'],
'packageValue': 'Sign=WXPay', 'packageValue': pay_params['package'] ?? 'Sign=WXPay',
'nonceStr': 'random_string', 'nonceStr': pay_params['noncestr'],
'timestamp': DateTime.now().millisecondsSinceEpoch ~/ 1000, 'timestamp': pay_params['timestamp'],
'sign': 'signature_from_server', 'sign': pay_params['sign'],
}; };
} else {
print('创建支付订单失败: ${response?['message'] ?? '未知错误'}');
return null;
}
} catch (e) { } catch (e) {
print('创建支付订单失败: $e'); print('创建支付订单异常: $e');
return null; return null;
} }
} }
/// 查询支付结果 /// 查询支付结果
/// ///
/// [orderId] 订单ID /// [orderId] 订单ID
/// 返回支付状态:success, failed, pending /// 返回支付状态:success, failed, pending
Future<String?> queryPaymentStatus(String orderId) async { Future<String?> queryPaymentStatus(String orderId) async {
@ -125,7 +145,7 @@ class PayService extends GetxService {
// TODO: 调用后端API查询支付状态 // TODO: 调用后端API查询支付状态
// final response = await Api.queryPaymentStatus({'orderId': orderId}); // final response = await Api.queryPaymentStatus({'orderId': orderId});
// return response['status'] as String?; // return response['status'] as String?;
// 临时返回,实际使用时需要删除 // 临时返回,实际使用时需要删除
return 'pending'; return 'pending';
} catch (e) { } catch (e) {
@ -135,7 +155,7 @@ class PayService extends GetxService {
} }
/// 完整的支付流程 /// 完整的支付流程
/// ///
/// [orderId] 订单ID /// [orderId] 订单ID
/// [amount] 支付金额(分) /// [amount] 支付金额(分)
/// [description] 商品描述 /// [description] 商品描述
@ -151,10 +171,12 @@ class PayService extends GetxService {
try { try {
// 1. 创建支付订单 // 1. 创建支付订单
final paymentData = await createWechatPayOrder( final paymentData = await createWechatPayOrder(
orderId: orderId, productId: 'prod_001',
amount: amount, productName: '会员充值',
description: description, productDesc: '1个月会员服务',
userId: userId, amount: 1, // 29.99元,单位:分
paymentMethod: 4, // 微信支付
notifyUrl: 'https://api.ideapsound.com/api/home/pay_wechatnotify',
); );
if (paymentData == null) { if (paymentData == null) {
@ -172,4 +194,4 @@ class PayService extends GetxService {
return false; return false;
} }
} }
} }

9
lib/modules/login/controllers/weixinLogin.dart

@ -73,6 +73,11 @@ class FlWX {
/// 获取当前设置的 appSecret /// 获取当前设置的 appSecret
String? get appSecret => _appSecret; String? get appSecret => _appSecret;
String? _partnerId;
/// 获取当前设置的 partnerId
String? get partnerId => _partnerId;
String _appName = ''; String _appName = '';
String token = ''; String token = '';
String code = ''; String code = '';
@ -109,6 +114,9 @@ class FlWX {
/// 用于获取用户信息 /// 用于获取用户信息
String? appSecret, String? appSecret,
/// 商户号
String? partnerId,
/// app名称(用于分享时使用) /// app名称(用于分享时使用)
String appName = '', String appName = '',
@ -123,6 +131,7 @@ class FlWX {
_isRegister = true; _isRegister = true;
_appId = appId; _appId = appId;
_appSecret = appSecret; _appSecret = appSecret;
_partnerId = partnerId;
_appName = appName; _appName = appName;
_shareThumbnail = shareThumbnail; _shareThumbnail = shareThumbnail;
_params = params; _params = params;

Loading…
Cancel
Save