diff --git a/app/assets/javascripts/browsered/web-payments/google-pay.js b/app/assets/javascripts/browsered/web-payments/google-pay.js index 3c3dc3457..bfaef7baf 100644 --- a/app/assets/javascripts/browsered/web-payments/google-pay.js +++ b/app/assets/javascripts/browsered/web-payments/google-pay.js @@ -119,15 +119,27 @@ const processPayment = paymentData => { } } +const shortenGooglePayDescription = (fullPaymentDescription) => { + const MAX_LENGTH_PAYMENT_DESCRIPTION = 18 + + if (fullPaymentDescription.length > MAX_LENGTH_PAYMENT_DESCRIPTION) { + return fullPaymentDescription.substring(0, MAX_LENGTH_PAYMENT_DESCRIPTION - 1) + '…' + } else { + return fullPaymentDescription + } +} + const createGooglePaymentRequest = () => { const methodData = [{ supportedMethods: 'https://google.com/pay', data: getGooglePaymentsConfiguration() }] + const shortenedPaymentDescription = shortenGooglePayDescription(window.paymentDetails.description) + const details = { total: { - label: window.paymentDetails.description, + label: shortenedPaymentDescription, amount: { currency: 'GBP', value: window.paymentDetails.amount @@ -157,5 +169,6 @@ const googlePayNow = () => { module.exports = { createGooglePaymentRequest, - googlePayNow + googlePayNow, + shortenGooglePayDescription } diff --git a/package.json b/package.json index c53f5dbbf..b069b39f6 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "Charge", "parent", "showCardType", - "openregisterLocationPicker" + "openregisterLocationPicker", + "window" ], "ignore": [ "app/assets/javascripts/modules/*.js" diff --git a/test/unit/browsered/web-payments/google-pay.test.js b/test/unit/browsered/web-payments/google-pay.test.js new file mode 100644 index 000000000..def1225b2 --- /dev/null +++ b/test/unit/browsered/web-payments/google-pay.test.js @@ -0,0 +1,29 @@ +'use strict' + +global.window = { + Card: {}, + Charge: {} +} + +const { shortenGooglePayDescription } = require('../../../../app/assets/javascripts/browsered/web-payments/google-pay') +const { expect } = require('chai') + +const STRING_17_CHAR_LENGTH = 'abcdefghijklmnopq' +const STRING_18_CHAR_LENGTH = 'abcdefghijklmnopqr' +const STRING_19_CHAR_LENGTH = 'abcdefghijklmnopqrs' + +describe('Google Pay', () => { + describe('shorten payment description for Google pay', () => { + it('should return the original description when the length < 18', () => { + expect(shortenGooglePayDescription(STRING_17_CHAR_LENGTH)).to.equal(STRING_17_CHAR_LENGTH) + }) + + it('should return the original description when length = 18', () => { + expect(shortenGooglePayDescription(STRING_18_CHAR_LENGTH)).to.equal(STRING_18_CHAR_LENGTH) + }) + + it('should tuncate the description to 17 characters and add the elipses character when length > 18', () => { + expect(shortenGooglePayDescription(STRING_19_CHAR_LENGTH)).to.equal('abcdefghijklmnopq…') + }) +}) +})