From b42101dffeb328bc28b92c7e705870d101ec9d00 Mon Sep 17 00:00:00 2001 From: frosso Date: Mon, 6 May 2024 18:37:22 +0200 Subject: [PATCH] order attribution values --- .../compatibility/wc-order-attribution.js | 37 +++++++++++++++++++ client/tokenized-payment-request/index.js | 1 + .../payment-request.js | 19 ++++++++-- ...xpress-checkout-button-display-handler.php | 1 + 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 client/tokenized-payment-request/compatibility/wc-order-attribution.js diff --git a/client/tokenized-payment-request/compatibility/wc-order-attribution.js b/client/tokenized-payment-request/compatibility/wc-order-attribution.js new file mode 100644 index 00000000000..a707f8330ab --- /dev/null +++ b/client/tokenized-payment-request/compatibility/wc-order-attribution.js @@ -0,0 +1,37 @@ +/* global jQuery */ + +/** + * External dependencies + */ +import { addFilter } from '@wordpress/hooks'; + +addFilter( + 'wcpay.payment-request.cart-place-order-extension-data', + 'automattic/wcpay/payment-request', + ( extensionData ) => { + const orderAttributionValues = jQuery( + '#wcpay-express-checkout__order-attribution-inputs input' + ); + + if ( ! orderAttributionValues.length ) { + return extensionData; + } + + const orderAttributionData = {}; + orderAttributionValues.each( function () { + const name = jQuery( this ) + .attr( 'name' ) + .replace( 'wc_order_attribution_', '' ); + const value = jQuery( this ).val(); + + if ( name && value ) { + orderAttributionData[ name ] = value; + } + } ); + + return { + ...extensionData, + 'woocommerce/order-attribution': orderAttributionData, + }; + } +); diff --git a/client/tokenized-payment-request/index.js b/client/tokenized-payment-request/index.js index 52de26b3542..c792fcc2184 100644 --- a/client/tokenized-payment-request/index.js +++ b/client/tokenized-payment-request/index.js @@ -12,6 +12,7 @@ import PaymentRequestCartApi from './cart-api'; import WooPaymentsPaymentRequest from './payment-request'; import paymentRequestButtonUi from './button-ui'; import './compatibility/wc-deposits'; +import './compatibility/wc-order-attribution'; import './compatibility/wc-product-variations'; import '../checkout/express-checkout-buttons.scss'; diff --git a/client/tokenized-payment-request/payment-request.js b/client/tokenized-payment-request/payment-request.js index 686e7452a08..292c41e0486 100644 --- a/client/tokenized-payment-request/payment-request.js +++ b/client/tokenized-payment-request/payment-request.js @@ -3,7 +3,12 @@ * External dependencies */ import { __ } from '@wordpress/i18n'; -import { doAction, addAction, removeAction } from '@wordpress/hooks'; +import { + doAction, + addAction, + removeAction, + applyFilters, +} from '@wordpress/hooks'; /** * Internal dependencies @@ -281,9 +286,15 @@ export default class WooPaymentsPaymentRequest { paymentRequest.on( 'paymentmethod', async ( event ) => { // TODO: this works for PDPs - need to handle checkout scenarios for pay-for-order, cart, checkout. try { - const response = await _self.paymentRequestCartApi.placeOrder( - transformStripePaymentMethodForStoreApi( event ) - ); + const response = await _self.paymentRequestCartApi.placeOrder( { + // adding extension data as a separate action, + // so that we make it harder for external plugins to modify or intercept checkout data. + ...transformStripePaymentMethodForStoreApi( event ), + extensions: applyFilters( + 'wcpay.payment-request.cart-place-order-extension-data', + {} + ), + } ); const confirmationRequest = _self.wcpayApi.confirmIntent( response.payment_result.redirect_url diff --git a/includes/express-checkout/class-wc-payments-express-checkout-button-display-handler.php b/includes/express-checkout/class-wc-payments-express-checkout-button-display-handler.php index acc9ffd3c44..e1affafd506 100644 --- a/includes/express-checkout/class-wc-payments-express-checkout-button-display-handler.php +++ b/includes/express-checkout/class-wc-payments-express-checkout-button-display-handler.php @@ -118,6 +118,7 @@ public function display_express_checkout_buttons() { } $this->payment_request_button_handler->display_payment_request_button_html(); ?> + display_express_checkout_separator_if_necessary( $separator_starts_hidden );