@automattic/wpcom-checkout
Version:
Functions and components used by WordPress.com checkout.
130 lines • 5.76 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isEbanxCreditCardProcessingEnabledForCountry = isEbanxCreditCardProcessingEnabledForCountry;
exports.isValidCPF = isValidCPF;
exports.isValidCNPJ = isValidCNPJ;
exports.fullAddressFieldsRules = fullAddressFieldsRules;
exports.countrySpecificFieldRules = countrySpecificFieldRules;
exports.translatedEbanxError = translatedEbanxError;
const tslib_1 = require("tslib");
const cnpj_1 = require("@fnando/cnpj");
const cpf_1 = require("@fnando/cpf");
const i18n_calypso_1 = tslib_1.__importDefault(require("i18n-calypso"));
const payment_processor_countries_fields_1 = require("./payment-processor-countries-fields");
const index_1 = require("./index");
/**
* Returns whether we should Ebanx credit card processing for a particular country
* @param {string} countryCode - a two-letter country code, e.g., 'DE', 'BR'
* @param {import('@automattic/shopping-cart').ResponseCart} cart - The shopping cart
* @returns {boolean} Whether the country code requires ebanx payment processing
*/
function isEbanxCreditCardProcessingEnabledForCountry(countryCode, cart) {
return (typeof payment_processor_countries_fields_1.PAYMENT_PROCESSOR_COUNTRIES_FIELDS[countryCode] !== 'undefined' &&
(0, index_1.isPaymentMethodEnabled)('ebanx', cart.allowed_payment_methods?.map((method) => (0, index_1.translateWpcomPaymentMethodToCheckoutPaymentMethod)(method))));
}
/**
* CPF number (Cadastrado de Pessoas Físicas) is the Brazilian tax identification number.
* Total of 11 digits: 9 numbers followed by 2 verification numbers . E.g., 188.247.019-22
* @param {string} cpf - a Brazilian tax identification number
* @returns {boolean} Whether the cpf is valid or not
*/
function isValidCPF(cpf) {
return (0, cpf_1.isValid)(cpf);
}
/**
* CNPJ number (Cadastro Nacional da Pessoa Jurídica ) is the Brazilian tax identification number for companies.
* Total of 14 digits: 8 digits identify the company, a slash, 4 digit to identify the branch, followed by 2 verification numbers . E.g., 67.762.675/0001-49
* @param {string} cnpj - a Brazilian company tax identification number
* @returns {boolean} Whether the cnpj is valid or not
*/
function isValidCNPJ(cnpj) {
return (0, cnpj_1.isValid)(cnpj);
}
function fullAddressFieldsRules() {
return {
'street-number': {
description: i18n_calypso_1.default.translate('Street Number'),
rules: ['required', 'validStreetNumber'],
},
'address-1': {
description: i18n_calypso_1.default.translate('Address'),
rules: ['required'],
},
state: {
description: i18n_calypso_1.default.translate('State'),
rules: ['required'],
},
city: {
description: i18n_calypso_1.default.translate('City'),
rules: ['required'],
},
'postal-code': {
description: i18n_calypso_1.default.translate('Postal Code'),
rules: ['required'],
},
};
}
/**
* Returns country/processor specific validation rule sets for defined fields.
* @param {string} country two-letter country code to determine the required fields
*/
function countrySpecificFieldRules(country) {
const countryFields = payment_processor_countries_fields_1.PAYMENT_PROCESSOR_COUNTRIES_FIELDS[country]?.fields ?? [];
const allFields = Object.assign({
document: {
description: i18n_calypso_1.default.translate('Taxpayer Identification Number', { textOnly: true }),
rules: ['validBrazilTaxId'],
},
'phone-number': {
description: i18n_calypso_1.default.translate('Phone Number', { textOnly: true }),
rules: ['required'],
},
name: {
description: i18n_calypso_1.default.translate('Your Name', { textOnly: true }),
rules: ['required'],
},
nik: {
description: i18n_calypso_1.default.translate('NIK - Indonesia Identity Card Number', {
comment: 'NIK - Indonesia Identity Card Number required for Indonesian payment methods.',
textOnly: true,
}),
rules: ['validIndonesiaNik'],
},
pan: {
description: i18n_calypso_1.default.translate('PAN - Permanent account number', { textOnly: true }),
rules: ['validIndiaPan'],
},
gstin: {
description: i18n_calypso_1.default.translate('GSTIN - GST identification number', {
comment: 'GSTIN: India specific tax id number',
textOnly: true,
}),
rules: ['validIndiaGstin'],
},
'postal-code': {
description: i18n_calypso_1.default.translate('Postal Code', { textOnly: true }),
rules: ['required'],
},
}, fullAddressFieldsRules());
return Object.keys(allFields).reduce((filteredFields, fieldName) => {
if (countryFields.includes(fieldName)) {
filteredFields[fieldName] = allFields[fieldName];
}
return filteredFields;
}, {});
}
function translatedEbanxError(error) {
// It's unclear if this property still exists
switch (error.status_code) {
case 'BP-DR-55':
return i18n_calypso_1.default.translate('Invalid credit card CVV number');
case 'BP-DR-51':
case 'BP-DR-95':
return i18n_calypso_1.default.translate('Please enter your name.');
}
if (error.message) {
return error.message;
}
return i18n_calypso_1.default.translate('Your payment was not processed this time due to an error, please try to submit it again.');
}
//# sourceMappingURL=processor-specific.js.map