Skip to content

Commit

Permalink
working lemon-squeezy billing
Browse files Browse the repository at this point in the history
  • Loading branch information
d-ivashchuk committed Apr 1, 2024
1 parent 5778115 commit 1d6015a
Show file tree
Hide file tree
Showing 18 changed files with 751 additions and 155 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@trpc/server": "next",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"date-fns": "^3.6.0",
"loops": "^1.0.0",
"lost-pixel": "^3.16.0",
"lucide-react": "^0.363.0",
Expand Down
77 changes: 31 additions & 46 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- CreateTable
CREATE TABLE "LemonSqueezyWebhookEvent" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"eventName" TEXT NOT NULL,
"processed" BOOLEAN NOT NULL DEFAULT false,
"body" JSONB NOT NULL,
"processingError" TEXT,

CONSTRAINT "LemonSqueezyWebhookEvent_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "LemonSqueezySubscription" (
"id" SERIAL NOT NULL,
"lemonSqueezyId" TEXT NOT NULL,
"orderId" INTEGER NOT NULL,
"name" TEXT NOT NULL,
"email" TEXT NOT NULL,
"status" TEXT NOT NULL,
"statusFormatted" TEXT NOT NULL,
"renewsAt" TIMESTAMP(3),
"endsAt" TIMESTAMP(3),
"trialEndsAt" TIMESTAMP(3),
"price" TEXT NOT NULL,
"isUsageBased" BOOLEAN NOT NULL DEFAULT false,
"isPaused" BOOLEAN NOT NULL DEFAULT false,
"subscriptionItemId" INTEGER NOT NULL,
"userId" TEXT NOT NULL,
"planId" INTEGER NOT NULL,

CONSTRAINT "LemonSqueezySubscription_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "LemonSqueezySubscription_lemonSqueezyId_key" ON "LemonSqueezySubscription"("lemonSqueezyId");

-- AddForeignKey
ALTER TABLE "LemonSqueezySubscription" ADD CONSTRAINT "LemonSqueezySubscription_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:
- You are about to drop the column `planId` on the `LemonSqueezySubscription` table. All the data in the column will be lost.
- You are about to drop the column `statusFormatted` on the `LemonSqueezySubscription` table. All the data in the column will be lost.
- Added the required column `customerId` to the `LemonSqueezySubscription` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "LemonSqueezySubscription" DROP COLUMN "planId",
DROP COLUMN "statusFormatted",
ADD COLUMN "customerId" TEXT NOT NULL;
10 changes: 10 additions & 0 deletions prisma/migrations/20240401080450_remove_some_fields/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Warnings:
- You are about to drop the column `price` on the `LemonSqueezySubscription` table. All the data in the column will be lost.
- You are about to drop the column `subscriptionItemId` on the `LemonSqueezySubscription` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "LemonSqueezySubscription" DROP COLUMN "price",
DROP COLUMN "subscriptionItemId";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- AlterTable
ALTER TABLE "LemonSqueezySubscription" ADD COLUMN "customerPortalUpdateSubscriptionUrl" TEXT,
ADD COLUMN "customerPortalUrl" TEXT,
ADD COLUMN "updatePaymentMethodUrl" TEXT;
8 changes: 8 additions & 0 deletions prisma/migrations/20240401085643_add_variant_id/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `variantId` to the `LemonSqueezySubscription` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "LemonSqueezySubscription" ADD COLUMN "variantId" TEXT NOT NULL;
48 changes: 40 additions & 8 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,46 @@ model Session {
}

model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
role Role @default(USER)
accounts Account[]
sessions Session[]
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
role Role @default(USER)
accounts Account[]
sessions Session[]
LemonSqueezySubscription LemonSqueezySubscription[]
}

model LemonSqueezyWebhookEvent {
id Int @id @default(autoincrement())
eventName String
processed Boolean @default(false)
body Json
createdAt DateTime @default(now())
processingError String?
}

model LemonSqueezySubscription {
id Int @id @default(autoincrement())
lemonSqueezyId String @unique
orderId Int
name String
email String
status String
renewsAt DateTime?
endsAt DateTime?
trialEndsAt DateTime?
isUsageBased Boolean @default(false)
isPaused Boolean @default(false)
customerId String
variantId String
customerPortalUrl String?
updatePaymentMethodUrl String?
customerPortalUpdateSubscriptionUrl String?
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model VerificationToken {
Expand Down
84 changes: 84 additions & 0 deletions src/app/(lemon-squeezy)/ls-setup/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"use client";
import { LinkIcon, Loader2 } from "lucide-react";
import React from "react";
import { Button } from "~/components/ui/button";
import { Skeleton } from "~/components/ui/skeleton";
import { Badge } from "~/components/ui/badge";
import { api } from "~/trpc/react";
import Link from "next/link";

const Page = () => {
const utils = api.useUtils();
const lemonSqueezyWebhookQuery = api.ls.getWebhook.useQuery();
const createLemonSqueezyWebhookMutation = api.ls.createLsWebhook.useMutation({
onSuccess: () => utils.ls.getWebhook.invalidate(),
});

return (
<div className="space-y-4">
<h1 className="text-2xl">Lemon Squeezy setup</h1>
<p className="prose">
For <b>local development</b> run the following command in your terminal:{" "}
<br />
<code>npx localtunnel --port 3000 --subdomain your-domain</code> and
copy resulting URL to <b>LEMON_SQUEEZY_WEBHOOK_URL</b> in your .env
file. It shall look similar to this: <br />
<i>https://cascade.loca.lt/api/lemon-squeezy/webhook</i>
</p>
<p>
For production webhook use your hosted application url e.g.: <br />
<i>https://cascade.stackonfire.com/api/lemon-squeezy/webhook</i>
</p>
{lemonSqueezyWebhookQuery.isLoading && (
<Skeleton className="h-[20px] w-[200px]" />
)}
{lemonSqueezyWebhookQuery.data && (
<div className="space-y-2">
<div className="flex space-x-2">
<h2 className="text-lg">Webhook URL</h2>
{lemonSqueezyWebhookQuery.data.attributes.url && (
<Badge variant="destructive" className="max-h-6 self-center">
{lemonSqueezyWebhookQuery.data.attributes.test_mode
? "Test Mode"
: "Production"}
</Badge>
)}
</div>
<div className="flex space-x-2 text-muted-foreground">
<div className="flex h-2 w-2 animate-pulse self-center rounded-full bg-green-500" />{" "}
<p>{lemonSqueezyWebhookQuery.data.attributes.url}</p>
<Link
href="https://app.lemonsqueezy.com/settings/webhooks"
target="_blank"
className="self-center transition-colors hover:text-foreground"
>
<LinkIcon className="h-4 w-4" />
</Link>
</div>
<div className="flex space-x-1">
{lemonSqueezyWebhookQuery.data.attributes.events.map((event) => (
<Badge variant="outline" key={event}>
{event}
</Badge>
))}
</div>
</div>
)}
<Button
onClick={() => createLemonSqueezyWebhookMutation.mutate()}
disabled={
createLemonSqueezyWebhookMutation.isPending ||
lemonSqueezyWebhookQuery.isLoading ||
!!lemonSqueezyWebhookQuery.data?.attributes.url
}
>
{createLemonSqueezyWebhookMutation.isPending && (
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
)}
Create Lemon Squeezy webhook
</Button>
</div>
);
};

export default Page;
Loading

0 comments on commit 1d6015a

Please sign in to comment.