Improve and update billing flow
This commit updates various components in the billing flow due to a new schema that supports multiple line items per plan. The added flexibility rendered 'line-items-mapper.ts' redundant, which has been removed. Additionally, webhooks have been created for handling account membership insertions and deletions, as well as handling subscription deletions when an account is deleted. This message also introduces a new service to handle sending out invitation emails. Lastly, the validation of the billing provider has been improved for increased security and stability.
This commit is contained in:
16
packages/database-webhooks/src/server/record-change.type.ts
Normal file
16
packages/database-webhooks/src/server/record-change.type.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
export type Tables = Database['public']['Tables'];
|
||||
|
||||
export type TableChangeType = 'INSERT' | 'UPDATE' | 'DELETE';
|
||||
|
||||
export interface RecordChange<
|
||||
Table extends keyof Tables,
|
||||
Row = Tables[Table]['Row'],
|
||||
> {
|
||||
type: TableChangeType;
|
||||
table: Table;
|
||||
record: Row;
|
||||
schema: 'public';
|
||||
old_record: null | Row;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { Logger } from '@kit/shared/logger';
|
||||
import { getSupabaseRouteHandlerClient } from '@kit/supabase/route-handler-client';
|
||||
|
||||
import { RecordChange, Tables } from '../record-change.type';
|
||||
import { DatabaseWebhookRouterService } from './database-webhook-router.service';
|
||||
|
||||
export class DatabaseWebhookHandlerService {
|
||||
private readonly namespace = 'database-webhook-handler';
|
||||
|
||||
async handleWebhook(request: Request, webhooksSecret: string) {
|
||||
Logger.info(
|
||||
{
|
||||
name: this.namespace,
|
||||
},
|
||||
'Received webhook from DB. Processing...',
|
||||
);
|
||||
|
||||
// check if the signature is valid
|
||||
this.assertSignatureIsAuthentic(request, webhooksSecret);
|
||||
|
||||
const json = await request.json();
|
||||
|
||||
await this.handleWebhookBody(json);
|
||||
}
|
||||
|
||||
private handleWebhookBody(body: RecordChange<keyof Tables>) {
|
||||
const client = getSupabaseRouteHandlerClient({
|
||||
admin: true,
|
||||
});
|
||||
|
||||
const service = new DatabaseWebhookRouterService(client);
|
||||
|
||||
return service.handleWebhook(body);
|
||||
}
|
||||
|
||||
private assertSignatureIsAuthentic(request: Request, webhooksSecret: string) {
|
||||
const header = request.headers.get('X-Supabase-Event-Signature');
|
||||
|
||||
if (header !== webhooksSecret) {
|
||||
throw new Error('Invalid signature');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
import { RecordChange, Tables } from '../record-change.type';
|
||||
|
||||
export class DatabaseWebhookRouterService {
|
||||
constructor(private readonly adminClient: SupabaseClient<Database>) {}
|
||||
|
||||
handleWebhook(body: RecordChange<keyof Tables>) {
|
||||
switch (body.table) {
|
||||
case 'invitations': {
|
||||
const payload = body as RecordChange<typeof body.table>;
|
||||
|
||||
return this.handleInvitations(payload);
|
||||
}
|
||||
|
||||
case 'subscriptions': {
|
||||
const payload = body as RecordChange<typeof body.table>;
|
||||
|
||||
return this.handleSubscriptions(payload);
|
||||
}
|
||||
|
||||
case 'accounts_memberships': {
|
||||
const payload = body as RecordChange<typeof body.table>;
|
||||
|
||||
return this.handleAccountsMemberships(payload);
|
||||
}
|
||||
|
||||
default:
|
||||
throw new Error('No handler for this table');
|
||||
}
|
||||
}
|
||||
|
||||
private async handleInvitations(body: RecordChange<'invitations'>) {
|
||||
const { AccountInvitationsWebhookService } = await import(
|
||||
'@kit/team-accounts/webhooks'
|
||||
);
|
||||
|
||||
const service = new AccountInvitationsWebhookService(this.adminClient);
|
||||
|
||||
return service.handleInvitationWebhook(body.record);
|
||||
}
|
||||
|
||||
private async handleSubscriptions(body: RecordChange<'subscriptions'>) {
|
||||
const { BillingWebhooksService } = await import('@kit/billing-gateway');
|
||||
const service = new BillingWebhooksService();
|
||||
|
||||
return service.handleSubscriptionDeletedWebhook(body.record);
|
||||
}
|
||||
|
||||
private handleAccountsMemberships(
|
||||
payload: RecordChange<'accounts_memberships'>,
|
||||
) {
|
||||
// no-op
|
||||
return Promise.resolve(undefined);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user