Hiren
Hiren

Reputation: 1435

Stripe: Add default card and do not override old card

I am working with Stripe Customers, Subscription and Cards.

Now, I have a scenario where, Customer can have multiple cards.

Now, Customer adds a new card. And I have to mark that new added card as default_source.

So what I am doing is

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);

This piece of code is updating a customer and marking the current card as default_source which is as expected.

But if the Customer already has card then it overrides the old Card with new one. So the old card is deleted from that Customer.

Now what I want is, If Customer already have the card then I want to mark that card as secondary and then add new card and mark it default_source.

So how can i do that?

Upvotes: 0

Views: 452

Answers (1)

karllekko
karllekko

Reputation: 7218

You'll want https://stripe.com/docs/api/sources/attach?lang=java and https://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);

Upvotes: 1

Related Questions