import {z} from 'zod' export type MandateToken = string; export type Mode = 'live' | 'test'; export type MandateMethod = 'creditcard' | 'directdebit' | 'paypal'; export type MandateDetailsCardLabel = 'American Express' | 'Carta Si' | 'Carte Bleue' | 'Cartes Bancaires' | 'Dankort' | 'Diners Club' | 'Discover' | 'JCB' | 'Laser' | 'Maestro' | 'Mastercard' | 'Unionpay' | 'Visa' | 'null' | null; export type MandateScopes = 'customer-present' | 'customer-not-present'; export type MandateStatus = 'valid' | 'pending' | 'invalid'; export type CustomerToken = string; export type CreatedAt = string; export type TestmodeCreate = boolean | null; export type Url = {href: string, type: string}; export type MandateRequest = {resource?: string | undefined, id?: MandateToken | undefined, mode?: Mode | undefined, method: MandateMethod, consumerName: string, consumerAccount?: (string | null) | undefined, consumerBic?: (string | null) | undefined, consumerEmail?: (string | null) | undefined, details?: ({consumerName?: (string | null) | undefined, consumerAccount?: (string | null) | undefined, consumerBic?: (string | null) | undefined, cardHolder?: (string | null) | undefined, cardNumber?: (string | null) | undefined, cardExpiryDate?: (string | null) | undefined, cardLabel?: MandateDetailsCardLabel | undefined, cardFingerprint?: (string | null) | undefined}) | undefined, signatureDate?: (string | null) | undefined, mandateReference?: (string | null) | undefined, paypalBillingAgreementId?: (string | null) | undefined, payPalVaultId?: (string | null) | undefined, scopes?: (Array | null) | undefined, status?: MandateStatus | undefined, customerId?: CustomerToken | undefined, createdAt?: CreatedAt | undefined, testmode?: TestmodeCreate | undefined, _links?: {self: Url, customer: Url, documentation: Url} | undefined}; export type CreateApiV2CustomersCustomerIdMandatesArgs = {'idempotency-key'?: string | undefined, body: MandateRequest}; export type CreateApiV2CustomersCustomerIdMandatesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const mandateToken = z.string(); export const mode = z.enum(["live", "test"]); export const mandateMethodResponse = z.enum(["creditcard", "directdebit", "paypal"]); export const mandateDetailsCardLabelResponse = z.enum(["American Express", "Carta Si", "Carte Bleue", "Cartes Bancaires", "Dankort", "Diners Club", "Discover", "JCB", "Laser", "Maestro", "Mastercard", "Unionpay", "Visa", "null"]).nullable(); export const mandateScopesResponse = z.enum(["customer-present", "customer-not-present"]); export const mandateStatusResponse = z.enum(["valid", "pending", "invalid"]); export const customerToken = z.string(); export const createdAt = z.string(); export const testmodeCreate = z.boolean().nullable(); export const url = z.object({href: z.string(), type: z.string()}); export const mandateResponse = z.object({resource: z.string(), id: mandateToken, mode: mode, method: mandateMethodResponse, consumerName: z.string().optional(), consumerAccount: z.string().nullable().optional(), consumerBic: z.string().nullable().optional(), consumerEmail: z.string().nullable().optional(), details: z.object({consumerName: z.string().nullable().optional(), consumerAccount: z.string().nullable().optional(), consumerBic: z.string().nullable().optional(), cardHolder: z.string().nullable().optional(), cardNumber: z.string().nullable().optional(), cardExpiryDate: z.string().nullable().optional(), cardLabel: mandateDetailsCardLabelResponse.optional(), cardFingerprint: z.string().nullable().optional()}), signatureDate: z.string().nullable(), mandateReference: z.string().nullable(), paypalBillingAgreementId: z.string().nullable().optional(), payPalVaultId: z.string().nullable().optional(), scopes: z.array(mandateScopesResponse).nullable().optional(), status: mandateStatusResponse, customerId: customerToken, createdAt: createdAt, testmode: testmodeCreate.optional(), _links: z.object({self: url, customer: url, documentation: url})}); 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 }; /** * Create mandate * * POST /v2/customers/{customerId}/mandates * * Also accepts auth: advancedAccessToken, oAuth */ export const createApiV2CustomersCustomerIdMandates = async (args: CreateApiV2CustomersCustomerIdMandatesArgs, options?: CreateApiV2CustomersCustomerIdMandatesOptions) => { const {baseUrl = "https://api.mollie.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/hal+json") if (args['idempotency-key'] != null) headers.set("idempotency-key", String(args['idempotency-key'])) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/customers/${encodeURIComponent(String(args.customerId))}/mandates${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(mandateResponse, await res.json()) };