Spring Boot中通过客户端Token创建Stripe支付方式并关联已有客户
解决方案:Spring Boot后端接收前端Token并关联至Stripe客户
核心流程
- 前端通过Stripe.js收集卡片信息生成支付Token(或推荐的PaymentMethod ID),并传递给后端
- 后端借助Stripe Java SDK,将Token转换为PaymentMethod后关联到已有客户
- 可选:将该PaymentMethod设为客户的默认支付方式
步骤1:引入Stripe依赖
在pom.xml中添加Stripe Java SDK的依赖:
<dependency> <groupId>com.stripe</groupId> <artifactId>stripe-java</artifactId> <version>23.3.0</version> <!-- 替换为最新稳定版本 --> </dependency>
步骤2:配置Stripe API密钥
在application.properties中配置你的Stripe秘钥:
stripe.api-key=sk_test_your_secret_key_here
步骤3:后端处理逻辑代码
方法1:接收前端Token,创建PaymentMethod并关联客户
如果前端传递的是卡片Token(格式如tok_visa),可通过以下代码将其转换为PaymentMethod并关联到已有客户:
import com.stripe.Stripe; import com.stripe.exception.StripeException; import com.stripe.model.Customer; import com.stripe.model.PaymentMethod; import com.stripe.param.PaymentMethodCreateParams; import com.stripe.param.PaymentMethodAttachParams; import com.stripe.param.CustomerUpdateParams; import jakarta.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service public class StripePaymentService { @Value("${stripe.api-key}") private String stripeApiKey; @PostConstruct public void init() { Stripe.apiKey = stripeApiKey; } /** * 将前端传入的卡片Token转换为PaymentMethod并关联到已有客户 * @param customerId 已存在的Stripe客户ID(格式如cus_xxxxxx) * @param cardToken 前端通过Stripe.js生成的卡片Token * @return 关联后的PaymentMethod对象 * @throws StripeException */ public PaymentMethod attachPaymentMethodToCustomer(String customerId, String cardToken) throws StripeException { // 1. 用卡片Token创建PaymentMethod PaymentMethodCreateParams createParams = PaymentMethodCreateParams.builder() .setType(PaymentMethodCreateParams.Type.CARD) .setCard(PaymentMethodCreateParams.Card.builder() .setToken(cardToken) .build()) .build(); PaymentMethod paymentMethod = PaymentMethod.create(createParams); // 2. 将PaymentMethod关联到指定客户 PaymentMethodAttachParams attachParams = PaymentMethodAttachParams.builder() .setCustomer(customerId) .build(); return paymentMethod.attach(attachParams); } // 可选:设置该PaymentMethod为客户的默认支付方式 public Customer setDefaultPaymentMethod(String customerId, String paymentMethodId) throws StripeException { Customer customer = Customer.retrieve(customerId); CustomerUpdateParams updateParams = CustomerUpdateParams.builder() .setInvoiceSettings(CustomerUpdateParams.InvoiceSettings.builder() .setDefaultPaymentMethod(paymentMethodId) .build()) .build(); return customer.update(updateParams); } }
方法2:如果前端直接传PaymentMethod ID(推荐)
如果前端使用最新版Stripe Elements生成PaymentMethod ID(格式如pm_xxxxxx),可直接关联到客户,无需转换Token:
public PaymentMethod attachExistingPaymentMethodToCustomer(String customerId, String paymentMethodId) throws StripeException { PaymentMethod paymentMethod = PaymentMethod.retrieve(paymentMethodId); PaymentMethodAttachParams attachParams = PaymentMethodAttachParams.builder() .setCustomer(customerId) .build(); return paymentMethod.attach(attachParams); }
关键注意事项
- 禁止后端直接处理卡号:所有卡片信息必须通过前端Stripe.js/Elements收集,规避PCI合规风险
- 秘钥安全:确保Stripe秘钥(以
sk_开头)仅在后端存储,绝不暴露给前端 - 错误处理:实际业务代码中需捕获
StripeException,根据错误类型(如卡片无效、客户不存在等)向前端返回友好提示
内容的提问来源于stack exchange,提问作者Neel Mani
相关产品推荐
相关产品推荐

