Skip to content

Commit c362b20

Browse files
committed
chore(clerk-js,backend): Replace /commerce endpoints with /billing endpoints
1 parent 465cf07 commit c362b20

File tree

7 files changed

+37
-30
lines changed

7 files changed

+37
-30
lines changed

.changeset/young-nails-matter.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
'@clerk/backend': patch
4+
---
5+
6+
Replace `/commerce` endpoints with `/billing` endpoints.

packages/backend/src/api/endpoints/BillingApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { BillingSubscriptionItem } from '../resources/CommerceSubscriptionI
77
import type { PaginatedResourceResponse } from '../resources/Deserializer';
88
import { AbstractAPI } from './AbstractApi';
99

10-
const basePath = '/commerce';
10+
const basePath = '/billing';
1111
const organizationBasePath = '/organizations';
1212
const userBasePath = '/users';
1313

packages/clerk-js/src/core/modules/billing/namespace.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,22 @@ import {
2828
} from '../../resources/internal';
2929

3030
export class Billing implements BillingNamespace {
31+
static readonly #pathRoot = '/billing';
32+
static path(subPath: string, param?: { orgId?: string }): string {
33+
const { orgId } = param || {};
34+
let prefix = '';
35+
if (orgId) {
36+
prefix = `/organizations/${orgId}`;
37+
}
38+
prefix = '/me';
39+
return `${prefix}${Billing.#pathRoot}${subPath}`;
40+
}
41+
3142
getPlans = async (params?: GetPlansParams): Promise<ClerkPaginatedResponse<BillingPlanResource>> => {
3243
const { for: forParam, ...safeParams } = params || {};
3344
const searchParams = { ...safeParams, payer_type: forParam === 'organization' ? 'org' : 'user' };
3445
return await BaseResource._fetch({
35-
path: `/commerce/plans`,
46+
path: Billing.path('/plans'),
3647
method: 'GET',
3748
search: convertPageToOffsetSearchParams(searchParams),
3849
}).then(res => {
@@ -48,15 +59,15 @@ export class Billing implements BillingNamespace {
4859
// Inconsistent API
4960
getPlan = async (params: { id: string }): Promise<BillingPlanResource> => {
5061
const plan = (await BaseResource._fetch({
51-
path: `/commerce/plans/${params.id}`,
62+
path: Billing.path(`/plans/${params.id}`),
5263
method: 'GET',
5364
})) as unknown as BillingPlanJSON;
5465
return new BillingPlan(plan);
5566
};
5667

5768
getSubscription = async (params: GetSubscriptionParams): Promise<BillingSubscriptionResource> => {
5869
return await BaseResource._fetch({
59-
path: params.orgId ? `/organizations/${params.orgId}/commerce/subscription` : `/me/commerce/subscription`,
70+
path: Billing.path(`/subscription`, { orgId: params.orgId }),
6071
method: 'GET',
6172
}).then(res => new BillingSubscription(res?.response as BillingSubscriptionJSON));
6273
};
@@ -65,7 +76,7 @@ export class Billing implements BillingNamespace {
6576
const { orgId, ...rest } = params;
6677

6778
return await BaseResource._fetch({
68-
path: orgId ? `/organizations/${orgId}/commerce/statements` : `/me/commerce/statements`,
79+
path: Billing.path(`/statements`, { orgId }),
6980
method: 'GET',
7081
search: convertPageToOffsetSearchParams(rest),
7182
}).then(res => {
@@ -82,9 +93,7 @@ export class Billing implements BillingNamespace {
8293
getStatement = async (params: { id: string; orgId?: string }): Promise<BillingStatementResource> => {
8394
const statement = (
8495
await BaseResource._fetch({
85-
path: params.orgId
86-
? `/organizations/${params.orgId}/commerce/statements/${params.id}`
87-
: `/me/commerce/statements/${params.id}`,
96+
path: Billing.path(`/statements/${params.id}`, { orgId: params.orgId }),
8897
method: 'GET',
8998
})
9099
)?.response as unknown as BillingStatementJSON;
@@ -97,7 +106,7 @@ export class Billing implements BillingNamespace {
97106
const { orgId, ...rest } = params;
98107

99108
return await BaseResource._fetch({
100-
path: orgId ? `/organizations/${orgId}/commerce/payment_attempts` : `/me/commerce/payment_attempts`,
109+
path: Billing.path(`/payment_attempts`, { orgId }),
101110
method: 'GET',
102111
search: convertPageToOffsetSearchParams(rest),
103112
}).then(res => {
@@ -112,9 +121,7 @@ export class Billing implements BillingNamespace {
112121

113122
getPaymentAttempt = async (params: { id: string; orgId?: string }): Promise<BillingPaymentResource> => {
114123
const paymentAttempt = (await BaseResource._fetch({
115-
path: params.orgId
116-
? `/organizations/${params.orgId}/commerce/payment_attempts/${params.id}`
117-
: `/me/commerce/payment_attempts/${params.id}`,
124+
path: Billing.path(`/payment_attempts/${params.id}`, { orgId: params.orgId }),
118125
method: 'GET',
119126
})) as unknown as BillingPaymentJSON;
120127
return new BillingPayment(paymentAttempt);
@@ -124,7 +131,7 @@ export class Billing implements BillingNamespace {
124131
const { orgId, ...rest } = params;
125132
const json = (
126133
await BaseResource._fetch<BillingCheckoutJSON>({
127-
path: orgId ? `/organizations/${orgId}/commerce/checkouts` : `/me/commerce/checkouts`,
134+
path: Billing.path(`/checkouts`, { orgId }),
128135
method: 'POST',
129136
body: rest as any,
130137
})

packages/clerk-js/src/core/modules/billing/payment-source-methods.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import type {
99

1010
import { convertPageToOffsetSearchParams } from '../../../utils/convertPageToOffsetSearchParams';
1111
import { BaseResource, BillingInitializedPaymentSource, BillingPaymentSource } from '../../resources/internal';
12+
import { Billing } from './namespace';
1213

1314
export const initializePaymentSource = async (params: InitializePaymentSourceParams) => {
1415
const { orgId, ...rest } = params;
1516
const json = (
1617
await BaseResource._fetch({
17-
path: orgId
18-
? `/organizations/${orgId}/commerce/payment_sources/initialize`
19-
: `/me/commerce/payment_sources/initialize`,
18+
path: Billing.path(`/payment_sources/initialize`, { orgId }),
2019
method: 'POST',
2120
body: rest as any,
2221
})
@@ -29,7 +28,7 @@ export const addPaymentSource = async (params: AddPaymentSourceParams) => {
2928

3029
const json = (
3130
await BaseResource._fetch({
32-
path: orgId ? `/organizations/${orgId}/commerce/payment_sources` : `/me/commerce/payment_sources`,
31+
path: Billing.path(`/payment_sources`, { orgId }),
3332
method: 'POST',
3433
body: rest as any,
3534
})
@@ -41,7 +40,7 @@ export const getPaymentSources = async (params: GetPaymentSourcesParams) => {
4140
const { orgId, ...rest } = params;
4241

4342
return await BaseResource._fetch({
44-
path: orgId ? `/organizations/${orgId}/commerce/payment_sources` : `/me/commerce/payment_sources`,
43+
path: Billing.path(`/payment_sources`, { orgId }),
4544
method: 'GET',
4645
search: convertPageToOffsetSearchParams(rest),
4746
}).then(res => {

packages/clerk-js/src/core/resources/BillingCheckout.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
import { unixEpochToDate } from '@/utils/date';
1212

1313
import { billingTotalsFromJSON } from '../../utils';
14+
import { Billing } from '../modules/billing/namespace';
1415
import { BillingPayer } from './BillingPayer';
1516
import { BaseResource, BillingPaymentSource, BillingPlan, isClerkAPIResponseError } from './internal';
1617

@@ -60,9 +61,7 @@ export class BillingCheckout extends BaseResource implements BillingCheckoutReso
6061
return retry(
6162
() =>
6263
this._basePatch({
63-
path: this.payer.organizationId
64-
? `/organizations/${this.payer.organizationId}/commerce/checkouts/${this.id}/confirm`
65-
: `/me/commerce/checkouts/${this.id}/confirm`,
64+
path: Billing.path(`/checkouts/${this.id}/confirm`, { orgId: this.payer.organizationId }),
6665
body: params as any,
6766
}),
6867
{

packages/clerk-js/src/core/resources/BillingPaymentSource.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
RemovePaymentSourceParams,
1010
} from '@clerk/types';
1111

12+
import { Billing } from '../modules/billing/namespace';
1213
import { BaseResource, DeletedObject } from './internal';
1314

1415
export class BillingPaymentSource extends BaseResource implements BillingPaymentSourceResource {
@@ -47,9 +48,7 @@ export class BillingPaymentSource extends BaseResource implements BillingPayment
4748
const { orgId } = params ?? {};
4849
const json = (
4950
await BaseResource._fetch({
50-
path: orgId
51-
? `/organizations/${orgId}/commerce/payment_sources/${this.id}`
52-
: `/me/commerce/payment_sources/${this.id}`,
51+
path: Billing.path(`/payment_sources/${this.id}`, { orgId }),
5352
method: 'DELETE',
5453
})
5554
)?.response as unknown as DeletedObjectJSON;
@@ -60,9 +59,7 @@ export class BillingPaymentSource extends BaseResource implements BillingPayment
6059
public async makeDefault(params?: MakeDefaultPaymentSourceParams) {
6160
const { orgId } = params ?? {};
6261
await BaseResource._fetch({
63-
path: orgId
64-
? `/organizations/${orgId}/commerce/payers/default_payment_source`
65-
: `/me/commerce/payers/default_payment_source`,
62+
path: Billing.path(`/payers/default_payment_source`, { orgId }),
6663
method: 'PUT',
6764
body: { payment_source_id: this.id } as any,
6865
});

packages/clerk-js/src/core/resources/BillingSubscription.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
import { unixEpochToDate } from '@/utils/date';
1414

1515
import { billingMoneyAmountFromJSON } from '../../utils';
16+
import { Billing } from '../modules/billing/namespace';
1617
import { BaseResource, BillingPlan, DeletedObject } from './internal';
1718

1819
export class BillingSubscription extends BaseResource implements BillingSubscriptionResource {
@@ -110,9 +111,7 @@ export class BillingSubscriptionItem extends BaseResource implements BillingSubs
110111
const { orgId } = params;
111112
const json = (
112113
await BaseResource._fetch({
113-
path: orgId
114-
? `/organizations/${orgId}/commerce/subscription_items/${this.id}`
115-
: `/me/commerce/subscription_items/${this.id}`,
114+
path: Billing.path(`/subscription_items/${this.id}`, { orgId }),
116115
method: 'DELETE',
117116
})
118117
)?.response as unknown as DeletedObjectJSON;

0 commit comments

Comments
 (0)