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. 104
      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 '../../../data/services/user_portrait.dart';
@ -87,6 +88,9 @@ class InitialBinding extends Bindings {
// OTA检查服务
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,
);
}
//创建支付订单
static paycreateorder([params]) {
return DioManager().request(
NWMethod.post,
'/api/home/pay_createorder',
params: params,
);
}
}

104
lib/data/services/pay_service.dart

@ -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>;
// }
// 临时返回示例数据结构,实际使用时需要删除
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',
};
// 调用后端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': 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) {

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

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

Loading…
Cancel
Save