diff --git a/connect.yaml b/connect.yaml index 94a1286..a8e7d32 100644 --- a/connect.yaml +++ b/connect.yaml @@ -11,6 +11,10 @@ deployAs: description: Commercetools Composable Commerce API region required: true default: "europe-west1.gcp" + - key: ENABLE_MOLLIE_CARD_COMPONENT + description: Enable Mollie cart component (0 or 1) + required: true + default: "0" - key: DEBUG description: Debug mode (0 or 1) required: false diff --git a/processor/.env.example b/processor/.env.example index 631be2e..4ba0f38 100644 --- a/processor/.env.example +++ b/processor/.env.example @@ -13,6 +13,7 @@ CTP_API_URL=https://api..commercetools.com DEBUG= ## Either 1 for enable or 0 for disable MOLLIE_API_KEY= MOLLIE_PROFILE_ID= +ENABLE_MOLLIE_CARD_COMPONENT=0 ## Either 1 for enable or 0 for disable ## NGROK CONNECTOR_EXTENSION_TOKEN= diff --git a/processor/.env.jest b/processor/.env.jest index b8d116f..89d754f 100644 --- a/processor/.env.jest +++ b/processor/.env.jest @@ -9,5 +9,6 @@ CTP_REGION=europe-west1.gcp MOLLIE_API_KEY=12345678901234567890123456789012 MOLLIE_PROFILE_ID=pfl_12345 DEBUG=0 +ENABLE_MOLLIE_CARD_COMPONENT=0 CONNECT_SERVICE_URL=http://localhost:3000/processor diff --git a/processor/bin/ngrok.sh b/processor/bin/ngrok.sh index fb5857b..48b9057 100755 --- a/processor/bin/ngrok.sh +++ b/processor/bin/ngrok.sh @@ -12,7 +12,7 @@ fi # Start NGROK in background echo "⚡️ Starting ngrok" -ngrok http 8889 --authtoken ${CONNECTOR_EXTENSION_TOKEN} > /dev/null & +ngrok http 8080 --authtoken ${CONNECTOR_EXTENSION_TOKEN} > /dev/null & # Wait for ngrok to be available while ! nc -z localhost 4040; do diff --git a/processor/package-lock.json b/processor/package-lock.json index de27807..84c10dc 100644 --- a/processor/package-lock.json +++ b/processor/package-lock.json @@ -1,12 +1,12 @@ { "name": "shopmacher-mollie-processor", - "version": "0.0.13", + "version": "0.0.19", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "shopmacher-mollie-processor", - "version": "0.0.13", + "version": "0.0.19", "hasInstallScript": true, "license": "MIT", "dependencies": { diff --git a/processor/package.json b/processor/package.json index 7c842bb..111cff2 100644 --- a/processor/package.json +++ b/processor/package.json @@ -1,7 +1,7 @@ { "name": "shopmacher-mollie-processor", "description": "Integration between commercetools and mollie payment service provider", - "version": "0.0.18", + "version": "0.0.19", "main": "index.js", "private": true, "scripts": { diff --git a/processor/src/service/payment.service.ts b/processor/src/service/payment.service.ts index 4d5e7f0..833164b 100644 --- a/processor/src/service/payment.service.ts +++ b/processor/src/service/payment.service.ts @@ -34,6 +34,7 @@ import { setTransactionCustomField, } from '../commercetools/action.commercetools'; import { readConfiguration } from '../utils/config.utils'; +import { toBoolean } from 'validator'; import { CancelParameters, CreateParameters, @@ -65,7 +66,12 @@ export const handleListPaymentMethodsByPayment = async (ctPayment: Payment): Pro const hasCardPayment = methods.findIndex((method: Method) => method.id === PaymentMethod.creditcard); if (hasCardPayment >= 0) { - ctUpdateActions.push(setCustomFields(CustomFields.payment.profileId, readConfiguration().mollie.profileId)); + ctUpdateActions.push( + setCustomFields( + CustomFields.payment.profileId, + toBoolean(readConfiguration().mollie.enableCardComponent, true) ? readConfiguration().mollie.profileId : '', + ), + ); } return { diff --git a/processor/src/types/index.types.ts b/processor/src/types/index.types.ts index 7571879..06793ae 100644 --- a/processor/src/types/index.types.ts +++ b/processor/src/types/index.types.ts @@ -25,6 +25,7 @@ export type ConnectorEnvVars = { mollie: { apiKey: string; profileId: string; - debug?: string; + debug: string; + enableCardComponent: string; }; }; diff --git a/processor/src/utils/config.utils.ts b/processor/src/utils/config.utils.ts index 6414c64..1e6c663 100644 --- a/processor/src/utils/config.utils.ts +++ b/processor/src/utils/config.utils.ts @@ -21,6 +21,7 @@ export const readConfiguration = () => { apiKey: process.env.MOLLIE_API_KEY as string, debug: process.env.DEBUG as string, profileId: process.env.MOLLIE_PROFILE_ID as string, + enableCardComponent: process.env.ENABLE_MOLLIE_CARD_COMPONENT as string, }, }; @@ -29,7 +30,7 @@ export const readConfiguration = () => { if (validationErrors.length) { throw new CustomError( 'InvalidEnvironmentVariablesError', - 'Invalid Environment Variables please check your .env file', + 'Invalid Environment Variables please check your .env file. Details: ' + JSON.stringify(validationErrors), ); } diff --git a/processor/src/validators/env.validators.ts b/processor/src/validators/env.validators.ts index 3f6451e..3b0d8a1 100644 --- a/processor/src/validators/env.validators.ts +++ b/processor/src/validators/env.validators.ts @@ -58,16 +58,29 @@ const envValidators = [ referencedBy: 'environmentVariables', }), - optional(standardString)( + standardString( ['mollie', 'debug'], { code: 'InvalidDebug', - message: 'Mollie debug should be a valid string.', + message: 'Mollie debug should be a valid string of either "0" or "1".', + referencedBy: 'environmentVariables', + }, + { + min: 1, + max: 1, + }, + ), + + standardString( + ['mollie', 'enableCardComponent'], + { + code: 'InvalidEnableCardComponent', + message: 'Enable Mollie card component should be a valid string of either "0" or "1".', referencedBy: 'environmentVariables', }, { min: 1, - max: undefined, + max: 1, }, ), ]; diff --git a/processor/tests/utils/config.utils.spec.ts b/processor/tests/utils/config.utils.spec.ts index 1e9fc90..b4e54f0 100644 --- a/processor/tests/utils/config.utils.spec.ts +++ b/processor/tests/utils/config.utils.spec.ts @@ -3,7 +3,7 @@ import CustomError from '../../src/errors/custom.error'; import { describe, expect, test } from '@jest/globals'; describe('Test src/utils/config.utils.ts', () => { - it('should return the correct configuration when all env vars are valid', () => { + test('should return the correct configuration when all env vars are valid', () => { const config = readConfiguration(); expect(config).toEqual({ commerceTools: { @@ -17,6 +17,7 @@ describe('Test src/utils/config.utils.ts', () => { apiKey: process.env.MOLLIE_API_KEY, debug: process.env.DEBUG, profileId: process.env.MOLLIE_PROFILE_ID, + enableCardComponent: process.env.ENABLE_MOLLIE_CARD_COMPONENT, }, }); }); @@ -55,4 +56,9 @@ describe('Test src/utils/config.utils.ts', () => { delete process.env.DEBUG; expect(() => readConfiguration()).toThrow(CustomError); }); + + test('should throw an error when ENABLE_MOLLIE_CARD_COMPONENT is not defined', () => { + delete process.env.ENABLE_MOLLIE_CARD_COMPONENT; + expect(() => readConfiguration()).toThrow(CustomError); + }); }); diff --git a/processor/tests/validators/env.validators.spec.ts b/processor/tests/validators/env.validators.spec.ts index ed4ed34..2b681fa 100644 --- a/processor/tests/validators/env.validators.spec.ts +++ b/processor/tests/validators/env.validators.spec.ts @@ -94,10 +94,22 @@ describe('Test env.validators.ts', () => { field2: 'debug', error: { code: 'InvalidDebug', - message: 'Mollie debug should be a valid string.', + message: 'Mollie debug should be a valid string of either "0" or "1".', referencedBy: 'environmentVariables', }, - condition: { min: 1, max: undefined }, + condition: { min: 1, max: 1 }, + }, + { + index1: 8, + index2: 0, + field1: 'mollie', + field2: 'enableCardComponent', + error: { + code: 'InvalidEnableCardComponent', + message: 'Enable Mollie card component should be a valid string of either "0" or "1".', + referencedBy: 'environmentVariables', + }, + condition: { min: 1, max: 1 }, }, ])( 'should return the correct validation array contains [%s, %s]', diff --git a/processor/tests/validators/helpers.validators.spec.ts b/processor/tests/validators/helpers.validators.spec.ts index 2fe7299..3a295df 100644 --- a/processor/tests/validators/helpers.validators.spec.ts +++ b/processor/tests/validators/helpers.validators.spec.ts @@ -236,6 +236,7 @@ describe('Test helpers.validators.ts', () => { apiKey: process.env.MOLLIE_API_KEY as string, debug: (process.env.DEBUG ?? '0') as string, profileId: process.env.MOLLIE_PROFILE_ID as string, + enableCardComponent: (process.env.ENABLE_CARD_COMPONENT ?? '0') as string, }, }; const error = getValidateMessages(envValidators, vars); @@ -256,6 +257,7 @@ describe('Test helpers.validators.ts', () => { apiKey: process.env.MOLLIE_API_KEY as string, debug: (process.env.DEBUG ?? '0') as string, profileId: process.env.MOLLIE_PROFILE_ID as string, + enableCardComponent: (process.env.ENABLE_CARD_COMPONENT ?? '0') as string, }, }; const error = getValidateMessages(envValidators, vars);