Skip to content

Commit

Permalink
MOL-524: adjust getCountryCode for list payment methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tdang1-shopmacher committed Oct 17, 2024
1 parent 3d01dfa commit 4cac30d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 43 deletions.
2 changes: 1 addition & 1 deletion application/custom-application-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const config = {
cloudIdentifier: CLOUD_IDENTIFIER,
env: {
development: {
initialProjectKey: 'shopm-adv-windev',
initialProjectKey: 'shopm-adv-dev',
},
production: {
applicationId: CUSTOM_APPLICATION_ID,
Expand Down
2 changes: 1 addition & 1 deletion application/cypress/e2e/welcome.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Test welcome.cy.ts', () => {
});
});

cy.findByText('Mollie payment methods').should('exist');
cy.findByText('Mollie yment methods').should('exist');
cy.findByText('Content will follow...').should('not.exist');
cy.get('[data-testid="no-data-notification"]').should('exist');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
const config = {
headers: {
'Content-Type': 'application/json',
'ngrok-skip-browser-warning': 'true',
},
};

Expand Down
98 changes: 58 additions & 40 deletions processor/src/service/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import { ApplePaySessionRequest, CustomPayment, SupportedPaymentMethods } from '
import { parseStringToJsonObject } from '../utils/app.utils';
import ApplePaySession from '@mollie/api-client/dist/types/src/data/applePaySession/ApplePaySession';
import { getMethodConfigObjects } from '../commercetools/customObjects.commercetools';
import { getCartFromPayment } from '../commercetools/cart.commercetools';

type CustomMethod = {
id: string;
Expand Down Expand Up @@ -145,6 +144,54 @@ const shouldEnableCardComponent = (validatedMethods: CustomMethod[]): boolean =>
);
};

const mapMollieMethodToCustomMethod = (method: Method) => ({
id: method.id,
name: { 'en-GB': method.description },
description: { 'en-GB': '' },
image: method.image.svg,
order: 0,
});

const getBillingCountry = (ctPayment: Payment): string | undefined => {
const requestField = ctPayment.custom?.fields[CustomFields.payment.request];
return requestField ? JSON.parse(requestField).billingCountry : undefined;
};

const removeCreditCardMethod = (methods: CustomMethod[]) => {
const index = methods.findIndex((method) => method.id === PaymentMethod.creditcard);
if (index !== -1) {
methods.splice(index, 1);
}
};

const filterMethodsByPricingConstraints = (
methods: CustomMethod[],
configObjects: CustomObject[],
ctPayment: Payment,
billingCountry: string,
) => {
const currencyCode = ctPayment.amountPlanned.currencyCode;
const amount = ctPayment.amountPlanned.centAmount / Math.pow(10, ctPayment.amountPlanned.fractionDigits);

configObjects.forEach((item: CustomObject) => {
const pricingConstraint = item.value.pricingConstraints?.find((constraint: PricingConstraintItem) => {
return constraint.countryCode === billingCountry && constraint.currencyCode === currencyCode;
});

if (pricingConstraint) {
if (
(pricingConstraint.minAmount && amount < pricingConstraint.minAmount) ||
(pricingConstraint.maxAmount && amount > pricingConstraint.maxAmount)
) {
const index = methods.findIndex((method) => method.id === item.value.id);
if (index !== -1) {
methods.splice(index, 1);
}
}
}
});
};

/**
* Handles listing payment methods by payment.
*
Expand All @@ -158,60 +205,31 @@ export const handleListPaymentMethodsByPayment = async (ctPayment: Payment): Pro
const methods: List<Method> = await listPaymentMethods(mollieOptions);
const configObjects: CustomObject[] = await getMethodConfigObjects();

const cart = await getCartFromPayment(ctPayment.id);
const customMethods = methods.map((method) => ({
id: method.id,
name: { 'en-GB': method.description },
description: { 'en-GB': '' },
image: method.image.svg,
order: 0,
}));
const billingCountry = getBillingCountry(ctPayment);

const customMethods = methods.map(mapMollieMethodToCustomMethod);

const validatedMethods = validateAndSortMethods(customMethods, configObjects);

const enableCardComponent = shouldEnableCardComponent(validatedMethods);

if (enableCardComponent) {
validatedMethods.splice(
validatedMethods.findIndex((method) => method.id === PaymentMethod.creditcard),
1,
);
removeCreditCardMethod(validatedMethods);
}

if (cart.country) {
const currencyCode = ctPayment.amountPlanned.currencyCode;

configObjects.forEach((item: CustomObject) => {
const pricingConstraint = item.value.pricingConstraints?.find((pricingConstraint: PricingConstraintItem) => {
return pricingConstraint.countryCode === cart.country && pricingConstraint.currencyCode === currencyCode;
}) as PricingConstraintItem;

if (pricingConstraint) {
const amount = ctPayment.amountPlanned.centAmount / Math.pow(10, ctPayment.amountPlanned.fractionDigits);

if (
(pricingConstraint.minAmount && amount < pricingConstraint.minAmount) ||
(pricingConstraint.maxAmount && amount > pricingConstraint.maxAmount)
) {
const index: number = validatedMethods.findIndex((method) => method.id === item.value.id);

validatedMethods.splice(index, 1);
}
}
});
if (billingCountry) {
filterMethodsByPricingConstraints(validatedMethods, configObjects, ctPayment, billingCountry);
}

const availableMethods = JSON.stringify({
count: validatedMethods.length,
methods: validatedMethods.length ? validatedMethods : [],
});

const ctUpdateActions: UpdateAction[] = [];

ctUpdateActions.push(
const ctUpdateActions: UpdateAction[] = [
setCustomFields(CustomFields.payment.profileId, enableCardComponent ? readConfiguration().mollie.profileId : ''),
);
ctUpdateActions.push(setCustomFields(CustomFields.payment.response, availableMethods));
setCustomFields(CustomFields.payment.response, availableMethods),
];

return {
statusCode: 200,
Expand All @@ -226,7 +244,7 @@ export const handleListPaymentMethodsByPayment = async (ctPayment: Payment): Pro
},
);
if (error instanceof CustomError) {
Promise.reject(error);
return Promise.reject(error);
}

return { statusCode: 200, actions: [] };
Expand Down
13 changes: 12 additions & 1 deletion processor/tests/service/payment.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,19 @@ describe('Test listPaymentMethodsByPayment', () => {
expect(response?.actions?.[0]?.action).toBe('setCustomField');
expect((response?.actions?.[1] as any)?.value).toBe(
JSON.stringify({
count: 2,
count: 3,
methods: [
{
id: 'paypal',
name: {
'en-GB': 'PayPal',
},
description: {
'en-GB': '',
},
image: 'https://www.mollie.com/external/icons/payment-methods/applepay.svg',
order: 0,
},
{
id: 'bancontact',
name: {
Expand Down

0 comments on commit 4cac30d

Please sign in to comment.