Created
May 17, 2020 02:50
-
-
Save ryanbelke/e30dd9e6a01935301d79df1a61a26fb1 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
| async function submitOrder() { | |
| // event.preventDefault(); | |
| // // Use elements.getElement to get a reference to the mounted Element. | |
| const cardElement = elements.getElement(CardElement); | |
| // // Pass the Element directly to other Stripe.js methods: | |
| // // e.g. createToken - https://stripe.com/docs/js/tokens_sources/create_token?type=cardElement | |
| // get token back from stripe to process credit card | |
| const token = await stripe.createToken(cardElement); | |
| const userToken = Cookies.get("token"); | |
| const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/orders`, { | |
| method: "POST", | |
| headers: userToken && { Authorization: `Bearer ${userToken}` }, | |
| body: JSON.stringify({ | |
| amount: Number(Math.round(appContext.cart.total + "e2") + "e-2"), | |
| dishes: appContext.cart.items, | |
| address: data.address, | |
| city: data.city, | |
| state: data.state, | |
| token: token.token.id, | |
| }), | |
| }); | |
| if (!response.ok) { | |
| setError(response.statusText); | |
| } | |
| // OTHER stripe methods you can use depending on app | |
| // // or createPaymentMethod - https://stripe.com/docs/js/payment_intents/create_payment_method | |
| // stripe.createPaymentMethod({ | |
| // type: "card", | |
| // card: cardElement, | |
| // }); | |
| // // or confirmCardPayment - https://stripe.com/docs/js/payment_intents/confirm_card_payment | |
| // stripe.confirmCardPayment(paymentIntentClientSecret, { | |
| // payment_method: { | |
| // card: cardElement, | |
| // }, | |
| // }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment