Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PXP-2537 Update PPCP apple pay to use tax and shipping estimate calls #15

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions src/paypal/ppcp_apple/ppcpOnShippingContactSelectedApple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ import {
getCurrency,
getShipping,
getShippingLines,
setTaxes
setTaxes,
estimateShippingLines,
estimateTaxes,
getOrderInitialData
} from '@boldcommerce/checkout-frontend-library';

export async function ppcpOnShippingContactSelectedApple(event: ApplePayShippingContactSelectedEvent): Promise<void> {
const {iso_code: currencyCode} = getCurrency();
const applePaySession = getPPCPApplePaySessionChecked();
const address = formatApplePayContactToCheckoutAddress(event.shippingContact);
address.first_name = address.first_name.trim() || 'fistName';
address.last_name = address.last_name.trim() || 'lastName';
address.address_line_1 = address.address_line_1.trim() || 'addressLine1';
address.phone_number = address.phone_number.trim() || '0000000000';
const {general_settings} = getOrderInitialData();
const rsaEnabled = general_settings.checkout_process.rsa_enabled;

let shippingResponse = null;

const fail = () => {
const {totalAmountDue} = getTotals();
Expand All @@ -32,13 +35,29 @@ export async function ppcpOnShippingContactSelectedApple(event: ApplePayShipping
});
};

const response = await callShippingAddressEndpoint(address, false);
if(rsaEnabled) {
shippingResponse = await estimateShippingLines(address, API_RETRY);
} else {
address.first_name = address.first_name.trim() || 'fistName';
address.last_name = address.last_name.trim() || 'lastName';
address.address_line_1 = address.address_line_1.trim() || 'addressLine1';
address.phone_number = address.phone_number.trim() || '0000000000';
shippingResponse = await callShippingAddressEndpoint(address, false);
}

