In this tutorial, we will learn how to add a Stripe payment integration to a Webflow website. Stripe is a popular online payment platform that allows businesses to accept payments securely and efficiently.
Step 1: Create a Stripe Account
If you don’t already have a Stripe account, head over to the Stripe website and sign up for an account. It’s free and only takes a few minutes.
Step 2: Obtain API Keys
Once you have created your Stripe account, navigate to the Developers section and click on API Keys. Here, you will find your Publishable Key and Secret Key. These keys are essential for integrating Stripe with your Webflow site.
Step 3: Add Custom Code in Webflow
In your Webflow project, locate the page where you want to add the Stripe payment integration. Open the page settings by clicking on the gear icon in the right sidebar.
- In the page settings panel, select the ‘Custom Code’ tab.
- Paste the following code snippet into the ‘‘Head’‘ section:
<script src="https://js.stripe.com/v3/"></script>
<script>
var stripe = Stripe('YOUR_PUBLISHABLE_KEY');
// Additional code for handling payments goes here
</script>
Note: Replace ‘YOUR_PUBLISHABLE_KEY’ with your actual publishable key obtained from your Stripe account.
Step 4: Create a Payment Form
Now, let’s create a simple payment form to collect the necessary information from your customers.
- Add an HTML form element to your Webflow page using the Webflow Designer.
- Inside the form, include fields for collecting customer information such as name, email, and payment details like card number, expiration date, and CVC code.
- Add a submit button to the form to initiate the payment process.
Example:
<form action="/charge" method="POST">
<input type="text" name="name" placeholder="Name">
<input type="email" name="email" placeholder="Email">
<input type="text" name="cardNumber" placeholder="Card Number">
<input type="text" name="expDate" placeholder="Expiration Date">
<input type="text" name="cvc" placeholder="CVC">
<button type="submit">Pay Now</button>
</form>
Step 5: Handle Payments on the Server
To securely process payments and interact with Stripe’s API, you’ll need server-side code. In this example, we’ll use Node.js with Express.js as our server framework.
- Create a new route in your server code to handle the payment request from your Webflow form.
- In this route handler, use the Stripe library to create a charge using the secret key obtained from your Stripe account. Make sure to validate and sanitize the input data before creating a charge.
- Once the payment is successfully processed, you can redirect the user to a success page or display a confirmation message.
// Assuming you have set up your server with Express.js and installed the 'stripe' npm package
app.post('/charge', async (req, res) => {
const { name, email, cardNumber, expDate, cvc } = req.body;
// Validate and sanitize input data
try {
const charge = await stripe.charges.create({
amount: 1000, // Amount in cents
currency: 'usd',
source: cardNumber,
description: 'Example Charge',
});
// Payment successful! Redirect or display success message
} catch (error) {
// Handle payment error
}
});
Congratulations! You have successfully integrated Stripe into your Webflow website. Users can now make payments securely using the payment form you created.
Remember to test your payment flow thoroughly before deploying it to production. You can use Stripe’s test mode and test card numbers for testing purposes.