diff --git a/application/custom-application-config.ts b/application/custom-application-config.ts index 95fe782..37fc7b4 100644 --- a/application/custom-application-config.ts +++ b/application/custom-application-config.ts @@ -15,7 +15,7 @@ const config = { cloudIdentifier: CLOUD_IDENTIFIER, env: { development: { - initialProjectKey: 'shopm-adv-windev', + initialProjectKey: 'shopm-adv-dev', }, production: { applicationId: CUSTOM_APPLICATION_ID, diff --git a/application/cypress/e2e/welcome.cy.ts b/application/cypress/e2e/welcome.cy.ts index 6133421..fb57fdd 100644 --- a/application/cypress/e2e/welcome.cy.ts +++ b/application/cypress/e2e/welcome.cy.ts @@ -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'); }); diff --git a/application/src/hooks/use-mollie-connector/use-mollie-connector.ts b/application/src/hooks/use-mollie-connector/use-mollie-connector.ts index ef2e3fd..261f138 100644 --- a/application/src/hooks/use-mollie-connector/use-mollie-connector.ts +++ b/application/src/hooks/use-mollie-connector/use-mollie-connector.ts @@ -25,6 +25,7 @@ import { const config = { headers: { 'Content-Type': 'application/json', + 'ngrok-skip-browser-warning': 'true', }, }; diff --git a/processor/src/service/payment.service.ts b/processor/src/service/payment.service.ts index c005402..ccfb7d6 100644 --- a/processor/src/service/payment.service.ts +++ b/processor/src/service/payment.service.ts @@ -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; @@ -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. * @@ -158,47 +205,20 @@ export const handleListPaymentMethodsByPayment = async (ctPayment: Payment): Pro const methods: List = 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({ @@ -206,12 +226,10 @@ export const handleListPaymentMethodsByPayment = async (ctPayment: Payment): Pro 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, @@ -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: [] }; diff --git a/processor/tests/service/payment.service.spec.ts b/processor/tests/service/payment.service.spec.ts index 15624b9..8de0ec2 100644 --- a/processor/tests/service/payment.service.spec.ts +++ b/processor/tests/service/payment.service.spec.ts @@ -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: {