Created
June 19, 2020 14:58
-
-
Save SimonHoiberg/52a4581124f3605fd3f9ce3714d946e8 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 handleRetryPayment = async () => { | |
if (!stripe || !elements) { | |
return; | |
} | |
const invoiceID = localStorage.getItem('latest_invoice_id'); | |
try { | |
if (!invoiceID) { | |
throw Error('Could not process payment. Please refresh and try again.'); | |
} | |
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.retryInvoice(customerID, paymentID, invoiceID); | |
localStorage.removeItem('latest_invoice_id'); | |
} catch (error) { | |
console.error(error); | |
// Let the user know that something went wrong here... | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment