Skip to content

Commit

Permalink
PP-13313: Validations for Worldpay credentials for a MOTO gateway acc…
Browse files Browse the repository at this point in the history
…ount
  • Loading branch information
oswaldquek committed Dec 4, 2024
1 parent f6b71fd commit cc92ca9
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const { response } = require('@utils/response')
const formatSimplifiedAccountPathsFor = require('../../../../../utils/simplified-account/format/format-simplified-account-paths-for')
const paths = require('@root/paths')
const { body, validationResult } = require('express-validator')
const formatValidationErrors = require('@utils/simplified-account/format/format-validation-errors')

function get (req, res) {
return response(req, res, 'simplified-account/settings/worldpay-details/credentials', {
backLink: formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.worldpayDetails.index,
req.service.externalId, req.account.type)
})
}

const worldpayCredentialsValidations = [
body('merchantCode').not().isEmpty().withMessage('Enter your merchant code').bail()
.custom((value, { req }) => {
const merchantCode = req.body.merchantCode
if (req.account.allowMoto && !merchantCode.endsWith('MOTO') && !merchantCode.endsWith('MOTOGBP')) {
throw new Error('Enter a MOTO merchant code. MOTO payments are enabled for the account')
}
}),
body('username').not().isEmpty().withMessage('Enter your username'),
body('password').not().isEmpty().withMessage('Enter your password')
]

async function post (req, res) {
await Promise.all(worldpayCredentialsValidations.map(validation => validation.run(req)))
const validationErrors = validationResult(req)
if (!validationErrors.isEmpty()) {
const formattedErrors = formatValidationErrors(validationErrors)
return errorResponse(req, res, {
summary: formattedErrors.errorSummary,
formErrors: formattedErrors.formErrors
})
}
}

const errorResponse = (req, res, errors) => {
return response(req, res, 'simplified-account/settings/worldpay-details/credentials', {
errors,
merchantCode: req.body.merchantCode,
username: req.body.username,
password: req.body.password,
backLink: formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.worldpayDetails.index,
req.service.externalId, req.account.type)
})
}

