Skip to content

Commit

Permalink
Merge pull request #44 from commercetools/deployment
Browse files Browse the repository at this point in the history
43 Deployment
  • Loading branch information
lojzatran committed Apr 16, 2019
2 parents a8f54ec + 8cd4dd9 commit 598d8ba
Show file tree
Hide file tree
Showing 44 changed files with 910 additions and 255 deletions.
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Created by .ignore support plugin (hsz.mobi)
### Node template
# Logs
logs
*.log
Expand All @@ -19,4 +17,9 @@ node_modules/

# IntelliJ
.idea
*.iml
*.iml

secrets.yaml

# NYC test coverage
.nyc_output/
20 changes: 18 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
language: node_js
node_js:
- '10'
- '8'
env:
- TEST_DIR=notification
- TEST_DIR=extension
before_install:
- npm i -g npm@^6.9.0
- npm i -g npm@^6.4.1
services:
- docker
script:
- cd $TEST_DIR
- npm ci
- npm run test
- if [ "$TEST_DIR" = "extension" ]; then npm run cypress; fi
- cd $TRAVIS_BUILD_DIR

before_deploy:
- docker login -u="${DOCKER_USERNAME}" -p="${DOCKER_PASSWORD}"

after_deploy:
- docker logout

deploy:
- provider: script
script: bash ./travis-build.sh "$TEST_DIR"
on:
tags: true
condition: $TRAVIS_TAG =~ ^v[0-9]+.[0-9]+.[0-9]+
7 changes: 7 additions & 0 deletions extension/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.gitignore
.git
test
cypress
cypress.
node_modules
json
13 changes: 13 additions & 0 deletions extension/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM mhart/alpine-node:8
MAINTAINER Professional Services <[email protected]>

WORKDIR /app

RUN apk --update add make python

RUN apk --update add git

COPY . /app

RUN npm ci --only=prod
ENTRYPOINT ["npm", "run", "start"]
2 changes: 1 addition & 1 deletion extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"scripts": {
"test": "mocha --exit --timeout 30000 --full-trace test/**/*.spec.js",
"start": "node ./src/index.js",
"start": "node ./src/init.js",
"lint": "eslint .",
"cypress:run": "cypress run",
"cypress:open": "cypress open",
Expand Down
8 changes: 4 additions & 4 deletions extension/resources/payment-interface-interaction-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
],
"fieldDefinitions": [
{
"name": "timestamp",
"name": "createdAt",
"label": {
"en": "timestamp"
"en": "createdAt"
},
"required": true,
"type": {
"name": "String"
"name": "DateTime"
},
"inputHint": "SingleLine"
},
Expand Down Expand Up @@ -76,4 +76,4 @@
}
]
}
]
]
8 changes: 6 additions & 2 deletions extension/src/config/init/ensure-api-extensions.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const _ = require('lodash')
const utils = require('../../utils')

const apiExtensionTemplate = require('../../../resources/api-extension.json')

const logger = utils.getLogger()
async function ensureApiExtensions (ctpClient, ctpAdyenIntegrationBaseUrl) {
try {
const extensionDraft = _.template(JSON.stringify(apiExtensionTemplate))({ ctpAdyenIntegrationBaseUrl })
const { body } = await ctpClient.fetch(ctpClient.builder.extensions.where(`key="${apiExtensionTemplate.key}"`))
if (body.results.length === 0)
if (body.results.length === 0) {
await ctpClient.create(ctpClient.builder.extensions, extensionDraft)
logger.info('Successfully created api extension')
}
} catch (e) {
console.error('Error when creating API extension, skipping...', JSON.stringify(e))
logger.error(e, 'Error when creating api extension, skipping...')
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
const Promise = require('bluebird')
const utils = require('../../utils')

const interfaceInteractionTypes = require('../../../resources/payment-interface-interaction-types.json')

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)
if (body.results.length === 0) {
await ctpClient.create(ctpClient.builder.types, type)
logger.info('Successfully created an interfaceInteraction type')
}
} catch (e) {
console.error('Error when creating interface interaction custom type, skipping...', JSON.stringify(e))
logger.error(e, 'Error when creating interface interaction custom type, skipping...')
}
}, { concurrency: 3 })
}
Expand Down
10 changes: 8 additions & 2 deletions extension/src/config/init/ensure-payment-custom-type.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const paymentCustomType = require('../../../resources/payment-custom-types.json')

const utils = require('../../utils')

const logger = utils.getLogger()

async function ensurePaymentCustomType (ctpClient) {
try {
const { body } = await ctpClient.fetch(ctpClient.builder.types.where(`key="${paymentCustomType.key}"`))
if (body.results.length === 0)
if (body.results.length === 0) {
await ctpClient.create(ctpClient.builder.types, paymentCustomType)
logger.info('Successfully created payment custom type')
}
} catch (e) {
console.error('Error when creating payment custom type, skipping...', JSON.stringify(e))
logger.error(e, 'Error when creating payment custom type, skipping...')
}
}

Expand Down
5 changes: 3 additions & 2 deletions extension/src/index.js → extension/src/init.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const init = require('./server.js').setupServer()
require('./config/config')
const utils = require('./utils')

const { ensureResources } = require('./config/init/ensure-resources')

const port = parseInt(process.env.EXTENSION_PORT || 8080, 10)
const logger = utils.getLogger()

