DEV Community

New Bing
New Bing

Posted on

2

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.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay