mirror of
https://github.com/documenso/documenso.git
synced 2026-07-11 13:35:20 +10:00
3887aa67c8
Replace per-event webhook handlers with a single sync function that fetches the current state from Stripe and converges the local subscription, claim, and organisation type. - Create organisations upfront before checkout, restricted as "pending payment" until the first payment syncs - Add rate-limited subscription sync route, triggered on checkout success so the UI doesn't wait on webhooks - Surface pending payment state in banner, billing table, and limits
33 lines
866 B
TypeScript
33 lines
866 B
TypeScript
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
|
import { stripe } from '@documenso/lib/server-only/stripe';
|
|
|
|
export type CreateCheckoutSessionOptions = {
|
|
customerId: string;
|
|
priceId: string;
|
|
returnUrl: string;
|
|
};
|
|
|
|
export const createCheckoutSession = async ({ customerId, priceId, returnUrl }: CreateCheckoutSessionOptions) => {
|
|
const session = await stripe.checkout.sessions.create({
|
|
customer: customerId,
|
|
mode: 'subscription',
|
|
line_items: [
|
|
{
|
|
price: priceId,
|
|
quantity: 1,
|
|
},
|
|
],
|
|
success_url: `${returnUrl}?success=true`,
|
|
cancel_url: `${returnUrl}?canceled=true`,
|
|
billing_address_collection: 'required',
|
|
});
|
|
|
|
if (!session.url) {
|
|
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
|
message: 'Failed to create checkout session',
|
|
});
|
|
}
|
|
|
|
return session.url;
|
|
};
|