@malga-checkout-full/core
Version:
Core components for Malga Checkout Full
100 lines (99 loc) • 3.18 kB
JavaScript
import { v4 as uuid } from 'uuid';
import { cleanTextOnlyNumbers } from '@malga-checkout/utils';
const getDocumentType = (identification) => {
if (identification.length === 11)
return 'cpf';
return 'cnpj';
};
const getCustomerDocumentBrl = (customer) => {
const identification = cleanTextOnlyNumbers(customer.identification);
return {
type: getDocumentType(identification),
country: 'BR',
number: identification,
};
};
const getCustomerDocument = (customer) => ({
type: customer.documentType,
country: customer.documentCountry,
number: customer.identification,
});
export const formatCustomer = (customer, isInternationalCustomer) => {
const customerAddress = {
zipCode: customer.zipCode,
street: customer.street,
streetNumber: customer.streetNumber,
complement: customer.complement,
district: customer.district,
city: customer.city,
state: customer.state,
country: customer.country,
};
const hasCustomerAddress = Object.values(customerAddress).some((value) => value);
const address = hasCustomerAddress ? customerAddress : null;
const document = isInternationalCustomer
? getCustomerDocument(customer)
: getCustomerDocumentBrl(customer);
return {
name: customer.name,
email: customer.email,
phoneNumber: customer.phoneNumber,
document,
address,
};
};
export const formatProducts = (isSession, items, products) => {
if (isSession) {
return items.map((item) => ({
name: item.name,
unitPrice: item.unitPrice,
quantity: item.quantity,
sku: item.name,
risk: 'Low',
}));
}
if (products) {
return products.map((product) => ({
name: product.name,
quantity: product.quantity,
sku: product.sku,
unitPrice: product.amount,
risk: product.risk,
}));
}
return [];
};
export const formatFraudAnalysis = (customer, products, usePartialCustomer) => ({
customer,
cart: products,
usePartialCustomer,
});
export const formatFraudAnalysisWithCustomerId = (products, usePartialCustomer) => ({
cart: products,
usePartialCustomer,
});
export const formatPaymentSession = (paymentSession, transactionConfig) => {
if (!paymentSession)
return;
return Object.assign(Object.assign({}, paymentSession), { checkoutPaymentMethods: paymentSession.checkoutPaymentMethods, transactionConfig: Object.assign(Object.assign({}, transactionConfig), paymentSession.transactionConfig), customization: paymentSession.customization });
};
export const formatSuccess = (MalgaPaymentsSuccess) => (Object.assign({}, MalgaPaymentsSuccess));
const formatDripPaymentMethod = (drip, products) => {
const items = products.map((product) => ({
id: uuid(),
title: product.name,
quantity: product.quantity,
unitPrice: product.amount,
}));
return {
items: drip.items || items,
browser: drip.browser || null,
};
};
export const formatPaymentMethods = (paymentMethods, products) => {
const currentPaymentMethods = paymentMethods;
if (currentPaymentMethods.drip) {
currentPaymentMethods.drip = formatDripPaymentMethod(paymentMethods.drip, products);
}
return currentPaymentMethods;
};