Skip to content

Instantly share code, notes, and snippets.

@fyyying
Last active July 11, 2020 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fyyying/a66ae9b95a869d8d87e33faea88c7305 to your computer and use it in GitHub Desktop.
Save fyyying/a66ae9b95a869d8d87e33faea88c7305 to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from "react";
import {
CardElement,
useStripe,
useElements
} from "@stripe/react-stripe-js";
export default function CheckoutForm() {
const [succeeded, setSucceeded] = useState(false);
const [error, setError] = useState(null);
const [processing, setProcessing] = useState('');
const [disabled, setDisabled] = useState(true);
const [clientSecret, setClientSecret] = useState('');
const stripe = useStripe();
const elements = useElements();
useEffect(() => {
// Create PaymentIntent as soon as the page loads
window
.fetch("/create-payment-intent", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({service: [{ id: "xl-tshirt" }]})
})
.then(res => {
return res.json();
})
.then(data => {
setClientSecret(data.clientSecret);
});
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment