Skip to content

Commit 2bbeaf3

Browse files
chore(backend): Add webhook event types for Commerce related events (#6338)
1 parent 1af2504 commit 2bbeaf3

File tree

4 files changed

+180
-1
lines changed

4 files changed

+180
-1
lines changed

.changeset/lucky-papers-act.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/backend': minor
3+
---
4+
5+
Add types for Commerce webhooks

packages/backend/src/api/resources/JSON.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export const ObjectType = {
6363
TestingToken: 'testing_token',
6464
Role: 'role',
6565
Permission: 'permission',
66+
CommercePayer: 'commerce_payer',
67+
CommercePaymentAttempt: 'commerce_payment_attempt',
68+
CommerceSubscription: 'commerce_subscription',
69+
CommerceSubscriptionItem: 'commerce_subscription_item',
6670
} as const;
6771

6872
export type ObjectType = (typeof ObjectType)[keyof typeof ObjectType];
@@ -757,6 +761,136 @@ export interface IdPOAuthAccessTokenJSON extends ClerkResourceJSON {
757761
updated_at: number;
758762
}
759763

764+
export interface CommercePayerJSON extends ClerkResourceJSON {
765+
object: typeof ObjectType.CommercePayer;
766+
instance_id: string;
767+
user_id?: string;
768+
first_name?: string;
769+
last_name?: string;
770+
email: string;
771+
organization_id?: string;
772+
organization_name?: string;
773+
image_url: string;
774+
created_at: number;
775+
updated_at: number;
776+
}
777+
778+
export interface CommercePayeeJSON {
779+
id: string;
780+
gateway_type: string;
781+
gateway_external_id: string;
782+
gateway_status: 'active' | 'pending' | 'restricted' | 'disconnected';
783+
}
784+
785+
export interface CommerceAmountJSON {
786+
amount: number;
787+
amount_formatted: string;
788+
currency: string;
789+
currency_symbol: string;
790+
}
791+
792+
export interface CommerceTotalsJSON {
793+
subtotal: CommerceAmountJSON;
794+
tax_total: CommerceAmountJSON;
795+
grand_total: CommerceAmountJSON;
796+
}
797+
798+
export interface CommercePaymentSourceJSON {
799+
id: string;
800+
gateway: string;
801+
gateway_external_id: string;
802+
gateway_external_account_id?: string;
803+
payment_method: string;
804+
status: 'active' | 'disconnected';
805+
card_type?: string;
806+
last4?: string;
807+
}
808+
809+
export interface CommercePaymentFailedReasonJSON {
810+
code: string;
811+
decline_code: string;
812+
}
813+
814+
export interface CommerceSubscriptionCreditJSON {
815+
amount: CommerceAmountJSON;
816+
cycle_days_remaining: number;
817+
cycle_days_total: number;
818+
cycle_remaining_percent: number;
819+
}
820+
821+
export interface CommercePlanJSON {
822+
id: string;
823+
instance_id: string;
824+
product_id: string;
825+
name: string;
826+
slug: string;
827+
description?: string;
828+
is_default: boolean;
829+
is_recurring: boolean;
830+
amount: number;
831+
period: 'month' | 'annual';
832+
interval: number;
833+
has_base_fee: boolean;
834+
currency: string;
835+
annual_monthly_amount: number;
836+
publicly_visible: boolean;
837+
}
838+
839+
export interface CommerceSubscriptionItemJSON extends ClerkResourceJSON {
840+
object: typeof ObjectType.CommerceSubscriptionItem;
841+
status: 'abandoned' | 'active' | 'canceled' | 'ended' | 'expired' | 'incomplete' | 'past_due' | 'upcoming';
842+
credit: CommerceSubscriptionCreditJSON;
843+
proration_date: string;
844+
plan_period: 'month' | 'annual';
845+
period_start: number;
846+
period_end?: number;
847+
canceled_at?: number;
848+
past_due_at?: number;
849+
lifetime_paid: number;
850+
next_payment_amount: number;
851+
next_payment_date: number;
852+
amount: CommerceAmountJSON;
853+
plan: CommercePlanJSON;
854+
plan_id: string;
855+
}
856+
857+
export interface CommercePaymentAttemptJSON extends ClerkResourceJSON {
858+
object: typeof ObjectType.CommercePaymentAttempt;
859+
instance_id: string;
860+
payment_id: string;
861+
statement_id: string;
862+
gateway_external_id: string;
863+
status: 'pending' | 'paid' | 'failed';
864+
created_at: number;
865+
updated_at: number;
866+
paid_at?: number;
867+
failed_at?: number;
868+
failed_reason?: CommercePaymentFailedReasonJSON;
869+
billing_date: number;
870+
charge_type: 'checkout' | 'recurring';
871+
payee: CommercePayeeJSON;
872+
payer: CommercePayerJSON;
873+
totals: CommerceTotalsJSON;
874+
payment_source: CommercePaymentSourceJSON;
875+
subscription_items: CommerceSubscriptionItemJSON[];
876+
}
877+
878+
export interface CommerceSubscriptionJSON extends ClerkResourceJSON {
879+
object: typeof ObjectType.CommerceSubscription;
880+
status: 'abandoned' | 'active' | 'canceled' | 'ended' | 'expired' | 'incomplete' | 'past_due' | 'upcoming';
881+
active_at?: number;
882+
canceled_at?: number;
883+
created_at: number;
884+
ended_at?: number;
885+
past_due_at?: number;
886+
updated_at: number;
887+
latest_payment_id: string;
888+
payer_id: string;
889+
payer: CommercePayerJSON;
890+
payment_source_id: string;
891+
items: CommerceSubscriptionItemJSON[];
892+
}
893+
760894
export interface WebhooksSvixJSON {
761895
svix_url: string;
762896
}

packages/backend/src/api/resources/Webhooks.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import type {
2+
CommercePaymentAttemptJSON,
3+
CommerceSubscriptionItemJSON,
4+
CommerceSubscriptionJSON,
25
DeletedObjectJSON,
36
EmailJSON,
47
OrganizationDomainJSON,
@@ -62,6 +65,29 @@ export type PermissionWebhookEvent = Webhook<
6265

6366
export type WaitlistEntryWebhookEvent = Webhook<'waitlistEntry.created' | 'waitlistEntry.updated', WaitlistEntryJSON>;
6467

68+
export type CommercePaymentAttemptWebhookEvent = Webhook<
69+
'paymentAttempt.created' | 'paymentAttempt.updated',
70+
CommercePaymentAttemptJSON
71+
>;
72+
73+
export type CommerceSubscriptionWebhookEvent = Webhook<
74+
'subscription.created' | 'subscription.updated' | 'subscription.active' | 'subscription.past_due',
75+
CommerceSubscriptionJSON
76+
>;
77+
78+
export type CommerceSubscriptionItemWebhookEvent = Webhook<
79+
| 'subscriptionItem.created'
80+
| 'subscriptionItem.updated'
81+
| 'subscriptionItem.active'
82+
| 'subscriptionItem.canceled'
83+
| 'subscriptionItem.upcoming'
84+
| 'subscriptionItem.ended'
85+
| 'subscriptionItem.abandoned'
86+
| 'subscriptionItem.incomplete'
87+
| 'subscriptionItem.past_due',
88+
CommerceSubscriptionItemJSON
89+
>;
90+
6591
export type WebhookEvent =
6692
| UserWebhookEvent
6793
| SessionWebhookEvent
@@ -73,6 +99,9 @@ export type WebhookEvent =
7399
| OrganizationInvitationWebhookEvent
74100
| RoleWebhookEvent
75101
| PermissionWebhookEvent
76-
| WaitlistEntryWebhookEvent;
102+
| WaitlistEntryWebhookEvent
103+
| CommercePaymentAttemptWebhookEvent
104+
| CommerceSubscriptionWebhookEvent
105+
| CommerceSubscriptionItemWebhookEvent;
77106

78107
export type WebhookEventType = WebhookEvent['type'];

packages/backend/src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ export type {
100100
PaginatedResponseJSON,
101101
TestingTokenJSON,
102102
WebhooksSvixJSON,
103+
CommercePayerJSON,
104+
CommercePayeeJSON,
105+
CommerceAmountJSON,
106+
CommerceTotalsJSON,
107+
CommercePaymentSourceJSON,
108+
CommercePaymentFailedReasonJSON,
109+
CommerceSubscriptionCreditJSON,
110+
CommercePlanJSON,
111+
CommerceSubscriptionItemJSON,
112+
CommercePaymentAttemptJSON,
113+
CommerceSubscriptionJSON,
103114
} from './api/resources/JSON';
104115

105116
/**

0 commit comments

Comments
 (0)