Skip to content

Commit

Permalink
Consistently populating Adyen_paymentMethod (#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
zenit2001 committed Jun 12, 2024
1 parent ce60315 commit 408e0eb
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 11 deletions.
1 change: 1 addition & 0 deletions jest/__mocks__/dw/crypto/Encoding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const Encoding = jest.fn();
1 change: 1 addition & 0 deletions jest/__mocks__/dw/crypto/MessageDigest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MessageDigest = jest.fn();
1 change: 1 addition & 0 deletions jest/__mocks__/dw/svc/LocalServiceRegistry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const createService = jest.fn();
1 change: 1 addition & 0 deletions jest/__mocks__/dw/util/Bytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const Bytes = jest.fn();
1 change: 1 addition & 0 deletions jest/__mocks__/dw/util/Currency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const getCurrency = jest.fn();
1 change: 1 addition & 0 deletions jest/__mocks__/dw/util/UUIDUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const createUUID = jest.fn();
4 changes: 3 additions & 1 deletion jest/sfccCartridgeMocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,6 @@ jest.mock(
getAllLineItems: jest.fn((lineItem) => lineItem),
}),
{ virtual: true },
);
);

jest.mock('*/cartridge/models/shipping/shippingMethod', () => jest.fn(), { virtual: true });
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ function doPaymentsCall(order, paymentInstrument, paymentRequest) {
if (!order) {
return responseObject;
}
// set custom payment method field to sync with OMS.
// for card payments (scheme) we will store the brand
order.custom.Adyen_paymentMethod =
paymentRequest?.paymentMethod?.brand ||
paymentRequest?.paymentMethod?.type;
paymentResponse.fullResponse = responseObject;
paymentResponse.redirectObject = responseObject.action
? responseObject.action
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* eslint-disable global-require */
const savePaymentDetails = require('../adyenHelper').savePaymentDetails;
describe('savePaymentDetails', () => {
let paymentInstrument;
let order;
let result;

beforeEach(() => {
paymentInstrument = {
paymentTransaction: {
custom: {}
},
getCreditCardToken: jest.fn(),
setCreditCardToken: jest.fn()
};
order = {
custom: {}
};
result = {};
});

it('should set the transactionID and Adyen_pspReference', () => {
result.pspReference = 'testReference';
savePaymentDetails(paymentInstrument, order, result);
expect(paymentInstrument.paymentTransaction.transactionID).toBe('testReference');
expect(paymentInstrument.paymentTransaction.custom.Adyen_pspReference).toBe('testReference');
});

it('should set Adyen_paymentMethod from additionalData', () => {
result.additionalData = { paymentMethod: 'visa' };
savePaymentDetails(paymentInstrument, order, result);
expect(paymentInstrument.paymentTransaction.custom.Adyen_paymentMethod).toBe('visa');
expect(order.custom.Adyen_paymentMethod).toBe('visa');
});

it('should set Adyen_paymentMethod from paymentMethod', () => {
result.paymentMethod = { type: 'mc' };
savePaymentDetails(paymentInstrument, order, result);
expect(paymentInstrument.paymentTransaction.custom.Adyen_paymentMethod).toBe(JSON.stringify('mc'));
expect(order.custom.Adyen_paymentMethod).toBe(JSON.stringify('mc'));
});

it('should set the credit card token if not already exists', () => {
result.additionalData = { 'recurring.recurringDetailReference': 'token123' };
paymentInstrument.getCreditCardToken.mockReturnValue(null);
savePaymentDetails(paymentInstrument, order, result);
expect(paymentInstrument.setCreditCardToken).toHaveBeenCalledWith('token123');
});

it('should not set the credit card token if already exists', () => {
result.additionalData = { 'recurring.recurringDetailReference': 'token123' };
paymentInstrument.getCreditCardToken.mockReturnValue('existingToken');
savePaymentDetails(paymentInstrument, order, result);
expect(paymentInstrument.setCreditCardToken).not.toHaveBeenCalled();
});

it('should set the authCode and Adyen_value', () => {
result.resultCode = 'Authorised';
savePaymentDetails(paymentInstrument, order, result);
expect(paymentInstrument.paymentTransaction.custom.authCode).toBe('Authorised');
expect(order.custom.Adyen_value).toBe('0');
});

it('should set Adyen_donationToken if present', () => {
result.donationToken = 'donation-token-123';
savePaymentDetails(paymentInstrument, order, result);
expect(paymentInstrument.paymentTransaction.custom.Adyen_donationToken).toBe('donation-token-123');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*/
const dwsvc = require('dw/svc');
const dwsystem = require('dw/system');
const dwutil = require('dw/util');
const LocalServiceRegistry = require('dw/svc/LocalServiceRegistry');
const Currency = require('dw/util/Currency');
const URLUtils = require('dw/web/URLUtils');
const Bytes = require('dw/util/Bytes');
const MessageDigest = require('dw/crypto/MessageDigest');
Expand All @@ -44,7 +43,7 @@ let adyenHelperObj = {
let adyenService = null;

try {
adyenService = dwsvc.LocalServiceRegistry.createService(service, {
adyenService = LocalServiceRegistry.createService(service, {
createRequest(svc, args) {
svc.setRequestMethod('POST');
if (args) {
Expand Down Expand Up @@ -652,6 +651,7 @@ let adyenHelperObj = {
},

// saves the payment details in the paymentInstrument's custom object
// set custom payment method field to sync with OMS
savePaymentDetails(paymentInstrument, order, result) {
paymentInstrument.paymentTransaction.transactionID = result.pspReference;
paymentInstrument.paymentTransaction.custom.Adyen_pspReference =
Expand All @@ -660,10 +660,14 @@ let adyenHelperObj = {
if (result.additionalData?.paymentMethod) {
paymentInstrument.paymentTransaction.custom.Adyen_paymentMethod =
result.additionalData.paymentMethod;
order.custom.Adyen_paymentMethod = result.additionalData.paymentMethod;
} else if (result.paymentMethod) {
paymentInstrument.paymentTransaction.custom.Adyen_paymentMethod = JSON.stringify(
result.paymentMethod.type,
);
order.custom.Adyen_paymentMethod = JSON.stringify(
result.paymentMethod.type,
);
}

// For authenticated shoppers we are setting the token on other place already
Expand Down Expand Up @@ -695,7 +699,7 @@ let adyenHelperObj = {

// converts the currency value for the Adyen Checkout API
getCurrencyValueForApi(amount) {
const currencyCode = dwutil.Currency.getCurrency(amount.currencyCode) || session.currency.currencyCode;
const currencyCode = Currency.getCurrency(amount.currencyCode) || session.currency.currencyCode;
const digitsNumber = adyenHelperObj.getFractionDigits(
currencyCode.toString(),
);
Expand Down

0 comments on commit 408e0eb

Please sign in to comment.