Skip to content

Instantly share code, notes, and snippets.

@nandanmen
Created April 25, 2026 18:07
Show Gist options
  • Select an option

  • Save nandanmen/a747c58d967129df8256595b91d4a6d8 to your computer and use it in GitHub Desktop.

Select an option

Save nandanmen/a747c58d967129df8256595b91d4a6d8 to your computer and use it in GitHub Desktop.
import { getCloudflareContext } from "@opennextjs/cloudflare";
import discountTable from "./discount-table.json";
import { BuyButtonClient } from "./buy-button-client";
const discountToCode = {
30: "AZOTQ1NA",
40: "Q2MTYZOQ",
50: "CXMDI1NA",
60: "KWNDQYMA",
};
const getCheckoutUrl = async (discount: keyof typeof discountToCode) => {
const code = discountToCode[discount];
if (!code) return null;
const response = await fetch("https://api.lemonsqueezy.com/v1/checkouts", {
method: "POST",
headers: {
Accept: "application/vnd.api+json",
"Content-Type": "application/vnd.api+json",
Authorization: `Bearer ${process.env.LS_API_KEY}`,
},
body: JSON.stringify({
data: {
type: "checkouts",
attributes: {
checkout_data: {
discount_code: code,
},
},
relationships: {
store: {
data: {
type: "stores",
id: "59171",
},
},
variant: {
data: {
type: "variants",
id: "712879",
},
},
},
},
}),
});
const data = (await response.json()) as {
data: { attributes: { url: string } };
};
if (!data?.data?.attributes?.url) return null;
return data.data.attributes.url;
};
export async function BuyButton() {
const ctx = await getCloudflareContext({ async: true });
const country = ctx.cf?.country;
const discountData = country
? discountTable[country as keyof typeof discountTable]
: undefined;
return (
<BuyButtonClient
discountData={
discountData
? {
...discountData,
checkoutUrl: await getCheckoutUrl(
discountData.discount as keyof typeof discountToCode,
),
}
: undefined
}
/>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment