Skip to content

Instantly share code, notes, and snippets.

@SimonHoiberg
Created June 19, 2020 15:02
Show Gist options
  • Save SimonHoiberg/6a6277c524e446170d3638ab76698a09 to your computer and use it in GitHub Desktop.
Save SimonHoiberg/6a6277c524e446170d3638ab76698a09 to your computer and use it in GitHub Desktop.
const handleUpdatePaymentMethod = async () => {
if (!stripe || !elements) {
return;
}
try {
const { error, paymentMethod } = await stripe.createPaymentMethod({
type: 'card',
card: elements.getElement(CardNumberElement) as any,
billing_details: {
name: nameOfCard,
},
});
if (error || !paymentMethod) {
throw Error(error?.message || 'Something is not right...');
}
const customerID = await StripeManager.getStripeCustomerID();
if (!customerID) {
throw Error('Could not identify customer');
}
const paymentID = paymentMethod.id;
await StripeManager.updatePaymentMethod(customerID, paymentID);
// You may want to refetch the payment method after this...
} catch (error) {
Toast.error(error.message);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment