原生Stripe支付宝支付UTS插件
Project Start
2025-08-28
Crowdfunding Success
2025-07-31
Launch
2025-08-01
此项目是基于 Stripe Connect API 专为 uniapp/uniappx 的 App 项目定制的 UTS 插件,支持支付宝支付集成。
import * as StripeConnect from "@/uni_modules/bsf-stripe-alipay";
在使用支付功能前,需要先初始化 Stripe 配置。
StripeConnect.initPaymentConfiguration(
  "pk_test_your_publishable_key", // Stripe 可发布密钥
  "acct_your_connect_account_id"   // Stripe Connect 账户ID
);
参数说明:
publicKey (String): Stripe 可发布密钥connectAccountId (String): Stripe Connect 账户ID发起支付宝支付流程。
StripeConnect.confirmAlipayPayment({
  secretKey: "pi_xxx_secret_xxx",           // PaymentIntent 客户端密钥
  stripeAccountId: "acct_xxx",              // Stripe 账户ID(可选)
  returnUrl: "your-app://stripe-redirect",  // 支付完成后的返回URL(iOS必需)
  
  // 支付成功回调
  onSuccess: () => {
    console.log("支付成功!");
    uni.showToast({
      title: '支付成功',
      icon: 'success'
    });
  },
  
  // 支付失败回调
  onFailure: (error) => {
    console.error("支付失败:", error.errMsg);
    uni.showToast({
      title: error.errMsg,
      icon: 'none'
    });
  },
  
  // 支付取消回调
  onCancel: () => {
    console.log("用户取消支付");
    uni.showToast({
      title: '支付已取消',
      icon: 'none'
    });
  }
});
参数说明:
secretKey (String): PaymentIntent 的客户端密钥stripeAccountId (String, 可选): Stripe 账户IDreturnUrl (String, 可选): iOS 支付完成后的返回URLonSuccess (Function, 可选): 支付成功回调onFailure (Function, 可选): 支付失败回调,参数为错误对象onCancel (Function, 可选): 支付取消回调<template>
  <view class="container">
    <button @click="initStripe" class="btn">初始化 Stripe</button>
    <button @click="payWithAlipay" class="btn" :disabled="!isInitialized">支付宝支付</button>
  </view>
</template>
<script>
import * as StripeConnect from "@/uni_modules/bsf-stripe-alipay";
export default {
  data() {
    return {
      isInitialized: false,
      paymentIntentSecret: ''
    }
  },
  
  methods: {
    // 初始化 Stripe 配置
    initStripe() {
      try {
        StripeConnect.initPaymentConfiguration(
          "pk_test_your_publishable_key",
          "acct_your_connect_account_id"
        );
        this.isInitialized = true;
        uni.showToast({
          title: 'Stripe 初始化成功',
          icon: 'success'
        });
      } catch (error) {
        console.error('初始化失败:', error);
        uni.showToast({
          title: '初始化失败',
          icon: 'none'
        });
      }
    },
    
    // 发起支付宝支付
    async payWithAlipay() {
      // 首先从服务器获取 PaymentIntent
      const paymentIntent = await this.createPaymentIntent();
      
      if (!paymentIntent) {
        uni.showToast({
          title: '获取支付信息失败',
          icon: 'none'
        });
        return;
      }
      
      // 发起支付
      StripeConnect.confirmAlipayPayment({
        secretKey: paymentIntent.client_secret,
        returnUrl: "your-app://stripe-redirect", // iOS 必需
        
        onSuccess: () => {
          console.log("支付成功!");
          uni.showModal({
            title: '支付结果',
            content: '支付成功!',
            showCancel: false
          });
        },
        
        onFailure: (error) => {
          console.error("支付失败:", error);
          uni.showModal({
            title: '支付失败',
            content: error.errMsg || '支付过程中出现错误',
            showCancel: false
          });
        },
        
        onCancel: () => {
          console.log("用户取消支付");
          uni.showToast({
            title: '支付已取消',
            icon: 'none'
          });
        }
      });
    },
    
    // 从服务器创建 PaymentIntent
    async createPaymentIntent() {
      try {
        const response = await uni.request({
          url: 'https://your-server.com/api/create-payment-intent',
          method: 'POST',
          data: {
            amount: 2000, // 金额(分)
            currency: 'cny',
            payment_method_types: ['alipay']
          }
        });
        return response.data;
      } catch (error) {
        console.error('创建 PaymentIntent 失败:', error);
        return null;
      }
    }
  }
}
</script>
| 错误码 | 说明 | 
|---|---|
| 9010001 | Stripe未初始化 | 
| 9010002 | 支付失败 | 
| 9010003 | 需要额外操作,请重试 | 
| 9010004 | 支付处理中,请稍后查询结果 | 
| 9010005 | 未知支付状态 | 
| 9010006 | 支付失败 | 
| 9010007 | 支付初始化失败 | 
manifest.json 中配置 URL Scheme{
  "app-plus": {
    "distribute": {
      "ios": {
        "urlschemewhitelist": ["your-app"]
      }
    }
  }
}
需要在服务器端集成 Stripe API 来创建 PaymentIntent,参考 Stripe 官方文档
文档正在制作中,敬请期待...
No versions released yet