Skip to content

Instantly share code, notes, and snippets.

@ryanbelke
Created May 17, 2020 02:50
Show Gist options
  • Select an option

  • Save ryanbelke/e30dd9e6a01935301d79df1a61a26fb1 to your computer and use it in GitHub Desktop.

Select an option

Save ryanbelke/e30dd9e6a01935301d79df1a61a26fb1 to your computer and use it in GitHub Desktop.
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