Get Paid with Paddle + Next.js
Table of Contents
Introduction
This tutorial will guide you through integrating Paddle with a Next.js application. You'll learn how to create a Next.js app, set up Paddle for payment processing, handle webhooks, and test your checkout process. This is particularly useful for developers looking to monetize their applications or services.
Step 1: Create Next App
Start by setting up your Next.js application.
- Open your terminal.
- Run the following command to create a new Next.js app:
npx create-next-app@latest my-next-app
- Navigate into your project directory:
cd my-next-app
- Start your development server:
npm run dev
Step 2: Paddle Overview
Understand the basics of Paddle and its functionalities.
- Paddle is a payment processor that manages subscriptions and payments.
- You need a Paddle account to get your Vendor ID and access API keys.
Step 3: Get Paddle Vendor ID
Obtain your Vendor ID from the Paddle dashboard.
- Log in to your Paddle account.
- Navigate to the Developer section to find your Vendor ID.
- Keep this ID handy for later use in your application.
Step 4: Create Paddle Component
Set up a component to handle Paddle's checkout process.
- Inside your Next.js app, create a new file called
PaddleCheckout.js
. - Import necessary libraries and set up your component:
import React from 'react'; const PaddleCheckout = () => { const handleCheckout = () => { window.Paddle.Checkout.open({ product: YOUR_PRODUCT_ID, vendor: YOUR_VENDOR_ID, }); }; return ( <button onClick={handleCheckout}>Buy Now</button> ); }; export default PaddleCheckout;
- Replace
YOUR_PRODUCT_ID
andYOUR_VENDOR_ID
with your actual IDs.
Step 5: Use Paddle Component
Integrate your Paddle component into your application.
- Open the
index.js
file in thepages
directory. - Import the PaddleCheckout component:
import PaddleCheckout from '../components/PaddleCheckout';
- Add the component to your JSX:
export default function Home() { return ( <div> <h1>Welcome to My Next.js App</h1> <PaddleCheckout /> </div> ); }
Step 6: Test Paddle Checkout
Verify that the Paddle checkout process is functioning.
- Run your Next.js app using
npm run dev
. - Open your browser and navigate to
http://localhost:3000
. - Click the "Buy Now" button and ensure the Paddle checkout window appears.
Step 7: Paddle Webhooks
Learn how to handle Paddle webhooks for real-time payment updates.
- Webhooks allow your application to receive notifications from Paddle when certain events occur (e.g., successful payment).
- Install the
verify-paddle-webhook
package:npm install verify-paddle-webhook
Step 8: Use LocalTunnel
Use LocalTunnel to expose your local server to the internet for webhook testing.
- Install LocalTunnel globally:
npm install -g localtunnel
- Run LocalTunnel to expose your app:
lt --port 3000
- Note the provided URL; you'll use it when configuring your webhooks in Paddle.
Step 9: Create Webhook API
Set up an API route in your Next.js app to handle incoming webhooks.
- Create a new file
webhook.js
in thepages/api
directory. - Set up the webhook handler:
import { verifyPaddleWebhook } from 'verify-paddle-webhook'; export default async function handler(req, res) { const isValid = verifyPaddleWebhook(req.body, YOUR_VENDOR_ID); if (isValid) { // Handle the webhook event res.status(200).json({ message: 'Webhook received' }); } else { res.status(400).json({ message: 'Invalid webhook' }); } }
- Replace
YOUR_VENDOR_ID
with your actual Vendor ID.
Step 10: Test Webhook
Test your webhook setup to ensure everything is working correctly.
- Use a tool like Postman or Curl to send a test POST request to your webhook URL.
- Check that your webhook handler responds appropriately.
Conclusion
You have successfully integrated Paddle with your Next.js application. You've created a checkout component, set up webhooks for payment notifications, and tested everything locally. Next steps could include deploying your application and refining your payment handling logic based on user feedback.