import { MethodType } from './MethodType'; import type { InputField } from './InputField'; import { inputFieldFromJson } from './InputField'; import { type NativePayData, nativePayDataFromTypeAndJson, } from './NativePayData'; import { type SavedBankAccount, savedBankAccountFromJson, } from './SavedBankAccount'; export function methodTypeFromJson(json: string) { const methodTypeMap = { expressMethod: MethodType.ExpressMethod, express_method: MethodType.ExpressMethod, customerBalance: MethodType.CustomerBalance, customer_balance: MethodType.CustomerBalance, savedCard: MethodType.SavedCard, saved_card: MethodType.SavedCard, paymentMethod: MethodType.PaymentMethod, payment_method: MethodType.PaymentMethod, payoutMethod: MethodType.PayoutMethod, payout_method: MethodType.PayoutMethod, savedBankAccount: MethodType.SavedBankAccount, saved_bank_account: MethodType.SavedBankAccount, }; return ( methodTypeMap[(json as keyof typeof methodTypeMap) ?? ''] ?? MethodType.PaymentMethod ); } export function methodTypeToJson( methodType: MethodType | null | undefined ): string | null { switch (methodType) { case MethodType.ExpressMethod: return 'expressMethod'; case MethodType.CustomerBalance: return 'customerBalance'; case MethodType.SavedCard: return 'savedCard'; case MethodType.PaymentMethod: return 'paymentMethod'; case MethodType.PayoutMethod: return 'payoutMethod'; case MethodType.SavedBankAccount: return 'savedBankAccount'; default: return null; } } export class CustomerBalance { balance?: number; id?: string; icon?: string; isSelected?: boolean; type?: MethodType; } export function customerBalanceFromJson(json: { [key: string]: any; }): CustomerBalance { return { balance: json.balance !== undefined ? Number(json.balance) : undefined, id: json.id, icon: json.icon, isSelected: json.isSelected ?? json.is_selected, type: methodTypeFromJson(json.type), }; } export function customerBalanceToJson(customerBalance: CustomerBalance): { [key: string]: any; } { return { balance: customerBalance.balance, id: customerBalance.id, icon: customerBalance.icon, isSelected: customerBalance.isSelected, type: methodTypeToJson(customerBalance.type), }; } export class PaymentMethod { id?: string; title?: string; isSelected?: boolean; confirmationRequired?: boolean; icons?: string[]; type?: MethodType; requiredBillingData?: [InputField]; requiredShippingData?: [InputField]; } export function paymentMethodFromJson(json: { [key: string]: any; }): PaymentMethod { return { id: json.id, title: json.title, isSelected: json.isSelected ?? json.is_selected, confirmationRequired: json.confirmation_required ?? json.confirmationRequired, icons: json.checkoutIcons || json.icons || [], type: methodTypeFromJson(json.type), requiredBillingData: json.required_billing_fields ? json.required_billing_fields.map((e: any) => inputFieldFromJson(e)) : undefined, requiredShippingData: json.required_shipping_fields ? json.required_shipping_fields.map((e: any) => inputFieldFromJson(e)) : undefined, }; } export class PayoutMethod { id?: string; title?: string; isSelected?: boolean; confirmationRequired?: boolean; icons?: string[]; type?: MethodType; requiredBillingData?: [InputField]; } export function payoutMethodFromJson(json: { [key: string]: any; }): PayoutMethod { return { id: json.id, title: json.title, isSelected: json.isSelected ?? json.is_selected, confirmationRequired: json.confirmation_required ?? json.confirmationRequired, icons: json.checkoutIcons || json.icons || [], type: methodTypeFromJson(json.type), requiredBillingData: json.required_billing_fields ? json.required_billing_fields.map((e: any) => inputFieldFromJson(e)) : undefined, }; } export class ExpressMethod { id?: string; title?: string; isSelected?: boolean; confirmationRequired?: boolean; icons?: string[]; type?: MethodType; requiredBillingData?: [InputField]; requiredShippingData?: [InputField]; nativePayData?: NativePayData; } export function expressMethodFromJson(json: { [key: string]: any; }): ExpressMethod { // Handle native_pay_data: string (iOS) or object (Android) let nativePayJson: any; // Check if native_pay_data is a string that needs parsing (iOS case) if (typeof json.native_pay_data === 'string') { try { // Parse the JSON string from native_pay_data nativePayJson = JSON.parse(json.native_pay_data); } catch (e) { // Log parsing error and set to undefined if parsing fails console.error( 'Failed to parse native_pay_data string:', json.native_pay_data, e ); nativePayJson = undefined; } } else if (typeof json.native_pay_data === 'object') { // Use the object directly if it's already parsed (Android case) nativePayJson = json.native_pay_data; } return { id: json.id, title: json.title, isSelected: json.isSelected ?? json.is_selected, confirmationRequired: json.confirmation_required ?? json.confirmationRequired, icons: json.checkoutIcons || json.icons || [], type: methodTypeFromJson(json.type), requiredBillingData: json.required_billing_fields ? json.required_billing_fields.map((e: any) => inputFieldFromJson(e)) : undefined, requiredShippingData: json.required_shipping_fields ? json.required_shipping_fields.map((e: any) => inputFieldFromJson(e)) : undefined, nativePayData: nativePayJson ? nativePayDataFromTypeAndJson(json.id, nativePayJson) : undefined, }; } export class SavedCard { id?: string; brand?: string; last4?: string; expiryMonth?: string; expiryYear?: string; country?: string; logo?: string; requireCvv?: boolean; cvvConfig?: CvvConfig; type?: MethodType; bin?: string; } export function savedCardFromJson(json: { [key: string]: any }): SavedCard { return { id: json.id, brand: json.brand, last4: json.last_4 || json.last4, expiryMonth: json.expiry_month || json.expiryMonth, expiryYear: json.expiry_year || json.expiryYear, country: json.country, logo: json.logo, requireCvv: json.require_cvv ?? json.requires_cvv ?? json.requireCvv, cvvConfig: json.cvv_config || json.cvvConfig ? cvvConfigFromJson(json.cvv_config || json.cvvConfig) : undefined, type: methodTypeFromJson(json.type), bin: json.bin, }; } export function savedCardToJson(savedCard: SavedCard): { [key: string]: any } { return { id: savedCard.id, brand: savedCard.brand, last_4: savedCard.last4, expiry_month: savedCard.expiryMonth, expiry_year: savedCard.expiryYear, country: savedCard.country, logo: savedCard.logo, require_cvv: savedCard.requireCvv, cvv_config: savedCard.cvvConfig ? cvvConfigToJson(savedCard.cvvConfig) : undefined, type: methodTypeToJson(savedCard.type), bin: savedCard.bin, }; } export class CvvConfig { digitsCount?: number; } function cvvConfigFromJson(json: { [key: string]: any }): CvvConfig { return { digitsCount: json.digits_count || json.digitsCount, }; } function cvvConfigToJson(cvvConfig: CvvConfig): { [key: string]: any } { return { digits_count: cvvConfig.digitsCount, }; } export class IntentMethods { customerBalances?: CustomerBalance[]; paymentMethods?: PaymentMethod[]; expressMethods?: ExpressMethod[]; savedCards?: SavedCard[]; payoutMethods?: PayoutMethod[]; savedBankAccounts?: SavedBankAccount[]; constructor(params: { customerBalances?: CustomerBalance[]; paymentMethods?: PaymentMethod[]; expressMethods?: ExpressMethod[]; savedCards?: SavedCard[]; payoutMethods?: PayoutMethod[]; savedBankAccounts?: SavedBankAccount[]; }) { this.customerBalances = params.customerBalances; this.paymentMethods = params.paymentMethods; this.expressMethods = params.expressMethods; this.savedCards = params.savedCards; this.payoutMethods = params.payoutMethods; this.savedBankAccounts = params.savedBankAccounts; } } export function intentMethodsFromJson(json: { [key: string]: any; }): IntentMethods { return new IntentMethods({ customerBalances: ( json.customer_balances || json.customerBalances || [] ).map((e: any) => customerBalanceFromJson(e)), paymentMethods: (json.payment_methods || json.paymentMethods || []).map( (e: any) => paymentMethodFromJson(e) ), expressMethods: (json.express_methods || json.expressMethods || []).map( (e: any) => expressMethodFromJson(e) ), savedCards: (json.saved_cards || json.savedCards || []).map((e: any) => savedCardFromJson(e) ), payoutMethods: (json.payout_methods || json.payoutMethods || []).map( (e: any) => payoutMethodFromJson(e) ), savedBankAccounts: ( json.saved_bank_accounts || json.savedBankAccounts || [] ).map((e: any) => savedBankAccountFromJson(e)), }); }