Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/firebase/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ export type CheckoutGatesConfig = {
requireStaffSignature?: boolean;
/* Reject checkout when items subtotal is lower than this value */
minSubtotal?: number;
/* Reject checkout from companies: buyer with `registry_type: 'j'` or a CNPJ (14 digits) */
personsOnly?: boolean;
};

export type PassportConfig = {
Expand Down
20 changes: 19 additions & 1 deletion packages/modules/src/firebase/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { checkoutSchema } from '../index';
import { ajv, sendRequestError } from './ajv';
import fixItems from './functions-checkout/fix-items';
import readOrSaveCustomer from './functions-checkout/read-or-save-customer';
import { getSubtotalGateError } from './functions-checkout/customer-gates';
import {
getCompanyGateError,
getSubtotalGateError,
} from './functions-checkout/customer-gates';
import requestModule from './functions-checkout/request-to-module';
import {
sendError,
Expand Down Expand Up @@ -77,6 +80,21 @@ export default async (req: Request, res: Response) => {
) {
return sendError(res, 403, 'CKT803', 'Chato');
}
// Before saving the customer, transaction buyers always carry registry type and document
const companyGateError = getCompanyGateError(config.get().checkout, [
customer,
...(Array.isArray(body.transaction) ? body.transaction : [body.transaction])
.map((transaction) => transaction?.buyer),
]);
if (companyGateError) {
return sendError(
res,
companyGateError.status,
companyGateError.code,
companyGateError.message,
companyGateError.userMessage,
);
}
const savedCustomer = await readOrSaveCustomer({
...customer,
addresses: !shippingAddr.line_address?.includes('***')
Expand Down
31 changes: 31 additions & 0 deletions packages/modules/src/firebase/functions-checkout/customer-gates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,37 @@ export const getCustomerGateError = (
return null;
};

type BuyerRegistry = {
registry_type?: string,
doc_number?: string,
} | null | undefined;

const isCompanyBuyer = (buyer: BuyerRegistry) => {
if (!buyer) return false;
if (buyer.registry_type === 'j') return true;
// Brazilian CNPJ, catches companies sent with `registry_type: 'p'`
return typeof buyer.doc_number === 'string'
&& buyer.doc_number.replace(/\D/g, '').length === 14;
};

/* Gate for stores that sell to individuals only (no CNPJ).
Receives the checkout customer and every transaction buyer, any company rejects. */
export const getCompanyGateError = (
gates: CheckoutGatesConfig | undefined,
buyers: BuyerRegistry[],
): CheckoutGateError | null => {
if (!gates?.personsOnly || !buyers.some(isCompanyBuyer)) return null;
return {
status: 403,
code: 'CKT807',
message: 'Checkout is restricted to individuals, company buyers are not allowed',
userMessage: {
en_us: 'This store sells to individuals only, purchases with company registration are not allowed',
pt_br: 'Esta loja vende somente para pessoa física (CPF), compras com CNPJ não são permitidas',
},
};
};

export const getSubtotalGateError = (
gates: CheckoutGatesConfig | undefined,
subtotal: number,
Expand Down
19 changes: 19 additions & 0 deletions packages/modules/tests/3-customer-gates.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, test, expect } from 'vitest';
import {
getCustomerGateError,
getCompanyGateError,
getSubtotalGateError,
} from '../src/firebase/functions-checkout/customer-gates';

Expand Down Expand Up @@ -37,4 +38,22 @@ describe('Checkout customer gates (customers-only stores)', () => {
expect(getSubtotalGateError(gates, 300)).toBeNull();
expect(getSubtotalGateError({ minSubtotal: 0 }, 1)).toBeNull();
});

test('personsOnly rejects company buyers only', () => {
const gates = { personsOnly: true };
const person = { registry_type: 'p', doc_number: '12345678909' };
expect(getCompanyGateError(undefined, [{ registry_type: 'j' }])).toBeNull();
expect(getCompanyGateError({}, [{ registry_type: 'j' }])).toBeNull();
expect(getCompanyGateError(gates, [person, person])).toBeNull();
expect(getCompanyGateError(gates, [person, undefined, null])).toBeNull();
const err = getCompanyGateError(gates, [person, { registry_type: 'j' }]);
expect(err?.status).toBe(403);
expect(err?.code).toBe('CKT807');
expect(err?.userMessage.pt_br).toContain('CNPJ');
// CNPJ sent as individual registry
expect(getCompanyGateError(gates, [{ registry_type: 'p', doc_number: '11222333000181' }])
?.code).toBe('CKT807');
expect(getCompanyGateError(gates, [{ doc_number: '11.222.333/0001-81' }])?.code)
.toBe('CKT807');
});
});
Loading