Skip to content

Commit

Permalink
PP-11578 Support Google Pay for sandbox
Browse files Browse the repository at this point in the history
- Show Google Pay option for sandbox accounts, if the
`allow_google_pay` flag is set to true for the gateway account
- At this time, do not support an environment variable for disabling
Google Pay on sandbox. We suspect we will not have the need to
disable it globally, as if it stops working on Sandbox it will not
affect paying users.

- Add pact test for making a request to connector to authorise a
sandbox Google Pay payment when connector returns a success response.
This commit does not add a Pact test for a declined response - this
will come in a later commit as we need a special state for this.
  • Loading branch information
stephencdaly committed Nov 9, 2023
1 parent cb5410c commit 8cbc780
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 9 deletions.
6 changes: 6 additions & 0 deletions app/assets/javascripts/browsered/web-payments/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ const getGooglePaymentsConfiguration = (paymentProvider) => {
...(paymentProvider === 'worldpay' && {
gateway: 'worldpay',
gatewayMerchantId: window.googlePayGatewayMerchantID
}),
// Providing 'worldpay' as the gateway with a blank gatewayMerchantId allows us to initiate a Google Pay payment
// when we're using the Google Pay test environment, which allows us to complete a Google Pay payment for sandbox
...(paymentProvider === 'sandbox' && {
gateway: 'worldpay',
gatewayMerchantId: ''
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/utils/wallet-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function googlePayEnabled (charge) {
const googlePayEnabledForStripe = (process.env.STRIPE_GOOGLE_PAY_ENABLED || 'true') === 'true'

const globallyEnabledForProvider = (charge.paymentProvider === 'worldpay' && googlePayEnabledForWorldpay) ||
(charge.paymentProvider === 'stripe' && googlePayEnabledForStripe)
(charge.paymentProvider === 'stripe' && googlePayEnabledForStripe) ||
charge.paymentProvider === 'sandbox'

return (globallyEnabledForProvider || shouldOverrideGlobalWalletFlagForGatewayAccount(charge)) &&
charge.gatewayAccount.allowGooglePay
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/wallet-payment.fixtures.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const successfulLastDigitsCardNumber = '4242'

const fixtures = {
worldpayGoogleAuthRequestDetails: (ops = {}) => {
worldpayOrSandboxGoogleAuthRequestDetails: (ops = {}) => {
const data = {
payment_info: {
last_digits_card_number: ops.lastDigitsCardNumber !== undefined ? ops.lastDigitsCardNumber : successfulLastDigitsCardNumber,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict'

const path = require('path')
const { Pact } = require('@pact-foundation/pact')
const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')

const connectorClient = require('../../../app/services/clients/connector.client')
const fixtures = require('../../fixtures/wallet-payment.fixtures')
const { PactInteractionBuilder } = require('../../test-helpers/pact/pact-interaction-builder')
const { pactify } = require('../../test-helpers/pact/pact-base')()

const expect = chai.expect
chai.use(chaiAsPromised)

const TEST_CHARGE_ID = 'testChargeId'
const GOOGLE_AUTH_PATH = `/v1/frontend/charges/${TEST_CHARGE_ID}/wallets/google`
const PORT = Math.floor(Math.random() * 48127) + 1024
const BASEURL = `http://127.0.0.1:${PORT}`

describe('Connector Client - Google Pay authorisation API - Sandbox payment', function () {
const provider = new Pact({
consumer: 'frontend',
provider: 'connector',
port: PORT,
log: path.resolve(process.cwd(), 'logs', 'mockserver-integration.log'),
dir: path.resolve(process.cwd(), 'pacts'),
spec: 2,
pactfileWriteMode: 'merge'
})

before(() => provider.setup())
after(() => provider.finalize())

describe('Authorise Sandbox Google Pay payment', function () {
describe('authorisation success', function () {
const successfulGoogleAuthRequest = fixtures.worldpayOrSandboxGoogleAuthRequestDetails()
const authorisationSuccessResponse = fixtures.webPaymentSuccessResponse()
before(() => {
const builder = new PactInteractionBuilder(GOOGLE_AUTH_PATH)
.withRequestBody(successfulGoogleAuthRequest)
.withMethod('POST')
.withState('a sandbox account exists with a charge with id testChargeId that is in state ENTERING_CARD_DETAILS.')
.withUponReceiving('a valid sandbox google pay auth request which should be authorised')
.withResponseBody(pactify(authorisationSuccessResponse))
.withStatusCode(200)
.build()
return provider.addInteraction(builder)
})

afterEach(() => provider.verify())

it('should return authorisation success', function (done) {
const payload = successfulGoogleAuthRequest
connectorClient({ baseUrl: BASEURL }).chargeAuthWithWallet({
chargeId: TEST_CHARGE_ID,
wallet: 'google',
payload: payload,
paymentProvider: 'worldpay'
}).then(res => {
expect(res.body.status).to.be.equal('AUTHORISATION SUCCESS')
done()
}).catch((err) => done(new Error('should not be hit: ' + JSON.stringify(err))))
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const { pactify } = require('../../test-helpers/pact/pact-base')()
const expect = chai.expect
chai.use(chaiAsPromised)

describe('connectors client - stripe google authentication API', function () {
describe('Connectors Client - Google Pay authorisation API - Stripe payment', function () {
const provider = new Pact({
consumer: 'frontend',
provider: 'connector',
Expand All @@ -43,7 +43,7 @@ describe('connectors client - stripe google authentication API', function () {
before(() => provider.setup())
after(() => provider.finalize())

describe('Authenticate Stripe google payment', function () {
describe('Authorise Stripe Google Pay payment', function () {
describe('authorisation success', function () {
const successfulGoogleAuthRequest = fixtures.stripeGoogleAuthRequestDetails()
const authorisationSuccessResponse = fixtures.webPaymentSuccessResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ chai.use(chaiAsPromised)

const GOOGLE_DDC_RESULT = 'some long opaque string that’s a device data collection result'

describe('connectors client - worldpay google authentication API', function () {
describe('Connector Client - Google Pay authorisation API - Worldpay payment', function () {
const provider = new Pact({
consumer: 'frontend',
provider: 'connector',
Expand All @@ -40,9 +40,9 @@ describe('connectors client - worldpay google authentication API', function () {
before(() => provider.setup())
after(() => provider.finalize())

describe('Authenticate Worldpay google payment', function () {
describe('Authorise Worldpay Google Pay payment', function () {
describe('authorisation success', function () {
const successfulGoogleAuthRequest = fixtures.worldpayGoogleAuthRequestDetails({ worldpay3dsFlexDdcResult: GOOGLE_DDC_RESULT })
const successfulGoogleAuthRequest = fixtures.worldpayOrSandboxGoogleAuthRequestDetails({ worldpay3dsFlexDdcResult: GOOGLE_DDC_RESULT })
const authorisationSuccessResponse = fixtures.webPaymentSuccessResponse()
before(() => {
const builder = new PactInteractionBuilder(GOOGLE_AUTH_PATH)
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('connectors client - worldpay google authentication API', function () {
})

describe('authorisation success with no last card digits', function () {
const successfulGoogleAuthRequest = fixtures.worldpayGoogleAuthRequestDetails({
const successfulGoogleAuthRequest = fixtures.worldpayOrSandboxGoogleAuthRequestDetails({
lastDigitsCardNumber: '',
worldpay3dsFlexDdcResult: GOOGLE_DDC_RESULT
})
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('connectors client - worldpay google authentication API', function () {
})

describe('authorisation success with no ddc result', function () {
const successfulGoogleAuthRequest = fixtures.worldpayGoogleAuthRequestDetails()
const successfulGoogleAuthRequest = fixtures.worldpayOrSandboxGoogleAuthRequestDetails()
const authorisationSuccessResponse = fixtures.webPaymentSuccessResponse()

before(() => {
Expand Down
57 changes: 57 additions & 0 deletions test/utils/wallet-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,43 @@ describe('Wallet utils', () => {
expect(applePayEnabled(charge)).to.eq(false)
})
})

describe('Sandbox account', () => {
it('should return true if globally enabled for Worldpay (and sandbox) and enabled for account', () => {
process.env.WORLDPAY_APPLE_PAY_ENABLED = 'true'
process.env.WORLDPAY_GOOGLE_PAY_ENABLED = 'false'
process.env.STRIPE_APPLE_PAY_ENABLED = 'false'
process.env.STRIPE_GOOGLE_PAY_ENABLED = 'false'
const charge = createChargeWithApplePayEnabled('sandbox')
expect(applePayEnabled(charge)).to.eq(true)
})

it('should return true if environment variable not set and enabled for account', () => {
const charge = createChargeWithApplePayEnabled('sandbox')
expect(applePayEnabled(charge)).to.eq(true)
})

it('should return false if globally disabled for Worldpay (and sandbox) and account is not in test account list', () => {
process.env.WORLDPAY_APPLE_PAY_ENABLED = 'false'
process.env.PAY_TEST_GATEWAY_ACCOUNTS = ['33']
const charge = createChargeWithApplePayEnabled('sandbox')
expect(applePayEnabled(charge)).to.eq(false)
})

it('should return true if globally disabled for Worldpay (and sandbox) but account is in test account list', () => {
process.env.WORLDPAY_APPLE_PAY_ENABLED = 'false'
process.env.PAY_TEST_GATEWAY_ACCOUNTS = [gatewayAccountId.toString()]

const charge = createChargeWithApplePayEnabled('sandbox')
expect(applePayEnabled(charge)).to.eq(true)
})

it('should return false if not enabled for account', () => {
process.env.WORLDPAY_APPLE_PAY_ENABLED = 'true'
const charge = createCharge('sandbox', false, true)
expect(applePayEnabled(charge)).to.eq(false)
})
})
})

describe('googlePayEnabled', () => {
Expand Down Expand Up @@ -167,6 +204,26 @@ describe('Wallet utils', () => {
expect(googlePayEnabled(charge)).to.eq(false)
})
})

describe('Sandbox account', () => {
it('should return true if enabled for gateway account', () => {
process.env.WORLDPAY_APPLE_PAY_ENABLED = 'false'
process.env.WORLDPAY_GOOGLE_PAY_ENABLED = 'false'
process.env.STRIPE_APPLE_PAY_ENABLED = 'false'
process.env.STRIPE_GOOGLE_PAY_ENABLED = 'false'
const charge = createChargeWithGooglePayEnabled('sandbox')
expect(googlePayEnabled(charge)).to.eq(true)
})

it('should return false if not enabled for gateway account', () => {
process.env.WORLDPAY_APPLE_PAY_ENABLED = 'false'
process.env.WORLDPAY_GOOGLE_PAY_ENABLED = 'false'
process.env.STRIPE_APPLE_PAY_ENABLED = 'false'
process.env.STRIPE_GOOGLE_PAY_ENABLED = 'false'
const charge = createCharge('sandbox', true, false)
expect(googlePayEnabled(charge)).to.eq(false)
})
})
})
})

Expand Down

0 comments on commit 8cbc780

Please sign in to comment.