import {z} from 'zod' export type UUID = string; export type Currency = 'JPY' | 'USD' | 'EUR' | 'TWD' | 'KRW' | 'PLN' | 'GBP' | 'HKD' | 'SGD' | 'NZD' | 'AUD' | 'IDR' | 'MYR' | 'PHP' | 'THB' | 'CNY' | 'BRL' | 'CHF' | 'CAD' | 'VND'; export type CustomerEmail = string; export type CreditCardNumber = string; export type CreditCardMonth = number; export type CreditCardYear = number; export type CreditCardVerificationValue = string; export type CustomerName = string; export type CustomerGivenName = string; export type CustomerFamilyName = string; export type Intent = 'one_time' | 'card_on_file' | 'subscription' | 'installment'; export type Installments = '3' | '5' | '6' | '10' | '12' | '15' | '18' | '20' | '24' | 'revolving'; export type PaymentDetailsCreditCard = {type: 'credit_card', email?: CustomerEmail | undefined, number: CreditCardNumber, month: CreditCardMonth, year: CreditCardYear, verification_value?: CreditCardVerificationValue | undefined, name?: CustomerName | undefined, given_name?: CustomerGivenName | undefined, family_name?: CustomerFamilyName | undefined, expiry_days?: number | undefined, intent?: Intent | undefined, initiator?: ('merchant' | 'customer') | undefined, usage?: ('first' | 'used') | undefined, scheme_reference?: string | undefined, installments?: Installments | undefined, shipping_address_name?: string | undefined, shipping_address_line1?: string | undefined, shipping_address_line2?: string | undefined, shipping_address_city?: string | undefined, shipping_address_state?: string | undefined, shipping_address_zip?: string | undefined, shipping_address_country?: string | undefined, billing_address_name?: string | undefined, billing_address_line1?: string | undefined, billing_address_line2?: string | undefined, billing_address_city?: string | undefined, billing_address_state?: string | undefined, billing_address_zip?: string | undefined, billing_address_country?: string | undefined}; export type SocialIdBirthDate = string; export type SocialIdCorporateNumber = string; export type PaymentDetailsCreditCardKorea = {type: 'credit_card_korea', number: CreditCardNumber, month: CreditCardMonth, year: CreditCardYear, verification_value?: CreditCardVerificationValue | undefined, corporate_card?: boolean | undefined, social_id?: (SocialIdBirthDate | SocialIdCorporateNumber) | undefined, first_two_digits_of_pin: string, shipping_address_name?: string | undefined, shipping_address_line1?: string | undefined, shipping_address_line2?: string | undefined, shipping_address_city?: string | undefined, shipping_address_state?: string | undefined, shipping_address_zip?: string | undefined, shipping_address_country?: string | undefined, billing_address_name?: string | undefined, billing_address_line1?: string | undefined, billing_address_line2?: string | undefined, billing_address_city?: string | undefined, billing_address_state?: string | undefined, billing_address_zip?: string | undefined, billing_address_country?: string | undefined}; export type PaymentDetailsCreditCardBrazil = {type: 'credit_card_brazil', email: CustomerEmail, number: CreditCardNumber, month: CreditCardMonth, year: CreditCardYear, verification_value: CreditCardVerificationValue, name: CustomerName, cpf_or_cnpj: string, customer_ip: string, shipping_address_name?: string | undefined, shipping_address_line1?: string | undefined, shipping_address_line2?: string | undefined, shipping_address_city?: string | undefined, shipping_address_state?: string | undefined, shipping_address_zip?: string | undefined, shipping_address_country?: string | undefined, billing_address_name?: string | undefined, billing_address_line1?: string | undefined, billing_address_line2?: string | undefined, billing_address_city?: string | undefined, billing_address_state?: string | undefined, billing_address_zip?: string | undefined, billing_address_country?: string | undefined}; export type PaymentDetailsCreditCardTerminal = {type: 'credit_card_terminal', number: CreditCardNumber, month?: CreditCardMonth | undefined, year?: CreditCardYear | undefined, sequence_number?: string | undefined, field55?: string | undefined, posDataCode?: string | undefined, track2?: string | undefined, flowType?: string | undefined, shipping_address_name?: string | undefined, shipping_address_line1?: string | undefined, shipping_address_line2?: string | undefined, shipping_address_city?: string | undefined, shipping_address_state?: string | undefined, shipping_address_zip?: string | undefined, shipping_address_country?: string | undefined, billing_address_name?: string | undefined, billing_address_line1?: string | undefined, billing_address_line2?: string | undefined, billing_address_city?: string | undefined, billing_address_state?: string | undefined, billing_address_zip?: string | undefined, billing_address_country?: string | undefined}; export type PaymentDetailsOnlyCreditCards = PaymentDetailsCreditCard | PaymentDetailsCreditCardKorea | PaymentDetailsCreditCardBrazil | PaymentDetailsCreditCardTerminal; export type UpdateCustomerRequest = {currency?: Currency | undefined, payment_details?: PaymentDetailsOnlyCreditCards | undefined, email?: string | undefined, metadata?: Record | undefined}; export type PatchApiCustomersIdArgs = {id: UUID, body: UpdateCustomerRequest}; export type PatchApiCustomersIdOptions = {baseUrl?: string | undefined, apiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string().min(25).max(25); export const locale = z.enum(["ja", "en", "ko"]); export const customer = z.object({id: uUID, resource: z.literal("customer"), email: z.string().nullable(), source: z.object({type: z.string(), brand: z.string(), last_four_digits: z.string(), month: z.number().int(), year: z.number().int()}).nullable(), metadata: z.object({}), created_at: z.string(), locale: locale.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Customer: Update * * PATCH /customers/{id} */ export const patchApiCustomersId = async (args: PatchApiCustomersIdArgs, options?: PatchApiCustomersIdOptions) => { const {baseUrl = "https://komoju.com/api/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKey.username}:${apiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(customer, await res.json()) };