From 24fc78016f23174fcbb54bf81663064f0bc9b7b5 Mon Sep 17 00:00:00 2001 From: Shagufa92 <93454387+Shagufa92@users.noreply.github.com> Date: Thu, 8 Feb 2024 11:44:43 -0600 Subject: [PATCH 1/2] Upgrading Paypal express from Client-side only integration to a Server-side integration --- src/paypal/paypalCreateOrder.ts | 30 +- src/paypal/paypalOnApprove.ts | 121 ++----- src/paypal/paypalOnShippingChange.ts | 81 +---- tests/paypal/paypalCreateOrder.test.ts | 87 ++--- tests/paypal/paypalOnApprove.test.ts | 267 ++------------- tests/paypal/paypalOnShippingChange.test.ts | 358 ++------------------ 6 files changed, 171 insertions(+), 773 deletions(-) diff --git a/src/paypal/paypalCreateOrder.ts b/src/paypal/paypalCreateOrder.ts index 5c4bf08..1074494 100644 --- a/src/paypal/paypalCreateOrder.ts +++ b/src/paypal/paypalCreateOrder.ts @@ -1,8 +1,32 @@ import {CreateOrderActions, CreateOrderData} from '@paypal/paypal-js/types/components/buttons'; import {CreateOrderRequestBody} from '@paypal/paypal-js/types/apis/orders'; import {getPaypalOrder} from 'src/paypal'; +import { + IApiSuccessResponse, + IWalletPayCreateOrderRequest, IWalletPayCreateOrderResponse, + walletPayCreateOrder +} from '@boldcommerce/checkout-frontend-library'; +import {API_RETRY} from 'src/types'; +import {displayError} from 'src/actions'; -export async function paypalCreateOrder(data: CreateOrderData, actions: CreateOrderActions): Promise { - const paypalOrder: CreateOrderRequestBody = getPaypalOrder(); - return actions.order.create(paypalOrder); +export async function paypalCreateOrder(): Promise { + + const payment: IWalletPayCreateOrderRequest = { + gateway_type: 'paypal', + payment_data: { + locale: navigator.language, + payment_type: 'paypal', + } + }; + + const paymentResult = await walletPayCreateOrder(payment, API_RETRY); + if(paymentResult.success) { + const {data} = paymentResult.response as IApiSuccessResponse; + const {payment_data} = data as IWalletPayCreateOrderResponse; + const orderId = payment_data.id as string; + return orderId; + } else { + displayError('There was an unknown error while loading the payment.', 'payment_gateway', 'unknown_error'); + return ''; + } } diff --git a/src/paypal/paypalOnApprove.ts b/src/paypal/paypalOnApprove.ts index 0e80926..8956331 100644 --- a/src/paypal/paypalOnApprove.ts +++ b/src/paypal/paypalOnApprove.ts @@ -1,103 +1,52 @@ -import {OnApproveActions, OnApproveData} from '@paypal/paypal-js/types/components/buttons'; -import {OrderResponseBody, ShippingInfo} from '@paypal/paypal-js/types/apis/orders'; +import {OnApproveData} from '@paypal/paypal-js/types/components/buttons'; import { - callBillingAddressEndpoint, - callGuestCustomerEndpoint, - callShippingAddressEndpoint, - getFirstAndLastName, getTotals, - isObjectEquals } from 'src/utils'; -import {formatPaypalToApiAddress} from 'src/paypal/formatPaypalToApiAddress'; import { addPayment, getCurrency, IAddPaymentRequest, - setTaxes, + IWalletPayOnApproveRequest, + walletPayOnApprove, } from '@boldcommerce/checkout-frontend-library'; import {API_RETRY} from 'src/types'; import {getPaypalGatewayPublicId} from 'src/paypal/managePaypalState'; import {orderProcessing, displayError} from 'src/actions'; -export async function paypalOnApprove(data: OnApproveData, actions: OnApproveActions): Promise { +export async function paypalOnApprove(data: OnApproveData): Promise { const {iso_code: currencyCode} = getCurrency(); - return actions.order?.get().then(async ({ id, payer, purchase_units }: OrderResponseBody) => { - // extract all shipping info - const { name, address: shippingAddress } = purchase_units[0].shipping as ShippingInfo; - const shippingNames = getFirstAndLastName(name?.full_name); - - // extract all billing info - const {name: payerName, address: billingAddress} = payer; - const billingNames = {firstName: payerName?.given_name || '', lastName: payerName?.surname || ''}; - const phone = payer.phone?.phone_number.national_number || ''; - const email = payer.email_address || ''; - const isBillingAddressFilled = ( - !!billingAddress?.address_line_1 - && !!billingAddress.admin_area_1 - && !!billingAddress.admin_area_2 - && !!billingAddress.country_code - && !!billingAddress.postal_code - ); - - // set customer - const customerResult = await callGuestCustomerEndpoint(billingNames.firstName, billingNames.lastName, email); - const success = customerResult.success; - if(!success){ - displayError('There was an unknown error while validating your customer information.', 'generic', 'unknown_error'); - return; - } - - // check if shipping and billing are the same - const isSameNames = isObjectEquals(shippingNames, billingNames); - const isSameAddress = isBillingAddressFilled && isObjectEquals(shippingAddress, billingAddress); - const isBillingSame = isSameNames && isSameAddress; - const formattedShippingAddress = formatPaypalToApiAddress(shippingAddress, shippingNames.firstName, shippingNames.lastName, phone); - const formattedBillingAddress = formatPaypalToApiAddress(isBillingAddressFilled ? billingAddress : shippingAddress, billingNames.firstName, billingNames.lastName, phone); - - // check and update shipping address - const shippingAddressResponse = await callShippingAddressEndpoint(formattedShippingAddress, true); - if(!shippingAddressResponse.success){ - displayError('There was an unknown error while validating your shipping address.', 'shipping', 'unknown_error'); - return; - } - - - // check and update billing address - const billingAddressToSet = isBillingSame ? formattedShippingAddress : formattedBillingAddress; - const billingAddressResponse = await callBillingAddressEndpoint(billingAddressToSet, (!isBillingSame && isBillingAddressFilled)); - if(!billingAddressResponse.success){ - displayError('There was an unknown error while validating your billing address.', 'generic', 'unknown_error'); - return; - } - - - // update taxes - - const taxResponse = await setTaxes(API_RETRY); - if(!taxResponse.success){ - displayError('There was an unknown error while calculating the taxes.', 'payment_gateway', 'no_tax'); - return; - } - - - // add payment - const totals = getTotals(); - const payment: IAddPaymentRequest = { - token: `${id}:${payer.payer_id}`, - nonce: `${id}:${payer.payer_id}`, // TODO: Temporarily required - It is not in the API documentation, but required for Paypal Express - gateway_public_id: getPaypalGatewayPublicId(), - currency: currencyCode, - amount: totals.totalAmountDue, - wallet_pay_type: 'paypal', - } as IAddPaymentRequest; - const paymentResult = await addPayment(payment, API_RETRY); - if(!paymentResult.success){ - displayError('There was an unknown error while processing your payment.', 'payment_gateway', 'unknown_error'); - return; + const body: IWalletPayOnApproveRequest = { + gateway_type: 'paypal', + payment_data: { + locale: navigator.language, + paypal_order_id: data.orderID } + }; + + const res = await walletPayOnApprove(body, API_RETRY); + + if (!res.success) { + displayError('There was an unknown error while processing your payment.', 'payment_gateway', 'unknown_error'); + return; + } + + const totals = getTotals(); + const payment: IAddPaymentRequest = { + token: `${data.orderID}:${data.payerID}`, + nonce: `${data.orderID}:${data.payerID}`, // TODO: Temporarily required - It is not in the API documentation, but required for Paypal Express + gateway_public_id: getPaypalGatewayPublicId(), + currency: currencyCode, + amount: totals.totalAmountDue, + wallet_pay_type: 'paypal', + } as IAddPaymentRequest; + const paymentResult = await addPayment(payment, API_RETRY); + if (!paymentResult.success) { + displayError('There was an unknown error while processing your payment.', 'payment_gateway', 'unknown_error'); + return; + } + + // finalize order + orderProcessing(); - // finalize order - orderProcessing(); - }); } diff --git a/src/paypal/paypalOnShippingChange.ts b/src/paypal/paypalOnShippingChange.ts index cd1f594..48b9748 100644 --- a/src/paypal/paypalOnShippingChange.ts +++ b/src/paypal/paypalOnShippingChange.ts @@ -1,75 +1,24 @@ import {OnShippingChangeActions, OnShippingChangeData} from '@paypal/paypal-js/types/components/buttons'; +import {API_RETRY,} from 'src'; import { - getPaypalPatchOperations, - API_RETRY, - formatPaypalToApiAddress, - isSimilarStrings, - callShippingAddressEndpoint, - paypalConstants, - getPhoneNumber -} from 'src'; -import { - changeShippingLine, - estimateShippingLines, - estimateTaxes, - getOrderInitialData, - getShipping, - getShippingLines, - setTaxes, + IWalletPayOnShippingRequest, + walletPayOnShipping, } from '@boldcommerce/checkout-frontend-library'; -import {OrderResponseBody, UpdateOrderRequestBody} from '@paypal/paypal-js/types/apis/orders'; +import {OrderResponseBody} from '@paypal/paypal-js/types/apis/orders'; export async function paypalOnShippingChange(data: OnShippingChangeData, actions: OnShippingChangeActions): Promise { - const {shipping_address: address, selected_shipping_option: selectedOption} = data; - const {reject, order: {patch: patch}} = actions; - const {MAX_STRING_LENGTH: maxStringSize} = paypalConstants; - const {general_settings} = getOrderInitialData(); - const rsaEnabled = general_settings.checkout_process.rsa_enabled; - - if (address) { - const formattedAddress = formatPaypalToApiAddress(address, undefined, undefined , getPhoneNumber()); - let success = false; - if (rsaEnabled) { - const shippingLinesResponse = await estimateShippingLines(formattedAddress, API_RETRY); - if (shippingLinesResponse.success) { - success = true; - } - } else { - const shippingAddressResponse = await callShippingAddressEndpoint(formattedAddress, false); - if (!shippingAddressResponse.success) { - return reject(); - } - const shippingLinesResponse = await getShippingLines(API_RETRY); - if (shippingLinesResponse.success) { - success = true; - } + const body: IWalletPayOnShippingRequest = { + gateway_type: 'paypal', + payment_data: { + locale: navigator.language, + paypal_order_id: data.orderID, + shipping_address: data.shipping_address, + shipping_options: data.selected_shipping_option, } + }; - - if (success) { - const {selected_shipping: selectedShipping, available_shipping_lines: shippingLines} = getShipping(); - if (selectedOption) { - const option = shippingLines.find(line => isSimilarStrings(line.description.substring(0, maxStringSize), selectedOption.label)); - option && await changeShippingLine(option.id, API_RETRY); - } else if (!selectedShipping && shippingLines.length > 0) { - await changeShippingLine(shippingLines[0].id, API_RETRY); - } - await getShippingLines(API_RETRY); - } + const res = await walletPayOnShipping(body, API_RETRY); + if (!res.success) { + return actions.reject(); } - - let taxResponse; - if (rsaEnabled && address) { - const formattedAddress = formatPaypalToApiAddress(address, undefined, undefined , getPhoneNumber()); - taxResponse = await estimateTaxes(formattedAddress, API_RETRY); - } else { - taxResponse = await setTaxes(API_RETRY); - } - - if (taxResponse.success) { - const patchOperations = getPaypalPatchOperations(!!selectedOption); - return await patch(patchOperations as UpdateOrderRequestBody); - } - - return reject(); } diff --git a/tests/paypal/paypalCreateOrder.test.ts b/tests/paypal/paypalCreateOrder.test.ts index 93be90c..2630dcb 100644 --- a/tests/paypal/paypalCreateOrder.test.ts +++ b/tests/paypal/paypalCreateOrder.test.ts @@ -1,62 +1,49 @@ -import {getPaypalOrder, paypalCreateOrder} from 'src'; -import {CreateOrderActions} from '@paypal/paypal-js/types/components/buttons'; -import {AmountWithBreakdown, AmountWithCurrencyCode} from '@paypal/paypal-js'; -import {CreateOrderRequestBody, PurchaseItem} from '@paypal/paypal-js/types/apis/orders'; +import {displayError, paypalCreateOrder} from 'src'; import {mocked} from 'jest-mock'; - -jest.mock('src/paypal/getPaypalOrder'); -const getPaypalOrderMock = mocked(getPaypalOrder, true); -const createOrderActionMock: CreateOrderActions = {order: {create: jest.fn()}}; +import { + baseReturnObject, + IWalletPayCreateOrderResponse, + walletPayCreateOrder +} from '@boldcommerce/checkout-frontend-library'; +import {applicationStateMock} from '@boldcommerce/checkout-frontend-library/lib/variables/mocks'; + +jest.mock('@boldcommerce/checkout-frontend-library/lib/walletPay/walletPayCreateOrder'); +jest.mock('src/actions/displayError'); +const walletPayCreateOrderMock = mocked(walletPayCreateOrder, true); +const displayErrorMock = mocked(displayError, true); describe('testing paypalCreateOrder function', () => { - const publicOrderId = 'abc123'; - const breakdownItemMock: AmountWithCurrencyCode = { - currency_code: 'USD', - value: '0.00', - }; - const breakdownItem100Mock: AmountWithCurrencyCode = { - currency_code: 'USD', - value: '100.00', - }; - - const amountWithBreakdownMock: AmountWithBreakdown = { - currency_code: 'USD', - value: '100.00', - breakdown: { - item_total: breakdownItem100Mock, - shipping: breakdownItemMock, - tax_total: breakdownItemMock, - discount: breakdownItemMock, - shipping_discount: breakdownItemMock, - } - }; - const itemsMock: Array = [ - { - name: 'Some Name', - quantity: '1', - unit_amount: breakdownItem100Mock - } - ]; - const paypalOrderMock: CreateOrderRequestBody = { - purchase_units: [{ - custom_id: publicOrderId, - amount: amountWithBreakdownMock, - items: itemsMock - }] - }; - beforeEach(() => { jest.clearAllMocks(); - getPaypalOrderMock.mockReturnValue(paypalOrderMock); }); - test('testing call paypalCreateOrder success', async () => { - await paypalCreateOrder({paymentSource: 'paypal'}, createOrderActionMock); + test('testing with successful call', async () => { + const response: IWalletPayCreateOrderResponse = { + payment_data: { + id: 'test-order' + }, + application_state: applicationStateMock + }; + + const paymentReturn = {...baseReturnObject}; + paymentReturn.success = true; + paymentReturn.response = {data: response}; + walletPayCreateOrderMock.mockReturnValue(Promise.resolve(paymentReturn)); + + const result = await paypalCreateOrder(); + expect(result).toBe('test-order'); + }); + - expect(getPaypalOrderMock).toHaveBeenCalledTimes(1); - expect(createOrderActionMock.order.create).toHaveBeenCalledTimes(1); - expect(createOrderActionMock.order.create).toHaveBeenCalledWith(paypalOrderMock); + test('testing with unsuccessful call', async () => { + const paymentReturn = {...baseReturnObject}; + paymentReturn.success = false; + walletPayCreateOrderMock.mockReturnValue(Promise.resolve(paymentReturn)); + const result = await paypalCreateOrder(); + expect(displayErrorMock).toHaveBeenCalledTimes(1); + expect(displayErrorMock).toHaveBeenCalledWith('There was an unknown error while loading the payment.', 'payment_gateway', 'unknown_error'); + expect(result).toBe(''); }); }); diff --git a/tests/paypal/paypalOnApprove.test.ts b/tests/paypal/paypalOnApprove.test.ts index 11e7c69..6c051b3 100644 --- a/tests/paypal/paypalOnApprove.test.ts +++ b/tests/paypal/paypalOnApprove.test.ts @@ -1,14 +1,8 @@ -import {OnApproveActions, OnApproveData} from '@paypal/paypal-js/types/components/buttons'; +import {OnApproveData} from '@paypal/paypal-js/types/components/buttons'; import { - API_RETRY, - callBillingAddressEndpoint, - callGuestCustomerEndpoint, - callShippingAddressEndpoint, displayError, - formatPaypalToApiAddress, - getFirstAndLastName, - getPaypalGatewayPublicId, + displayError, orderProcessing, - paypalOnApprove + paypalOnApprove, } from 'src'; import {mocked} from 'jest-mock'; import { @@ -16,36 +10,25 @@ import { baseReturnObject, getApplicationState, getCurrency, - IAddress, - setTaxes + walletPayOnApprove } from '@boldcommerce/checkout-frontend-library'; import {applicationStateMock, currencyMock} from '@boldcommerce/checkout-frontend-library/lib/variables/mocks'; import {OrderResponseBody} from '@paypal/paypal-js/types/apis/orders'; + +jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getCurrency'); +jest.mock('@boldcommerce/checkout-frontend-library/lib/walletPay/walletPayOnApprove'); jest.mock('@boldcommerce/checkout-frontend-library/lib/payment/addPayment'); jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getApplicationState'); -jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getCurrency'); -jest.mock('@boldcommerce/checkout-frontend-library/lib/taxes/setTaxes'); jest.mock('src/actions/orderProcessing'); jest.mock('src/actions/displayError'); -jest.mock('src/paypal/formatPaypalToApiAddress'); -jest.mock('src/paypal/managePaypalState'); -jest.mock('src/utils/getFirstAndLastName'); -jest.mock('src/utils/callBillingAddressEndpoint'); -jest.mock('src/utils/callGuestCustomerEndpoint'); -jest.mock('src/utils/callShippingAddressEndpoint'); + const addPaymentMock = mocked(addPayment, true); const getApplicationStateMock = mocked(getApplicationState, true); const getCurrencyMock = mocked(getCurrency, true); -const setTaxesMock = mocked(setTaxes, true); const orderProcessingMock = mocked(orderProcessing, true); const displayErrorMock = mocked(displayError, true); -const getFirstAndLastNameMock = mocked(getFirstAndLastName, true); -const getPaypalGatewayPublicIdMock = mocked(getPaypalGatewayPublicId, true); -const formatPaypalToApiAddressMock = mocked(formatPaypalToApiAddress, true); -const callBillingAddressEndpointMock = mocked(callBillingAddressEndpoint, true); -const callGuestCustomerEndpointMock = mocked(callGuestCustomerEndpoint, true); -const callShippingAddressEndpointMock = mocked(callShippingAddressEndpoint, true); +const walletPayOnApproveMock = mocked(walletPayOnApprove, true); const onApproveActionsMock = { redirect: jest.fn(), @@ -57,38 +40,9 @@ const onApproveActionsMock = { patch: jest.fn(), } }; -const onApproveDataMock: OnApproveData = {orderID: '123', facilitatorAccessToken: ''}; +const onApproveDataMock: OnApproveData = {orderID: '123', facilitatorAccessToken: '', payerID: 'payerId'}; const successReturn = {...baseReturnObject, success: true}; const failureReturn = {...baseReturnObject, success: false}; -const filled1Address: IAddress = { - address_line_1: 'Test line 1', - address_line_2: 'Test line 2', - business_name: '', - city: 'Test city', - country: 'Test Country', - country_code: 'test_country_code', - first_name: 'John', - last_name: 'Doe', - phone_number: '0000000000', - postal_code: 'M1M1M1', - province: 'Test Province', - province_code: 'test_province_code' -}; -const filled2Address: IAddress = { - address_line_1: 'Another Test line 1', - address_line_2: 'Another Test line 2', - business_name: '', - city: 'Another Test city', - country: 'Another Test Country', - country_code: 'another_test_country_code', - first_name: 'Jane', - last_name: 'Doe', - phone_number: '0000000000', - postal_code: 'M2M2M2', - province: 'Another Test Province', - province_code: 'another_test_province_code' -}; -const johnNames = {firstName: 'John', lastName: 'Doe'}; const janeNames = {firstName: 'Jane', lastName: 'Doe'}; const paypalEmptyAddress = { address_line_1: '', @@ -106,14 +60,7 @@ const paypalFilled1Address = { admin_area_1: 'test_province_code', admin_area_2: 'test_city' }; -const paypalFilled2Address = { - address_line_1: 'Another Test Line 1', - address_line_2: 'Another Test Line 2', - postal_code: 'M2M2M2', - country_code: 'another_test_country_code', - admin_area_1: 'another_test_province_code', - admin_area_2: 'another_test_city' -}; + const payer = { payer_id: 'test_payer_id', name: {given_name: janeNames.firstName, surname: janeNames.lastName}, @@ -135,207 +82,33 @@ const paypalOrderResponseMock: OrderResponseBody = { update_time: '', id: 'test_id', payer, - purchase_units: purchaseUnits + purchase_units: purchaseUnits, + payment_source: '' } as unknown as OrderResponseBody; -const paymentRequest = { - token: 'test_id:test_payer_id', - nonce: 'test_id:test_payer_id', - wallet_pay_type: 'paypal', - gateway_public_id: 'abc123', - currency: 'USD', - amount: 10000, -}; describe('testing paypalOnApprove function', () => { beforeEach(() => { jest.clearAllMocks(); getCurrencyMock.mockReturnValue(currencyMock); - addPaymentMock.mockReturnValue(Promise.resolve(successReturn)); - getApplicationStateMock.mockReturnValue(applicationStateMock); - setTaxesMock.mockReturnValue(Promise.resolve(successReturn)); - getFirstAndLastNameMock.mockReturnValue(johnNames); - getPaypalGatewayPublicIdMock.mockReturnValue('abc123'); - formatPaypalToApiAddressMock - .mockReturnValueOnce(filled1Address) - .mockReturnValueOnce(filled2Address); - callBillingAddressEndpointMock.mockReturnValue(Promise.resolve(successReturn)); - callGuestCustomerEndpointMock.mockReturnValue(Promise.resolve(successReturn)); - callShippingAddressEndpointMock.mockReturnValue(Promise.resolve(successReturn)); - onApproveActionsMock.order.get.mockReturnValue(Promise.resolve(paypalOrderResponseMock)); - }); - - test('billing empty and success', async () => { - await paypalOnApprove(onApproveDataMock, onApproveActionsMock); - expect(getCurrencyMock).toHaveBeenCalledTimes(1); - expect(onApproveActionsMock.order.get).toHaveBeenCalledTimes(1); - expect(getFirstAndLastNameMock).toHaveBeenCalledTimes(1); - expect(getFirstAndLastNameMock).toHaveBeenCalledWith(purchaseUnits[0].shipping?.name?.full_name); - expect(callGuestCustomerEndpointMock).toHaveBeenCalledTimes(1); - expect(callGuestCustomerEndpointMock).toHaveBeenCalledWith(payer.name.given_name, payer.name.surname, payer.email_address); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(2); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(paypalFilled1Address, johnNames.firstName, johnNames.lastName, payer.phone.phone_number.national_number); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(paypalFilled1Address, janeNames.firstName, janeNames.lastName, payer.phone.phone_number.national_number); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callShippingAddressEndpointMock).toHaveBeenCalledWith(filled1Address, true); - expect(callBillingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callBillingAddressEndpointMock).toHaveBeenCalledWith(filled2Address, false); - expect(setTaxesMock).toHaveBeenCalledTimes(1); - expect(setTaxesMock).toHaveBeenCalledWith(API_RETRY); - expect(getApplicationStateMock).toHaveBeenCalledTimes(1); - expect(getPaypalGatewayPublicIdMock).toHaveBeenCalledTimes(1); - expect(addPaymentMock).toHaveBeenCalledTimes(1); - expect(addPaymentMock).toHaveBeenCalledWith(paymentRequest, API_RETRY); - expect(orderProcessingMock).toHaveBeenCalledTimes(1); - }); - - test('billing filled equal shipping, equal names and success', async () => { - const newName = {given_name: johnNames.firstName, surname: johnNames.lastName}; - const newPayer = {...payer, name: newName, address: paypalFilled1Address}; - const paypalOrder = {...paypalOrderResponseMock, payer: newPayer}; - onApproveActionsMock.order.get.mockReturnValueOnce(Promise.resolve(paypalOrder)); - - await paypalOnApprove(onApproveDataMock, onApproveActionsMock); - - expect(getCurrencyMock).toHaveBeenCalledTimes(1); - expect(onApproveActionsMock.order.get).toHaveBeenCalledTimes(1); - expect(getFirstAndLastNameMock).toHaveBeenCalledTimes(1); - expect(getFirstAndLastNameMock).toHaveBeenCalledWith(purchaseUnits[0].shipping?.name?.full_name); - expect(callGuestCustomerEndpointMock).toHaveBeenCalledTimes(1); - expect(callGuestCustomerEndpointMock).toHaveBeenCalledWith(johnNames.firstName, johnNames.lastName, payer.email_address); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(2); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(paypalFilled1Address, johnNames.firstName, johnNames.lastName, payer.phone.phone_number.national_number); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(paypalFilled1Address, johnNames.firstName, johnNames.lastName, payer.phone.phone_number.national_number); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callShippingAddressEndpointMock).toHaveBeenCalledWith(filled1Address, true); - expect(callBillingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callBillingAddressEndpointMock).toHaveBeenCalledWith(filled1Address, false); - expect(setTaxesMock).toHaveBeenCalledTimes(1); - expect(setTaxesMock).toHaveBeenCalledWith(API_RETRY); - expect(getApplicationStateMock).toHaveBeenCalledTimes(1); - expect(getPaypalGatewayPublicIdMock).toHaveBeenCalledTimes(1); - expect(addPaymentMock).toHaveBeenCalledTimes(1); - expect(addPaymentMock).toHaveBeenCalledWith(paymentRequest, API_RETRY); - expect(orderProcessingMock).toHaveBeenCalledTimes(1); - }); - - test('billing filled diff then shipping and success', async () => { - const newPayer = {...payer, address: paypalFilled2Address}; - const paypalOrder = {...paypalOrderResponseMock, payer: newPayer}; - onApproveActionsMock.order.get.mockReturnValueOnce(Promise.resolve(paypalOrder)); - - await paypalOnApprove(onApproveDataMock, onApproveActionsMock); - - expect(getCurrencyMock).toHaveBeenCalledTimes(1); - expect(onApproveActionsMock.order.get).toHaveBeenCalledTimes(1); - expect(getFirstAndLastNameMock).toHaveBeenCalledTimes(1); - expect(getFirstAndLastNameMock).toHaveBeenCalledWith(purchaseUnits[0].shipping?.name?.full_name); - expect(callGuestCustomerEndpointMock).toHaveBeenCalledTimes(1); - expect(callGuestCustomerEndpointMock).toHaveBeenCalledWith(payer.name.given_name, payer.name.surname, payer.email_address); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(2); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(paypalFilled1Address, johnNames.firstName, johnNames.lastName, payer.phone.phone_number.national_number); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(paypalFilled2Address, janeNames.firstName, janeNames.lastName, payer.phone.phone_number.national_number); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callShippingAddressEndpointMock).toHaveBeenCalledWith(filled1Address, true); - expect(callBillingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callBillingAddressEndpointMock).toHaveBeenCalledWith(filled2Address, true); - expect(setTaxesMock).toHaveBeenCalledTimes(1); - expect(setTaxesMock).toHaveBeenCalledWith(API_RETRY); - expect(getApplicationStateMock).toHaveBeenCalledTimes(1); - expect(getPaypalGatewayPublicIdMock).toHaveBeenCalledTimes(1); - expect(addPaymentMock).toHaveBeenCalledTimes(1); - expect(addPaymentMock).toHaveBeenCalledWith(paymentRequest, API_RETRY); - expect(orderProcessingMock).toHaveBeenCalledTimes(1); - }); - - test('null keys in order', async () => { - const newPayer = {...payer, address: null, email_address: null, phone: null, name: null}; - const newShipping = {...purchaseUnits[0].shipping, name: null}; - const newPurchaseUnits = [{shipping: newShipping}]; - const paypalOrder = {...paypalOrderResponseMock, payer: newPayer, purchase_units: newPurchaseUnits}; - onApproveActionsMock.order.get.mockReturnValueOnce(Promise.resolve(paypalOrder)); - - await paypalOnApprove(onApproveDataMock, onApproveActionsMock); - - expect(getCurrencyMock).toHaveBeenCalledTimes(1); - expect(onApproveActionsMock.order.get).toHaveBeenCalledTimes(1); - expect(getFirstAndLastNameMock).toHaveBeenCalledTimes(1); - expect(getFirstAndLastNameMock).toHaveBeenCalledWith(undefined); - expect(callGuestCustomerEndpointMock).toHaveBeenCalledTimes(1); - expect(callGuestCustomerEndpointMock).toHaveBeenCalledWith('', '', ''); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(2); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(paypalFilled1Address, '', '', ''); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(paypalFilled1Address, '', '', ''); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callShippingAddressEndpointMock).toHaveBeenCalledWith(filled1Address, true); - expect(callBillingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callBillingAddressEndpointMock).toHaveBeenCalledWith(filled2Address, false); - expect(setTaxesMock).toHaveBeenCalledTimes(1); - expect(setTaxesMock).toHaveBeenCalledWith(API_RETRY); - expect(getApplicationStateMock).toHaveBeenCalledTimes(1); - expect(getPaypalGatewayPublicIdMock).toHaveBeenCalledTimes(1); - expect(addPaymentMock).toHaveBeenCalledTimes(1); - expect(addPaymentMock).toHaveBeenCalledWith(paymentRequest, API_RETRY); - expect(orderProcessingMock).toHaveBeenCalledTimes(1); - }); - - test('action missing order in order', async () => { - const actions = {...onApproveActionsMock, order: null}; - - await paypalOnApprove(onApproveDataMock, actions as unknown as OnApproveActions); - - expect(getCurrencyMock).toHaveBeenCalledTimes(1); - expect(onApproveActionsMock.order.get).toHaveBeenCalledTimes(0); - expect(getFirstAndLastNameMock).toHaveBeenCalledTimes(0); - expect(callGuestCustomerEndpointMock).toHaveBeenCalledTimes(0); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(0); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(0); - expect(callBillingAddressEndpointMock).toHaveBeenCalledTimes(0); - expect(setTaxesMock).toHaveBeenCalledTimes(0); - expect(getApplicationStateMock).toHaveBeenCalledTimes(0); - expect(getPaypalGatewayPublicIdMock).toHaveBeenCalledTimes(0); - expect(addPaymentMock).toHaveBeenCalledTimes(0); - expect(orderProcessingMock).toHaveBeenCalledTimes(0); - }); - -}); - -describe('testing paypalOnApprove error cases', () => { - - beforeEach(() => { - jest.clearAllMocks(); - getCurrencyMock.mockReturnValue(currencyMock); getApplicationStateMock.mockReturnValue(applicationStateMock); - getFirstAndLastNameMock.mockReturnValue(johnNames); - getPaypalGatewayPublicIdMock.mockReturnValue('abc123'); - formatPaypalToApiAddressMock - .mockReturnValueOnce(filled1Address) - .mockReturnValueOnce(filled2Address); - callBillingAddressEndpointMock.mockReturnValue(Promise.resolve(successReturn)); - callGuestCustomerEndpointMock.mockReturnValue(Promise.resolve(successReturn)); - callShippingAddressEndpointMock.mockReturnValue(Promise.resolve(successReturn)); onApproveActionsMock.order.get.mockReturnValue(Promise.resolve(paypalOrderResponseMock)); }); const data = [ - {name: 'error in customer endpoint', customer: failureReturn, shipping: failureReturn, billing: failureReturn, taxes: failureReturn, payment: failureReturn, displayErrorCount: 1 }, - {name: 'error in shipping endpoint', customer: successReturn, shipping: failureReturn, billing: failureReturn, taxes: failureReturn, payment: failureReturn, displayErrorCount: 1 }, - {name: 'error in billing endpoint', customer: successReturn, shipping: successReturn, billing: failureReturn, taxes: failureReturn, payment: failureReturn, displayErrorCount: 1 }, - {name: 'error in taxes endpoint', customer: successReturn, shipping: successReturn, billing: successReturn, taxes: failureReturn, payment: failureReturn, displayErrorCount: 1 }, - {name: 'error in payment endpoint', customer: successReturn, shipping: successReturn, billing: successReturn, taxes: successReturn, payment: failureReturn, displayErrorCount: 1 }, - {name: 'success in all endpoint', customer: successReturn, shipping: successReturn, billing: successReturn, taxes: successReturn, payment: successReturn, displayErrorCount: 0 }, + {name: 'error in wallet pay endpoint endpoint', onApprove: failureReturn, payment: successReturn, displayErrorCount: 1, orderProcessing: 0 }, + {name: 'error in add payment', onApprove: successReturn, payment: failureReturn, displayErrorCount: 1, orderProcessing: 0 }, + {name: 'success in all endpoint', onApprove: successReturn, payment: successReturn, displayErrorCount: 0, orderProcessing: 1 }, ]; - test.each(data)('$name', async ({customer, shipping, billing, taxes, payment, displayErrorCount}) => { - callGuestCustomerEndpointMock.mockReturnValue(Promise.resolve(customer)); - callShippingAddressEndpointMock.mockReturnValue(Promise.resolve(shipping)); - callBillingAddressEndpointMock.mockReturnValue(Promise.resolve(billing)); - setTaxesMock.mockReturnValue(Promise.resolve(taxes)); + test.each(data)('$name', async ({onApprove, payment, displayErrorCount, orderProcessing}) => { + walletPayOnApproveMock.mockReturnValue(Promise.resolve(onApprove)); addPaymentMock.mockReturnValue(Promise.resolve(payment)); - await paypalOnApprove(onApproveDataMock, onApproveActionsMock); + await paypalOnApprove(onApproveDataMock); expect(displayErrorMock).toHaveBeenCalledTimes(displayErrorCount); + expect(orderProcessingMock).toHaveBeenCalledTimes(orderProcessing); }); diff --git a/tests/paypal/paypalOnShippingChange.test.ts b/tests/paypal/paypalOnShippingChange.test.ts index e165835..c3438f0 100644 --- a/tests/paypal/paypalOnShippingChange.test.ts +++ b/tests/paypal/paypalOnShippingChange.test.ts @@ -1,103 +1,18 @@ import {OnShippingChangeActions, OnShippingChangeData} from '@paypal/paypal-js/types/components/buttons'; -import { - API_RETRY, - callShippingAddressEndpoint, - formatPaypalToApiAddress, - getPaypalPatchOperations, - IPaypalPatchOperation, - paypalOnShippingChange -} from 'src'; import {mocked} from 'jest-mock'; import { baseReturnObject, - changeShippingLine, - estimateShippingLines, - estimateTaxes, - getOrderInitialData, - getShipping, - getShippingLines, - IOrderInitialData, - ISetShippingAddressRequest, - IShipping + IWalletPayOnShippingResponse, + walletPayOnShipping, + } from '@boldcommerce/checkout-frontend-library'; -import {setTaxes} from '@boldcommerce/checkout-frontend-library/lib/taxes/setTaxes'; -import {orderInitialDataMock, shippingMock} from '@boldcommerce/checkout-frontend-library/lib/variables/mocks'; -import {AmountWithBreakdown, ShippingInfoOption} from '@paypal/paypal-js/types/apis/orders'; +import {applicationStateMock,} from '@boldcommerce/checkout-frontend-library/lib/variables/mocks'; +import {paypalOnShippingChange} from 'src'; -jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getShipping'); -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/taxes/setTaxes'); -jest.mock('@boldcommerce/checkout-frontend-library/lib/address/setShippingAddress'); -jest.mock('@boldcommerce/checkout-frontend-library/lib/address/updateShippingAddress'); -jest.mock('@boldcommerce/checkout-frontend-library/lib/state/getOrderInitialData'); -jest.mock('@boldcommerce/checkout-frontend-library/lib/taxes/estimateTaxes'); -jest.mock('@boldcommerce/checkout-frontend-library/lib/shipping/estimateShippingLines'); -jest.mock('src/paypal/formatPaypalToApiAddress'); -jest.mock('src/paypal/getPaypalPatchOperations'); -jest.mock('src/utils/callShippingAddressEndpoint'); -const formatPaypalToApiAddressMock = mocked(formatPaypalToApiAddress, true); -const getPaypalPatchOperationsMock = mocked(getPaypalPatchOperations, true); -const getShippingMock = mocked(getShipping, true); -const getShippingLinesMock = mocked(getShippingLines, true); -const changeShippingLineMock = mocked(changeShippingLine, true); -const setTaxesMock = mocked(setTaxes, true); -const callShippingAddressEndpointMock = mocked(callShippingAddressEndpoint, true); -const getOrderInitialDataMock = mocked(getOrderInitialData, true); -const estimateShippingLinesMock = mocked(estimateShippingLines, true); -const estimateTaxesMock = mocked(estimateTaxes, true); +jest.mock('@boldcommerce/checkout-frontend-library/lib/walletPay/walletPayOnShipping'); +const walletPayOnShippingMock = mocked(walletPayOnShipping, true); describe('testing paypalOnShippingChange function', () => { - const onShippingChangeActionsMock: OnShippingChangeActions = { - resolve: jest.fn().mockReturnValue(Promise.resolve()), - reject: jest.fn().mockReturnValue(Promise.resolve()), - order: { - patch: jest.fn().mockReturnValue(Promise.resolve({id: '123'})) - }, - }; - const formattedAddress: ISetShippingAddressRequest = { - address_line_1: '', - address_line_2: '', - business_name: '', - city: 'Winnipeg', - country: 'CA', - country_code: 'CA', - first_name: '', - last_name: '', - phone_number: '', - postal_code: 'R3Y0L6', - province: 'MB', - province_code: 'MB', - }; - const paypalShippingOptions: Array = [{ - amount: {currency_code: 'CAD', value: '1.00'}, - id: 'test_select_shipping_line_id', - label: 'Test Description', - selected: true, - type: 'SHIPPING'}]; - const paypalAmountWithBreakdown: AmountWithBreakdown = { - breakdown: { - discount: {currency_code: 'CAD', value: '0.00'}, - item_total: {currency_code: 'CAD', value: '0.00'}, - shipping: {currency_code: 'CAD', value: '1.00'}, - shipping_discount: {currency_code: 'CAD', value: '0.01'}, - tax_total: {currency_code: 'CAD', value: '0.00'} - }, - currency_code: 'CAD', - value: '0.00' - }; - const paypalPatchOperations: Array = [ - { - op: 'replace', - path: '/purchase_units/@reference_id==\'default\'/amount', - value: paypalAmountWithBreakdown - }, - { - op: 'replace', - path: '/purchase_units/@reference_id==\'default\'/shipping/options', - value: paypalShippingOptions - } - ]; const dataMock: OnShippingChangeData = { forceRestAPI: true, shipping_address : {city: 'Winnipeg', state: 'MB', country_code: 'CA', postal_code: 'R3Y0L6'}, @@ -107,245 +22,46 @@ describe('testing paypalOnShippingChange function', () => { amount: {currency_code: 'CAD', value: '1.00'} }, }; - const successfulReturnObject = {...baseReturnObject, success: true}; + + const actionMock: OnShippingChangeActions = { + resolve: jest.fn().mockReturnValue(Promise.resolve()), + reject: jest.fn().mockReturnValue(Promise.resolve()), + order: { + patch: jest.fn().mockReturnValue(Promise.resolve({id: '123'})) + }, + }; beforeEach(() => { jest.clearAllMocks(); - formatPaypalToApiAddressMock.mockReturnValue(formattedAddress); - getPaypalPatchOperationsMock.mockReturnValue(paypalPatchOperations); - getShippingMock.mockReturnValue(shippingMock); - getShippingLinesMock.mockReturnValue(Promise.resolve(successfulReturnObject)); - changeShippingLineMock.mockReturnValue(Promise.resolve(successfulReturnObject)); - setTaxesMock.mockReturnValue(Promise.resolve(successfulReturnObject)); - callShippingAddressEndpointMock.mockReturnValue(Promise.resolve(successfulReturnObject)); - getOrderInitialDataMock.mockReturnValue(orderInitialDataMock); - estimateShippingLinesMock.mockReturnValue(Promise.resolve(successfulReturnObject)); - estimateTaxesMock.mockReturnValue(Promise.resolve(successfulReturnObject)); }); - test('new addr, old addr empty, all data in and success API calls', async () => { - const result = await paypalOnShippingChange(dataMock, onShippingChangeActionsMock); - - expect(result).toStrictEqual({id: '123'}); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(1); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(dataMock.shipping_address, undefined, undefined , ''); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callShippingAddressEndpointMock).toHaveBeenCalledWith(formattedAddress, false); - expect(getShippingLinesMock).toHaveBeenCalledTimes(2); - expect(getShippingLinesMock).toHaveBeenCalledWith(API_RETRY); - expect(getShippingMock).toHaveBeenCalledTimes(1); - expect(changeShippingLineMock).toHaveBeenCalledTimes(0); - expect(setTaxesMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledWith(true); - expect(onShippingChangeActionsMock.reject).toHaveBeenCalledTimes(0); - expect(onShippingChangeActionsMock.order.patch).toHaveBeenCalledTimes(1); - }); - - test('new addr, old addr empty, all data in and success API calls with rsa', async () => { - const {general_settings: generalSettings} = orderInitialDataMock; - const {checkout_process: checkoutProcess} = generalSettings; - const newGeneralSettings = {...generalSettings, checkout_process: {...checkoutProcess, rsa_enabled: true}}; - const initialData: IOrderInitialData = {...orderInitialDataMock, general_settings: newGeneralSettings}; - getOrderInitialDataMock.mockReturnValue(initialData); - - const result = await paypalOnShippingChange(dataMock, onShippingChangeActionsMock); - - expect(result).toStrictEqual({id: '123'}); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(2); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(dataMock.shipping_address, undefined, undefined , ''); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(0); - expect(estimateShippingLinesMock).toHaveBeenCalledTimes(1); - expect(estimateShippingLinesMock).toHaveBeenCalledWith(formattedAddress, API_RETRY); - expect(getShippingLinesMock).toHaveBeenCalledTimes(1); - expect(getShippingLinesMock).toHaveBeenCalledWith(API_RETRY); - expect(getShippingMock).toHaveBeenCalledTimes(1); - expect(changeShippingLineMock).toHaveBeenCalledTimes(0); - expect(setTaxesMock).toHaveBeenCalledTimes(0); - expect(estimateTaxesMock).toHaveBeenCalledTimes(1); - expect(estimateTaxesMock).toHaveBeenCalledWith(formattedAddress, API_RETRY); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledWith(true); - expect(onShippingChangeActionsMock.reject).toHaveBeenCalledTimes(0); - expect(onShippingChangeActionsMock.order.patch).toHaveBeenCalledTimes(1); + test('testing with successful call', async () => { + const response: IWalletPayOnShippingResponse = { + payment_data: { + }, + application_state: applicationStateMock + }; + + const paymentReturn = {...baseReturnObject}; + paymentReturn.success = true; + paymentReturn.response = {data: response}; + walletPayOnShippingMock.mockReturnValue(Promise.resolve(paymentReturn)); + + await paypalOnShippingChange(dataMock, actionMock); + expect(walletPayOnShippingMock).toHaveBeenCalledTimes(1); + expect(actionMock.reject).toHaveBeenCalledTimes(0); }); - test('no addr, no selected option and success API calls', async () => { - const data = {...dataMock, shipping_address: undefined, selected_shipping_option: undefined}; - const result = await paypalOnShippingChange(data, onShippingChangeActionsMock); + test('testing with unsuccessful call', async () => { + const paymentReturn = {...baseReturnObject}; + paymentReturn.success = false; + walletPayOnShippingMock.mockReturnValue(Promise.resolve(paymentReturn)); - expect(result).toStrictEqual({id: '123'}); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(0); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(0); - expect(getShippingLinesMock).toHaveBeenCalledTimes(0); - expect(getShippingMock).toHaveBeenCalledTimes(0); - expect(changeShippingLineMock).toHaveBeenCalledTimes(0); - expect(setTaxesMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledWith(false); - expect(onShippingChangeActionsMock.reject).toHaveBeenCalledTimes(0); - expect(onShippingChangeActionsMock.order.patch).toHaveBeenCalledTimes(1); + await paypalOnShippingChange(dataMock, actionMock); + expect(walletPayOnShippingMock).toHaveBeenCalledTimes(1); + expect(actionMock.reject).toHaveBeenCalledTimes(1); }); - test('addr null values, no selected option and address invalid', async () => { - const data = {...dataMock, shipping_address: { - city: null, state: null, country_code: null, postal_code: null - }, selected_shipping_option: undefined}; - callShippingAddressEndpointMock.mockReturnValueOnce(Promise.resolve(baseReturnObject)); - - const result = await paypalOnShippingChange(data as unknown as OnShippingChangeData, onShippingChangeActionsMock); - - expect(result).toBe(undefined); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(1); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callShippingAddressEndpointMock).toHaveBeenCalledWith(formattedAddress, false); - expect(getShippingLinesMock).toHaveBeenCalledTimes(0); - expect(getShippingMock).toHaveBeenCalledTimes(0); - expect(changeShippingLineMock).toHaveBeenCalledTimes(0); - expect(setTaxesMock).toHaveBeenCalledTimes(0); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledTimes(0); - expect(onShippingChangeActionsMock.reject).toHaveBeenCalledTimes(1); - expect(onShippingChangeActionsMock.order.patch).toHaveBeenCalledTimes(0); - }); - - test('same addr, old addr not empty, all data in and success API calls', async () => { - const result = await paypalOnShippingChange(dataMock, onShippingChangeActionsMock); - - expect(result).toStrictEqual({id: '123'}); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(1); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(dataMock.shipping_address, undefined, undefined , ''); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callShippingAddressEndpointMock).toHaveBeenCalledWith(formattedAddress, false); - expect(getShippingLinesMock).toHaveBeenCalledTimes(2); - expect(getShippingLinesMock).toHaveBeenCalledWith(API_RETRY); - expect(getShippingMock).toHaveBeenCalledTimes(1); - expect(changeShippingLineMock).toHaveBeenCalledTimes(0); - expect(setTaxesMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledWith(true); - expect(onShippingChangeActionsMock.reject).toHaveBeenCalledTimes(0); - expect(onShippingChangeActionsMock.order.patch).toHaveBeenCalledTimes(1); - }); - - test('new addr, old addr not empty, all data in and success API calls', async () => { - const result = await paypalOnShippingChange(dataMock, onShippingChangeActionsMock); - - expect(result).toStrictEqual({id: '123'}); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(1); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(dataMock.shipping_address, undefined, undefined , ''); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callShippingAddressEndpointMock).toHaveBeenCalledWith(formattedAddress, false); - expect(getShippingLinesMock).toHaveBeenCalledTimes(2); - expect(getShippingLinesMock).toHaveBeenCalledWith(API_RETRY); - expect(getShippingMock).toHaveBeenCalledTimes(1); - expect(changeShippingLineMock).toHaveBeenCalledTimes(0); - expect(setTaxesMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledWith(true); - expect(onShippingChangeActionsMock.reject).toHaveBeenCalledTimes(0); - expect(onShippingChangeActionsMock.order.patch).toHaveBeenCalledTimes(1); - }); - - test('same addr, old addr not empty, no selected line, no lines, and success API calls', async () => { - getShippingMock.mockReturnValueOnce( - {...shippingMock, selected_shipping: null, available_shipping_lines: []} as unknown as IShipping - ); - - const result = await paypalOnShippingChange(dataMock, onShippingChangeActionsMock); - - expect(result).toStrictEqual({id: '123'}); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(1); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(dataMock.shipping_address, undefined, undefined , ''); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callShippingAddressEndpointMock).toHaveBeenCalledWith(formattedAddress, false); - expect(getShippingLinesMock).toHaveBeenCalledTimes(2); - expect(getShippingLinesMock).toHaveBeenCalledWith(API_RETRY); - expect(getShippingMock).toHaveBeenCalledTimes(1); - expect(changeShippingLineMock).toHaveBeenCalledTimes(0); - expect(setTaxesMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledWith(true); - expect(onShippingChangeActionsMock.reject).toHaveBeenCalledTimes(0); - expect(onShippingChangeActionsMock.order.patch).toHaveBeenCalledTimes(1); - }); - - test('same addr, old addr not empty, no selected line, with line matching selected option, and success API calls', async () => { - getShippingMock.mockReturnValueOnce( - {...shippingMock, - selected_shipping: null, - available_shipping_lines: [ - {id: '1', description: dataMock.selected_shipping_option?.label} - ]} as unknown as IShipping - ); - - const result = await paypalOnShippingChange(dataMock, onShippingChangeActionsMock); - - expect(result).toStrictEqual({id: '123'}); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(1); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(dataMock.shipping_address, undefined, undefined , ''); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callShippingAddressEndpointMock).toHaveBeenCalledWith(formattedAddress, false); - expect(getShippingLinesMock).toHaveBeenCalledTimes(2); - expect(getShippingLinesMock).toHaveBeenCalledWith(API_RETRY); - expect(getShippingMock).toHaveBeenCalledTimes(1); - expect(changeShippingLineMock).toHaveBeenCalledTimes(1); - expect(changeShippingLineMock).toHaveBeenCalledWith('1', API_RETRY); - expect(setTaxesMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledWith(true); - expect(onShippingChangeActionsMock.reject).toHaveBeenCalledTimes(0); - expect(onShippingChangeActionsMock.order.patch).toHaveBeenCalledTimes(1); - }); - - test('same addr, old addr not empty, no selected option and line, and success API calls', async () => { - const data = {...dataMock, selected_shipping_option: undefined}; - getShippingMock.mockReturnValueOnce( - {...shippingMock, selected_shipping: null} as unknown as IShipping - ); - - const result = await paypalOnShippingChange(data, onShippingChangeActionsMock); - - expect(result).toStrictEqual({id: '123'}); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(1); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(dataMock.shipping_address, undefined, undefined , ''); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callShippingAddressEndpointMock).toHaveBeenCalledWith(formattedAddress, false); - expect(getShippingLinesMock).toHaveBeenCalledTimes(2); - expect(getShippingLinesMock).toHaveBeenCalledWith(API_RETRY); - expect(getShippingMock).toHaveBeenCalledTimes(1); - expect(changeShippingLineMock).toHaveBeenCalledTimes(1); - expect(changeShippingLineMock).toHaveBeenCalledWith('test_select_shipping_line_id', API_RETRY); - expect(setTaxesMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledWith(false); - expect(onShippingChangeActionsMock.reject).toHaveBeenCalledTimes(0); - expect(onShippingChangeActionsMock.order.patch).toHaveBeenCalledTimes(1); - }); - - test('same addr, old addr not empty, no selected option and line, and fail taxes API call', async () => { - const data = {...dataMock, selected_shipping_option: undefined}; - getShippingMock.mockReturnValueOnce( - {...shippingMock, selected_shipping: null} as unknown as IShipping - ); - setTaxesMock.mockReturnValueOnce(Promise.resolve(baseReturnObject)); - - const result = await paypalOnShippingChange(data, onShippingChangeActionsMock); - - expect(result).toBe(undefined); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledTimes(1); - expect(formatPaypalToApiAddressMock).toHaveBeenCalledWith(dataMock.shipping_address, undefined, undefined , ''); - expect(callShippingAddressEndpointMock).toHaveBeenCalledTimes(1); - expect(callShippingAddressEndpointMock).toHaveBeenCalledWith(formattedAddress, false); - expect(getShippingLinesMock).toHaveBeenCalledTimes(2); - expect(getShippingLinesMock).toHaveBeenCalledWith(API_RETRY); - expect(getShippingMock).toHaveBeenCalledTimes(1); - expect(changeShippingLineMock).toHaveBeenCalledTimes(1); - expect(changeShippingLineMock).toHaveBeenCalledWith('test_select_shipping_line_id', API_RETRY); - expect(setTaxesMock).toHaveBeenCalledTimes(1); - expect(getPaypalPatchOperationsMock).toHaveBeenCalledTimes(0); - expect(onShippingChangeActionsMock.reject).toHaveBeenCalledTimes(1); - expect(onShippingChangeActionsMock.order.patch).toHaveBeenCalledTimes(0); - }); }); From 72240b382a67f83711412740635a1459c5f68657 Mon Sep 17 00:00:00 2001 From: Shagufa92 <93454387+Shagufa92@users.noreply.github.com> Date: Thu, 8 Feb 2024 12:27:49 -0600 Subject: [PATCH 2/2] Fixing eslint issue --- src/paypal/paypalCreateOrder.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/paypal/paypalCreateOrder.ts b/src/paypal/paypalCreateOrder.ts index 1074494..043ccce 100644 --- a/src/paypal/paypalCreateOrder.ts +++ b/src/paypal/paypalCreateOrder.ts @@ -1,6 +1,3 @@ -import {CreateOrderActions, CreateOrderData} from '@paypal/paypal-js/types/components/buttons'; -import {CreateOrderRequestBody} from '@paypal/paypal-js/types/apis/orders'; -import {getPaypalOrder} from 'src/paypal'; import { IApiSuccessResponse, IWalletPayCreateOrderRequest, IWalletPayCreateOrderResponse,