From 13a5c71c5f76ade56fce6058aec6820a8a87e16d Mon Sep 17 00:00:00 2001 From: frosso Date: Tue, 16 Jul 2024 18:04:54 +0200 Subject: [PATCH] better header names --- client/tokenized-payment-request/cart-api.js | 8 ++++---- .../test/cart-api.test.js | 14 +++++++------- ...payments-payment-request-button-handler.php | 4 ++-- ...payments-payment-request-button-handler.php | 18 +++++++++--------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/client/tokenized-payment-request/cart-api.js b/client/tokenized-payment-request/cart-api.js index 5467d5eace4..7f417236c38 100644 --- a/client/tokenized-payment-request/cart-api.js +++ b/client/tokenized-payment-request/cart-api.js @@ -40,7 +40,7 @@ export default class PaymentRequestCartApi { // the Store API nonce, which could later be overwritten. Nonce: getPaymentRequestData( 'nonce' ).tokenized_order_nonce, // needed for validation of address data, etc. - 'X-WooPayments-Express-Payment-Request-Nonce': + 'X-WooPayments-Tokenized-Cart-Nonce': getPaymentRequestData( 'nonce' ).tokenized_cart_nonce || undefined, // necessary to validate any request made to the backend from the PDP. @@ -84,7 +84,7 @@ export default class PaymentRequestCartApi { method: 'POST', path: '/wc/store/v1/checkout', headers: { - 'X-WooPayments-Express-Payment-Request': true, + 'X-WooPayments-Tokenized-Cart': true, ...this.cartRequestHeaders, }, data: paymentData, @@ -111,7 +111,7 @@ export default class PaymentRequestCartApi { */ async createSeparateCart() { this.cartRequestHeaders = { - // sending an empty value, so that the custom session handler is leveraged to create a separate cart. + // sending an empty value w/ the next request, so that the custom session handler is leveraged to create a separate cart. 'X-WooPayments-Tokenized-Cart-Session': '', }; @@ -134,7 +134,7 @@ export default class PaymentRequestCartApi { method: 'POST', path: '/wc/store/v1/cart/update-customer', headers: { - 'X-WooPayments-Express-Payment-Request': true, + 'X-WooPayments-Tokenized-Cart': true, ...this.cartRequestHeaders, }, data: customerData, diff --git a/client/tokenized-payment-request/test/cart-api.test.js b/client/tokenized-payment-request/test/cart-api.test.js index 9972e79af9e..9c83689e19f 100644 --- a/client/tokenized-payment-request/test/cart-api.test.js +++ b/client/tokenized-payment-request/test/cart-api.test.js @@ -26,7 +26,7 @@ describe( 'PaymentRequestCartApi', () => { global.wcpayPaymentRequestParams.button_context = 'product'; const headers = new Headers(); headers.append( - 'X-WooPayments-Express-Payment-Request-Nonce', + 'X-WooPayments-Tokenized-Cart-Nonce', 'tokenized_cart_nonce' ); headers.append( 'Nonce', 'nonce-value' ); @@ -63,8 +63,8 @@ describe( 'PaymentRequestCartApi', () => { ), credentials: 'omit', headers: expect.objectContaining( { - 'X-WooPayments-Express-Payment-Request': true, - 'X-WooPayments-Express-Payment-Request-Nonce': + 'X-WooPayments-Tokenized-Cart': true, + 'X-WooPayments-Tokenized-Cart-Nonce': 'tokenized_cart_nonce', Nonce: 'nonce-value', 'Cart-Token': 'cart-token-value', @@ -88,8 +88,8 @@ describe( 'PaymentRequestCartApi', () => { credentials: 'omit', // in this case, no additional headers should have been submitted. headers: expect.objectContaining( { - 'X-WooPayments-Express-Payment-Request': true, - 'X-WooPayments-Express-Payment-Request-Nonce': + 'X-WooPayments-Tokenized-Cart': true, + 'X-WooPayments-Tokenized-Cart-Nonce': 'global_tokenized_cart_nonce', } ), data: expect.objectContaining( { @@ -115,8 +115,8 @@ describe( 'PaymentRequestCartApi', () => { credentials: undefined, // in this case, no additional headers should have been submitted. headers: expect.objectContaining( { - 'X-WooPayments-Express-Payment-Request': true, - 'X-WooPayments-Express-Payment-Request-Nonce': + 'X-WooPayments-Tokenized-Cart': true, + 'X-WooPayments-Tokenized-Cart-Nonce': 'global_tokenized_cart_nonce', } ), data: expect.objectContaining( { diff --git a/includes/class-wc-payments-payment-request-button-handler.php b/includes/class-wc-payments-payment-request-button-handler.php index a7110156938..21975ba7419 100644 --- a/includes/class-wc-payments-payment-request-button-handler.php +++ b/includes/class-wc-payments-payment-request-button-handler.php @@ -174,12 +174,12 @@ public function tokenized_cart_set_payment_method_type( \WC_Order $order, \WP_RE * @return mixed */ public function tokenized_cart_store_api_address_normalization( $response, $server, $request ) { - if ( 'true' !== $request->get_header( 'X-WooPayments-Express-Payment-Request' ) ) { + if ( 'true' !== $request->get_header( 'X-WooPayments-Tokenized-Cart' ) ) { return $response; } // header added as additional layer of security. - $nonce = $request->get_header( 'X-WooPayments-Express-Payment-Request-Nonce' ); + $nonce = $request->get_header( 'X-WooPayments-Tokenized-Cart-Nonce' ); if ( ! wp_verify_nonce( $nonce, 'woopayments_tokenized_cart_nonce' ) ) { return $response; } diff --git a/tests/unit/test-class-wc-payments-payment-request-button-handler.php b/tests/unit/test-class-wc-payments-payment-request-button-handler.php index 222fff3d742..7a63179fc33 100644 --- a/tests/unit/test-class-wc-payments-payment-request-button-handler.php +++ b/tests/unit/test-class-wc-payments-payment-request-button-handler.php @@ -298,7 +298,7 @@ public function test_tokenized_cart_nonce_overwrite_when_header_is_present() { public function test_tokenized_cart_address_avoid_normalization_when_missing_header() { $request = new WP_REST_Request(); - $request->set_header( 'X-WooPayments-Express-Payment-Request', null ); + $request->set_header( 'X-WooPayments-Tokenized-Cart', null ); $request->set_header( 'Content-Type', 'application/json' ); $request->set_param( 'shipping_address', @@ -317,8 +317,8 @@ public function test_tokenized_cart_address_avoid_normalization_when_missing_hea public function test_tokenized_cart_address_avoid_normalization_when_wrong_nonce() { $request = new WP_REST_Request(); - $request->set_header( 'X-WooPayments-Express-Payment-Request', 'true' ); - $request->set_header( 'X-WooPayments-Express-Payment-Request-Nonce', 'invalid-nonce' ); + $request->set_header( 'X-WooPayments-Tokenized-Cart', 'true' ); + $request->set_header( 'X-WooPayments-Tokenized-Cart-Nonce', 'invalid-nonce' ); $request->set_header( 'Content-Type', 'application/json' ); $request->set_param( 'shipping_address', @@ -337,8 +337,8 @@ public function test_tokenized_cart_address_avoid_normalization_when_wrong_nonce public function test_tokenized_cart_address_state_normalization() { $request = new WP_REST_Request(); - $request->set_header( 'X-WooPayments-Express-Payment-Request', 'true' ); - $request->set_header( 'X-WooPayments-Express-Payment-Request-Nonce', wp_create_nonce( 'woopayments_tokenized_cart_nonce' ) ); + $request->set_header( 'X-WooPayments-Tokenized-Cart', 'true' ); + $request->set_header( 'X-WooPayments-Tokenized-Cart-Nonce', wp_create_nonce( 'woopayments_tokenized_cart_nonce' ) ); $request->set_header( 'Content-Type', 'application/json' ); $request->set_param( 'shipping_address', @@ -367,8 +367,8 @@ public function test_tokenized_cart_address_state_normalization() { public function test_tokenized_cart_address_postcode_normalization() { $request = new WP_REST_Request(); $request->set_route( '/wc/store/v1/cart/update-customer' ); - $request->set_header( 'X-WooPayments-Express-Payment-Request', 'true' ); - $request->set_header( 'X-WooPayments-Express-Payment-Request-Nonce', wp_create_nonce( 'woopayments_tokenized_cart_nonce' ) ); + $request->set_header( 'X-WooPayments-Tokenized-Cart', 'true' ); + $request->set_header( 'X-WooPayments-Tokenized-Cart-Nonce', wp_create_nonce( 'woopayments_tokenized_cart_nonce' ) ); $request->set_header( 'Content-Type', 'application/json' ); $request->set_param( 'shipping_address', @@ -399,8 +399,8 @@ public function test_tokenized_cart_address_postcode_normalization() { public function test_tokenized_cart_avoid_address_postcode_normalization_if_route_incorrect() { $request = new WP_REST_Request(); $request->set_route( '/wc/store/v1/checkout' ); - $request->set_header( 'X-WooPayments-Express-Payment-Request', 'true' ); - $request->set_header( 'X-WooPayments-Express-Payment-Request-Nonce', wp_create_nonce( 'woopayments_tokenized_cart_nonce' ) ); + $request->set_header( 'X-WooPayments-Tokenized-Cart', 'true' ); + $request->set_header( 'X-WooPayments-Tokenized-Cart-Nonce', wp_create_nonce( 'woopayments_tokenized_cart_nonce' ) ); $request->set_header( 'Content-Type', 'application/json' ); $request->set_param( 'shipping_address',