使用Stripe Android SDK预构建UI Payment Sheet实现订阅支付求助
使用Stripe Payment Sheet实现Android订阅支付
核心流程说明
订阅支付的关键是后端创建待激活的订阅,并生成关联的支付意图(PaymentIntent),再通过Android端的PaymentSheet完成支付,最终激活订阅。以下是完整实现步骤:
1. 后端实现(以Node.js为例)
所有敏感操作必须在后端完成,避免密钥暴露:
const stripe = require('stripe')('你的Stripe秘钥'); // 1. 创建或获取现有客户 const customer = await stripe.customers.create({ email: "user@your-app.com", // 替换为用户实际邮箱 }); // 2. 创建订阅价格(示例:每月9.99美元的订阅计划) const price = await stripe.prices.create({ unit_amount: 999, // 单位:分 currency: "usd", recurring: { interval: "month" }, // 订阅周期:每月 product_data: { name: "高级会员订阅" }, }); // 3. 创建待激活的订阅,关联支付意图 const subscription = await stripe.subscriptions.create({ customer: customer.id, items: [{ price: price.id }], payment_behavior: "default_incomplete", // 订阅先处于待激活状态 expand: ["latest_invoice.payment_intent"], // 展开获取关联的PaymentIntent }); // 4. 生成客户端临时密钥(用于保存支付方式) const ephemeralKey = await stripe.ephemeralKeys.create( { customer: customer.id }, { apiVersion: "2023-10-16" } // 使用你的Stripe API版本 ); // 返回给Android客户端的参数 const clientData = { clientSecret: subscription.latest_invoice.payment_intent.client_secret, customerId: customer.id, ephemeralKeySecret: ephemeralKey.secret, };
2. Android端实现
第一步:添加依赖
在app/build.gradle中引入Stripe SDK:
dependencies { implementation 'com.stripe:stripe-android:20.12.0' // 使用最新稳定版 }
第二步:集成PaymentSheet
import com.stripe.android.paymentsheet.PaymentSheet import com.stripe.android.paymentsheet.PaymentSheetResult import android.widget.Button import android.widget.Toast class SubscriptionPaymentActivity : AppCompatActivity() { private lateinit var paymentSheet: PaymentSheet // 从后端接口获取的参数 private lateinit var clientSecret: String private lateinit var customerId: String private lateinit var ephemeralKeySecret: String override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_subscription_payment) // 1. 调用后端接口获取clientSecret、customerId、ephemeralKeySecret // 此处省略网络请求逻辑,建议用Retrofit等框架实现 // 2. 初始化PaymentSheet paymentSheet = PaymentSheet(this, ::handlePaymentResult) // 3. 绑定支付按钮点击事件 findViewById<Button>(R.id.btn_subscribe).setOnClickListener { val paymentConfig = PaymentSheet.Configuration( customer = PaymentSheet.CustomerConfiguration( id = customerId, ephemeralKeySecret = ephemeralKeySecret ), merchantDisplayName = "你的应用名称" ) // 唤起PaymentSheet paymentSheet.presentWithPaymentIntent(clientSecret, paymentConfig) } } // 处理支付结果 private fun handlePaymentResult(result: PaymentSheetResult) { when (result) { is PaymentSheetResult.Completed -> { // 支付完成,建议通过后端Webhook确认订阅状态后再更新应用内权限 Toast.makeText(this, "订阅成功", Toast.LENGTH_SHORT).show() } is PaymentSheetResult.Canceled -> { Toast.makeText(this, "用户取消支付", Toast.LENGTH_SHORT).show() } is PaymentSheetResult.Failed -> { Toast.makeText(this, "支付失败:${result.error.message}", Toast.LENGTH_SHORT).show() } } } }
关键注意事项
- Webhook监听:必须监听Stripe的
customer.subscription.updated、invoice.paid等事件,确保订阅状态正确同步到后端,避免客户端本地判断误差。 - 版本兼容性:确保Stripe Android SDK版本≥20.0.0,旧版本可能不支持订阅场景的PaymentSheet集成。
- 安全规范:所有敏感操作(创建订阅、价格、客户)必须在后端完成,绝对不能在客户端暴露Stripe秘钥。
内容的提问来源于stack exchange,提问作者Ankit Dubariya
相关产品推荐
相关产品推荐

