Skip to content

Commit 142b3f9

Browse files
committed
[refactor] [fix] simplify the processPurchaseInfo function + Bug fixes
1 parent 3cde56c commit 142b3f9

3 files changed

Lines changed: 37 additions & 10 deletions

File tree

src/app/credits/credits.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use client';
22

3-
import { Subscribe } from '@/react-starter/components/subscribe';
3+
import { Topup } from '@/react-starter/components/topup';
44
import { formatNumber } from '@/react-starter/utils/formatter';
55

6-
export default function Credits(props: { credits?: number; hasSubscription: boolean }) {
7-
const { credits, hasSubscription } = props;
6+
export default function Credits(props: { credits?: number }) {
7+
const { credits } = props;
88

99
const info = (
1010
<div className="text-center">
@@ -13,5 +13,5 @@ export default function Credits(props: { credits?: number; hasSubscription: bool
1313
</div>
1414
);
1515

16-
return <Subscribe>{info}</Subscribe>;
16+
return <Topup>{info}</Topup>;
1717
}

src/app/credits/page.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import AppMain, { AppContent } from '@/components/app-main';
22
import { auth } from '@/lib/auth';
3-
import { getCredits, getUserEntitlement } from '@/lib/user-entitlement';
3+
import { getCredits } from '@/lib/user-entitlement';
44
import { headers } from 'next/headers';
55
import { redirect } from 'next/navigation';
66
import { ErrorBoundary } from '@/components/error';
@@ -17,7 +17,6 @@ export default async function CreditsPage() {
1717
redirect('/login');
1818
}
1919

20-
const entitlement = await getUserEntitlement(session.user.id);
2120
const credits = await getCredits(session.user.id);
2221

2322
const checkout = await freemius.checkout.create({
@@ -30,7 +29,7 @@ export default async function CreditsPage() {
3029
<AppContent>
3130
<ErrorBoundary>
3231
<AppCheckoutProvider checkout={checkout.serialize()}>
33-
<Credits credits={credits} hasSubscription={!!entitlement} />
32+
<Credits credits={credits} />
3433
</AppCheckoutProvider>
3534
</ErrorBoundary>
3635
</AppContent>

src/lib/user-entitlement.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,43 @@ import { freemius } from './freemius';
1717
// #region Freemius SDK Supporting Functions for User Entitlements
1818

1919
export async function processPurchaseInfo(fsPurchase: PurchaseInfo): Promise<void> {
20-
// Todo: fill me
20+
const user = await getUserByEmail(fsPurchase.email);
21+
22+
if (!user) {
23+
return;
24+
}
25+
26+
const credit = await getCreditsForUserPurchase(user, fsPurchase);
27+
28+
// Save purchase info in our DB
29+
await prisma.userFsEntitlement.upsert({
30+
where: { fsLicenseId: fsPurchase.licenseId },
31+
create: fsPurchase.toEntitlementRecord({ userId: user.id }),
32+
update: fsPurchase.toEntitlementRecord(),
33+
});
34+
35+
if (credit > 0) {
36+
await addCredits(user.id, credit);
37+
}
2138
}
2239

2340
export async function getUserEntitlement(userId: string): Promise<UserFsEntitlement | null> {
24-
// Todo: fill me
41+
const entitlements = await prisma.userFsEntitlement.findMany({
42+
where: { userId, type: 'subscription' },
43+
});
44+
45+
return freemius.entitlement.getActive(entitlements);
2546
}
2647

2748
export const getFsUser: UserRetriever = async () => {
28-
// Todo: fill me
49+
const session = await auth.api.getSession({
50+
headers: await headers(),
51+
});
52+
53+
const entitlement = session ? await getUserEntitlement(session.user.id) : null;
54+
const email = session?.user.email ?? undefined;
55+
56+
return freemius.entitlement.getFsUser(entitlement, email);
2957
};
3058

3159
function getEntitledCredits(fsPurchase: PurchaseInfo): number {

0 commit comments

Comments
 (0)