diff --git a/.changeset/young-nails-matter.md b/.changeset/young-nails-matter.md new file mode 100644 index 00000000000..86e94acb714 --- /dev/null +++ b/.changeset/young-nails-matter.md @@ -0,0 +1,6 @@ +--- +'@clerk/clerk-js': patch +'@clerk/backend': patch +--- + +Replace `/commerce` endpoints with `/billing` endpoints. diff --git a/packages/backend/src/api/endpoints/BillingApi.ts b/packages/backend/src/api/endpoints/BillingApi.ts index 1a4dbf7ed33..c14d3a5edf5 100644 --- a/packages/backend/src/api/endpoints/BillingApi.ts +++ b/packages/backend/src/api/endpoints/BillingApi.ts @@ -7,7 +7,7 @@ import type { BillingSubscriptionItem } from '../resources/CommerceSubscriptionI import type { PaginatedResourceResponse } from '../resources/Deserializer'; import { AbstractAPI } from './AbstractApi'; -const basePath = '/commerce'; +const basePath = '/billing'; const organizationBasePath = '/organizations'; const userBasePath = '/users'; diff --git a/packages/clerk-js/src/core/modules/billing/namespace.ts b/packages/clerk-js/src/core/modules/billing/namespace.ts index 6976b653729..8fa6bc54cfa 100644 --- a/packages/clerk-js/src/core/modules/billing/namespace.ts +++ b/packages/clerk-js/src/core/modules/billing/namespace.ts @@ -28,11 +28,22 @@ import { } from '../../resources/internal'; export class Billing implements BillingNamespace { + static readonly #pathRoot = '/billing'; + static path(subPath: string, param?: { orgId?: string }): string { + const { orgId } = param || {}; + let prefix = ''; + if (orgId) { + prefix = `/organizations/${orgId}`; + } + prefix = '/me'; + return `${prefix}${Billing.#pathRoot}${subPath}`; + } + getPlans = async (params?: GetPlansParams): Promise> => { const { for: forParam, ...safeParams } = params || {}; const searchParams = { ...safeParams, payer_type: forParam === 'organization' ? 'org' : 'user' }; return await BaseResource._fetch({ - path: `/commerce/plans`, + path: Billing.path('/plans'), method: 'GET', search: convertPageToOffsetSearchParams(searchParams), }).then(res => { @@ -48,7 +59,7 @@ export class Billing implements BillingNamespace { // Inconsistent API getPlan = async (params: { id: string }): Promise => { const plan = (await BaseResource._fetch({ - path: `/commerce/plans/${params.id}`, + path: Billing.path(`/plans/${params.id}`), method: 'GET', })) as unknown as BillingPlanJSON; return new BillingPlan(plan); @@ -56,7 +67,7 @@ export class Billing implements BillingNamespace { getSubscription = async (params: GetSubscriptionParams): Promise => { return await BaseResource._fetch({ - path: params.orgId ? `/organizations/${params.orgId}/commerce/subscription` : `/me/commerce/subscription`, + path: Billing.path(`/subscription`, { orgId: params.orgId }), method: 'GET', }).then(res => new BillingSubscription(res?.response as BillingSubscriptionJSON)); }; @@ -65,7 +76,7 @@ export class Billing implements BillingNamespace { const { orgId, ...rest } = params; return await BaseResource._fetch({ - path: orgId ? `/organizations/${orgId}/commerce/statements` : `/me/commerce/statements`, + path: Billing.path(`/statements`, { orgId }), method: 'GET', search: convertPageToOffsetSearchParams(rest), }).then(res => { @@ -82,9 +93,7 @@ export class Billing implements BillingNamespace { getStatement = async (params: { id: string; orgId?: string }): Promise => { const statement = ( await BaseResource._fetch({ - path: params.orgId - ? `/organizations/${params.orgId}/commerce/statements/${params.id}` - : `/me/commerce/statements/${params.id}`, + path: Billing.path(`/statements/${params.id}`, { orgId: params.orgId }), method: 'GET', }) )?.response as unknown as BillingStatementJSON; @@ -97,7 +106,7 @@ export class Billing implements BillingNamespace { const { orgId, ...rest } = params; return await BaseResource._fetch({ - path: orgId ? `/organizations/${orgId}/commerce/payment_attempts` : `/me/commerce/payment_attempts`, + path: Billing.path(`/payment_attempts`, { orgId }), method: 'GET', search: convertPageToOffsetSearchParams(rest), }).then(res => { @@ -112,9 +121,7 @@ export class Billing implements BillingNamespace { getPaymentAttempt = async (params: { id: string; orgId?: string }): Promise => { const paymentAttempt = (await BaseResource._fetch({ - path: params.orgId - ? `/organizations/${params.orgId}/commerce/payment_attempts/${params.id}` - : `/me/commerce/payment_attempts/${params.id}`, + path: Billing.path(`/payment_attempts/${params.id}`, { orgId: params.orgId }), method: 'GET', })) as unknown as BillingPaymentJSON; return new BillingPayment(paymentAttempt); @@ -124,7 +131,7 @@ export class Billing implements BillingNamespace { const { orgId, ...rest } = params; const json = ( await BaseResource._fetch({ - path: orgId ? `/organizations/${orgId}/commerce/checkouts` : `/me/commerce/checkouts`, + path: Billing.path(`/checkouts`, { orgId }), method: 'POST', body: rest as any, }) diff --git a/packages/clerk-js/src/core/modules/billing/payment-source-methods.ts b/packages/clerk-js/src/core/modules/billing/payment-source-methods.ts index 5dd2fc7bbc9..e6d7af35d0e 100644 --- a/packages/clerk-js/src/core/modules/billing/payment-source-methods.ts +++ b/packages/clerk-js/src/core/modules/billing/payment-source-methods.ts @@ -9,14 +9,13 @@ import type { import { convertPageToOffsetSearchParams } from '../../../utils/convertPageToOffsetSearchParams'; import { BaseResource, BillingInitializedPaymentSource, BillingPaymentSource } from '../../resources/internal'; +import { Billing } from './namespace'; export const initializePaymentSource = async (params: InitializePaymentSourceParams) => { const { orgId, ...rest } = params; const json = ( await BaseResource._fetch({ - path: orgId - ? `/organizations/${orgId}/commerce/payment_sources/initialize` - : `/me/commerce/payment_sources/initialize`, + path: Billing.path(`/payment_sources/initialize`, { orgId }), method: 'POST', body: rest as any, }) @@ -29,7 +28,7 @@ export const addPaymentSource = async (params: AddPaymentSourceParams) => { const json = ( await BaseResource._fetch({ - path: orgId ? `/organizations/${orgId}/commerce/payment_sources` : `/me/commerce/payment_sources`, + path: Billing.path(`/payment_sources`, { orgId }), method: 'POST', body: rest as any, }) @@ -41,7 +40,7 @@ export const getPaymentSources = async (params: GetPaymentSourcesParams) => { const { orgId, ...rest } = params; return await BaseResource._fetch({ - path: orgId ? `/organizations/${orgId}/commerce/payment_sources` : `/me/commerce/payment_sources`, + path: Billing.path(`/payment_sources`, { orgId }), method: 'GET', search: convertPageToOffsetSearchParams(rest), }).then(res => { diff --git a/packages/clerk-js/src/core/resources/BillingCheckout.ts b/packages/clerk-js/src/core/resources/BillingCheckout.ts index 4e3225b3d37..e357328c694 100644 --- a/packages/clerk-js/src/core/resources/BillingCheckout.ts +++ b/packages/clerk-js/src/core/resources/BillingCheckout.ts @@ -11,6 +11,7 @@ import type { import { unixEpochToDate } from '@/utils/date'; import { billingTotalsFromJSON } from '../../utils'; +import { Billing } from '../modules/billing/namespace'; import { BillingPayer } from './BillingPayer'; import { BaseResource, BillingPaymentSource, BillingPlan, isClerkAPIResponseError } from './internal'; @@ -60,9 +61,7 @@ export class BillingCheckout extends BaseResource implements BillingCheckoutReso return retry( () => this._basePatch({ - path: this.payer.organizationId - ? `/organizations/${this.payer.organizationId}/commerce/checkouts/${this.id}/confirm` - : `/me/commerce/checkouts/${this.id}/confirm`, + path: Billing.path(`/checkouts/${this.id}/confirm`, { orgId: this.payer.organizationId }), body: params as any, }), { diff --git a/packages/clerk-js/src/core/resources/BillingPaymentSource.ts b/packages/clerk-js/src/core/resources/BillingPaymentSource.ts index 1339dc4db70..c3b5221c992 100644 --- a/packages/clerk-js/src/core/resources/BillingPaymentSource.ts +++ b/packages/clerk-js/src/core/resources/BillingPaymentSource.ts @@ -9,6 +9,7 @@ import type { RemovePaymentSourceParams, } from '@clerk/types'; +import { Billing } from '../modules/billing/namespace'; import { BaseResource, DeletedObject } from './internal'; export class BillingPaymentSource extends BaseResource implements BillingPaymentSourceResource { @@ -47,9 +48,7 @@ export class BillingPaymentSource extends BaseResource implements BillingPayment const { orgId } = params ?? {}; const json = ( await BaseResource._fetch({ - path: orgId - ? `/organizations/${orgId}/commerce/payment_sources/${this.id}` - : `/me/commerce/payment_sources/${this.id}`, + path: Billing.path(`/payment_sources/${this.id}`, { orgId }), method: 'DELETE', }) )?.response as unknown as DeletedObjectJSON; @@ -60,9 +59,7 @@ export class BillingPaymentSource extends BaseResource implements BillingPayment public async makeDefault(params?: MakeDefaultPaymentSourceParams) { const { orgId } = params ?? {}; await BaseResource._fetch({ - path: orgId - ? `/organizations/${orgId}/commerce/payers/default_payment_source` - : `/me/commerce/payers/default_payment_source`, + path: Billing.path(`/payers/default_payment_source`, { orgId }), method: 'PUT', body: { payment_source_id: this.id } as any, }); diff --git a/packages/clerk-js/src/core/resources/BillingSubscription.ts b/packages/clerk-js/src/core/resources/BillingSubscription.ts index a5550d4c8d8..ed6bc617101 100644 --- a/packages/clerk-js/src/core/resources/BillingSubscription.ts +++ b/packages/clerk-js/src/core/resources/BillingSubscription.ts @@ -13,6 +13,7 @@ import type { import { unixEpochToDate } from '@/utils/date'; import { billingMoneyAmountFromJSON } from '../../utils'; +import { Billing } from '../modules/billing/namespace'; import { BaseResource, BillingPlan, DeletedObject } from './internal'; export class BillingSubscription extends BaseResource implements BillingSubscriptionResource { @@ -110,9 +111,7 @@ export class BillingSubscriptionItem extends BaseResource implements BillingSubs const { orgId } = params; const json = ( await BaseResource._fetch({ - path: orgId - ? `/organizations/${orgId}/commerce/subscription_items/${this.id}` - : `/me/commerce/subscription_items/${this.id}`, + path: Billing.path(`/subscription_items/${this.id}`, { orgId }), method: 'DELETE', }) )?.response as unknown as DeletedObjectJSON;