Sure, I can give you an overview of the code that you would use to implement the checkout page for bookings with Stripe integration in Next.js. Here are the steps you would follow:
- Create a form component that collects the user's information, including the desired dates and payment details. You can use Next.js forms or a third-party library like Formik to create the form.
import { useState } from 'react';
import { Formik, Form, Field } from 'formik';
import { CardElement } from '@stripe/react-stripe-js';
const BookingForm = ({ onSubmit }) => {
const [processing, setProcessing] = useState(false);
const handleSubmit = async (values, { resetForm }) => {
setProcessing(true);
await onSubmit(values);
resetForm();
setProcessing(false);
}
return (
<Formik initialValues={{ name: '', email: '', dates: '', card: '' }} onSubmit={handleSubmit}>
{({ values, handleChange }) => (
<Form>
<label htmlFor="name">Name</label>
<Field id="name" name="name" placeholder="Name" onChange={handleChange} value={values.name} />
<label htmlFor="email">Email</label>
<Field id="email" name="email" placeholder="Email" onChange={handleChange} value={values.email} />
<label htmlFor="dates">Dates</label>
<Field id="dates" name="dates" placeholder="yyyy-mm-dd to yyyy-mm-dd" onChange={handleChange} value={values.dates} />
<label htmlFor="card">Card Information</label>
<CardElement id="card" onChange={(event) => handleChange({ target: { name: "card", value: event.complete ? event : ''}})} />
<button type="submit" disabled={processing}>Submit</button>
</Form>
)}
</Formik>
);
}
- In your Next.js page component, you can use the
BookingForm component to collect the user's information and process the payment using Stripe's API. You can use the stripe React hook provided by Stripe to load the Stripe elements and create a payment intent.
import { loadStripe } from '@stripe/stripe-js';
import { Elements } from '@stripe/react-stripe-js';
import { useState } from 'react';
import BookingForm from '../components/BookingForm';
const stripePromise = loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY);
const BookingPage = () => {
const [error, setError] = useState(null);
const [showConfirmation, setShowConfirmation] = useState(false);
const handleSubmit = async (values) => {
// Check availability of dates
const datesAvailable = await checkDatesAvailability(values.dates);
if (!datesAvailable) {
setError('The dates you selected are not available, please try again with different dates');
return;
}
// Create payment intent
const { paymentIntent } = await createPaymentIntent({ amount: 1000, currency: 'usd' });
// Confirm payment on admin approval
const { status } = await waitForApproval(paymentIntent.id);
if (status === 'succeeded') {
setShowConfirmation(true);
} else {
setError('Your payment was not processed, please try again later');
}
}
return (
<div>
<h1>Booking Form</h1>
{ error && <p>{error}</p> }
{ !showConfirmation && (
<Elements stripe={stripePromise}>
<BookingForm onSubmit={handleSubmit} />
</Elements>
)}
{ showConfirmation && (
<p>Thank you, your booking has been requested and will be confirmed by an admin shortly</p>
)}
</div>
);
}
-
In the code above, the handleSubmit function is responsible for processing the payment using Stripe's API. You can replace the checkDatesAvailability, createPaymentIntent, and waitForApproval functions with your own implementation to check the availability of dates, create a payment intent, and wait for admin approval.
-
Note how the setError and setShowConfirmation functions are used to update the state of the component and display the relevant message to the user.
-
Finally, you need to create a serverless function or API endpoint that handles the webhook from Stripe when the payment is captured. This endpoint should update your database with the booking information and send an email to the user confirming the booking.
This is just an overview of the code you would need to implement a checkout page for bookings with Stripe integration in Next.js. Hope it helps!