没有用于创建Stripe订阅的附加付款来源

时间:2019-09-12 12:09:04

标签: java stripe-payments

我目前正在将我的应用程序从使用Stripe Charges 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:该客户没有附加的付款来源;代码:resource_missing

客户有一个卡套(只有1张,所以它必须是默认卡),客户创建呼叫发生在订阅创建呼叫之前,并且客户ID传递到了子创建呼叫。我还需要传递其他参数吗?


在创建客户时,我尝试设置Customer.invoice_settings.default_payment_method,这使我无法进行订阅创建。从Stripe仪表板看一切正常,除了我正在使用SCA测试卡进行测试外,因此在客户进一步进行身份验证之前,交易是不完整的。

我需要响应中的客户端秘密令牌才能继续,我认为我可以从#Subscription.getLatestInvoiceObject().getPaymentIntentObject().getClientSecret()获取它,但是getLatestInvoiceObject()调用返回了null

我没有在默认为collection_method的Subscription对象上设置charge_automatically。这就是我想要的,因为客户处于会话中,因此Invoicenull可能很有意义,但是如何获得客户机密以传递回前端?订阅响应返回的SetupIntent对象公开了客户端机密,但该对象也是null

1 个答案:

答案 0 :(得分:1)

订阅Invoices将使用这三种付款方式中的任何一种(按优先顺序排列):

  1. Subscription的{​​{1}}(reference)或default_payment_methodreference
  2. default_source的{​​{1}}(reference
  3. Customer的{​​{1}}(注意:客户没有默认的PaymentMethod概念)

还值得注意的是,PaymentMethods和Sources是两个不同的资源。卡(ID为invoice_settings.default_payment_method前缀的对象)可以用作这两种类型。