Skip to content

Commit 629f746

Browse files
committed
create customer when navigating to checkout if not exists
1 parent b169432 commit 629f746

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

app/server-fns/polar.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,26 @@ export const getCheckoutUrlServerFn = createServerFn({
117117
throw signInPricingRedirect;
118118
}
119119

120+
let customerId = user.polarCustomerId;
121+
122+
if (!customerId) {
123+
const customer = await polar.customers.create({
124+
email: user.email,
125+
metadata: {
126+
userId,
127+
},
128+
});
129+
130+
await db
131+
.update(users)
132+
.set({
133+
polarCustomerId: customer.id,
134+
})
135+
.where(eq(users.id, userId));
136+
137+
customerId = customer.id;
138+
}
139+
120140
const products = await getProductsServerFn();
121141

122142
const product = products.find((x) => x.nameId === data.productName);

0 commit comments

Comments
 (0)