UNPKG

@eleva-io/erp-sdk

Version:

SDK oficial para el ERP de Eleva

74 lines 3.37 kB
import { ElevaError } from '../../../errors'; import { GlobalBankAccountType } from '../bank-account/types'; import { validateAccountNumberABA, validateRoutingNumberABA } from '../bank-account/validations/bank_account/aba'; import { validateAccountNumberCLABE } from '../bank-account/validations/bank_account/clabe'; import { getIBANCountryCode, IBAN_COUNTRY_CODES, validateAccountNumberIBAN, } from '../bank-account/validations/bank_account/iban'; import { getSwiftCountry, validateCodeSwiftBic } from '../bank-account/validations/bank_account/swift'; export async function validatePaymentMethod(req) { const swiftCountryCode = req.swiftBic ? getSwiftCountry(req.swiftBic) : undefined; switch (req.type) { case GlobalBankAccountType.ABA: { if (!req.routingNumber) { throw ElevaError.invalidField({ error: { validation: 'routingNumber', code: 'invalid_string', message: 'Routing number is required', path: ['routingNumber'], }, message: 'failed to validate ABA bank account', }); } validateAccountNumberABA(req.accountNumber, 'accountNumber'); validateRoutingNumberABA(req.routingNumber, 'routingNumber'); if (swiftCountryCode !== 'US') { throw ElevaError.invalidField({ error: { validation: 'swiftBic', code: 'invalid_string', message: 'Invalid Swift BIC', path: ['swiftBic'], }, message: 'failed to validate ABA bank account', }); } break; } case GlobalBankAccountType.CLABE: { validateAccountNumberCLABE(req.accountNumber, 'accountNumber'); if (swiftCountryCode !== 'MX') { throw ElevaError.invalidField({ error: { validation: 'swiftBic', code: 'invalid_string', message: 'Invalid Swift BIC', path: ['swiftBic'], }, message: 'failed to validate CLABE bank account', }); } break; } case GlobalBankAccountType.IBAN: { validateAccountNumberIBAN(req.accountNumber, 'accountNumber'); const ibanCountryCode = getIBANCountryCode(req.accountNumber); if ((req.swiftBic || ibanCountryCode !== 'ES') && (!swiftCountryCode || !IBAN_COUNTRY_CODES.includes(swiftCountryCode) || swiftCountryCode !== ibanCountryCode)) { throw ElevaError.invalidField({ error: { validation: 'swiftBic', code: 'invalid_string', message: 'Invalid Swift BIC', path: ['swiftBic'], }, message: 'failed to validate IBAN bank account', }); } break; } } if (req.swiftBic) { validateCodeSwiftBic(req.swiftBic, 'swiftBic'); } } //# sourceMappingURL=payment_method.js.map