mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
feat: make billing page functional
This commit is contained in:
1
packages/lib/ee/LICENSE.md
Normal file
1
packages/lib/ee/LICENSE.md
Normal file
@ -0,0 +1 @@
|
||||
<!-- This is a stub -->
|
||||
32
packages/lib/ee/stripe/create-customer.ts
Normal file
32
packages/lib/ee/stripe/create-customer.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import { User } from '@documenso/prisma/client';
|
||||
|
||||
import { stripe } from '../../server-only/stripe';
|
||||
import { getSubscriptionByUserId } from '../../server-only/subscription/get-subscription-by-user-id';
|
||||
|
||||
export type CreateCustomerOptions = {
|
||||
user: User;
|
||||
};
|
||||
|
||||
export const createCustomer = async ({ user }: CreateCustomerOptions) => {
|
||||
const existingSubscription = await getSubscriptionByUserId({ userId: user.id });
|
||||
|
||||
if (existingSubscription) {
|
||||
throw new Error('User already has a subscription');
|
||||
}
|
||||
|
||||
const customer = await stripe.customers.create({
|
||||
name: user.name ?? undefined,
|
||||
email: user.email,
|
||||
metadata: {
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
return await prisma.subscription.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
customerId: customer.id,
|
||||
},
|
||||
});
|
||||
};
|
||||
19
packages/lib/ee/stripe/get-portal-session.ts
Normal file
19
packages/lib/ee/stripe/get-portal-session.ts
Normal file
@ -0,0 +1,19 @@
|
||||
'use server';
|
||||
|
||||
import { stripe } from '../../server-only/stripe';
|
||||
|
||||
export type GetPortalSessionOptions = {
|
||||
customerId: string;
|
||||
returnUrl: string;
|
||||
};
|
||||
|
||||
export const getPortalSession = async ({ customerId, returnUrl }: GetPortalSessionOptions) => {
|
||||
'use server';
|
||||
|
||||
const session = await stripe.billingPortal.sessions.create({
|
||||
customer: customerId,
|
||||
return_url: returnUrl,
|
||||
});
|
||||
|
||||
return session.url;
|
||||
};
|
||||
@ -3,7 +3,7 @@
|
||||
"version": "1.0.0",
|
||||
"main": "./index.ts",
|
||||
"types": "./index.ts",
|
||||
"license": "MIT",
|
||||
"license": "SEE LICENSE IN LICENSE.md",
|
||||
"files": [
|
||||
"client-only/",
|
||||
"server-only/",
|
||||
|
||||
@ -0,0 +1,15 @@
|
||||
'use server';
|
||||
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
export type GetSubscriptionByUserIdOptions = {
|
||||
userId: number;
|
||||
};
|
||||
|
||||
export const getSubscriptionByUserId = ({ userId }: GetSubscriptionByUserIdOptions) => {
|
||||
return prisma.subscription.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user