-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4353a86
commit 09d4b2e
Showing
7 changed files
with
144 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { loadStripe } from '@stripe/stripe-js'; | ||
import { Elements, CardElement, useStripe, useElements } from '@stripe/react-stripe-js'; | ||
|
||
const stripePromise = loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY || ''); | ||
|
||
const CheckoutForm = ({customerId}) => { | ||
const stripe = useStripe(); | ||
const elements = useElements(); | ||
const [isProcessing, setIsProcessing] = useState(false); | ||
|
||
const handleSubmit = async (e) => { | ||
e.preventDefault(); | ||
|
||
if (!stripe || !elements) return; | ||
|
||
setIsProcessing(true); | ||
|
||
const cardElement = elements.getElement(CardElement); | ||
|
||
// Call your backend to create a SetupIntent and get the clientSecret | ||
const { client_secret } = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/subscriptions/create-setup-intent`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ customerId }), // Pass customerId | ||
}).then(res => res.json()); | ||
|
||
console.log('Client Secret:', client_secret); | ||
|
||
const { error, setupIntent } = await stripe.confirmCardSetup(client_secret, { | ||
// IntegrationError: Missing value for stripe.confirmCardSetup intent secret: value should be a client_secret string. | ||
payment_method: { | ||
card: cardElement, | ||
}, | ||
}); | ||
|
||
if (error) { | ||
console.error(error); | ||
// Handle error | ||
} else { | ||
console.log('Setup Intent:', setupIntent); | ||
// Now your customer has a payment method attached | ||
// You can call your backend to create a subscription for this customer | ||
} | ||
|
||
setIsProcessing(false); | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSubmit}> | ||
<CardElement /> | ||
<button type="submit" disabled={isProcessing}> | ||
{isProcessing ? 'Processing...' : 'Submit'} | ||
</button> | ||
</form> | ||
); | ||
}; | ||
|
||
const StripeCheckout = ({ customerId }) => { | ||
return ( | ||
<Elements stripe={stripePromise}> | ||
<CheckoutForm customerId={customerId} /> | ||
</Elements> | ||
); | ||
}; | ||
|
||
export default StripeCheckout; |