条纹:添加默认卡,并且不覆盖旧卡

时间:2018-11-27 09:36:28

标签: java stripe-payments

我正在使用Stripe Customers, Subscription and Cards

现在,我有一个场景,客户可以拥有多张卡。

现在,客户添加了一张新卡。而且我必须将该新添加的卡标记为default_source

所以我在做什么

Map<String, Object> params = new HashMap<String, Object>();
params.put("source", token.getId());
Customer customer = Customer.retrieve(user.getStripeId());
customerId = customer.getId();
Customer updatedCustomer = customer.update(params);

这部分代码正在更新客户,并按预期将当前卡标记为default_source

但是,如果客户已经拥有卡,则它将用新卡覆盖旧卡。因此,旧卡将从该客户中删除。

现在我想要的是,如果客户已经拥有该卡,那么我想将该卡标记为辅助卡,然后添加新卡并将其标记为default_source

那我该怎么办?

1 个答案:

答案 0 :(得分:1)

您需要https://stripe.com/docs/api/sources/attach?lang=javahttps://stripe.com/docs/api/customers/update?lang=java#update_customer-default_source

Customer customer = Customer.retrieve(user.getStripeId());
// add a new payment source to the customer
Map<String, Object> params = new HashMap<String, Object>();
params.put("source", token.getId());
Source source = customer.getSources().create(params);

// make it the default
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("default_source", source.getId());
customer.update(updateParams);