Skip to content

Commit aac851d

Browse files
committed
[webhook] Beautify the webhook listener code
1 parent e458c8c commit aac851d

6 files changed

Lines changed: 26 additions & 39 deletions

File tree

src/app/billing/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { freemius } from '@/lib/freemius';
33
import { headers } from 'next/headers';
44
import { redirect } from 'next/navigation';
55
import { CustomerPortal } from '@/react-starter/components/customer-portal';
6-
import FSCheckoutProvider from '@/components/fs-checkout';
6+
import AppCheckoutProvider from '@/components/app-checkout-provider';
77
import AppMain, { AppContent } from '@/components/app-main';
88

99
export default async function Billing() {
@@ -23,9 +23,9 @@ export default async function Billing() {
2323
return (
2424
<AppMain title="Billing" isLoggedIn={true}>
2525
<AppContent>
26-
<FSCheckoutProvider checkout={checkout.serialize()}>
26+
<AppCheckoutProvider checkout={checkout.serialize()}>
2727
<CustomerPortal endpoint={process.env.NEXT_PUBLIC_APP_URL! + '/api/portal'} />
28-
</FSCheckoutProvider>
28+
</AppCheckoutProvider>
2929
</AppContent>
3030
</AppMain>
3131
);

src/app/chat/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { headers } from 'next/headers';
44
import { examples } from '@/lib/ai';
55
import AiApp from './ai-app';
66
import { freemius } from '@/lib/freemius';
7-
import FSCheckoutProvider from '@/components/fs-checkout';
7+
import AppCheckoutProvider from '@/components/app-checkout-provider';
88

99
export default async function Dashboard() {
1010
const session = await auth.api.getSession({
@@ -18,9 +18,9 @@ export default async function Dashboard() {
1818

1919
return (
2020
<AppMain title="New Chat" isLoggedIn={!!session}>
21-
<FSCheckoutProvider checkout={checkout.serialize()}>
21+
<AppCheckoutProvider checkout={checkout.serialize()}>
2222
<AiApp examples={examples} />
23-
</FSCheckoutProvider>
23+
</AppCheckoutProvider>
2424
</AppMain>
2525
);
2626
}

src/app/credits/credits.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use client';
22

33
import { Subscribe } from '@/react-starter/components/subscribe';
4-
import { Topup } from '@/react-starter/components/topup';
54
import { formatNumber } from '@/react-starter/utils/formatter';
65

76
export default function Credits(props: { credits?: number; hasSubscription: boolean }) {
@@ -14,9 +13,5 @@ export default function Credits(props: { credits?: number; hasSubscription: bool
1413
</div>
1514
);
1615

17-
if (hasSubscription) {
18-
return <Topup>{info}</Topup>;
19-
}
20-
2116
return <Subscribe>{info}</Subscribe>;
2217
}

src/app/credits/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { redirect } from 'next/navigation';
66
import { ErrorBoundary } from '@/components/error';
77
import Credits from './credits';
88
import { freemius } from '@/lib/freemius';
9-
import FSCheckoutProvider from '@/components/fs-checkout';
9+
import AppCheckoutProvider from '@/components/app-checkout-provider';
1010

1111
export default async function CreditsPage() {
1212
const session = await auth.api.getSession({
@@ -29,9 +29,9 @@ export default async function CreditsPage() {
2929
<AppMain title="Credits & Topups" isLoggedIn={true}>
3030
<AppContent>
3131
<ErrorBoundary>
32-
<FSCheckoutProvider checkout={checkout.serialize()}>
32+
<AppCheckoutProvider checkout={checkout.serialize()}>
3333
<Credits credits={credits} hasSubscription={!!entitlement} />
34-
</FSCheckoutProvider>
34+
</AppCheckoutProvider>
3535
</ErrorBoundary>
3636
</AppContent>
3737
</AppMain>

src/app/webhook/route.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import { freemius } from '@/lib/freemius';
22
import { deleteEntitlement, renewCreditsFromWebhook, syncEntitlementFromWebhook } from '@/lib/user-entitlement';
3+
import { WebhookEventType } from '@freemius/sdk';
34

45
const listener = freemius.webhook.createListener();
56

6-
listener.on(
7-
[
8-
'license.created',
9-
'license.extended',
10-
'license.shortened',
11-
'license.updated',
12-
'license.cancelled',
13-
'license.expired',
14-
'license.plan.changed',
15-
],
16-
async ({ objects: { license } }) => {
17-
if (license && license.id) {
18-
await syncEntitlementFromWebhook(license.id);
19-
}
7+
const licenseEvents: WebhookEventType[] = [
8+
'license.created',
9+
'license.extended',
10+
'license.shortened',
11+
'license.updated',
12+
'license.cancelled',
13+
'license.expired',
14+
'license.plan.changed',
15+
];
16+
17+
listener.on(licenseEvents, async ({ objects: { license } }) => {
18+
if (license && license.id) {
19+
await syncEntitlementFromWebhook(license.id);
2020
}
21-
);
21+
});
2222

2323
listener.on('license.deleted', async ({ data }) => {
2424
await deleteEntitlement(data.license_id);
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,15 @@
22

33
import { CheckoutProvider } from '@/react-starter/components/checkout-provider';
44
import { CheckoutSerialized } from '@freemius/sdk';
5-
import { IconCircleCheck } from '@tabler/icons-react';
65
import * as React from 'react';
76
import { toast } from 'sonner';
87
import { useRouter } from 'next/navigation';
98

10-
const handlePurchase = () => {
11-
toast.success(`Purchase successful`, {
12-
icon: <IconCircleCheck className="w-6 h-6 text-grow" />,
13-
description: 'You can now use the feature you just purchased.',
14-
});
15-
};
16-
17-
export default function FSCheckoutProvider(props: { children: React.ReactNode; checkout: CheckoutSerialized }) {
9+
export default function AppCheckoutProvider(props: { children: React.ReactNode; checkout: CheckoutSerialized }) {
1810
const router = useRouter();
1911

2012
const onAfterSync = React.useCallback(() => {
21-
handlePurchase();
13+
toast.success(`Successfully updated your subscription! Now you can continue using the app.`);
2214
router.refresh();
2315
}, [router]);
2416

0 commit comments

Comments
 (0)