You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

143 lines
5.0 KiB

import 'package:get/get.dart';
import '../../../data/services/pay_service.dart';
class PaymentController extends GetxController {
final PayService _payService = Get.find<PayService>();
// 支付状态
final RxBool isLoading = false.obs;
final RxString paymentStatus = ''.obs;
final RxString errorMessage = ''.obs;
// 测试订单信息
final RxString testProductId = 'vip_101'.obs;
final RxDouble testAmount = 0.01.obs; // 测试金额 1分钱
// 支付方式选择
final RxInt selectedPaymentMethod = 1.obs; // 1=微信支付, 2=支付宝支付
@override
void onInit() {
super.onInit();
// 生成测试订单ID
// testProductId.value = 'test_${DateTime.now().millisecondsSinceEpoch}';
}
/// 测试微信支付
Future<void> testWechatPayment() async {
try {
isLoading.value = true;
paymentStatus.value = 'startingWechatPayment'.tr; // 正在发起微信支付...
errorMessage.value = '';
// 调用支付服务,使用回调处理结果
final launched = await _payService.processWechatPayment(
productId: testProductId.value,
amount: (testAmount.value * 100).toInt(), // 转换为分
description: 'paymentTestWechatDescription'.tr, // 微信支付测试
onResult: (success, message) {
// 支付结果回调
isLoading.value = false;
if (success) {
paymentStatus.value = 'wechatPaymentSuccess'.tr; // 微信支付成功!
errorMessage.value = '';
Get.snackbar(
'paymentSuccess'.tr, // 支付成功
message ?? 'wechatPaymentTestCompleted'.tr, // 微信支付测试完成
snackPosition: SnackPosition.TOP,
);
} else {
paymentStatus.value = 'wechatPaymentFailed'.tr; // 微信支付失败
errorMessage.value = message ?? 'unknownError'.tr; // 未知错误
Get.snackbar(
'paymentFailed'.tr, // 支付失败
message ?? 'unknownError'.tr, // 未知错误
snackPosition: SnackPosition.TOP,
);
}
},
);
if (!launched) {
// 如果支付没有成功启动
isLoading.value = false;
paymentStatus.value = 'wechatPaymentLaunchFailed'.tr; // 微信支付启动失败
errorMessage.value = 'cannotLaunchWechatPayment'.tr; // 无法启动微信支付
Get.snackbar('paymentLaunchFailed'.tr, 'cannotLaunchWechatPayment'.tr);
}
} catch (e) {
isLoading.value = false;
paymentStatus.value = 'wechatPaymentException'.tr; // 微信支付异常
errorMessage.value = e.toString();
Get.snackbar('paymentException'.tr, e.toString()); // 支付异常
}
}
/// 测试支付宝支付
Future<void> testAlipayPayment() async {
try {
isLoading.value = true;
paymentStatus.value = 'startingAlipayPayment'.tr; // 正在发起支付宝支付...
errorMessage.value = '';
// 调用支付宝支付服务
final launched = await _payService.processAlipayPayment(
productId: testProductId.value, // 这里传递的是产品ID
amount: (testAmount.value * 100).toInt(), // 转换为分
description: 'paymentTestAlipayDescription'.tr, // 支付宝支付测试
onResult: (success, message) {
// 支付结果回调
isLoading.value = false;
if (success) {
paymentStatus.value = 'alipayPaymentSuccess'.tr; // 支付宝支付成功!
errorMessage.value = '';
} else {
paymentStatus.value = 'alipayPaymentFailed'.tr; // 支付宝支付失败
errorMessage.value = message ?? 'unknownError'.tr; // 未知错误
}
},
);
if (!launched) {
// 如果支付没有成功启动
isLoading.value = false;
paymentStatus.value = 'alipayPaymentLaunchFailed'.tr; // 支付宝支付启动失败
errorMessage.value = 'cannotLaunchAlipayPayment'.tr; // 无法启动支付宝支付
Get.snackbar('paymentLaunchFailed'.tr, 'cannotLaunchAlipayPayment'.tr);
}
} catch (e) {
isLoading.value = false;
paymentStatus.value = 'alipayPaymentException'.tr; // 支付宝支付异常
errorMessage.value = e.toString();
Get.snackbar('paymentException'.tr, e.toString()); // 支付异常
}
}
/// 根据选择的支付方式进行支付
Future<void> processPayment() async {
if (selectedPaymentMethod.value == 1) {
await testWechatPayment();
} else if (selectedPaymentMethod.value == 2) {
await testAlipayPayment();
}
}
/// 选择支付方式
void selectPaymentMethod(int method) {
selectedPaymentMethod.value = method;
}
/// 重置状态
void resetStatus() {
paymentStatus.value = '';
errorMessage.value = '';
// testProductId.value = 'test_${DateTime.now().millisecondsSinceEpoch}';
}
/// 更新测试金额
void updateTestAmount(double amount) {
testAmount.value = amount;
}
}