Stripe 订阅创建时未附加付款来源。

3

目前我正在将我的应用从使用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。
1个回答

4

订阅 Invoices 将使用以下三种可用的付款方式(按优先顺序):

  1. Subscriptiondefault_payment_method (参考) 或 default_source (参考)
  2. Customerinvoice_settings.default_payment_method (参考)
  3. Customerdefault_source (注意:在客户中没有默认 PaymentMethod 的概念)

同时值得注意的是,PaymentMethods 和 Sources 是两个不同的资源。卡片 (ID 以 card_ 为前缀的对象) 可以用作任一类型。


1
您可以使用源(src_card_对象)创建带有来源的客户 - taintedzodiac
在开始我的API更新之前,我的客户创建代码通常会使用从前端Stripe.js收到的令牌设置“source”参数,例如Stripe.createToken,但现在已更改为Stripe.createPaymentMethod,我传递到后端的令牌导致错误“您不能将PaymentMethod ID作为'source'参数提供”...现在我的令牌看起来像“pm_...”,这是我在创建客户时拥有的唯一卡付款表示。我能用这个做些什么吗? - RTF
1
你需要将pm_对象设置为Subscription.default_payment_methodCustomer.invoice_settings.default_payment_method - taintedzodiac
好的,那么我是否需要在以后设置新创建的客户的“default_source”,以便所有这些客户的付款都使用该卡,还是仅设置客户的“invoice_settings.default_payment_method”就足够了? - RTF
1
default_source 用于旧的 Sources API,而 invoice_settings.default_payment_method 则用于新的 PaymentMethods API。就订阅/发票而言,您只需要设置后者即可。 - taintedzodiac
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接