mirror of
https://github.com/documenso/documenso.git
synced 2026-06-22 04:12:06 +10:00
feat: add stripe sync (#2877)
This commit is contained in:
@@ -8,6 +8,13 @@ import { match } from 'ts-pattern';
|
||||
export type OnSubscriptionUpdatedOptions = {
|
||||
subscription: Stripe.Subscription;
|
||||
previousAttributes: Partial<Stripe.Subscription> | null;
|
||||
/**
|
||||
* When true, the organisationClaim will not be synced.
|
||||
*
|
||||
* Used by the admin sync route to update only the Subscription
|
||||
* row while leaving claim entitlements untouched.
|
||||
*/
|
||||
bypassClaimUpdate?: boolean;
|
||||
};
|
||||
|
||||
type StripeWebhookResponse = {
|
||||
@@ -15,7 +22,11 @@ type StripeWebhookResponse = {
|
||||
message: string;
|
||||
};
|
||||
|
||||
export const onSubscriptionUpdated = async ({ subscription, previousAttributes }: OnSubscriptionUpdatedOptions) => {
|
||||
export const onSubscriptionUpdated = async ({
|
||||
subscription,
|
||||
previousAttributes,
|
||||
bypassClaimUpdate = false,
|
||||
}: OnSubscriptionUpdatedOptions) => {
|
||||
const customerId = typeof subscription.customer === 'string' ? subscription.customer : subscription.customer.id;
|
||||
|
||||
// Todo: logging
|
||||
@@ -121,7 +132,8 @@ export const onSubscriptionUpdated = async ({ subscription, previousAttributes }
|
||||
});
|
||||
|
||||
// Override current organisation claim if new one is found.
|
||||
if (newClaimFound) {
|
||||
// Skipped when bypassClaimUpdate is set.
|
||||
if (!bypassClaimUpdate && newClaimFound) {
|
||||
await tx.organisationClaim.update({
|
||||
where: {
|
||||
id: organisation.organisationClaim.id,
|
||||
|
||||
@@ -32,6 +32,7 @@ import { resetOrganisationMonthlyStatRoute } from './reset-organisation-monthly-
|
||||
import { resetTwoFactorRoute } from './reset-two-factor-authentication';
|
||||
import { resyncLicenseRoute } from './resync-license';
|
||||
import { swapOrganisationSubscriptionRoute } from './swap-organisation-subscription';
|
||||
import { syncOrganisationSubscriptionRoute } from './sync-organisation-subscription';
|
||||
import { updateAdminOrganisationRoute } from './update-admin-organisation';
|
||||
import { updateOrganisationMemberRoleRoute } from './update-organisation-member-role';
|
||||
import { updateRecipientRoute } from './update-recipient';
|
||||
@@ -46,9 +47,14 @@ export const adminRouter = router({
|
||||
create: createAdminOrganisationRoute,
|
||||
update: updateAdminOrganisationRoute,
|
||||
delete: deleteOrganisationRoute,
|
||||
swapSubscription: swapOrganisationSubscriptionRoute,
|
||||
resetMonthlyStat: resetOrganisationMonthlyStatRoute,
|
||||
findStats: findOrganisationStatsRoute,
|
||||
subscription: {
|
||||
swap: swapOrganisationSubscriptionRoute,
|
||||
sync: syncOrganisationSubscriptionRoute,
|
||||
},
|
||||
stats: {
|
||||
find: findOrganisationStatsRoute,
|
||||
reset: resetOrganisationMonthlyStatRoute,
|
||||
},
|
||||
},
|
||||
organisationMember: {
|
||||
promoteToOwner: promoteMemberToOwnerRoute,
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import { onSubscriptionUpdated } from '@documenso/ee/server-only/stripe/webhook/on-subscription-updated';
|
||||
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||
import { Stripe, stripe } from '@documenso/lib/server-only/stripe';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
import { adminProcedure } from '../trpc';
|
||||
import {
|
||||
ZSyncOrganisationSubscriptionRequestSchema,
|
||||
ZSyncOrganisationSubscriptionResponseSchema,
|
||||
} from './sync-organisation-subscription.types';
|
||||
|
||||
export const syncOrganisationSubscriptionRoute = adminProcedure
|
||||
.input(ZSyncOrganisationSubscriptionRequestSchema)
|
||||
.output(ZSyncOrganisationSubscriptionResponseSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { organisationId, syncClaims } = input;
|
||||
|
||||
ctx.logger.info({
|
||||
input: {
|
||||
organisationId,
|
||||
syncClaims,
|
||||
},
|
||||
});
|
||||
|
||||
const organisation = await prisma.organisation.findUnique({
|
||||
where: { id: organisationId },
|
||||
include: {
|
||||
subscription: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!organisation) {
|
||||
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||
message: 'Organisation not found',
|
||||
});
|
||||
}
|
||||
|
||||
if (!organisation.subscription) {
|
||||
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
||||
message: 'Organisation has no subscription to sync',
|
||||
});
|
||||
}
|
||||
|
||||
let stripeSubscription: Stripe.Subscription;
|
||||
|
||||
try {
|
||||
stripeSubscription = await stripe.subscriptions.retrieve(organisation.subscription.planId, {
|
||||
expand: ['items.data.price.product'],
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof Stripe.errors.StripeInvalidRequestError && error.code === 'resource_missing') {
|
||||
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||
message: 'Subscription not found on Stripe',
|
||||
});
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
const stripeCustomerId =
|
||||
typeof stripeSubscription.customer === 'string' ? stripeSubscription.customer : stripeSubscription.customer.id;
|
||||
|
||||
if (organisation.customerId !== stripeCustomerId) {
|
||||
ctx.logger.error({
|
||||
message: 'Organisation customerId does not match Stripe subscription customer',
|
||||
organisationId,
|
||||
localCustomerId: organisation.customerId,
|
||||
stripeCustomerId,
|
||||
});
|
||||
|
||||
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
||||
message: `Organisation customerId mismatch: local=${organisation.customerId ?? 'null'}, Stripe=${stripeCustomerId}`,
|
||||
});
|
||||
}
|
||||
|
||||
await onSubscriptionUpdated({
|
||||
subscription: stripeSubscription,
|
||||
previousAttributes: null,
|
||||
bypassClaimUpdate: !syncClaims,
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const ZSyncOrganisationSubscriptionRequestSchema = z.object({
|
||||
organisationId: z.string(),
|
||||
syncClaims: z.boolean(),
|
||||
});
|
||||
|
||||
export const ZSyncOrganisationSubscriptionResponseSchema = z.void();
|
||||
|
||||
export type TSyncOrganisationSubscriptionRequest = z.infer<typeof ZSyncOrganisationSubscriptionRequestSchema>;
|
||||
export type TSyncOrganisationSubscriptionResponse = z.infer<typeof ZSyncOrganisationSubscriptionResponseSchema>;
|
||||
Reference in New Issue
Block a user