Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save barryosull/1469efcbcaa0d185d03d2c7744e58e34 to your computer and use it in GitHub Desktop.
Save barryosull/1469efcbcaa0d185d03d2c7744e58e34 to your computer and use it in GitHub Desktop.
There is a bug in the Stripe documentation. It gives an incorrect code example for trying to reuse an existing card. Here's the fix.
/**
* There is a bug in the Stripe SCA documentation for reusing saved cards.
* It gives an incorrect code sample for trying to reuse an existing card via Card Elements.
*
* The offending sample: https://stripe.com/docs/payments/cards/charging-saved-cards#submit-payment-automatic-confirmation
*/
/** It says to do this **/
cardButton.addEventListener('click', function(ev) {
stripe.handleCardPayment(
clientSecret, cardElement, {
payment_method_data: {
billing_details: {name: cardholderName.value}
}
}
).then(function(result) {
if (result.error) {
// Display error.message in your UI.
} else {
// The payment has succeeded. Display a success message.
}
});
});
/**
* When you should actually do this
*
* You don't need to create a card element when reusing a card.
* Instead pass in the clientSecret and the payment method.
*/
cardButton.addEventListener('click', function(ev) {
error.style.display = "none";
stripe.handleCardPayment(
clientSecret, {
payment_method: '{{PAYMENT_METHOD_ID}}'
}
).then(function(result) {
if (result.error) {
// Display error.message in your UI.
} else {
// The payment has succeeded. Display a success message.
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment