From 9c3c7a81cc6d423a2b839ee72c58b61cdab4063a Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 28 Nov 2024 13:25:54 -0300 Subject: [PATCH 01/19] Fix missing order attribution metadata when using ECE --- .../class-wc-stripe-express-checkout-element.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/includes/payment-methods/class-wc-stripe-express-checkout-element.php b/includes/payment-methods/class-wc-stripe-express-checkout-element.php index c944dab05..652942334 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-element.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-element.php @@ -420,9 +420,25 @@ public function display_express_checkout_button_html() { add_order_attribution_inputs(); + } + $this->display_express_checkout_button_separator_html(); } + /** + * Add order attribution inputs to the page. + * + * @return void + */ + public function add_order_attribution_inputs() { + echo ''; + } + /** * Display express checkout button separator. */ From fc795a2b9494f67f3dbbbf818b812a9a1e64c435 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 28 Nov 2024 14:24:41 -0300 Subject: [PATCH 02/19] Adding order attribution fields to block checkout --- client/blocks/upe/index.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/client/blocks/upe/index.js b/client/blocks/upe/index.js index 189fed2ca..43c1ce396 100644 --- a/client/blocks/upe/index.js +++ b/client/blocks/upe/index.js @@ -104,5 +104,23 @@ if ( getBlocksConfiguration()?.isECEEnabled ) { registerExpressPaymentMethod( paymentRequestPaymentMethod ); } +const addOrderAttributionInputs = () => { + const orderAttributionInputs = document.createElement( + 'wc-order-attribution-inputs' + ); + orderAttributionInputs.id = + 'wc-stripe-express-checkout__order-attribution-inputs'; + document.body.appendChild( orderAttributionInputs ); +}; + // Update token labels when the checkout form is loaded. updateTokenLabelsWhenLoaded(); + +// Add order attribution inputs to the page. +addOrderAttributionInputs(); + +// Populate order attribution inputs with order tracking data. +const orderAttribution = window?.wc_order_attribution; +if ( orderAttribution ) { + orderAttribution.setOrderTracking( orderAttribution.params.allowTracking ); +} From d8cad3409b7709f3a10e33498dc6ad1e8957ab88 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 28 Nov 2024 14:24:55 -0300 Subject: [PATCH 03/19] Adding order attribution fields to PRB --- .../payment-request/payment-request-express.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/client/blocks/payment-request/payment-request-express.js b/client/blocks/payment-request/payment-request-express.js index 39252558a..56f1d5053 100644 --- a/client/blocks/payment-request/payment-request-express.js +++ b/client/blocks/payment-request/payment-request-express.js @@ -1,4 +1,5 @@ import { __ } from '@wordpress/i18n'; +import { useEffect } from '@wordpress/element'; import { Elements, PaymentRequestButtonElement, @@ -70,6 +71,17 @@ const PaymentRequestExpressComponent = ( { ); useCancelHandler( paymentRequest, onClose ); + useEffect( () => { + if ( paymentRequest ) { + const orderAttribution = window?.wc_order_attribution; + if ( orderAttribution ) { + orderAttribution.setOrderTracking( + orderAttribution.params.allowTracking + ); + } + } + }, [ paymentRequest ] ); + // locale is not a valid value for the paymentRequestButton style. // Make sure `theme` defaults to 'dark' if it's not found in the server provided configuration. let { @@ -100,6 +112,8 @@ const PaymentRequestExpressComponent = ( { const { LoadingMask } = components; + + if ( isCustom ) { return ( + ); } @@ -132,6 +147,7 @@ const PaymentRequestExpressComponent = ( { onPaymentRequestButtonClick( evt, paymentRequest ); } } /> + ); } @@ -159,6 +175,7 @@ const PaymentRequestExpressComponent = ( { paymentRequest, } } /> + ); }; From b92922aa3d1cb96aff86e7e79df040193f97badd Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 28 Nov 2024 17:45:13 -0300 Subject: [PATCH 04/19] Add order attribution data to ECE --- client/express-checkout/utils/normalize.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/client/express-checkout/utils/normalize.js b/client/express-checkout/utils/normalize.js index 7af50f605..55d04e742 100644 --- a/client/express-checkout/utils/normalize.js +++ b/client/express-checkout/utils/normalize.js @@ -32,11 +32,21 @@ export const normalizeOrderData = ( event, paymentMethodId ) => { const email = event?.billingDetails?.email ?? ''; const billing = event?.billingDetails?.address ?? {}; const shipping = event?.shippingAddress ?? {}; + const phoneField = event?.billingDetails?.phone ?? event?.payerPhone ?? ''; + const phone = phoneField.replace( /[() -]/g, '' ); - const phone = - event?.billingDetails?.phone?.replace( /[() -]/g, '' ) ?? - event?.payerPhone?.replace( /[() -]/g, '' ) ?? - ''; + // Get order attribution data from the hidden inputs. + const orderAttributionData = {}; + const orderAttributionWrapper = document.getElementsByTagName( + 'wc-order-attribution-inputs' + ); + if ( orderAttributionWrapper.length ) { + const orderAttributionInputs = orderAttributionWrapper[ 0 ].children; + for ( let i = 0; i < orderAttributionInputs.length; i++ ) { + orderAttributionData[ orderAttributionInputs[ i ].name ] = + orderAttributionInputs[ i ].value; + } + } return { billing_first_name: @@ -72,6 +82,7 @@ export const normalizeOrderData = ( event, paymentMethodId ) => { express_checkout_type: event?.expressPaymentType, express_payment_type: event?.expressPaymentType, 'wc-stripe-is-deferred-intent': true, + ...orderAttributionData, }; }; From aae2475a42b2c740ff227b69f2abc93feeb00ba3 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 28 Nov 2024 17:45:47 -0300 Subject: [PATCH 05/19] Adding inputs only when they don't exist --- client/blocks/upe/index.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/client/blocks/upe/index.js b/client/blocks/upe/index.js index 43c1ce396..fc9cface3 100644 --- a/client/blocks/upe/index.js +++ b/client/blocks/upe/index.js @@ -104,23 +104,33 @@ if ( getBlocksConfiguration()?.isECEEnabled ) { registerExpressPaymentMethod( paymentRequestPaymentMethod ); } -const addOrderAttributionInputs = () => { +const addOrderAttributionInputsIfNotExists = () => { + const elementId = 'wc-stripe-express-checkout__order-attribution-inputs'; + if ( document.getElementById( elementId ) ) { + return; + } + const orderAttributionInputs = document.createElement( 'wc-order-attribution-inputs' ); - orderAttributionInputs.id = - 'wc-stripe-express-checkout__order-attribution-inputs'; + orderAttributionInputs.id = elementId; document.body.appendChild( orderAttributionInputs ); }; +const populateOrderAttributionInputs = () => { + const orderAttribution = window?.wc_order_attribution; + if ( orderAttribution ) { + orderAttribution.setOrderTracking( + orderAttribution.params.allowTracking + ); + } +}; + // Update token labels when the checkout form is loaded. updateTokenLabelsWhenLoaded(); // Add order attribution inputs to the page. -addOrderAttributionInputs(); +addOrderAttributionInputsIfNotExists(); // Populate order attribution inputs with order tracking data. -const orderAttribution = window?.wc_order_attribution; -if ( orderAttribution ) { - orderAttribution.setOrderTracking( orderAttribution.params.allowTracking ); -} +populateOrderAttributionInputs(); From f7379572ae04ee73b109f96928a6fb10a338ce67 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 28 Nov 2024 17:48:52 -0300 Subject: [PATCH 06/19] Moving logic to a new method --- client/express-checkout/utils/normalize.js | 37 ++++++++++++++-------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/client/express-checkout/utils/normalize.js b/client/express-checkout/utils/normalize.js index 55d04e742..9519a2416 100644 --- a/client/express-checkout/utils/normalize.js +++ b/client/express-checkout/utils/normalize.js @@ -35,19 +35,6 @@ export const normalizeOrderData = ( event, paymentMethodId ) => { const phoneField = event?.billingDetails?.phone ?? event?.payerPhone ?? ''; const phone = phoneField.replace( /[() -]/g, '' ); - // Get order attribution data from the hidden inputs. - const orderAttributionData = {}; - const orderAttributionWrapper = document.getElementsByTagName( - 'wc-order-attribution-inputs' - ); - if ( orderAttributionWrapper.length ) { - const orderAttributionInputs = orderAttributionWrapper[ 0 ].children; - for ( let i = 0; i < orderAttributionInputs.length; i++ ) { - orderAttributionData[ orderAttributionInputs[ i ].name ] = - orderAttributionInputs[ i ].value; - } - } - return { billing_first_name: name?.split( ' ' )?.slice( 0, 1 )?.join( ' ' ) ?? '', @@ -82,10 +69,32 @@ export const normalizeOrderData = ( event, paymentMethodId ) => { express_checkout_type: event?.expressPaymentType, express_payment_type: event?.expressPaymentType, 'wc-stripe-is-deferred-intent': true, - ...orderAttributionData, + ...extractOrderAttributionData(), }; }; +/** + * Get order attribution data from the hidden inputs. + * + * @return {Object} Order attribution data. + */ +export const extractOrderAttributionData = () => { + const orderAttributionWrapper = document.getElementsByTagName( + 'wc-order-attribution-inputs' + ); + if ( ! orderAttributionWrapper.length ) { + return {}; + } + + const orderAttributionData = {}; + const orderAttributionInputs = orderAttributionWrapper[ 0 ].children; + for ( let i = 0; i < orderAttributionInputs.length; i++ ) { + orderAttributionData[ orderAttributionInputs[ i ].name ] = + orderAttributionInputs[ i ].value; + } + return orderAttributionData; +}; + /** * Normalize Pay for Order data from Stripe's object to the expected format for WC. * From a690ceb0d33d239fc33fbbafb19cc3a61b676d7c Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 28 Nov 2024 18:09:53 -0300 Subject: [PATCH 07/19] Fix order attribution for PRB --- assets/js/stripe-payment-request.js | 2 +- client/blocks/normalize.js | 24 ++++++++++++++++++- .../payment-request-express.js | 6 +---- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/assets/js/stripe-payment-request.js b/assets/js/stripe-payment-request.js index 8c57ff7ec..f0e048c33 100644 --- a/assets/js/stripe-payment-request.js +++ b/assets/js/stripe-payment-request.js @@ -173,7 +173,7 @@ jQuery( function( $ ) { if ( ! data[ name ] ) { data[ name ] = value; } - + // if shipping same as billing is selected, copy the billing field to shipping field. const shipToDiffAddress = $( '#ship-to-different-address' ).find( 'input' ).is( ':checked' ); if ( ! shipToDiffAddress ) { diff --git a/client/blocks/normalize.js b/client/blocks/normalize.js index e1702a3ac..faa91ba2a 100644 --- a/client/blocks/normalize.js +++ b/client/blocks/normalize.js @@ -79,7 +79,29 @@ const normalizeOrderData = ( paymentMethodEvent, paymentRequestType ) => { data.shipping_postcode = shipping?.postalCode; } - return data; + return { ...data, ...extractOrderAttributionData() }; +}; + +/** + * Get order attribution data from the hidden inputs. + * + * @return {Object} Order attribution data. + */ +export const extractOrderAttributionData = () => { + const orderAttributionWrapper = document.getElementsByTagName( + 'wc-order-attribution-inputs' + ); + if ( ! orderAttributionWrapper.length ) { + return {}; + } + + const orderAttributionData = {}; + const orderAttributionInputs = orderAttributionWrapper[ 0 ].children; + for ( let i = 0; i < orderAttributionInputs.length; i++ ) { + orderAttributionData[ orderAttributionInputs[ i ].name ] = + orderAttributionInputs[ i ].value; + } + return orderAttributionData; }; /** diff --git a/client/blocks/payment-request/payment-request-express.js b/client/blocks/payment-request/payment-request-express.js index 56f1d5053..6a1fbb6cb 100644 --- a/client/blocks/payment-request/payment-request-express.js +++ b/client/blocks/payment-request/payment-request-express.js @@ -112,8 +112,6 @@ const PaymentRequestExpressComponent = ( { const { LoadingMask } = components; - - if ( isCustom ) { return ( - ); } @@ -147,7 +144,6 @@ const PaymentRequestExpressComponent = ( { onPaymentRequestButtonClick( evt, paymentRequest ); } } /> - ); } @@ -175,7 +171,6 @@ const PaymentRequestExpressComponent = ( { paymentRequest, } } /> - ); }; @@ -190,6 +185,7 @@ export const PaymentRequestExpress = ( props ) => { return ( + ); }; From a3108661c61607b942c744217f509e4376681bcc Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 28 Nov 2024 18:11:58 -0300 Subject: [PATCH 08/19] Move method to shared file --- client/blocks/normalize.js | 27 ++++------------------ client/blocks/utils.js | 22 ++++++++++++++++++ client/express-checkout/utils/normalize.js | 24 ++----------------- 3 files changed, 28 insertions(+), 45 deletions(-) diff --git a/client/blocks/normalize.js b/client/blocks/normalize.js index faa91ba2a..aca1f100b 100644 --- a/client/blocks/normalize.js +++ b/client/blocks/normalize.js @@ -9,7 +9,10 @@ * @typedef {import('@woocommerce/type-defs/billing').BillingData} CartBillingAddress */ -import { getBlocksConfiguration } from 'wcstripe/blocks/utils'; +import { + extractOrderAttributionData, + getBlocksConfiguration, +} from 'wcstripe/blocks/utils'; /** * Normalizes order data received upon creating an order using the store's AJAX API. @@ -82,28 +85,6 @@ const normalizeOrderData = ( paymentMethodEvent, paymentRequestType ) => { return { ...data, ...extractOrderAttributionData() }; }; -/** - * Get order attribution data from the hidden inputs. - * - * @return {Object} Order attribution data. - */ -export const extractOrderAttributionData = () => { - const orderAttributionWrapper = document.getElementsByTagName( - 'wc-order-attribution-inputs' - ); - if ( ! orderAttributionWrapper.length ) { - return {}; - } - - const orderAttributionData = {}; - const orderAttributionInputs = orderAttributionWrapper[ 0 ].children; - for ( let i = 0; i < orderAttributionInputs.length; i++ ) { - orderAttributionData[ orderAttributionInputs[ i ].name ] = - orderAttributionInputs[ i ].value; - } - return orderAttributionData; -}; - /** * Normalizes an address received upon updating shipping options using the store's AJAX API. * diff --git a/client/blocks/utils.js b/client/blocks/utils.js index 79fbd9e73..bbdf9f7ad 100644 --- a/client/blocks/utils.js +++ b/client/blocks/utils.js @@ -84,3 +84,25 @@ export const getApiKey = () => { } return apiKey; }; + +/** + * Get order attribution data from the hidden inputs. + * + * @return {Object} Order attribution data. + */ +export const extractOrderAttributionData = () => { + const orderAttributionWrapper = document.getElementsByTagName( + 'wc-order-attribution-inputs' + ); + if ( ! orderAttributionWrapper.length ) { + return {}; + } + + const orderAttributionData = {}; + const orderAttributionInputs = orderAttributionWrapper[ 0 ].children; + for ( let i = 0; i < orderAttributionInputs.length; i++ ) { + orderAttributionData[ orderAttributionInputs[ i ].name ] = + orderAttributionInputs[ i ].value; + } + return orderAttributionData; +}; diff --git a/client/express-checkout/utils/normalize.js b/client/express-checkout/utils/normalize.js index 9519a2416..dfd78c38b 100644 --- a/client/express-checkout/utils/normalize.js +++ b/client/express-checkout/utils/normalize.js @@ -1,3 +1,5 @@ +import { extractOrderAttributionData } from 'wcstripe/blocks/utils'; + /** * Normalizes incoming cart total items for use as a displayItems with the Stripe api. * @@ -73,28 +75,6 @@ export const normalizeOrderData = ( event, paymentMethodId ) => { }; }; -/** - * Get order attribution data from the hidden inputs. - * - * @return {Object} Order attribution data. - */ -export const extractOrderAttributionData = () => { - const orderAttributionWrapper = document.getElementsByTagName( - 'wc-order-attribution-inputs' - ); - if ( ! orderAttributionWrapper.length ) { - return {}; - } - - const orderAttributionData = {}; - const orderAttributionInputs = orderAttributionWrapper[ 0 ].children; - for ( let i = 0; i < orderAttributionInputs.length; i++ ) { - orderAttributionData[ orderAttributionInputs[ i ].name ] = - orderAttributionInputs[ i ].value; - } - return orderAttributionData; -}; - /** * Normalize Pay for Order data from Stripe's object to the expected format for WC. * From c945fe2f5607e88700529ab3e29a53ca71ac9db1 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 28 Nov 2024 18:29:46 -0300 Subject: [PATCH 09/19] Moving order attribution inputs creation to a shared trait --- client/blocks/upe/index.js | 16 ---------------- ...ass-wc-stripe-express-checkout-element.php | 12 +++--------- .../class-wc-stripe-payment-request.php | 8 ++++++++ ...wc-stripe-add-order-attribution-inputs.php | 19 +++++++++++++++++++ woocommerce-gateway-stripe.php | 1 + 5 files changed, 31 insertions(+), 25 deletions(-) create mode 100644 includes/payment-methods/trait-wc-stripe-add-order-attribution-inputs.php diff --git a/client/blocks/upe/index.js b/client/blocks/upe/index.js index fc9cface3..56a1b2e22 100644 --- a/client/blocks/upe/index.js +++ b/client/blocks/upe/index.js @@ -104,19 +104,6 @@ if ( getBlocksConfiguration()?.isECEEnabled ) { registerExpressPaymentMethod( paymentRequestPaymentMethod ); } -const addOrderAttributionInputsIfNotExists = () => { - const elementId = 'wc-stripe-express-checkout__order-attribution-inputs'; - if ( document.getElementById( elementId ) ) { - return; - } - - const orderAttributionInputs = document.createElement( - 'wc-order-attribution-inputs' - ); - orderAttributionInputs.id = elementId; - document.body.appendChild( orderAttributionInputs ); -}; - const populateOrderAttributionInputs = () => { const orderAttribution = window?.wc_order_attribution; if ( orderAttribution ) { @@ -129,8 +116,5 @@ const populateOrderAttributionInputs = () => { // Update token labels when the checkout form is loaded. updateTokenLabelsWhenLoaded(); -// Add order attribution inputs to the page. -addOrderAttributionInputsIfNotExists(); - // Populate order attribution inputs with order tracking data. populateOrderAttributionInputs(); diff --git a/includes/payment-methods/class-wc-stripe-express-checkout-element.php b/includes/payment-methods/class-wc-stripe-express-checkout-element.php index 652942334..eff0a164b 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-element.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-element.php @@ -14,6 +14,9 @@ * WC_Stripe_Express_Checkout_Element class. */ class WC_Stripe_Express_Checkout_Element { + + use WC_Stripe_Add_Order_Attribution_Inputs; + /** * Stripe settings. * @@ -430,15 +433,6 @@ public function display_express_checkout_button_html() { $this->display_express_checkout_button_separator_html(); } - /** - * Add order attribution inputs to the page. - * - * @return void - */ - public function add_order_attribution_inputs() { - echo ''; - } - /** * Display express checkout button separator. */ diff --git a/includes/payment-methods/class-wc-stripe-payment-request.php b/includes/payment-methods/class-wc-stripe-payment-request.php index 9d503193e..484a347d1 100644 --- a/includes/payment-methods/class-wc-stripe-payment-request.php +++ b/includes/payment-methods/class-wc-stripe-payment-request.php @@ -18,6 +18,7 @@ class WC_Stripe_Payment_Request { use WC_Stripe_Pre_Orders_Trait; + use WC_Stripe_Add_Order_Attribution_Inputs; /** * Enabled. @@ -901,6 +902,13 @@ public function display_payment_request_button_html() { add_order_attribution_inputs(); + } + $this->display_payment_request_button_separator_html(); } diff --git a/includes/payment-methods/trait-wc-stripe-add-order-attribution-inputs.php b/includes/payment-methods/trait-wc-stripe-add-order-attribution-inputs.php new file mode 100644 index 000000000..a7d6df1a4 --- /dev/null +++ b/includes/payment-methods/trait-wc-stripe-add-order-attribution-inputs.php @@ -0,0 +1,19 @@ +'; + } +} diff --git a/woocommerce-gateway-stripe.php b/woocommerce-gateway-stripe.php index 933a8fae0..17a2bcf87 100644 --- a/woocommerce-gateway-stripe.php +++ b/woocommerce-gateway-stripe.php @@ -205,6 +205,7 @@ public function init() { require_once dirname( __FILE__ ) . '/includes/compat/class-wc-stripe-subscriptions-legacy-sepa-token-update.php'; require_once dirname( __FILE__ ) . '/includes/abstracts/abstract-wc-stripe-payment-gateway.php'; require_once dirname( __FILE__ ) . '/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php'; + require_once dirname( __FILE__ ) . '/includes/trait-wc-stripe-add-order-attribution-inputs.php'; require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-action-scheduler-service.php'; require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-webhook-state.php'; require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-webhook-handler.php'; From 3594784f58fc0c4cfa0476761155b3812a055117 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 28 Nov 2024 18:30:54 -0300 Subject: [PATCH 10/19] Fix include --- woocommerce-gateway-stripe.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/woocommerce-gateway-stripe.php b/woocommerce-gateway-stripe.php index 17a2bcf87..2ea0a67ed 100644 --- a/woocommerce-gateway-stripe.php +++ b/woocommerce-gateway-stripe.php @@ -205,7 +205,6 @@ public function init() { require_once dirname( __FILE__ ) . '/includes/compat/class-wc-stripe-subscriptions-legacy-sepa-token-update.php'; require_once dirname( __FILE__ ) . '/includes/abstracts/abstract-wc-stripe-payment-gateway.php'; require_once dirname( __FILE__ ) . '/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php'; - require_once dirname( __FILE__ ) . '/includes/trait-wc-stripe-add-order-attribution-inputs.php'; require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-action-scheduler-service.php'; require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-webhook-state.php'; require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-webhook-handler.php'; @@ -216,6 +215,7 @@ public function init() { require_once dirname( __FILE__ ) . '/includes/class-wc-gateway-stripe.php'; require_once dirname( __FILE__ ) . '/includes/constants/class-wc-stripe-currency-code.php'; require_once dirname( __FILE__ ) . '/includes/constants/class-wc-stripe-payment-methods.php'; + require_once dirname( __FILE__ ) . '/includes/payment-methods/trait-wc-stripe-add-order-attribution-inputs.php'; require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php'; require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method.php'; require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method-cc.php'; From 99ff71660740c6af6cafc0a881feb960a3e6f3f2 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 28 Nov 2024 19:14:57 -0300 Subject: [PATCH 11/19] Fix data not being sent for shortcode PRB --- assets/js/stripe-payment-request.js | 20 ++++++++++++++++++- .../class-wc-stripe-payment-request.php | 7 ------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/assets/js/stripe-payment-request.js b/assets/js/stripe-payment-request.js index f0e048c33..e016e38af 100644 --- a/assets/js/stripe-payment-request.js +++ b/assets/js/stripe-payment-request.js @@ -144,7 +144,7 @@ jQuery( function( $ ) { data = wc_stripe_payment_request.getRequiredFieldDataFromCheckoutForm( data ); - return data; + return { ...data, ...wc_stripe_payment_request.extractOrderAttributionData() }; }, /** @@ -833,6 +833,24 @@ jQuery( function( $ ) { } ); }, + /** + * Get order attribution data from the hidden inputs. + * + * @return {Object} Order attribution data. + */ + extractOrderAttributionData: function() { + const $orderAttributionWrapper = $( 'wc-order-attribution-inputs' ); + if ( ! $orderAttributionWrapper.length ) { + return {}; + } + + const orderAttributionData = {}; + $orderAttributionWrapper.children( 'input' ).each( function () { + orderAttributionData[ $(this).attr( 'name' ) ] = $(this).val(); + }); + return orderAttributionData; + }, + showPaymentRequestButton: function( prButton ) { if ( wc_stripe_payment_request.isCustomPaymentRequestButton( prButton ) ) { prButton.addClass( 'is-active' ); diff --git a/includes/payment-methods/class-wc-stripe-payment-request.php b/includes/payment-methods/class-wc-stripe-payment-request.php index 484a347d1..14125131b 100644 --- a/includes/payment-methods/class-wc-stripe-payment-request.php +++ b/includes/payment-methods/class-wc-stripe-payment-request.php @@ -18,7 +18,6 @@ class WC_Stripe_Payment_Request { use WC_Stripe_Pre_Orders_Trait; - use WC_Stripe_Add_Order_Attribution_Inputs; /** * Enabled. @@ -903,12 +902,6 @@ public function display_payment_request_button_html() { add_order_attribution_inputs(); - } - $this->display_payment_request_button_separator_html(); } From 2a2adb4a19a65a7bd7345da8a99113bffc933d95 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 28 Nov 2024 19:17:06 -0300 Subject: [PATCH 12/19] Removing unnecessary trait --- ...ass-wc-stripe-express-checkout-element.php | 11 +++++++++-- ...wc-stripe-add-order-attribution-inputs.php | 19 ------------------- woocommerce-gateway-stripe.php | 1 - 3 files changed, 9 insertions(+), 22 deletions(-) delete mode 100644 includes/payment-methods/trait-wc-stripe-add-order-attribution-inputs.php diff --git a/includes/payment-methods/class-wc-stripe-express-checkout-element.php b/includes/payment-methods/class-wc-stripe-express-checkout-element.php index eff0a164b..d000bec1a 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-element.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-element.php @@ -15,8 +15,6 @@ */ class WC_Stripe_Express_Checkout_Element { - use WC_Stripe_Add_Order_Attribution_Inputs; - /** * Stripe settings. * @@ -433,6 +431,15 @@ public function display_express_checkout_button_html() { $this->display_express_checkout_button_separator_html(); } + /** + * Add order attribution inputs to the page. + * + * @return void + */ + public function add_order_attribution_inputs() { + echo ''; + } + /** * Display express checkout button separator. */ diff --git a/includes/payment-methods/trait-wc-stripe-add-order-attribution-inputs.php b/includes/payment-methods/trait-wc-stripe-add-order-attribution-inputs.php deleted file mode 100644 index a7d6df1a4..000000000 --- a/includes/payment-methods/trait-wc-stripe-add-order-attribution-inputs.php +++ /dev/null @@ -1,19 +0,0 @@ -'; - } -} diff --git a/woocommerce-gateway-stripe.php b/woocommerce-gateway-stripe.php index 2ea0a67ed..933a8fae0 100644 --- a/woocommerce-gateway-stripe.php +++ b/woocommerce-gateway-stripe.php @@ -215,7 +215,6 @@ public function init() { require_once dirname( __FILE__ ) . '/includes/class-wc-gateway-stripe.php'; require_once dirname( __FILE__ ) . '/includes/constants/class-wc-stripe-currency-code.php'; require_once dirname( __FILE__ ) . '/includes/constants/class-wc-stripe-payment-methods.php'; - require_once dirname( __FILE__ ) . '/includes/payment-methods/trait-wc-stripe-add-order-attribution-inputs.php'; require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php'; require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method.php'; require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method-cc.php'; From 711d7e884d43aa8a9752f9573635719ab7516575 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 29 Nov 2024 08:35:42 -0300 Subject: [PATCH 13/19] Changelog and readme entries --- changelog.txt | 1 + readme.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/changelog.txt b/changelog.txt index f616171ca..240025513 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ *** Changelog *** = 9.0.0 - xxxx-xx-xx = +* Fix - Fix order attribution metadata not included in PRBs or Express Checkout Element. * Add - Pre-fill user email and phone number for Link in the Payment Element. * Remove - Remove Link autofill modal feature. * Update - Improve accuracy of webhook status information displayed in settings page. diff --git a/readme.txt b/readme.txt index 0fbb01f16..2c7241575 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.0.0 - xxxx-xx-xx = +* Fix - Fix order attribution metadata not included in PRBs or Express Checkout Element. * Add - Pre-fill user email and phone number for Link in the Payment Element. * Remove - Remove Link autofill modal feature. * Update - Improve accuracy of webhook status information displayed in settings page. From 1704171be0e3356094a8666ffa4f9cc69a62387b Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 29 Nov 2024 08:41:00 -0300 Subject: [PATCH 14/19] Reverting unnecessary changes --- .../payment-methods/class-wc-stripe-express-checkout-element.php | 1 - includes/payment-methods/class-wc-stripe-payment-request.php | 1 - 2 files changed, 2 deletions(-) diff --git a/includes/payment-methods/class-wc-stripe-express-checkout-element.php b/includes/payment-methods/class-wc-stripe-express-checkout-element.php index d000bec1a..652942334 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-element.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-element.php @@ -14,7 +14,6 @@ * WC_Stripe_Express_Checkout_Element class. */ class WC_Stripe_Express_Checkout_Element { - /** * Stripe settings. * diff --git a/includes/payment-methods/class-wc-stripe-payment-request.php b/includes/payment-methods/class-wc-stripe-payment-request.php index 14125131b..9d503193e 100644 --- a/includes/payment-methods/class-wc-stripe-payment-request.php +++ b/includes/payment-methods/class-wc-stripe-payment-request.php @@ -901,7 +901,6 @@ public function display_payment_request_button_html() { display_payment_request_button_separator_html(); } From e76a7643d42982a0ad595c99461ba56f2675a858 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 29 Nov 2024 08:43:02 -0300 Subject: [PATCH 15/19] Reverting unnecessary changes --- client/express-checkout/utils/normalize.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/client/express-checkout/utils/normalize.js b/client/express-checkout/utils/normalize.js index dfd78c38b..0101e664a 100644 --- a/client/express-checkout/utils/normalize.js +++ b/client/express-checkout/utils/normalize.js @@ -34,8 +34,11 @@ export const normalizeOrderData = ( event, paymentMethodId ) => { const email = event?.billingDetails?.email ?? ''; const billing = event?.billingDetails?.address ?? {}; const shipping = event?.shippingAddress ?? {}; - const phoneField = event?.billingDetails?.phone ?? event?.payerPhone ?? ''; - const phone = phoneField.replace( /[() -]/g, '' ); + + const phone = + event?.billingDetails?.phone?.replace( /[() -]/g, '' ) ?? + event?.payerPhone?.replace( /[() -]/g, '' ) ?? + ''; return { billing_first_name: From fcf32a9143a64a9039067bb91153f53c59d65d49 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 29 Nov 2024 09:25:05 -0300 Subject: [PATCH 16/19] Adding specific unit tests --- .../populate-order-attribution-inputs.test.js | 20 +++++++++++++++++++ client/blocks/upe/index.js | 10 +--------- .../upe/populate-order-attribution-inputs.js | 13 ++++++++++++ 3 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 client/blocks/upe/__tests__/populate-order-attribution-inputs.test.js create mode 100644 client/blocks/upe/populate-order-attribution-inputs.js diff --git a/client/blocks/upe/__tests__/populate-order-attribution-inputs.test.js b/client/blocks/upe/__tests__/populate-order-attribution-inputs.test.js new file mode 100644 index 000000000..b59e3a5bc --- /dev/null +++ b/client/blocks/upe/__tests__/populate-order-attribution-inputs.test.js @@ -0,0 +1,20 @@ +import { populateOrderAttributionInputs } from 'wcstripe/blocks/upe/populate-order-attribution-inputs'; + +describe( 'Unified Payment Element (Blocks)', () => { + describe( 'populateOrderAttributionInputs', () => { + test( 'order attribution global present', () => { + global.wc_order_attribution = { + params: { + allowTracking: true, + }, + setOrderTracking: jest.fn(), + }; + + populateOrderAttributionInputs(); + + expect( + global.wc_order_attribution.setOrderTracking + ).toHaveBeenCalledWith( true ); + } ); + } ); +} ); diff --git a/client/blocks/upe/index.js b/client/blocks/upe/index.js index 56a1b2e22..6d7b010b7 100644 --- a/client/blocks/upe/index.js +++ b/client/blocks/upe/index.js @@ -16,6 +16,7 @@ import { import WCStripeAPI from 'wcstripe/api'; import { getBlocksConfiguration } from 'wcstripe/blocks/utils'; import './styles.scss'; +import { populateOrderAttributionInputs } from 'wcstripe/blocks/upe/populate-order-attribution-inputs'; const api = new WCStripeAPI( getBlocksConfiguration(), @@ -104,15 +105,6 @@ if ( getBlocksConfiguration()?.isECEEnabled ) { registerExpressPaymentMethod( paymentRequestPaymentMethod ); } -const populateOrderAttributionInputs = () => { - const orderAttribution = window?.wc_order_attribution; - if ( orderAttribution ) { - orderAttribution.setOrderTracking( - orderAttribution.params.allowTracking - ); - } -}; - // Update token labels when the checkout form is loaded. updateTokenLabelsWhenLoaded(); diff --git a/client/blocks/upe/populate-order-attribution-inputs.js b/client/blocks/upe/populate-order-attribution-inputs.js new file mode 100644 index 000000000..26d6ce677 --- /dev/null +++ b/client/blocks/upe/populate-order-attribution-inputs.js @@ -0,0 +1,13 @@ +/** + * Populate order attribution inputs with order tracking data. + * + * @return {void} + */ +export const populateOrderAttributionInputs = () => { + const orderAttribution = window?.wc_order_attribution; + if ( orderAttribution ) { + orderAttribution.setOrderTracking( + orderAttribution.params.allowTracking + ); + } +}; From 0b3d3f49307c0e4a133d272d1ce20168d98d04f9 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 29 Nov 2024 09:37:18 -0300 Subject: [PATCH 17/19] Adding specific unit tests --- client/blocks/upe/__tests__/utils.test.js | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 client/blocks/upe/__tests__/utils.test.js diff --git a/client/blocks/upe/__tests__/utils.test.js b/client/blocks/upe/__tests__/utils.test.js new file mode 100644 index 000000000..3bcb0d1ae --- /dev/null +++ b/client/blocks/upe/__tests__/utils.test.js @@ -0,0 +1,26 @@ +import { render } from '@testing-library/react'; +import { extractOrderAttributionData } from 'wcstripe/blocks/utils'; + +describe( 'Blocks Utils', () => { + describe( 'extractOrderAttributionData', () => { + it( 'order attribution wrapper not found', () => { + const data = extractOrderAttributionData(); + expect( data ).toStrictEqual( {} ); + } ); + + it( 'order attribution wrapper exists', () => { + render( + + + + + ); + + const data = extractOrderAttributionData(); + expect( data ).toStrictEqual( { + foo: 'bar', + baz: 'qux', + } ); + } ); + } ); +} ); From 8ffc3e309851497c5a46d470b0610edcc7da143f Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 29 Nov 2024 09:45:30 -0300 Subject: [PATCH 18/19] Adding specific unit tests --- ...est-wc-stripe-express-checkout-element.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/phpunit/test-wc-stripe-express-checkout-element.php b/tests/phpunit/test-wc-stripe-express-checkout-element.php index 2b14f3e8d..2e83e120a 100644 --- a/tests/phpunit/test-wc-stripe-express-checkout-element.php +++ b/tests/phpunit/test-wc-stripe-express-checkout-element.php @@ -362,4 +362,26 @@ public function provide_test_display_express_checkout_button_separator_html() { ], ]; } + + /** + * Test for `add_order_attribution_data`. + * + * @return void + */ + public function test_add_order_attribution_data() { + $ajax_handler = $this->getMockBuilder( WC_Stripe_Express_Checkout_Ajax_Handler::class ) + ->disableOriginalConstructor() + ->getMock(); + + $helper = $this->getMockBuilder( WC_Stripe_Express_Checkout_Helper::class ) + ->disableOriginalConstructor() + ->getMock(); + + $element = new WC_Stripe_Express_Checkout_Element( $ajax_handler, $helper ); + + ob_start(); + $element->add_order_attribution_inputs(); + $output = ob_get_clean(); + $this->assertStringMatchesFormat( '%aid="wc-stripe-express-checkout__order-attribution-inputs"%a', $output ); + } } From 3d7de19d371b308fc714014297ae210fcd0c2eab Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Mon, 2 Dec 2024 11:10:31 -0300 Subject: [PATCH 19/19] Fix block checkout attribution --- .../blocks/{upe => }/__tests__/utils.test.js | 22 ++++++++++++- .../populate-order-attribution-inputs.test.js | 20 ------------ client/blocks/upe/index.js | 10 ++++-- .../upe/populate-order-attribution-inputs.js | 13 -------- client/blocks/utils.js | 32 +++++++++++++++++++ 5 files changed, 61 insertions(+), 36 deletions(-) rename client/blocks/{upe => }/__tests__/utils.test.js (57%) delete mode 100644 client/blocks/upe/__tests__/populate-order-attribution-inputs.test.js delete mode 100644 client/blocks/upe/populate-order-attribution-inputs.js diff --git a/client/blocks/upe/__tests__/utils.test.js b/client/blocks/__tests__/utils.test.js similarity index 57% rename from client/blocks/upe/__tests__/utils.test.js rename to client/blocks/__tests__/utils.test.js index 3bcb0d1ae..4d796866a 100644 --- a/client/blocks/upe/__tests__/utils.test.js +++ b/client/blocks/__tests__/utils.test.js @@ -1,5 +1,8 @@ import { render } from '@testing-library/react'; -import { extractOrderAttributionData } from 'wcstripe/blocks/utils'; +import { + extractOrderAttributionData, + populateOrderAttributionInputs, +} from 'wcstripe/blocks/utils'; describe( 'Blocks Utils', () => { describe( 'extractOrderAttributionData', () => { @@ -23,4 +26,21 @@ describe( 'Blocks Utils', () => { } ); } ); } ); + + describe( 'populateOrderAttributionInputs', () => { + test( 'order attribution global present', () => { + global.wc_order_attribution = { + params: { + allowTracking: true, + }, + setOrderTracking: jest.fn(), + }; + + populateOrderAttributionInputs(); + + expect( + global.wc_order_attribution.setOrderTracking + ).toHaveBeenCalledWith( true ); + } ); + } ); } ); diff --git a/client/blocks/upe/__tests__/populate-order-attribution-inputs.test.js b/client/blocks/upe/__tests__/populate-order-attribution-inputs.test.js deleted file mode 100644 index b59e3a5bc..000000000 --- a/client/blocks/upe/__tests__/populate-order-attribution-inputs.test.js +++ /dev/null @@ -1,20 +0,0 @@ -import { populateOrderAttributionInputs } from 'wcstripe/blocks/upe/populate-order-attribution-inputs'; - -describe( 'Unified Payment Element (Blocks)', () => { - describe( 'populateOrderAttributionInputs', () => { - test( 'order attribution global present', () => { - global.wc_order_attribution = { - params: { - allowTracking: true, - }, - setOrderTracking: jest.fn(), - }; - - populateOrderAttributionInputs(); - - expect( - global.wc_order_attribution.setOrderTracking - ).toHaveBeenCalledWith( true ); - } ); - } ); -} ); diff --git a/client/blocks/upe/index.js b/client/blocks/upe/index.js index 6d7b010b7..4f4c40c2c 100644 --- a/client/blocks/upe/index.js +++ b/client/blocks/upe/index.js @@ -14,9 +14,12 @@ import { expressCheckoutElementsStripeLink, } from 'wcstripe/blocks/express-checkout'; import WCStripeAPI from 'wcstripe/api'; -import { getBlocksConfiguration } from 'wcstripe/blocks/utils'; +import { + addOrderAttributionInputsIfNotExists, + getBlocksConfiguration, + populateOrderAttributionInputs, +} from 'wcstripe/blocks/utils'; import './styles.scss'; -import { populateOrderAttributionInputs } from 'wcstripe/blocks/upe/populate-order-attribution-inputs'; const api = new WCStripeAPI( getBlocksConfiguration(), @@ -108,5 +111,8 @@ if ( getBlocksConfiguration()?.isECEEnabled ) { // Update token labels when the checkout form is loaded. updateTokenLabelsWhenLoaded(); +// Add order attribution inputs to the page. +addOrderAttributionInputsIfNotExists(); + // Populate order attribution inputs with order tracking data. populateOrderAttributionInputs(); diff --git a/client/blocks/upe/populate-order-attribution-inputs.js b/client/blocks/upe/populate-order-attribution-inputs.js deleted file mode 100644 index 26d6ce677..000000000 --- a/client/blocks/upe/populate-order-attribution-inputs.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Populate order attribution inputs with order tracking data. - * - * @return {void} - */ -export const populateOrderAttributionInputs = () => { - const orderAttribution = window?.wc_order_attribution; - if ( orderAttribution ) { - orderAttribution.setOrderTracking( - orderAttribution.params.allowTracking - ); - } -}; diff --git a/client/blocks/utils.js b/client/blocks/utils.js index bbdf9f7ad..0b0833927 100644 --- a/client/blocks/utils.js +++ b/client/blocks/utils.js @@ -106,3 +106,35 @@ export const extractOrderAttributionData = () => { } return orderAttributionData; }; + +/** + * Populate order attribution inputs with order tracking data. + * + * @return {void} + */ +export const populateOrderAttributionInputs = () => { + const orderAttribution = window?.wc_order_attribution; + if ( orderAttribution ) { + orderAttribution.setOrderTracking( + orderAttribution.params.allowTracking + ); + } +}; + +/** + * Add order attribution inputs to the page. + * + * @return {void} + */ +export const addOrderAttributionInputsIfNotExists = () => { + const elementId = 'wc-stripe-express-checkout__order-attribution-inputs'; + if ( document.getElementById( elementId ) ) { + return; + } + + const orderAttributionInputs = document.createElement( + 'wc-order-attribution-inputs' + ); + orderAttributionInputs.id = elementId; + document.body.appendChild( orderAttributionInputs ); +};