目前我正在将我的应用从使用Stripe收费API迁移到使用Stripe PaymentIntents API,以遵守SCA法规。
我的订阅创建代码大致如下:
Map<String, Object> srchOpts = new HashMap<>();
srchOpts.put("email", userEmail);
List<Customer> matchingCustomers = Customer.list(srchOpts).getData();
Customer customer = null;
Subscription subscription = null;
if ( matchingCustomers.isEmpty() ){
Map<String, Object> params = new HashMap<String, Object>();
params.put("email", userEmail);
params.put("payment_token", stripeToken);
customer = Customer.create(params);
}
else if (matchingCustomers.size() == 1) {
customer = matchingCustomers.get(0);
Map<String, Object> params = new HashMap<String, Object>();
params.put("source", stripeToken);
PaymentSourceCollection paymentSources = customer.getSources();
paymentSources.create(params);
}
Map<String, Object> item = new HashMap<String, Object>();
item.put("plan", planId); // e.g. my-pro-plan (no trial days)
Map<String, Object> items = new HashMap<String, Object>();
items.put("0", item);
Map<String, Object> params = new HashMap<String, Object>();
params.put("items", items);
params.put("customer", customer.getId());
params.put("off_session", false);
subscription = Subscription.create(params);
我可以在 Stripe 仪表板(测试模式下)看到已创建客户并且有指定的信用卡,但 Subscription.create 调用失败并显示以下错误信息:
com.stripe.exception.InvalidRequestException: This customer has no attached payment source; code: resource_missing
该客户设置了信用卡(只有1张,所以必须是默认信用卡),订阅创建调用发生在客户创建调用之前,并将客户 ID 传递给 sub 创建调用。我是否需要传递其他参数?
我尝试在创建客户时设置 Customer.invoice_settings.default_payment_method ,这样就可以通过订阅创建。从 Stripe 仪表盘上看一切都很正常,除了我正在使用 SCA 测试卡进行测试,因此在客户进一步验证之前,交易是不完整的。
我需要来自响应的客户端密钥令牌以继续操作,我认为我可以从 #Subscription.getLatestInvoiceObject().getPaymentIntentObject().getClientSecret() 中获取它,但 getLatestInvoiceObject() 调用返回 null 。
我没有在 Subscription 对象中设置 collection_method ,它默认为 charge_automatically 。这正是我想要的,因为客户端正在进行会话,所以 Invoice 为空可能是有意义的,但是如何获取客户端密钥令牌以返回到前端呢?订阅响应返回的 SetupIntent 对象公开了一个客户端密钥令牌,但该对象也为 null。
src_
或card_
对象)创建带有来源的客户。 - taintedzodiacStripe.createToken
,但现在已更改为Stripe.createPaymentMethod
,我传递到后端的令牌导致错误“您不能将PaymentMethod ID作为'source'参数提供”...现在我的令牌看起来像“pm_...”,这是我在创建客户时拥有的唯一卡付款表示。我能用这个做些什么吗? - RTFpm_
对象设置为Subscription.default_payment_method
或Customer.invoice_settings.default_payment_method
。 - taintedzodiacdefault_source
用于旧的Sources
API,而invoice_settings.default_payment_method
则用于新的PaymentMethods
API。就订阅/发票而言,您只需要设置后者即可。 - taintedzodiac