|
| 1 | +import type { |
| 2 | + AddPaymentSourceParams, |
| 3 | + ClerkPaginatedResponse, |
| 4 | + CommerceInitializedPaymentSourceJSON, |
| 5 | + CommercePaymentSourceJSON, |
| 6 | + GetPaymentSourcesParams, |
| 7 | + InitializePaymentSourceParams, |
| 8 | +} from '@clerk/types'; |
| 9 | + |
| 10 | +import { convertPageToOffsetSearchParams } from '../../../utils/convertPageToOffsetSearchParams'; |
| 11 | +import { BaseResource, CommerceInitializedPaymentSource, CommercePaymentSource } from '../../resources/internal'; |
| 12 | + |
| 13 | +export const initializePaymentSource = async (params: InitializePaymentSourceParams) => { |
| 14 | + const { orgId, ...rest } = params; |
| 15 | + const json = ( |
| 16 | + await BaseResource._fetch({ |
| 17 | + path: orgId |
| 18 | + ? `/organizations/${orgId}/commerce/payment_sources/initialize` |
| 19 | + : `/me/commerce/payment_sources/initialize`, |
| 20 | + method: 'POST', |
| 21 | + body: rest as any, |
| 22 | + }) |
| 23 | + )?.response as unknown as CommerceInitializedPaymentSourceJSON; |
| 24 | + return new CommerceInitializedPaymentSource(json); |
| 25 | +}; |
| 26 | + |
| 27 | +export const addPaymentSource = async (params: AddPaymentSourceParams) => { |
| 28 | + const { orgId, ...rest } = params; |
| 29 | + |
| 30 | + const json = ( |
| 31 | + await BaseResource._fetch({ |
| 32 | + path: orgId ? `/organizations/${orgId}/commerce/payment_sources` : `/me/commerce/payment_sources`, |
| 33 | + method: 'POST', |
| 34 | + body: rest as any, |
| 35 | + }) |
| 36 | + )?.response as unknown as CommercePaymentSourceJSON; |
| 37 | + return new CommercePaymentSource(json); |
| 38 | +}; |
| 39 | + |
| 40 | +export const getPaymentSources = async (params: GetPaymentSourcesParams) => { |
| 41 | + const { orgId, ...rest } = params; |
| 42 | + |
| 43 | + return await BaseResource._fetch({ |
| 44 | + path: orgId ? `/organizations/${orgId}/commerce/payment_sources` : `/me/commerce/payment_sources`, |
| 45 | + method: 'GET', |
| 46 | + search: convertPageToOffsetSearchParams(rest), |
| 47 | + }).then(res => { |
| 48 | + const { data: paymentSources, total_count } = |
| 49 | + res?.response as unknown as ClerkPaginatedResponse<CommercePaymentSourceJSON>; |
| 50 | + return { |
| 51 | + total_count, |
| 52 | + data: paymentSources.map(paymentSource => new CommercePaymentSource(paymentSource)), |
| 53 | + }; |
| 54 | + }); |
| 55 | +}; |
0 commit comments