init.listen(port, async () => {
await ensureResources()
console.log(`Server running at http://127.0.0.1:${port}/`)
logger.info(`Server running at http://127.0.0.1:${port}/`)
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const config = configLoader.load()
async function handlePayment (paymentObject) {
const validator = _validatePayment(paymentObject)
if (validator.hasErrors())
return validator.buildCtpErrorResponse()
return {
actions: []
}
const { response, request } = await _completePayment(paymentObject)
const status = response.status === 200 ? c.SUCCESS : c.FAILURE
const responseBody = await response.json()
Expand All @@ -35,7 +37,6 @@ async function handlePayment (paymentObject) {
actions = _.compact(actions)

return {
version: paymentObject.version,
actions
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ async function handlePayment (paymentObject) {
actions = _.compact(actions)

return {
version: paymentObject.version,
actions
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const pU = require('../payment-utils')

const creditCardMakePayment = require('./credit-card-make-payment.handler')
const creditCardCompletePayment = require('./credit-card-complete-payment.handler')
const errorMessages = require('../../validator/error-messages')

async function handlePayment (paymentObject) {
const hasPendingTransaction = _.isObject(pU.getChargeTransactionPending(paymentObject))
Expand All @@ -14,10 +13,7 @@ async function handlePayment (paymentObject) {
if (hasInitTransaction)
return creditCardMakePayment.handlePayment(paymentObject)
return {
errors: [{
code: 'InvalidField',
message: errorMessages.MISSING_TXN_CHARGE_INIT_PENDING
}]
actions: []
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ async function handlePayment (paymentObject) {
const { request, response } = await _fetchPaymentMethods(paymentObject)
const responseBody = await response.json()
return {
version: paymentObject.version,
actions: [
pU.createAddInterfaceInteractionAction({
request, response: responseBody, type: 'getAvailablePaymentMethods'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ async function handlePayment (paymentObject) {
actions = _.compact(actions)

return {
version: paymentObject.version,
actions
}
}
Expand Down
6 changes: 1 addition & 5 deletions extension/src/paymentHandler/kcp/kcp-payment.handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ const _ = require('lodash')

const pU = require('../payment-utils')
const kcpMakePayment = require('./kcp-make-payment.handler')
const errorMessages = require('../../validator/error-messages')

async function handlePayment (paymentObject) {
const hasInitTransaction = _.isObject(pU.getChargeTransactionInit(paymentObject))
if (hasInitTransaction)
return kcpMakePayment.handlePayment(paymentObject)
return {
errors: [{
code: 'InvalidField',
message: errorMessages.MISSING_TXN_CHARGE_INIT_PENDING
}]
actions: []
}
}

Expand Down
2 changes: 1 addition & 1 deletion extension/src/paymentHandler/payment-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function createAddInterfaceInteractionAction (
action: 'addInterfaceInteraction',
type: { key: c.CTP_INTERFACE_INTERACTION },
fields: {
timestamp: new Date(),
createdAt: new Date(),
response: JSON.stringify(response),
request: JSON.stringify(request),
type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ const config = configLoader.load()
async function handlePayment (paymentObject) {
const validator = _validatePayment(paymentObject)
if (validator.hasErrors())
return validator.buildCtpErrorResponse()

return {
actions: []
}
const { response, request } = await _callAdyen(paymentObject)
const status = response.status === 200 ? c.SUCCESS : c.FAILURE
const responseBody = await response.json()
Expand All @@ -36,7 +37,6 @@ async function handlePayment (paymentObject) {
actions = _.compact(actions)

return {
version: paymentObject.version,
actions
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ async function handlePayment (paymentObject) {
actions = _.compact(actions)

return {
version: paymentObject.version,
actions
}
}
Expand Down
6 changes: 1 addition & 5 deletions extension/src/paymentHandler/paypal/paypal.handler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const _ = require('lodash')

const pU = require('../payment-utils')
const errorMessages = require('../../validator/error-messages')
const paypalMakePayment = require('./paypal-make-payment.handler')
const paypalCompletePayment = require('./paypal-complete-payment.handler')

Expand All @@ -14,10 +13,7 @@ async function handlePayment (paymentObject) {
return paypalMakePayment.handlePayment(paymentObject)

return {
errors: [{
code: 'InvalidField',
message: errorMessages.MISSING_TXN_CHARGE_INIT_PENDING
}]
actions: []
}
}

Expand Down
1 change: 0 additions & 1 deletion extension/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const http = require('http')
const url = require('url')
const utils = require('./utils')
const { routes: defaultRoutes } = require('./routes')
require('./config/config')

const logger = utils.getLogger()

Expand Down
6 changes: 4 additions & 2 deletions extension/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const bunyan = require('bunyan')
const configLoader = require('./config/config')
const config = configLoader.load()

let logger

Expand All @@ -23,12 +25,12 @@ function sendResponse ({
response.end(JSON.stringify(data))
}

function getLogger (logLevel) {
function getLogger () {
if (!logger)
logger = bunyan.createLogger({
name: 'ctp-adyen-integration-extension',
stream: process.stderr,
level: logLevel || bunyan.ERROR
level: config.logLevel || bunyan.ERROR
})
return logger
}
Expand Down
6 changes: 3 additions & 3 deletions extension/test/fixtures/ctp-payment.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
},
"fields": {
"request": "request",
"timestamp": "2019-02-28T22:16:04.246Z",
"createdAt": "2019-02-28T22:16:04.246Z",
"response": "response=",
"status": "SUCCESS",
"type": "makePayment"
Expand All @@ -79,11 +79,11 @@
},
"fields": {
"request": "request",
"timestamp": "2019-02-28T22:16:19.086Z",
"createdAt": "2019-02-28T22:16:19.086Z",
"response": "response",
"status": "SUCCESS",
"type": "completePayment"
}
}
]
}
}
Loading

0 comments on commit 598d8ba

Please sign in to comment.