Created
April 19, 2020 17:39
-
-
Save OmarKhattab/6e271ca953836f6c0e1f19fd8d340bf8 to your computer and use it in GitHub Desktop.
This file contains 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
import { loadStripe } from '@stripe/stripe-js'; | |
import { | |
CardElement, | |
Elements, | |
useStripe, | |
useElements, | |
} from '@stripe/react-stripe-js'; | |
const stripePromise = loadStripe(process.env.NODE_ENV === 'production' ? 'pk_live_YOURKEY' : 'pk_test_YOURKEY') | |
const stripe = useStripe(); | |
const elements = useElements(); | |
const Example = () => { | |
const handleSubmit = async (event) => { | |
event.preventDefault(); | |
try { | |
const { error, paymentMethod } = await stripe.createPaymentMethod({ | |
type: 'card', | |
card: elements.getElement(CardElement), | |
}); | |
let response = await axios.post(`${CORE_URL}/place_order`, { | |
paymentMethod, | |
}) | |
} | |
catch (error) { | |
console.log(error) | |
} | |
}; | |
return ( | |
<Elements stripe={stripePromise}> | |
<form onSubmit={handleSubmit}> | |
<CardElement /> | |
<button type="submit" disabled={!stripe}> | |
Pay | |
</button> | |
</form> | |
</Elements> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment