I wish to create a payment method in stripe and attach it to a existing customer using java -spring boot. The stripe docs
shares an example code for creating a payment method using a card. The code is
PaymentMethodCreateParams params =
PaymentMethodCreateParams.builder()
.setType(PaymentMethodCreateParams.Type.CARD)
.setCard(
PaymentMethodCreateParams.Card.builder()
.setNumber("4242424242424242")
.setExpMonth(8L)
.setExpYear(2026L)
.setCvc("314")
.build()
)
.build();
PaymentMethod paymentMethod = PaymentMethod.create(params);
But upon running this code, we get response
{
"error": {
"message": "Sending credit card numbers directly to the Stripe API is generally unsafe. We suggest you use test tokens that map to the test card you are using, see https://stripe.com/docs/testing. To enable raw card data APIs in test mode, see https://support.stripe.com/questions/enabling-access-to-raw-card-data-apis.",
"request_log_url": "https://dashboard.stripe.com/test/logs/req_9c6QitRqUgqM1i?t=1699942446",
"type": "invalid_request_error"
}
}
Upon reading further i understood that we need to accept card details in front-end using stripe.js and get a card token which we will pass to server side.
My Question here is that after getting the token from client how do i create a payment method out of it and attach it to an existing customer. Any help in this direction would be greatly appreciated.
My Question here is that after getting the token from client how do i create a payment method out of it and attach it to an existing customer.
I don't really understand the question, the token and the payment method aren't different objects here really, but I understand the confusion.
It's worth mentioning that Stripe does have a function you can call on the frontend to create a Payment Method.
Now, if I recall correctly, you still can't hardcode the card details with it, it will ask you to use Elements instead, but it's not really relevant because this function is really not used beyond very specific use-cases.
The reason why is that collecting a payment method isn't just about collecting the card details, you also need to perform additional steps (i.e. authentication) to authorize your application to charge the saved card later. This is something that this function doesn't do, hence why it shouldn't be used unless you have some logic to perform these actions later.
If you want to collect payment methods for future use and the above documentation isn't relevant, then you have two options:
-Save the payment method without a payment - using a Setup Intent.
This would automatically create a payment method for you when it's successfully confirmed. If you add a customer
to it, it would automatically attach it to them as well.
-Save the payment method with a payment - using a Payment Intent.
Same logic applies here, except there's a charge in the flow as well.
These are the methods you should use, rather than creating Payment Methods, as they'll automatically perform any additional steps required to allow you to use the payment method later.