module.exports = {
get,
post
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ function get (req, res) {
}

module.exports.get = get
module.exports.worldpayCredentials = require('./credentials/worldpay-credentials.controller')
3 changes: 3 additions & 0 deletions app/models/GatewayAccount.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { GatewayAccountCredential, CREDENTIAL_STATE } = require('@models/GatewayA
* @class GatewayAccount
* @property {string} name - The name of the gateway account
* @property {string} id - The id of the gateway account
* @property {string} serviceId - The service id of the gateway account
* @property {string} type - The type of the gateway account (e.g. test/live)
* @property {string} description - The description of the gateway account
* @property {boolean} allowMoto - whether MOTO payments are enabled on the gateway account
Expand All @@ -18,6 +19,7 @@ class GatewayAccount {
* Create an instance of GatewayAccount
* @param {Object} gatewayAccountData - raw 'gateway account' object from server
* @param {string} gatewayAccountData.gateway_account_id - The ID of the gateway account
* @param {string} gatewayAccountData.service_id - The service ID of the gateway account
* @param {string} gatewayAccountData.external_id - The external ID of the gateway account
* @param {string} gatewayAccountData.service_name - The name of the gateway account
* @param {string} gatewayAccountData.type - The type of the gateway account
Expand All @@ -32,6 +34,7 @@ class GatewayAccount {
**/
constructor (gatewayAccountData) {
this.id = gatewayAccountData.gateway_account_id
this.serviceId = gatewayAccountData.service_id
this.externalId = gatewayAccountData.external_id
this.name = gatewayAccountData.service_name
this.type = gatewayAccountData.type
Expand Down
6 changes: 5 additions & 1 deletion app/models/WorldpayTasks.class.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict'

const formatSimplifiedAccountPathsFor = require('../utils/simplified-account/format/format-simplified-account-paths-for')
const paths = require('@root/paths')

class WorldpayTasks {
/**
* @param {GatewayAccount} gatewayAccount
Expand All @@ -12,7 +15,8 @@ class WorldpayTasks {

if (gatewayAccount.allowMoto) {
const worldpayCredentials = {
href: '#',
href: formatSimplifiedAccountPathsFor(paths.simplifiedAccount.settings.worldpayDetails.credentials,
gatewayAccount.serviceId, gatewayAccount.type),
id: 'worldpay-credentials',
linkText: 'Link your Worldpay account with GOV.UK Pay',
complete: true
Expand Down
6 changes: 2 additions & 4 deletions app/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,8 @@ module.exports = {
}
},
worldpayDetails: {
index: '/settings/worldpay-details'
},
worldpayDetails: {
index: '/settings/worldpay-details'
index: '/settings/worldpay-details',
credentials: '/settings/worldpay-details/credentials'
},
cardPayments: {
index: '/settings/card-payments'
Expand Down
2 changes: 2 additions & 0 deletions app/simplified-account-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ simplifiedAccount.get(paths.simplifiedAccount.settings.worldpayDetails.index, pe

// worldpay details
simplifiedAccount.get(paths.simplifiedAccount.settings.worldpayDetails.index, permission('gateway-credentials:read'), serviceSettingsController.worldpayDetails.get)
simplifiedAccount.get(paths.simplifiedAccount.settings.worldpayDetails.credentials, permission('gateway-credentials:update'), serviceSettingsController.worldpayDetails.worldpayCredentials.get)
simplifiedAccount.post(paths.simplifiedAccount.settings.worldpayDetails.credentials, permission('gateway-credentials:update'), serviceSettingsController.worldpayDetails.worldpayCredentials.post)

// stripe details
const stripeDetailsPath = paths.simplifiedAccount.settings.stripeDetails
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{% extends "../settings-layout.njk" %}

{% block settingsPageTitle %}
Worldpay details
{% endblock %}

{% block settingsContent %}

{{ govukBackLink({
text: "Back",
href: backLink
}) }}

{% if errors %}
{{ govukErrorSummary({
titleText: "There is a problem",
errorList: errors.summary
}) }}
{% endif %}

<h1 class="govuk-heading-l">Your Worldpay credentials</h1>

<p class="govuk-body govuk-!-margin-bottom-6 hint-and-body-width">
Go to your <a class="govuk-link" href="https://secure.worldpay.com/sso/public/auth/login.html">Worldpay account</a>
to get the details you need to enter here or
<a class="govuk-link" href="https://docs.payments.service.gov.uk/switching_to_live/set_up_a_live_worldpay_account/#connect-your-live-account-to-worldpay">read more in our documentation</a>.
</p>

<form id="credentials-form" method="post" novalidate>
<input id="csrf" name="csrfToken" type="hidden" value="{{csrf}}" />

{{ govukInput({
label: {
text: 'Merchant code'
},
id: 'merchantCode',
name: 'merchantCode',
classes: 'govuk-input--width-20',
type: 'text',
value: merchantCode,
errorMessage: form.errors.merchantId and {
text: form.errors.merchantId
}
})
}}

{{ govukInput({
label: {
text: 'Username'
},
id: 'username',
name: 'username',
classes: 'govuk-input--width-20',
type: 'text',
value: username,
errorMessage: form.errors.username and {
text: form.errors.username
},
autocomplete: 'off'
})
}}

{{ govukInput({
label: {
text: 'Password'
},
id: 'password',
name: 'password',
classes: "govuk-input--width-20",
type: 'password',
value: password,
errorMessage: form.errors.password and {
text: form.errors.password
},
autocomplete: 'off'
})
}}

{{
govukButton({
text: 'Save credentials',
attributes: {
id: 'submitCredentials'
}
})
}}
</form>
{% endblock %}

0 comments on commit cc92ca9

Please sign in to comment.