Next.js 15 integration guide

Integrating Fungies with Next.js 15: A Concise Guide

1. Introduction

Fungies is a payment merchant of record platform designed specifically for SaaS developers. It offers a streamlined way to handle payments, subscriptions, and checkout experiences with key benefits including:

  • No hidden fees pricing structure

  • Beautiful checkout solutions (Overlay, Embedded, and Hosted options)

  • Easy integration process

  • No-code store builder for quick setup

This guide will walk you through integrating Fungies checkout into your Next.js 15 application. We'll use straightforward language and provide practical code examples that you can adapt to your own projects.

2. Getting Started with Fungies & Next.js

Prerequisites

Before beginning the integration process, ensure you have:

  • Next.js 15 Project: Create one with npx create-next-app@latest my-fungies-app

    • Use TypeScript (recommended)

    • Use App Router (to leverage Next.js 15 features)

  • Fungies Account:

    • Sign up at Fungies.io

    • For testing, use the Sandbox environment: https://app.stage.fungies.net

    • Create a store in your dashboard

    • Set up at least one product or subscription

    • Note your API keys (public and secret)

    • Create a checkout element and note the checkout URL

  • Development Environment:

    • Node.js 18.17 or later

    • npm, yarn, or pnpm package manager

Installation

Install the Fungies JavaScript SDK in your Next.js project:bash

Setup

  1. Environment Variables

Create a .env.local file in your project root:

  1. SDK Initialization

Create a utility file to initialize the Fungies SDK (lib/fungies.ts):typescript

This utility ensures Fungies is only initialized on the client side, preventing server-side rendering errors.

3. Implementing Fungies Checkout

Checkout Options Overview

Fungies offers three main checkout integration options:

  1. Overlay Checkout: Displays in a modal overlay (simplest option)

  2. Embedded Checkout: Displays directly within your page layout

  3. Hosted Checkout: Redirects to a Fungies-hosted checkout page

We'll focus on implementing the Overlay and Embedded options as they provide the best balance of ease and user experience.

Overlay Checkout Implementation

Create a client component for the overlay checkout (components/OverlayCheckoutButton.tsx):typescript

Use this component in any page:tsx

Embedded Checkout Implementation

Create a container component for the embedded checkout (components/EmbeddedCheckout.tsx):typescript

Use the embedded checkout in your pages:tsx

4. Handling Payments & Events

Working with Checkout Data

Fungies provides several options for customizing the checkout experience:

Pre-filling Customer Information

typescript

Applying Discount Codes

typescript

Setting Quantities

typescript

Webhook Integration

Webhooks allow your application to receive real-time notifications about events in your Fungies account.Create an API route to handle webhook events:typescript

Security Considerations for Webhooks

  1. Always verify signatures using the webhook secret

  2. Store secrets in environment variables, never hardcode them

  3. Design webhook handlers to be idempotent as Fungies may retry webhook deliveries

  4. Catch and log errors but still return a 200 status to acknowledge receipt

  5. Keep webhook processing quick or move long-running tasks to a background job

5. Integrating with Next.js 15 Features

Server vs. Client Components

Next.js 15 uses React's Server Components by default, but Fungies SDK requires client-side JavaScript.Server Components are great for:

  • Fetching data from your backend or APIs

  • Accessing environment variables securely

  • Rendering static content

Client Components are necessary for:

  • Interacting with the Fungies SDK

  • Handling user interactions like button clicks

  • Managing local state

Here's a pattern for combining both:tsx

tsx

Server Actions

Next.js 15 includes Server Actions, which allow you to run server-side code from client components. This is perfect for operations that require your secret API key.typescript

Use this Server Action in a client component:tsx

Performance Tips

Lazy Loading Checkout Components

Use Next.js dynamic imports to lazy load checkout components:typescript

Optimizing Webhook Processing

For long-running webhook tasks, use a background process:typescript

6. Testing & Deployment

Testing with Fungies Sandbox

Fungies provides a Sandbox environment for testing:

Set up environment variables to switch between environments:typescript

Use this configuration in your API calls:typescript

Debugging Common Issues

SDK Not Loading

typescript

Webhook Verification Issues

typescript

Deployment

Environment Variables in Production

For Vercel deployments, add these environment variables in the Vercel dashboard:

Security Best Practices

  1. Never expose your secret API key in client-side code

  2. Add security headers to your webhook endpoints

  3. Validate user input before sending it to Fungies

  4. Use Server Actions for operations requiring the secret key

7. Conclusion

In this guide, we've covered how to integrate Fungies checkout into your Next.js 15 application. We've explored:

  1. Setting up the Fungies SDK in a Next.js environment

  2. Implementing both Overlay and Embedded checkout options

  3. Working with checkout data and customization

  4. Setting up webhooks to handle payment events

  5. Leveraging Next.js 15 features like Server Components and Server Actions

  6. Testing with the Fungies Sandbox environment

  7. Deploying your integration to production

For more information, refer to:

For support, contact [email protected] or join the Fungies community forums.

Last updated