Skip to content

Commit

Permalink
Tax calculation for paypal express (#1134)
Browse files Browse the repository at this point in the history
* fix(SFI-860): tax calculation for paypal express

* chore(SFI-860): remove unused variables
  • Loading branch information
shanikantsingh authored Aug 9, 2024
1 parent 5ee0f68 commit 1f2d40e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 30 deletions.
6 changes: 6 additions & 0 deletions jest/__mocks__/dw/order/BasketMgr.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export const getAdjustedMerchandizeTotalGrossPrice = jest.fn(() => ({
isAvailable,
}));

export const getAdjustedMerchandizeTotalNetPrice = jest.fn(() => ({
currencyCode: 'EUR',
isAvailable,
}));

export const getCreditCardToken = jest.fn(() => 'mockedCreditCardToken');
export const getPaymentMethod = jest.fn(() => 'mockedPaymentMethod');

Expand Down Expand Up @@ -132,6 +137,7 @@ export const getCurrentBasket = jest.fn(() => ({
getAllProductLineItems,
getTotalGrossPrice,
getAdjustedMerchandizeTotalGrossPrice,
getAdjustedMerchandizeTotalNetPrice,
getPaymentInstruments,
removeAllPaymentInstruments: jest.fn(),
removePaymentInstrument: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function makeExpressPaymentsCall(req, res, next) {
currentBasket.removeAllPaymentInstruments();
paymentInstrument = currentBasket.createPaymentInstrument(
constants.METHOD_ADYEN_COMPONENT,
currentBasket.getAdjustedMerchandizeTotalGrossPrice(),
currentBasket.getAdjustedMerchandizeTotalNetPrice(),
);
const { paymentProcessor } = PaymentMgr.getPaymentMethod(
paymentInstrument.paymentMethod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const BasketMgr = require('dw/order/BasketMgr');
const Transaction = require('dw/system/Transaction');
const Resource = require('dw/web/Resource');
const URLUtils = require('dw/web/URLUtils');
const basketCalculationHelpers = require('*/cartridge/scripts/helpers/basketCalculationHelpers');
const AdyenLogs = require('*/cartridge/adyen/logs/adyenCustomLogs');
const AdyenHelper = require('*/cartridge/adyen/utils/adyenHelper');
const { PAYMENTMETHODS } = require('*/cartridge/adyen/config/constants');
Expand Down Expand Up @@ -42,7 +43,9 @@ function callGetShippingMethods(req, res, next) {
return next();
}
updateShippingAddress(currentBasket, address);
currentBasket.updateTotals();
Transaction.wrap(() => {
basketCalculationHelpers.calculateTotals(currentBasket);
});
const currentShippingMethodsModels =
AdyenHelper.getApplicableShippingMethods(
currentBasket.getDefaultShipment(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,20 @@ describe('paypalHelper', () => {
}]
currentBasket = {
currencyCode: 'USD',
getAdjustedShippingTotalGrossPrice: jest.fn(),
getAdjustedMerchandizeTotalGrossPrice: jest.fn(),
}

result = {
pspReference: 'test',
paymentData: 'test',
amount: {
currency: 'USD',
value: 2000
value: 1000
},
taxTotal: {
amount: {
currency: 'USD',
value: 1000
}
},
deliveryMethods:[{
reference: '001',
Expand Down
14 changes: 0 additions & 14 deletions src/cartridges/int_adyen_SFRA/cartridge/adyen/utils/adyenHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ let adyenHelperObj = {
* @returns {{currencyCode: String, value: String}} - Shipping Cost including taxes
*/
getShippingCost(shippingMethod, shipment) {
const { shippingAddress } = shipment
const shipmentShippingModel = ShippingMgr.getShipmentShippingModel(shipment);
let shippingCost = shipmentShippingModel.getShippingCost(shippingMethod).getAmount();
collections.forEach(shipment.getProductLineItems(), (lineItem) => {
Expand All @@ -98,25 +97,12 @@ let adyenHelperObj = {
: new Money(0, product.getPriceModel().getPrice().getCurrencyCode());
shippingCost = shippingCost.add(productShippingCost);
})
shippingCost = shippingAddress ? shippingCost.addRate(adyenHelperObj.getShippingTaxRate(shippingMethod, shippingAddress)) : shippingCost;
return {
value: shippingCost.getValue(),
currencyCode: shippingCost.getCurrencyCode(),
};
},

/**
* Returns tax rate for specific Shipment / ShippingMethod pair.
* @param {dw.order.ShippingMethod} shippingMethod - the default shipment of the current basket
* @param {dw.order.shippingAddress} shippingAddress - shippingAddress for the default shipment
* @returns {Number} - tax rate in decimals.(eg.: 0.02 for 2%)
*/
getShippingTaxRate(shippingMethod, shippingAddress) {
const taxClassID = shippingMethod.getTaxClassID();
const taxJurisdictionID = TaxMgr.getTaxJurisdictionID(new ShippingLocation(shippingAddress));
return TaxMgr.getTaxRate(taxClassID, taxJurisdictionID);
},

/**
* Returns applicable shipping methods for specific Shipment / ShippingAddress pair.
* @param {dw.order.OrderAddress} address - the shipping address of the default shipment of the current basket
Expand Down
21 changes: 10 additions & 11 deletions src/cartridges/int_adyen_SFRA/cartridge/adyen/utils/paypalHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function getLineItems({ Order: order, Basket: basket }) {
* Returns applicable shipping methods(excluding store pickup methods)
* for specific Shipment / ShippingAddress pair.
* @param {String} pspReference - the pspReference returned from adyen /payments endpoint
* @param {dw.order.basket} amount - a shipment of the current basket
* @param {dw.order.basket} currentBasket - a shipment of the current basket
* @param {dw.util.ArrayList<ApplicableShippingMethodModel>} currentShippingMethods
* - a shipment of the current basket
* @param {String} paymentData - encrypted payment data from paypal component
Expand All @@ -88,18 +88,14 @@ function createPaypalUpdateOrderRequest(
currentShippingMethods,
paymentData,
) {
const adjustedShippingTotalGrossPrice = {
const totalGrossPrice = {
currency: currentBasket.currencyCode,
value: AdyenHelper.getCurrencyValueForApi(
currentBasket.getAdjustedShippingTotalGrossPrice(),
).value,
value: AdyenHelper.getCurrencyValueForApi(currentBasket.totalGrossPrice)
.value,
};
const adjustedMerchandizeTotalGrossPrice = {
const totalTax = {
currency: currentBasket.currencyCode,
value:
AdyenHelper.getCurrencyValueForApi(
currentBasket.getAdjustedMerchandizeTotalGrossPrice(),
).value + adjustedShippingTotalGrossPrice.value,
value: AdyenHelper.getCurrencyValueForApi(currentBasket.totalTax).value,
};
const deliveryMethods = currentShippingMethods.map((shippingMethod) => {
const { currencyCode, value } = shippingMethod.shippingCost;
Expand All @@ -119,7 +115,10 @@ function createPaypalUpdateOrderRequest(
return {
pspReference,
paymentData,
amount: adjustedMerchandizeTotalGrossPrice,
amount: totalGrossPrice,
taxTotal: {
amount: totalTax,
},
deliveryMethods,
};
}
Expand Down

0 comments on commit 1f2d40e

Please sign in to comment.