-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PP-13313 Validate Credentials with Worldpay (#4386)
* PP-13313 Validate Credentials with Worldpay
- Loading branch information
1 parent
cf0767a
commit bb0f7fb
Showing
10 changed files
with
245 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const WorldpayCredential = require('./WorldpayCredential.class') | ||
|
||
class Credential { | ||
/** | ||
* | ||
* @param {String} stripeAccountId | ||
* @returns {Credential} | ||
*/ | ||
withStripeAccountId (stripeAccountId) { | ||
if (stripeAccountId) { | ||
this.stripeAccountId = stripeAccountId | ||
} | ||
return this | ||
} | ||
|
||
/** | ||
* | ||
* @param {WorldpayCredential} oneOffCustomerInitiated | ||
* @returns {Credential} | ||
*/ | ||
withOneOffCustomerInitiated (oneOffCustomerInitiated) { | ||
if (oneOffCustomerInitiated) { | ||
this.oneOffCustomerInitiated = oneOffCustomerInitiated | ||
} | ||
return this | ||
} | ||
|
||
/** @deprecated this is a temporary compatability fix! If you find yourself using this for new code | ||
* you should instead add any rawResponse data as part of the constructor */ | ||
withRawResponse (data) { | ||
/** @deprecated this is a temporary compatability fix! If you find yourself using this for new code | ||
* you should instead add any rawResponse data as part of the constructor */ | ||
this.rawResponse = data | ||
return this | ||
} | ||
|
||
toJson () { | ||
return { | ||
...this.stripeAccountId && { stripe_account_id: this.stripeAccountId }, | ||
...this.oneOffCustomerInitiated && { one_off_customer_initiated: this.oneOffCustomerInitiated.toJson() } | ||
} | ||
} | ||
|
||
static fromJson (data) { | ||
return new Credential() | ||
.withStripeAccountId(data?.stripe_account_id) | ||
.withOneOffCustomerInitiated(WorldpayCredential.fromJson(data?.one_off_customer_initiated)) | ||
.withRawResponse(data) | ||
} | ||
} | ||
|
||
module.exports = Credential |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
app/models/gateway-account-credential/GatewayAccountCredentialUpdateRequest.class.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
class GatewayAccountCredentialUpdateRequest { | ||
/** | ||
* @param {String} userExternalId | ||
*/ | ||
constructor (userExternalId) { | ||
this.updates = [{ | ||
op: 'replace', | ||
path: 'last_updated_by_user_external_id', | ||
value: userExternalId | ||
}] | ||
} | ||
|
||
replace () { | ||
return safeOperation('replace', this) | ||
} | ||
|
||
formatPayload () { | ||
return this.updates | ||
} | ||
} | ||
|
||
const safeOperation = (op, request) => { | ||
return { | ||
credentials: (value) => { | ||
request.updates.push({ op, path: 'credentials', value }) | ||
return request | ||
} | ||
} | ||
} | ||
module.exports = { GatewayAccountCredentialUpdateRequest } |
24 changes: 24 additions & 0 deletions
24
app/models/gateway-account-credential/ValidationResult.class.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class ValidationResult { | ||
/** | ||
* | ||
* @param {String} result | ||
*/ | ||
withResult (result) { | ||
if (result) { | ||
this.result = result | ||
} | ||
return this | ||
} | ||
|
||
/** | ||
* | ||
* @param data | ||
* @returns {ValidationResult} | ||
*/ | ||
static fromJson (data) { | ||
return new ValidationResult() | ||
.withResult(data?.result) | ||
} | ||
} | ||
|
||
module.exports = ValidationResult |
42 changes: 42 additions & 0 deletions
42
app/models/gateway-account-credential/WorldpayCredential.class.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class WorldpayCredential { | ||
withMerchantCode (merchantCode) { | ||
if (merchantCode) { | ||
this.merchantCode = merchantCode | ||
} | ||
return this | ||
} | ||
|
||
withUsername (username) { | ||
if (username) { | ||
this.username = username | ||
} | ||
return this | ||
} | ||
|
||
withPassword (password) { | ||
if (password) { | ||
this.password = password | ||
} | ||
return this | ||
} | ||
|
||
toJson () { | ||
return { | ||
...this.merchantCode && { merchant_code: this.merchantCode }, | ||
...this.username && { username: this.username }, | ||
...this.password && { password: this.password } | ||
} | ||
} | ||
|
||
static fromJson (data) { | ||
if (!data) { | ||
return undefined | ||
} | ||
return new WorldpayCredential() | ||
.withMerchantCode(data?.merchant_code) | ||
.withUsername(data?.username) | ||
.withPassword(data?.password) | ||
} | ||
} | ||
|
||
module.exports = WorldpayCredential |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { ConnectorClient } = require('./clients/connector.client') | ||
const logger = require('../utils/logger')(__filename) | ||
|
||
const connectorClient = new ConnectorClient(process.env.CONNECTOR_URL) | ||
|
||
/** | ||
* | ||
* @param {String} serviceExternalId | ||
* @param {String} accountType | ||
* @param {WorldpayCredential} credential | ||
* @returns {Promise<Object>} | ||
*/ | ||
async function checkCredential (serviceExternalId, accountType, credential) { | ||
const credentialCheck = await connectorClient.postCheckWorldpayCredentialByServiceExternalIdAndAccountType( | ||
serviceExternalId, | ||
accountType, | ||
credential | ||
) | ||
if (credentialCheck.result !== 'valid') { | ||
logger.warn(`Credentials provided for service external ID [${serviceExternalId}], account type [${accountType}] failed validation with Worldpay`) | ||
return false | ||
} | ||
|
||
logger.info(`Successfully validated credentials for service external ID [${serviceExternalId}], account type [${accountType}] with Worldpay`) | ||
return true | ||
} | ||
|
||
module.exports = { | ||
checkCredential | ||
} |