From eab2573586ad363652cb97d650d37df90acef7d4 Mon Sep 17 00:00:00 2001 From: Tomasz Miller Date: Wed, 3 Jun 2020 11:04:25 +0200 Subject: [PATCH 1/3] Update payment-interface-interaction-types.json (#285) * Update payment-interface-interaction-types.json We can not create array of custom types as a custom type in the Commerce Tools. That is reason I suggest remove curly braces for that JSON object. * Fix for ensure-resources processing - JSON resources files were renamed regarding to their content - update for ensure-payment-custom-type and ensure-payment-custom-type - updates for doc files --- .../docs/DevelopmentAndDeploymentGuide.md | 2 +- extension/docs/IntegrationGuide.md | 4 +- ...om-types.json => payment-custom-type.json} | 0 .../payment-interface-interaction-type.json | 77 ++++++++++++++++++ .../payment-interface-interaction-types.json | 79 ------------------- ...nsure-interface-interaction-custom-type.js | 23 +++--- .../config/init/ensure-payment-custom-type.js | 2 +- extension/src/config/init/ensure-resources.js | 2 +- .../init/ensure-payment-custom-type.js | 2 +- ...om-types.json => payment-custom-type.json} | 0 10 files changed, 93 insertions(+), 98 deletions(-) rename extension/resources/{payment-custom-types.json => payment-custom-type.json} (100%) create mode 100644 extension/resources/payment-interface-interaction-type.json delete mode 100644 extension/resources/payment-interface-interaction-types.json rename notification/test/resources/{payment-custom-types.json => payment-custom-type.json} (100%) diff --git a/extension/docs/DevelopmentAndDeploymentGuide.md b/extension/docs/DevelopmentAndDeploymentGuide.md index daad3012d..0bcfdde41 100644 --- a/extension/docs/DevelopmentAndDeploymentGuide.md +++ b/extension/docs/DevelopmentAndDeploymentGuide.md @@ -59,7 +59,7 @@ with the UI (e.g. credit card 3ds). In such cases, we use Cypress.io. Run `npm r For deployment to lambda zip the extensions folder and specify `src/lambda.handler` as the entry point for the function -When deploying the lambda, it will NOT create the custom types for you. These are required for the extension to operate correctly. Please add [payment custom types](../resources/payment-custom-types.json) and [payment interface interaction types](../resources/payment-interface-interaction-types.json) manually. +When deploying the lambda, it will NOT create the custom types for you. These are required for the extension to operate correctly. Please add [payment custom types](../resources/payment-custom-type.json) and [payment interface interaction types](../resources/payment-interface-interaction-type.json) manually. You can create these by running the command `npm run create-custom-types` and providing the `CTP_PROJECT_KEY`, `CTP_CLIENT_ID` and `CTP_CLIENT_SECRET` environment variables. Example command (bash): `CTP_PROJECT_KEY="project_key" CTP_CLIENT_ID="client_id" CTP_CLIENT_SECRET="client_secret" npm run create-custom-types` diff --git a/extension/docs/IntegrationGuide.md b/extension/docs/IntegrationGuide.md index 6197fa2fc..878ca457a 100644 --- a/extension/docs/IntegrationGuide.md +++ b/extension/docs/IntegrationGuide.md @@ -34,8 +34,8 @@ Other used terms in the documentation: ## Requirements for CTP project All the requirements below are automatically created by the Extension module. 1. [API Extension subscription to Extension module endpoints](../resources/api-extension.json) -1. [Custom types for payments](../resources/payment-custom-types.json) -1. [Custom types for interface interactions](../resources/payment-interface-interaction-types.json) +1. [Custom types for payments](../resources/payment-custom-type.json) +1. [Custom types for interface interactions](../resources/payment-interface-interaction-type.json) **Note**: Extension module will not create if there are already resources with same key in the CTP project. In this case, you have to synchronize by yourself. diff --git a/extension/resources/payment-custom-types.json b/extension/resources/payment-custom-type.json similarity index 100% rename from extension/resources/payment-custom-types.json rename to extension/resources/payment-custom-type.json diff --git a/extension/resources/payment-interface-interaction-type.json b/extension/resources/payment-interface-interaction-type.json new file mode 100644 index 000000000..9709f5b2b --- /dev/null +++ b/extension/resources/payment-interface-interaction-type.json @@ -0,0 +1,77 @@ +{ + "key": "ctp-adyen-integration-interaction", + "name": { + "en": "ctp-adyen-integration-interaction" + }, + "resourceTypeIds": [ + "payment-interface-interaction" + ], + "fieldDefinitions": [ + { + "name": "createdAt", + "label": { + "en": "createdAt" + }, + "required": true, + "type": { + "name": "DateTime" + }, + "inputHint": "SingleLine" + }, + { + "name": "transactionId", + "label": { + "en": "transactionId" + }, + "required": false, + "type": { + "name": "String" + }, + "inputHint": "SingleLine" + }, + { + "name": "request", + "label": { + "en": "request" + }, + "required": true, + "type": { + "name": "String" + }, + "inputHint": "MultiLine" + }, + { + "name": "response", + "label": { + "en": "response" + }, + "required": true, + "type": { + "name": "String" + }, + "inputHint": "MultiLine" + }, + { + "name": "type", + "label": { + "en": "type" + }, + "required": true, + "type": { + "name": "String" + }, + "inputHint": "SingleLine" + }, + { + "name": "status", + "label": { + "en": "status" + }, + "required": false, + "type": { + "name": "String" + }, + "inputHint": "SingleLine" + } + ] +} diff --git a/extension/resources/payment-interface-interaction-types.json b/extension/resources/payment-interface-interaction-types.json deleted file mode 100644 index 58fff7390..000000000 --- a/extension/resources/payment-interface-interaction-types.json +++ /dev/null @@ -1,79 +0,0 @@ -[ - { - "key": "ctp-adyen-integration-interaction", - "name": { - "en": "ctp-adyen-integration-interaction" - }, - "resourceTypeIds": [ - "payment-interface-interaction" - ], - "fieldDefinitions": [ - { - "name": "createdAt", - "label": { - "en": "createdAt" - }, - "required": true, - "type": { - "name": "DateTime" - }, - "inputHint": "SingleLine" - }, - { - "name": "transactionId", - "label": { - "en": "transactionId" - }, - "required": false, - "type": { - "name": "String" - }, - "inputHint": "SingleLine" - }, - { - "name": "request", - "label": { - "en": "request" - }, - "required": true, - "type": { - "name": "String" - }, - "inputHint": "MultiLine" - }, - { - "name": "response", - "label": { - "en": "response" - }, - "required": true, - "type": { - "name": "String" - }, - "inputHint": "MultiLine" - }, - { - "name": "type", - "label": { - "en": "type" - }, - "required": true, - "type": { - "name": "String" - }, - "inputHint": "SingleLine" - }, - { - "name": "status", - "label": { - "en": "status" - }, - "required": false, - "type": { - "name": "String" - }, - "inputHint": "SingleLine" - } - ] - } -] diff --git a/extension/src/config/init/ensure-interface-interaction-custom-type.js b/extension/src/config/init/ensure-interface-interaction-custom-type.js index 7f81c9e8e..9e2437ac2 100644 --- a/extension/src/config/init/ensure-interface-interaction-custom-type.js +++ b/extension/src/config/init/ensure-interface-interaction-custom-type.js @@ -1,22 +1,19 @@ -const Promise = require('bluebird') -const utils = require('../../utils') +const interfaceInteractionType = require('../../../resources/payment-interface-interaction-type.json') -const interfaceInteractionTypes = require('../../../resources/payment-interface-interaction-types.json') +const utils = require('../../utils') const logger = utils.getLogger() async function ensureInterfaceInteractionCustomType (ctpClient) { - await Promise.map(interfaceInteractionTypes, async (type) => { - try { - const { body } = await ctpClient.fetch(ctpClient.builder.types.where(`key="${type.key}"`)) - if (body.results.length === 0) { - await ctpClient.create(ctpClient.builder.types, type) - logger.info('Successfully created an interfaceInteraction type') - } - } catch (e) { - logger.error(e, 'Error when creating interface interaction custom type, skipping...') + try { + const { body } = await ctpClient.fetch(ctpClient.builder.types.where(`key="${interfaceInteractionType.key}"`)) + if (body.results.length === 0) { + await ctpClient.create(ctpClient.builder.types, interfaceInteractionType) + logger.info('Successfully created an interfaceInteraction type') } - }, { concurrency: 3 }) + } catch (e) { + logger.error(e, 'Error when creating interface interaction custom type, skipping...') + } } module.exports = { diff --git a/extension/src/config/init/ensure-payment-custom-type.js b/extension/src/config/init/ensure-payment-custom-type.js index 093ecfb6c..e818497e1 100644 --- a/extension/src/config/init/ensure-payment-custom-type.js +++ b/extension/src/config/init/ensure-payment-custom-type.js @@ -1,4 +1,4 @@ -const paymentCustomType = require('../../../resources/payment-custom-types.json') +const paymentCustomType = require('../../../resources/payment-custom-type.json') const utils = require('../../utils') diff --git a/extension/src/config/init/ensure-resources.js b/extension/src/config/init/ensure-resources.js index 8f46c1adb..d46f44c0c 100644 --- a/extension/src/config/init/ensure-resources.js +++ b/extension/src/config/init/ensure-resources.js @@ -14,7 +14,7 @@ function ensureCustomTypes () { } function ensureResources () { - let config = configLoader.load() + const config = configLoader.load() if (!config.ensureResources) return Promise.resolve() return Promise.all([ ensureCustomTypes(), diff --git a/notification/test/integration/init/ensure-payment-custom-type.js b/notification/test/integration/init/ensure-payment-custom-type.js index 6a782c31f..e8dfce360 100644 --- a/notification/test/integration/init/ensure-payment-custom-type.js +++ b/notification/test/integration/init/ensure-payment-custom-type.js @@ -1,4 +1,4 @@ -const paymentCustomType = require('../../resources/payment-custom-types.json') +const paymentCustomType = require('../../resources/payment-custom-type.json') const utils = require('../../../src/utils/logger') diff --git a/notification/test/resources/payment-custom-types.json b/notification/test/resources/payment-custom-type.json similarity index 100% rename from notification/test/resources/payment-custom-types.json rename to notification/test/resources/payment-custom-type.json From 30947abc25dc4a3d955cf282f03eec6e65704fd2 Mon Sep 17 00:00:00 2001 From: Jamie Gardiner Date: Wed, 3 Jun 2020 11:21:09 +0100 Subject: [PATCH 2/3] fix exception being thrown in lambda.js when data is null in handlePayment response (#297) Null check around paymentResult.data, which can be null if paymentHandler short circuits when not an adyen payment --- extension/src/lambda.js | 4 ++-- extension/test/unit/lambda.spec.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/extension/src/lambda.js b/extension/src/lambda.js index 33b7ed579..fb25aaca4 100644 --- a/extension/src/lambda.js +++ b/extension/src/lambda.js @@ -9,8 +9,8 @@ exports.handler = async function (event) { return { responseType: paymentResult.success ? 'UpdateRequest' : 'FailedValidation', - errors: paymentResult.data.errors, - actions: paymentResult.data.actions + errors: paymentResult.data ? paymentResult.data.errors : undefined, + actions: paymentResult.data ? paymentResult.data.actions : undefined } } catch (e) { logger.error(e, `Unexpected error when processing event ${JSON.stringify(event)}`) diff --git a/extension/test/unit/lambda.spec.js b/extension/test/unit/lambda.spec.js index cfed243de..6daacd01a 100644 --- a/extension/test/unit/lambda.spec.js +++ b/extension/test/unit/lambda.spec.js @@ -40,6 +40,16 @@ describe('Lambda handler', () => { expect(result.actions).to.equal(undefined) }) + it('does not throw unhandled exception when handlePayment data is null', async () => { + sinon.stub(paymentHandler, 'handlePayment').returns({ success: true, data: null }) + + const result = await handler(event) + + expect(result.responseType).equals('UpdateRequest') + expect(result.errors).equals(undefined) + expect(result.actions).to.equal(undefined) + }) + it('logs and throws unhandled exceptions', async () => { const logSpy = sinon.spy() utils.getLogger().error = logSpy From 76098d2db2ae4948d673e430977b4365aa426595 Mon Sep 17 00:00:00 2001 From: aoz Date: Wed, 3 Jun 2020 12:27:31 +0200 Subject: [PATCH 3/3] add the reason of the paymentResult.data check in lambda function with a descriptive comment. --- extension/src/lambda.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extension/src/lambda.js b/extension/src/lambda.js index fb25aaca4..6bc3034c9 100644 --- a/extension/src/lambda.js +++ b/extension/src/lambda.js @@ -9,6 +9,8 @@ exports.handler = async function (event) { return { responseType: paymentResult.success ? 'UpdateRequest' : 'FailedValidation', + // Null check around paymentResult.data, + // which can be null if paymentHandler short circuits when not an adyen payment errors: paymentResult.data ? paymentResult.data.errors : undefined, actions: paymentResult.data ? paymentResult.data.actions : undefined }