if(shippingResponse.success){
let taxResponse = null;
let shippingResponseSuccess = true;

if(response.success){
const shippingLinesResponse = await getShippingLines(API_RETRY);
const taxResponse = await setTaxes(API_RETRY);
if (rsaEnabled) {
taxResponse = await estimateTaxes(address, API_RETRY);
} else {
const shippingLinesResponse = await getShippingLines(API_RETRY);
shippingResponseSuccess = shippingLinesResponse.success;
taxResponse = await setTaxes(API_RETRY);
}

if(shippingLinesResponse.success && taxResponse.success){
if(taxResponse.success && shippingResponseSuccess){
const {totalAmountDue} = getTotals();
const displayItems = getPaymentRequestDisplayItems().map(
({label, amount}) => ({
Expand Down
16 changes: 14 additions & 2 deletions src/paypal/ppcp_apple/ppcpOnShippingMethodSelectedApple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
getCurrency,
getShipping,
getShippingLines,
setTaxes
setTaxes,
estimateTaxes,
getShippingAddress,
getOrderInitialData
} from '@boldcommerce/checkout-frontend-library';
import ApplePayLineItem = ApplePayJS.ApplePayLineItem;
import ApplePayShippingMethodSelectedEvent = ApplePayJS.ApplePayShippingMethodSelectedEvent;
Expand All @@ -21,13 +24,22 @@ export async function ppcpOnShippingMethodSelectedApple(event: ApplePayShippingM
const {available_shipping_lines: shippingLines} = getShipping();
const selectedShippingMethod = event.shippingMethod;
const option = shippingLines.find(line => line.id === selectedShippingMethod.identifier);
const {general_settings} = getOrderInitialData();
const rsaEnabled = general_settings.checkout_process.rsa_enabled;
const address = getShippingAddress();

if (option) {
const response = await changeShippingLine(option.id, API_RETRY);

if (response.success) {
const shippingLinesResponse = await getShippingLines(API_RETRY);
const taxResponse = await setTaxes(API_RETRY);
let taxResponse = null;

if (rsaEnabled) {
taxResponse = await estimateTaxes(address, API_RETRY);
} else {
taxResponse = await setTaxes(API_RETRY);
}

if (shippingLinesResponse.success && taxResponse.success) {
const {totalAmountDue} = getTotals();
Expand Down
55 changes: 53 additions & 2 deletions tests/paypal/ppcp_apple/ppcpOnShippingContactSelectedApple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ import {
getCurrency,
getShipping,
getShippingLines,
setTaxes
setTaxes,
estimateTaxes,
estimateShippingLines,
getOrderInitialData
} from '@boldcommerce/checkout-frontend-library';
import {
addressesMock,
applicationStateMock,
currencyMock,
orderInitialDataMock,
shippingMock
} from '@boldcommerce/checkout-frontend-library/lib/variables/mocks';
import ApplePayShippingContactSelectedEvent = ApplePayJS.ApplePayShippingContactSelectedEvent;
Expand All @@ -31,8 +35,11 @@ jest.mock('src/utils/getPaymentRequestDisplayItems');
jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getCurrency');
jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getShipping');
jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getApplicationState');
jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getOrderInitialData');
jest.mock('@boldcommerce/checkout-frontend-library/lib/taxes/setTaxes');
jest.mock('@boldcommerce/checkout-frontend-library/lib/taxes/estimateTaxes');
jest.mock('@boldcommerce/checkout-frontend-library/lib/shipping/getShippingLines');
jest.mock('@boldcommerce/checkout-frontend-library/lib/shipping/estimateShippingLines');
const getPPCPApplePaySessionCheckedMock = mocked(getPPCPApplePaySessionChecked, true);
const formatApplePayContactToCheckoutAddressMock = mocked(formatApplePayContactToCheckoutAddress, true);
const getPaymentRequestDisplayItemsMock = mocked(getPaymentRequestDisplayItems, true);
Expand All @@ -41,7 +48,10 @@ const getCurrencyMock = mocked(getCurrency, true);
const getShippingMock = mocked(getShipping, true);
const getApplicationStateMock = mocked(getApplicationState, true);
const setTaxesMock = mocked(setTaxes, true);
const estimateTaxesMock = mocked(estimateTaxes, true);
const getShippingLinesMock = mocked(getShippingLines, true);
const estimateShippingLinesMock = mocked(estimateShippingLines, true);
const getOrderInitialDataMock = mocked(getOrderInitialData, true);

describe('testing ppcpOnShippingContactSelectedApple function',() => {
const successReturn = {...baseReturnObject, success: true};
Expand Down Expand Up @@ -76,22 +86,27 @@ describe('testing ppcpOnShippingContactSelectedApple function',() => {
callShippingAddressEndpointMock.mockReturnValue(Promise.resolve(successReturn));
formatApplePayContactToCheckoutAddressMock.mockReturnValue(addressesMock.shipping);
getShippingLinesMock.mockReturnValue(Promise.resolve(successReturn));
estimateShippingLinesMock.mockReturnValue(Promise.resolve(successReturn));
setTaxesMock.mockReturnValue(Promise.resolve(successReturn));
estimateTaxesMock.mockReturnValue(Promise.resolve(successReturn));
getPaymentRequestDisplayItemsMock.mockReturnValueOnce(displayItemMock);
});

test('call successfully',async () => {
test('call successfully with RSA disabled',async () => {
const expectedCompleteParam = {
newLineItems: displayItemMappedMock,
newShippingMethods: shippingMethodsMock,
newTotal: {amount: '100.00', label: 'Total'}
};
orderInitialDataMock.general_settings.checkout_process.rsa_enabled = false;
getOrderInitialDataMock.mockReturnValue(orderInitialDataMock);

await ppcpOnShippingContactSelectedApple(eventMock).then(() => {
expect(getCurrencyMock).toBeCalledTimes(1);
expect(getPPCPApplePaySessionCheckedMock).toBeCalledTimes(1);
expect(formatApplePayContactToCheckoutAddressMock).toBeCalledTimes(1);
expect(formatApplePayContactToCheckoutAddressMock).toBeCalledWith(addressContact);
expect(getOrderInitialDataMock).toBeCalledTimes(1);
expect(callShippingAddressEndpointMock).toBeCalledTimes(1);
expect(callShippingAddressEndpointMock).toBeCalledWith(addressesMock.shipping, false);
expect(getShippingLinesMock).toBeCalledTimes(1);
Expand All @@ -106,6 +121,33 @@ describe('testing ppcpOnShippingContactSelectedApple function',() => {
});
});

test('call successfully with RSA enabled',async () => {
const expectedCompleteParam = {
newLineItems: displayItemMappedMock,
newShippingMethods: shippingMethodsMock,
newTotal: {amount: '100.00', label: 'Total'}
};
orderInitialDataMock.general_settings.checkout_process.rsa_enabled = true;
getOrderInitialDataMock.mockReturnValue(orderInitialDataMock);

await ppcpOnShippingContactSelectedApple(eventMock).then(() => {
expect(getCurrencyMock).toBeCalledTimes(1);
expect(getPPCPApplePaySessionCheckedMock).toBeCalledTimes(1);
expect(formatApplePayContactToCheckoutAddressMock).toBeCalledTimes(1);
expect(formatApplePayContactToCheckoutAddressMock).toBeCalledWith(addressContact);
expect(getOrderInitialDataMock).toBeCalledTimes(1);
expect(estimateShippingLinesMock).toBeCalledTimes(1);
expect(estimateShippingLinesMock).toBeCalledWith(addressesMock.shipping, API_RETRY);
expect(estimateTaxesMock).toBeCalledTimes(1);
expect(estimateTaxesMock).toBeCalledWith(addressesMock.shipping, API_RETRY);
expect(getApplicationStateMock).toBeCalledTimes(1);
expect(getPaymentRequestDisplayItemsMock).toBeCalledTimes(1);
expect(getShippingMock).toBeCalledTimes(1);
expect(applePaySessionCompleteShippingContactSelection).toBeCalledTimes(1);
expect(applePaySessionCompleteShippingContactSelection).toBeCalledWith(expectedCompleteParam);
});
});

test('call successfully with address has empty fields',async () => {
formatApplePayContactToCheckoutAddressMock.mockReturnValue(
{
Expand Down Expand Up @@ -158,11 +200,16 @@ describe('testing ppcpOnShippingContactSelectedApple function',() => {
province: '',
province_code: '',
};

orderInitialDataMock.general_settings.checkout_process.rsa_enabled = false;
getOrderInitialDataMock.mockReturnValue(orderInitialDataMock);

await ppcpOnShippingContactSelectedApple(eventMockNew).then(() => {
expect(getCurrencyMock).toBeCalledTimes(1);
expect(getPPCPApplePaySessionCheckedMock).toBeCalledTimes(1);
expect(formatApplePayContactToCheckoutAddressMock).toBeCalledTimes(1);
expect(formatApplePayContactToCheckoutAddressMock).toBeCalledWith(addressContactWithEmptyFields);
expect(getOrderInitialDataMock).toBeCalledTimes(1);
expect(callShippingAddressEndpointMock).toBeCalledTimes(1);
expect(callShippingAddressEndpointMock).toBeCalledWith(expectedShipping, false);
expect(getShippingLinesMock).toBeCalledTimes(1);
Expand Down Expand Up @@ -221,11 +268,15 @@ describe('testing ppcpOnShippingContactSelectedApple function',() => {
newTotal: {amount: '100.00', label: 'Total'}
};

orderInitialDataMock.general_settings.checkout_process.rsa_enabled = false;
getOrderInitialDataMock.mockReturnValue(orderInitialDataMock);

await ppcpOnShippingContactSelectedApple(eventMock).then(() => {
expect(getCurrencyMock).toBeCalledTimes(1);
expect(getPPCPApplePaySessionCheckedMock).toBeCalledTimes(1);
expect(formatApplePayContactToCheckoutAddressMock).toBeCalledTimes(1);
expect(formatApplePayContactToCheckoutAddressMock).toBeCalledWith(addressContact);
expect(getOrderInitialDataMock).toBeCalledTimes(1);
expect(callShippingAddressEndpointMock).toBeCalledTimes(1);
expect(callShippingAddressEndpointMock).toBeCalledWith(addressesMock.shipping, false);
expect(getShippingLinesMock).toBeCalledTimes(shippingLineTimes);
Expand Down
56 changes: 53 additions & 3 deletions tests/paypal/ppcp_apple/ppcpOnShippingMethodSelectedApple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,42 @@ import {
getCurrency,
getShipping,
getShippingLines,
setTaxes
getOrderInitialData,
setTaxes,
estimateTaxes,
getShippingAddress
} from '@boldcommerce/checkout-frontend-library';
import ApplePayShippingMethodSelectedEvent = ApplePayJS.ApplePayShippingMethodSelectedEvent;
import {applicationStateMock, currencyMock, shippingMock} from '@boldcommerce/checkout-frontend-library/lib/variables/mocks';
import {
applicationStateMock,
currencyMock,
shippingMock,
shippingAddressMock,
orderInitialDataMock
} from '@boldcommerce/checkout-frontend-library/lib/variables/mocks';

jest.mock('src/paypal/managePaypalState');
jest.mock('src/utils/getPaymentRequestDisplayItems');
jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getCurrency');
jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getShipping');
jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getApplicationState');
jest.mock('@boldcommerce/checkout-frontend-library/lib/taxes/setTaxes');
jest.mock('@boldcommerce/checkout-frontend-library/lib/taxes/estimateTaxes');
jest.mock('@boldcommerce/checkout-frontend-library/lib/shipping/getShippingLines');
jest.mock('@boldcommerce/checkout-frontend-library/lib/shipping/changeShippingLine');
jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getShippingAddress');
jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getOrderInitialData');
const getPPCPApplePaySessionCheckedMock = mocked(getPPCPApplePaySessionChecked, true);
const getPaymentRequestDisplayItemsMock = mocked(getPaymentRequestDisplayItems, true);
const changeShippingLineMock = mocked(changeShippingLine, true);
const getCurrencyMock = mocked(getCurrency, true);
const getShippingMock = mocked(getShipping, true);
const getApplicationStateMock = mocked(getApplicationState, true);
const setTaxesMock = mocked(setTaxes, true);
const estimateTaxesMock = mocked(estimateTaxes, true);
const getShippingLinesMock = mocked(getShippingLines, true);
const getShippingAddressMock = mocked(getShippingAddress, true);
const getOrderInitialDataMock = mocked(getOrderInitialData, true);

describe('testing ppcpOnShippingMethodSelectedApple function',() => {
const successReturn = {...baseReturnObject, success: true};
Expand All @@ -56,23 +71,29 @@ describe('testing ppcpOnShippingMethodSelectedApple function',() => {
getPPCPApplePaySessionCheckedMock.mockReturnValue(applePaySessionObj);
getCurrencyMock.mockReturnValue(currencyMock);
getShippingMock.mockReturnValue(shippingMock);
getShippingAddressMock.mockReturnValue(shippingAddressMock);
getApplicationStateMock.mockReturnValue(applicationStateMock);
changeShippingLineMock.mockReturnValue(Promise.resolve(successReturn));
getShippingLinesMock.mockReturnValue(Promise.resolve(successReturn));
setTaxesMock.mockReturnValue(Promise.resolve(successReturn));
estimateTaxesMock.mockReturnValue(Promise.resolve(successReturn));
getPaymentRequestDisplayItemsMock.mockReturnValueOnce(displayItemMock);
});

test('call successfully',async () => {
test('call successfully with RSA disabled',async () => {
const expectedCompleteParam = {
newLineItems: displayItemMappedMock,
newTotal: {amount: '100.00', label: 'Total'}
};
orderInitialDataMock.general_settings.checkout_process.rsa_enabled = false;
getOrderInitialDataMock.mockReturnValue(orderInitialDataMock);

await ppcpOnShippingMethodSelectedApple(eventMock).then(() => {
expect(getCurrencyMock).toBeCalledTimes(1);
expect(getPPCPApplePaySessionCheckedMock).toBeCalledTimes(1);
expect(getShippingMock).toBeCalledTimes(1);
expect(getShippingAddressMock).toBeCalledTimes(1);
expect(getOrderInitialDataMock).toBeCalledTimes(1);
expect(changeShippingLineMock).toBeCalledTimes(1);
expect(changeShippingLineMock).toBeCalledWith('test_select_shipping_line_id', API_RETRY);
expect(getShippingLinesMock).toBeCalledTimes(1);
Expand All @@ -86,4 +107,33 @@ describe('testing ppcpOnShippingMethodSelectedApple function',() => {

});
});

test('call successfully with RSA enabled',async () => {
const expectedCompleteParam = {
newLineItems: displayItemMappedMock,
newTotal: {amount: '100.00', label: 'Total'}
};
orderInitialDataMock.general_settings.checkout_process.rsa_enabled = true;
getOrderInitialDataMock.mockReturnValue(orderInitialDataMock);


await ppcpOnShippingMethodSelectedApple(eventMock).then(() => {
expect(getCurrencyMock).toBeCalledTimes(1);
expect(getPPCPApplePaySessionCheckedMock).toBeCalledTimes(1);
expect(getShippingMock).toBeCalledTimes(1);
expect(getShippingAddressMock).toBeCalledTimes(1);
expect(getOrderInitialDataMock).toBeCalledTimes(1);
expect(changeShippingLineMock).toBeCalledTimes(1);
expect(changeShippingLineMock).toBeCalledWith('test_select_shipping_line_id', API_RETRY);
expect(getShippingLinesMock).toBeCalledTimes(1);
expect(getShippingLinesMock).toBeCalledWith(API_RETRY);
expect(estimateTaxesMock).toBeCalledTimes(1);
expect(estimateTaxesMock).toBeCalledWith(shippingAddressMock, API_RETRY);
expect(getApplicationStateMock).toBeCalledTimes(1);
expect(getPaymentRequestDisplayItemsMock).toBeCalledTimes(1);
expect(applePaySessionCompleteShippingMethodSelection).toBeCalledTimes(1);
expect(applePaySessionCompleteShippingMethodSelection).toBeCalledWith(expectedCompleteParam);

});
});
});