Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/proud-donuts-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
22 changes: 12 additions & 10 deletions .changeset/witty-doors-hear.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
'@clerk/types': minor
---

Expose stable commerce stable apis under `Clerk.commerce`
Expose Clerk Billing APIs.

## Render the pricing table component
- `Clerk.mountPricingTable`
- `Clerk.unmountPricingTable`

## Commerce namespace
- `Clerk.commerce.initializePaymentSource()`
- `Clerk.commerce.addPaymentSource()`
- `Clerk.commerce.getPaymentSources()`
- `Clerk.commerce.billing`
- `Clerk.commerce.billing.getPlans()`
- `Clerk.commerce.billing.getSubscriptions()`
- `Clerk.commerce.billing.getInvoices()`
- `Clerk.commerce.billing.startCheckout()`
## Manage payment methods
- `Clerk.[user|organization].initializePaymentSource()`
- `Clerk.[user|organization].addPaymentSource()`
- `Clerk.[user|organization].getPaymentSources()`

## Billing namespace
- `Clerk.billing`
- `Clerk.billing.getPlans()`
- `Clerk.billing.getSubscriptions()`
- `Clerk.billing.getInvoices()`
- `Clerk.billing.startCheckout()`
14 changes: 7 additions & 7 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import type {
ClerkOptions,
ClientJSONSnapshot,
ClientResource,
CommerceNamespace,
CommerceBillingNamespace,
CreateOrganizationParams,
CreateOrganizationProps,
CredentialReturn,
Expand Down Expand Up @@ -131,7 +131,7 @@ import { eventBus, events } from './events';
import type { FapiClient, FapiRequestCallback } from './fapiClient';
import { createFapiClient } from './fapiClient';
import { createClientFromJwt } from './jwt-client';
import { Commerce } from './modules/commerce';
import { CommerceBilling } from './modules/commerce';
import {
BaseResource,
Client,
Expand Down Expand Up @@ -187,7 +187,7 @@ export class Clerk implements ClerkInterface {
version: __PKG_VERSION__,
environment: process.env.NODE_ENV || 'production',
};
private static _commerce: CommerceNamespace;
private static _billing: CommerceBillingNamespace;

public client: ClientResource | undefined;
public session: SignedInSessionResource | null | undefined;
Expand Down Expand Up @@ -316,11 +316,11 @@ export class Clerk implements ClerkInterface {
return this.#options.standardBrowser || false;
}

get commerce(): CommerceNamespace {
if (!Clerk._commerce) {
Clerk._commerce = new Commerce();
get billing(): CommerceBillingNamespace {
if (!Clerk._billing) {
Clerk._billing = new CommerceBilling();
}
return Clerk._commerce;
return Clerk._billing;
}

public __internal_getOption<K extends keyof ClerkOptions>(key: K): ClerkOptions[K] {
Expand Down
69 changes: 0 additions & 69 deletions packages/clerk-js/src/core/modules/commerce/Commerce.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/clerk-js/src/core/modules/commerce/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './Commerce';
export * from './CommerceBilling';
export * from './payment-source-methods';
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type {
AddPaymentSourceParams,
ClerkPaginatedResponse,
CommerceInitializedPaymentSourceJSON,
CommercePaymentSourceJSON,
GetPaymentSourcesParams,
InitializePaymentSourceParams,
} from '@clerk/types';

import { convertPageToOffsetSearchParams } from '../../../utils/convertPageToOffsetSearchParams';
import { BaseResource, CommerceInitializedPaymentSource, CommercePaymentSource } from '../../resources/internal';

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`,
method: 'POST',
body: rest as any,
})
)?.response as unknown as CommerceInitializedPaymentSourceJSON;
return new CommerceInitializedPaymentSource(json);
};

export const addPaymentSource = async (params: AddPaymentSourceParams) => {
const { orgId, ...rest } = params;

const json = (
await BaseResource._fetch({
path: orgId ? `/organizations/${orgId}/commerce/payment_sources` : `/me/commerce/payment_sources`,
method: 'POST',
body: rest as any,
})
)?.response as unknown as CommercePaymentSourceJSON;
return new CommercePaymentSource(json);
};

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`,
method: 'GET',
search: convertPageToOffsetSearchParams(rest),
}).then(res => {
const { data: paymentSources, total_count } =
res?.response as unknown as ClerkPaginatedResponse<CommercePaymentSourceJSON>;
return {
total_count,
data: paymentSources.map(paymentSource => new CommercePaymentSource(paymentSource)),
};
});
};
22 changes: 22 additions & 0 deletions packages/clerk-js/src/core/resources/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {

import { convertPageToOffsetSearchParams } from '../../utils/convertPageToOffsetSearchParams';
import { unixEpochToDate } from '../../utils/date';
import { addPaymentSource, getPaymentSources, initializePaymentSource } from '../modules/commerce';
import { BaseResource, CommerceSubscription, OrganizationInvitation, OrganizationMembership } from './internal';
import { OrganizationDomain } from './OrganizationDomain';
import { OrganizationMembershipRequest } from './OrganizationMembershipRequest';
Expand Down Expand Up @@ -282,6 +283,27 @@ export class Organization extends BaseResource implements OrganizationResource {
}).then(res => new Organization(res?.response as OrganizationJSON));
};

initializePaymentSource: typeof initializePaymentSource = params => {
return initializePaymentSource({
...params,
orgId: this.id,
});
};

addPaymentSource: typeof addPaymentSource = params => {
return addPaymentSource({
...params,
orgId: this.id,
});
};

getPaymentSources: typeof getPaymentSources = params => {
return getPaymentSources({
...params,
orgId: this.id,
});
};

protected fromJSON(data: OrganizationJSON | OrganizationJSONSnapshot | null): this {
if (!data) {
return this;
Expand Down
13 changes: 13 additions & 0 deletions packages/clerk-js/src/core/resources/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { unixEpochToDate } from '../../utils/date';
import { normalizeUnsafeMetadata } from '../../utils/resourceParams';
import { getFullName } from '../../utils/user';
import { eventBus, events } from '../events';
import { addPaymentSource, getPaymentSources, initializePaymentSource } from '../modules/commerce';
import { BackupCode } from './BackupCode';
import {
BaseResource,
Expand Down Expand Up @@ -290,6 +291,18 @@ export class User extends BaseResource implements UserResource {
return new DeletedObject(json);
};

initializePaymentSource: typeof initializePaymentSource = params => {
return initializePaymentSource(params);
};

addPaymentSource: typeof addPaymentSource = params => {
return addPaymentSource(params);
};

getPaymentSources: typeof getPaymentSources = params => {
return getPaymentSources(params);
};

get verifiedExternalAccounts() {
return this.externalAccounts.filter(externalAccount => externalAccount.verification?.status == 'verified');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports[`Organization has the same initial properties 1`] = `
Organization {
"addMember": [Function],
"addPaymentSource": [Function],
"adminDeleteEnabled": true,
"createDomain": [Function],
"createdAt": 1970-01-01T00:00:12.345Z,
Expand All @@ -12,11 +13,13 @@ Organization {
"getInvitations": [Function],
"getMembershipRequests": [Function],
"getMemberships": [Function],
"getPaymentSources": [Function],
"getRoles": [Function],
"getSubscriptions": [Function],
"hasImage": true,
"id": "test_id",
"imageUrl": "https://img.clerk.com/eyJ0eXBlIjoiZGVmYXVsdCIsImlpZCI6Imluc18xbHlXRFppb2JyNjAwQUtVZVFEb1NsckVtb00iLCJyaWQiOiJ1c2VyXzJKbElJQTN2VXNjWXh1N2VUMnhINmFrTGgxOCIsImluaXRpYWxzIjoiREsifQ?width=160",
"initializePaymentSource": [Function],
"inviteMember": [Function],
"inviteMembers": [Function],
"maxAllowedMemberships": 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ OrganizationMembership {
"id": "test_id",
"organization": Organization {
"addMember": [Function],
"addPaymentSource": [Function],
"adminDeleteEnabled": true,
"createDomain": [Function],
"createdAt": 1970-01-01T00:00:12.345Z,
Expand All @@ -16,11 +17,13 @@ OrganizationMembership {
"getInvitations": [Function],
"getMembershipRequests": [Function],
"getMemberships": [Function],
"getPaymentSources": [Function],
"getRoles": [Function],
"getSubscriptions": [Function],
"hasImage": true,
"id": "test_org_id",
"imageUrl": "https://img.clerk.com/eyJ0eXBlIjoiZGVmYXVsdCIsImlpZCI6Imluc18xbHlXRFppb2JyNjAwQUtVZVFEb1NsckVtb00iLCJyaWQiOiJ1c2VyXzJKbElJQTN2VXNjWXh1N2VUMnhINmFrTGgxOCIsImluaXRpYWxzIjoiREsifQ?width=160",
"initializePaymentSource": [Function],
"inviteMember": [Function],
"inviteMembers": [Function],
"maxAllowedMemberships": 3,
Expand Down
14 changes: 5 additions & 9 deletions packages/clerk-js/src/ui/components/Checkout/CheckoutForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useClerk, useOrganization, useUser } from '@clerk/shared/react';
import { useOrganization } from '@clerk/shared/react';
import type {
ClerkAPIError,
ClerkRuntimeError,
Expand Down Expand Up @@ -114,22 +114,18 @@ const CheckoutFormElements = ({
checkout: CommerceCheckoutResource;
onCheckoutComplete: (checkout: CommerceCheckoutResource) => void;
}) => {
const { commerce } = useClerk();
const { user } = useUser();
const { organization } = useOrganization();
const { subscriberType } = useCheckoutContext();
const { subscriber, subscriberType } = useCheckoutContext();

const [paymentMethodSource, setPaymentMethodSource] = useState<PaymentMethodSource>('existing');
const [isSubmitting, setIsSubmitting] = useState(false);
const [submitError, setSubmitError] = useState<ClerkRuntimeError | ClerkAPIError | string | undefined>();

const { data, revalidate: revalidatePaymentSources } = useFetch(
commerce?.getPaymentSources,
{
...(subscriberType === 'org' ? { orgId: organization?.id } : {}),
},
subscriber().getPaymentSources,
{},
undefined,
`commerce-payment-sources-${user?.id}`,
`commerce-payment-sources-${subscriber().id}`,
);
const { data: paymentSources } = data || { data: [] };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ export const AddPaymentSource = (props: AddPaymentSourceProps) => {
onPayWithTestPaymentSourceSuccess,
showPayWithTestCardSection,
} = props;
const { commerce } = useClerk();
const { commerceSettings } = useEnvironment();
const { organization } = useOrganization();
const { user } = useUser();
const subscriberType = useSubscriberTypeContext();

const resource = subscriberType === 'org' ? organization : user;

const stripePromiseRef = useRef<Promise<Stripe | null> | null>(null);
const [stripe, setStripe] = useState<Stripe | null>(null);

Expand Down Expand Up @@ -73,13 +74,12 @@ export const AddPaymentSource = (props: AddPaymentSourceProps) => {
invalidate,
revalidate: revalidateInitializedPaymentSource,
} = useFetch(
commerce.initializePaymentSource,
resource?.initializePaymentSource,
{
gateway: 'stripe',
...(subscriberType === 'org' ? { orgId: organization?.id } : {}),
},
undefined,
`commerce-payment-source-initialize-${user?.id}`,
`commerce-payment-source-initialize-${resource?.id}`,
);

const externalGatewayId = initializedPaymentSource?.externalGatewayId;
Expand Down
Loading