From 88b5007562c67b8729f076ba85d8c6e3723f15b0 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 24 Jan 2025 13:31:04 -0300 Subject: [PATCH] Fix pay for order with blocks API on ECE (#3760) * Enabling Blocks API for ECE by default * Changelog and readme entries * Deprecating method instead of removing it * Deprecating the order creation method * Deprecated the pay for order endpoint * Fix pay for order with blocks API on ECE * Changelog and readme entries * Removing unused function * Fix tests --- changelog.txt | 1 + .../__tests__/event-handler.test.js | 6 +- client/express-checkout/event-handler.js | 5 +- .../utils/__tests__/normalize.test.js | 87 ------------------- client/express-checkout/utils/normalize.js | 18 ---- readme.txt | 1 + 6 files changed, 5 insertions(+), 113 deletions(-) diff --git a/changelog.txt b/changelog.txt index bc2e50dc8..1c44c1ee0 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ *** Changelog *** = 9.2.0 - xxxx-xx-xx = +* Fix - Fixes processing of orders through the Pay for Order page when using ECE with Blocks (Store) API. * Add - Enables the use of Blocks API for Express Checkout Element orders by default. * Add - Adds a new filter to allow changing the user attributed to an order when paying for it through the Order Pay page. * Fix - Fixes an error with the fingerprint property setting when using the legacy checkout. diff --git a/client/express-checkout/__tests__/event-handler.test.js b/client/express-checkout/__tests__/event-handler.test.js index 05a42766b..4c630f157 100644 --- a/client/express-checkout/__tests__/event-handler.test.js +++ b/client/express-checkout/__tests__/event-handler.test.js @@ -5,7 +5,6 @@ import { normalizeLineItems, normalizeShippingAddress, normalizeOrderData, - normalizePayForOrderData, } from '../utils'; import { onConfirmHandler, @@ -483,10 +482,7 @@ describe( 'Express checkout event handlers', () => { order ); - const expectedOrderData = normalizePayForOrderData( - event, - 'pm_123' - ); + const expectedOrderData = normalizeOrderData( event, 'pm_123' ); expect( api.expressCheckoutECEPayForOrder ).toHaveBeenCalledWith( 123, expectedOrderData diff --git a/client/express-checkout/event-handler.js b/client/express-checkout/event-handler.js index a5ff84a32..a5745cf47 100644 --- a/client/express-checkout/event-handler.js +++ b/client/express-checkout/event-handler.js @@ -2,7 +2,6 @@ import { __ } from '@wordpress/i18n'; import { getErrorMessageFromNotice, normalizeOrderData, - normalizePayForOrderData, normalizeShippingAddress, normalizeLineItems, getExpressCheckoutData, @@ -85,11 +84,11 @@ export const onConfirmHandler = async ( } else { orderResponse = await api.expressCheckoutECEPayForOrder( order, - normalizePayForOrderData( event, paymentMethod.id ) + normalizeOrderData( event, paymentMethod.id ) ); } - if ( orderResponse.payment_result.payment_status !== 'success' ) { + if ( orderResponse.payment_result?.payment_status !== 'success' ) { return abortPayment( event, getErrorMessageFromNotice( diff --git a/client/express-checkout/utils/__tests__/normalize.test.js b/client/express-checkout/utils/__tests__/normalize.test.js index ee4b5afa8..8489a9ad9 100644 --- a/client/express-checkout/utils/__tests__/normalize.test.js +++ b/client/express-checkout/utils/__tests__/normalize.test.js @@ -4,7 +4,6 @@ import { normalizeLineItems, normalizeOrderData, - normalizePayForOrderData, normalizeShippingAddress, } from '../normalize'; @@ -343,92 +342,6 @@ describe( 'Express checkout normalization', () => { } ); } ); - describe( 'normalizePayForOrderData', () => { - test( 'should normalize pay for order data with complete event and paymentMethodId', () => { - const event = { - billingDetails: { - name: 'John Doe', - email: 'john.doe@example.com', - address: { - organization: 'Some Company', - country: 'US', - line1: '123 Main St', - line2: 'Apt 4B', - city: 'New York', - state: 'NY', - postal_code: '10001', - }, - phone: '(123) 456-7890', - }, - shippingAddress: { - name: 'John Doe', - organization: 'Some Company', - address: { - country: 'US', - line1: '123 Main St', - line2: 'Apt 4B', - city: 'New York', - state: 'NY', - postal_code: '10001', - }, - }, - shippingRate: { id: 'rate_1' }, - expressPaymentType: 'express', - }; - - expect( normalizePayForOrderData( event, 'pm_123456' ) ).toEqual( { - payment_data: [ - { - key: 'payment_method', - value: 'stripe', - }, - { - key: 'wc-stripe-payment-method', - value: 'pm_123456', - }, - { - key: 'express_payment_type', - value: 'express', - }, - { - key: 'wc-stripe-is-deferred-intent', - value: true, - }, - ], - payment_method: 'stripe', - } ); - } ); - - test( 'should normalize pay for order data with empty event and empty payment method', () => { - const event = {}; - const paymentMethodId = ''; - - expect( - normalizePayForOrderData( event, paymentMethodId ) - ).toEqual( { - payment_data: [ - { - key: 'payment_method', - value: 'stripe', - }, - { - key: 'wc-stripe-payment-method', - value: '', - }, - { - key: 'express_payment_type', - value: undefined, - }, - { - key: 'wc-stripe-is-deferred-intent', - value: true, - }, - ], - payment_method: 'stripe', - } ); - } ); - } ); - describe( 'normalizeShippingAddress', () => { test( 'should normalize shipping address with all fields present', () => { const shippingAddress = { diff --git a/client/express-checkout/utils/normalize.js b/client/express-checkout/utils/normalize.js index 4f2de7ed1..84b3f312e 100644 --- a/client/express-checkout/utils/normalize.js +++ b/client/express-checkout/utils/normalize.js @@ -82,24 +82,6 @@ export const normalizeOrderData = ( event, paymentMethodId ) => { }; }; -/** - * Normalize Pay for Order data from Stripe's object to the expected format for WC (when using Blocks API). - * - * @param {Object} event Stripe's event object. - * @param {string} paymentMethodId Stripe's payment method id. - * - * @return {Object} Order object in the format WooCommerce expects. - */ -export const normalizePayForOrderData = ( event, paymentMethodId ) => { - return { - payment_method: 'stripe', - payment_data: buildBlocksAPIPaymentData( - event?.expressPaymentType, - paymentMethodId - ), - }; -}; - /** * Normalize shipping address information from Stripe's address object to * the cart shipping address object shape. diff --git a/readme.txt b/readme.txt index 618885bba..0f47a1e13 100644 --- a/readme.txt +++ b/readme.txt @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o == Changelog == = 9.2.0 - xxxx-xx-xx = +* Fix - Fixes processing of orders through the Pay for Order page when using ECE with Blocks (Store) API. * Add - Enables the use of Blocks API for Express Checkout Element orders by default. * Add - Adds a new filter to allow changing the user attributed to an order when paying for it through the Order Pay page. * Fix - Fixes an error with the fingerprint property setting when using the legacy checkout.