DEV Community

New Bing
New Bing

Posted on

How to use Lemon Squeezy as a payment in next.js

Really like the UX design of lemon squeezy and yesterday I supported lemon squeezy payments in the product GPT2API.

The front end of GPT2API uses the Next.js framework and is Next.js version 13 using the app model.

First, I create the lemon.js loader to make GPT2API support overlay mode.

import Script from "next/script"

export default function LemonPay({handler}) {
    function lemonLoaded() {
        window.createLemonSqueezy()
        LemonSqueezy.Setup({
            eventHandler: (event) => {
                if (handler) {
                    handler(event)
                }
            }
        })
    }
    return (
        <Script src="https://app.lemonsqueezy.com/js/lemon.js" onLoad={lemonLoaded}></Script>
    )
}
Enter fullscreen mode Exit fullscreen mode

The use the lemon.js loader and open the overlay to create order to pay.

'use client'

import { WalletCards } from "lucide-react"

const lemonStore = process.env.NEXT_PUBLIC_LEMONSQUEEZY_STORE
const lemonProduct = process.env.NEXT_PUBLIC_LEMONSQUEEZY_VARIANT_ID

export default function Profile({ }) {
    function buy() {
        const params = new URLSearchParams()
        params.set('checkout[email]', 'user@example.com')
        params.set('checkout[custom][type]', 'gpt2api')
        params.set('checkout[custom][value]', '600000')
        params.set('checkout[custom][email]', 'user@example.com')
        const checkoutUrl = `https://${lemonStore}.lemonsqueezy.com/checkout/buy/${lemonProduct}?` + params.toString()
        LemonSqueezy.Url.Open(checkoutUrl)
    }
    function lemonHanlder(evt) {
        console.info('lemon event', evt)
        if (evt.event === 'Checkout.Success') {
            // do something after checkout success.
        }
    }
    return (
        <>
            <div className="flex flex-col gap-4">
                <div className="w-full" onClick={buy}>
                    <WalletCards className="mr-1 w-4 h-4" /><span>Checkout</span>
                </div>
                <LemonPay handler={lemonHanlder} />
            </div>
        </>
    )
}
Enter fullscreen mode Exit fullscreen mode

Remember, the Lemon Squeezy checkout link is in this format.

https://${lemonStore}.lemonsqueezy.com/checkout/buy/${lemonProduct}

You can find this from the Lemon Squeezy Dashboard on the Products page.
First, go to Products and share it.

GPT2API Products

You can get the checkout link as below.

Checkout link of GPT2API

What is GPT2API?

Website: https://aicanvas.app/gpt
GPT2API is a platform to help you build API to make ChatGPT easier to use. You can build API and share it with the community, or you can call API from API.Hub, which is other API shared by the community.

Features:

  1. Build API with ChatGPT commands.
  2. Test API on the website.
  3. Share it with the community.
  4. Have the community extend the API.
  5. Sample code for your project.
  6. Cheap price for calling ChatGPT, $1 with 600K tokens.

If you have any questions about GPT2API or programming, you can contact me on the twitter. You are very welcome to experience GPT2API. I hope to get your comments.

Top comments (0)