插件使用tobias

  # 支付宝支付
  tobias: ^5.2.0


# Tobias 支付宝插件配置
tobias:
  url_scheme: chenhaiduanju_alipay
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:chenhaiduanju/common/index.dart';
import 'package:tobias/tobias.dart';

class VipController extends GetxController {
  VipController();
  Tobias tobias = Tobias();


  // 支付宝支付
  void alipayPay() async {
    // 支付宝支付逻辑
    try {
      var res = await UserApi.buyVip(1);
      if (res.isNotEmpty) {
        // 获取支付宝支付参数
        var payOrderString = res;
        print('支付宝支付参数: $payOrderString');

        if (payOrderString.isNotEmpty) {
          // 检查支付宝是否已安装
          bool isAliPayInstalled = await tobias.isAliPayInstalled;
          print('支付宝是否已安装: $isAliPayInstalled');

          if (!isAliPayInstalled) {
            Get.snackbar('提示', '请先安装支付宝客户端', backgroundColor: Colors.orange);
            return;
          }

          // 调用支付宝支付
          print('开始调用支付宝支付...');
          var result = await tobias.pay(payOrderString);
          print('支付宝支付结果: $result');

          // 处理支付结果
          String resultStatus = result['resultStatus'] ?? '';
          String memo = result['memo'] ?? '';
          
          if (resultStatus == '9000') {
            // 支付成功
            Get.snackbar('提示', '购买VIP成功!', backgroundColor: Colors.green);
          } else if (resultStatus == '8000') {
            // 正在处理中
            Get.snackbar('提示', '支付处理中,请稍后查询结果', backgroundColor: Colors.orange);
          } else if (resultStatus == '4000') {
            // 支付失败
            Get.snackbar('失败', '支付失败,请重试', backgroundColor: Colors.red);
          } else if (resultStatus == '5000') {
            // 重复请求
            Get.snackbar('提示', '重复请求,请检查订单状态', backgroundColor: Colors.orange);
          } else if (resultStatus == '6001') {
            // 用户取消
            Get.snackbar('提示', '支付已取消', backgroundColor: Colors.orange);
          } else if (resultStatus == '6002') {
            // 网络连接出错
            Get.snackbar('错误', '网络连接出错,请检查网络', backgroundColor: Colors.red);
          } else {
            // 其他错误
            Get.snackbar('错误', '支付异常: $memo', backgroundColor: Colors.red);
          }
        } else {
          Get.snackbar('错误', '获取支付参数失败', backgroundColor: Colors.red);
        }
      } else {
        Get.snackbar('错误', '创建订单失败', backgroundColor: Colors.red);
      }
    } catch (e) {
      print('购买VIP异常: $e');
      Get.snackbar('错误', '购买失败: $e', backgroundColor: Colors.red);
    }

    update(["vip"]);
  }
}

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