// Define the base type for NativePayData export type NativePayData = ApplePayData | GooglePayData | undefined; // Define ApplePayData as one of the possible types export class ApplePayData { type: 'apple_pay' = 'apple_pay'; countryCode?: string; merchantId?: string; currencyCode?: string; amount?: number; supportedNetworks?: string[]; supportedRegions?: string[]; methodId?: string; constructor( countryCode?: string, merchantId?: string, currencyCode?: string, amount?: number, supportedNetworks?: string[], supportedRegions?: string[], methodId?: string ) { this.countryCode = countryCode; this.merchantId = merchantId; this.currencyCode = currencyCode; this.amount = amount; this.supportedNetworks = supportedNetworks; this.supportedRegions = supportedRegions; this.methodId = methodId; } } export class GooglePayData { type: 'google_pay' = 'google_pay'; countryCode?: string; merchantId?: string; currencyCode?: string; amount?: number; merchantName?: string; gateway?: string; gatewayMerchantId?: string; allowedCardNetworks?: string[]; allowedCardAuthMethods?: string[]; constructor( countryCode?: string, merchantId?: string, currencyCode?: string, amount?: number, merchantName?: string, gateway?: string, gatewayMerchantId?: string, allowedCardNetworks?: string[], allowedCardAuthMethods?: string[] ) { this.countryCode = countryCode; this.merchantId = merchantId; this.currencyCode = currencyCode; this.amount = amount; this.merchantName = merchantName; this.gateway = gateway; this.gatewayMerchantId = gatewayMerchantId; this.allowedCardNetworks = allowedCardNetworks; this.allowedCardAuthMethods = allowedCardAuthMethods; } } export function googlePayFromJson(json: { [key: string]: any }): GooglePayData { return new GooglePayData( json.country_code, json.merchant_id, json.currency_code, json.amount ? parseFloat(json.amount) : undefined, json.merchant_name, json.gateway, json.gateway_merchant_id, json.allowed_card_networks ? Array.from(json.allowed_card_networks) : undefined, json.allowed_card_auth_methods ? Array.from(json.allowed_card_auth_methods) : undefined ); } export function applePayFromJson(json: { [key: string]: any }): ApplePayData { return new ApplePayData( json.country_code, json.merchant_id, json.currency_code, json.amount ? parseFloat(json.amount) : undefined, json.supported_networks ? Array.from(json.supported_networks) : undefined, json.supported_regions ? Array.from(json.supported_regions) : undefined, json.method_id ); } export function nativePayDataFromJson(json: { [key: string]: any; }): NativePayData { switch (json.type) { case 'apple_pay': return applePayFromJson(json); case 'google_pay': return googlePayFromJson(json); // Add cases for other types here default: throw new Error(`Unsupported NativePayData type: ${json.type}`); } } export function applePayDataToJson(data: ApplePayData): { [key: string]: any; } { return { type: data.type, country_code: data.countryCode, merchant_id: data.merchantId, currency_code: data.currencyCode, amount: data.amount, supported_networks: data.supportedNetworks, supported_regions: data.supportedRegions, method_id: data.methodId, }; } export function googlePayDataToJson(data: GooglePayData): { [key: string]: any; } { return { type: data.type, country_code: data.countryCode, merchant_id: data.merchantId, currency_code: data.currencyCode, amount: data.amount, merchant_name: data.merchantName, gateway: data.gateway, gateway_merchant_id: data.gatewayMerchantId, allowed_card_networks: data.allowedCardNetworks, allowed_card_auth_methods: data.allowedCardAuthMethods, }; } export function nativePayDataFromTypeAndJson( type: string, json: { [key: string]: any; } ): NativePayData { switch (type.toLowerCase()) { case 'apple_pay': return applePayFromJson(json); case 'google_pay': return googlePayFromJson(json); // Add cases for other types here default: return undefined; } }