Created
June 19, 2020 15:02
-
-
Save SimonHoiberg/6a6277c524e446170d3638ab76698a09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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