Skip to content

Commit

Permalink
better header names
Browse files Browse the repository at this point in the history
  • Loading branch information
frosso committed Jul 16, 2024
1 parent 58d6aa6 commit 13a5c71
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
8 changes: 4 additions & 4 deletions client/tokenized-payment-request/cart-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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': '',
};

Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions client/tokenized-payment-request/test/cart-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -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',
Expand All @@ -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( {
Expand All @@ -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( {
Expand Down
4 changes: 2 additions & 2 deletions includes/class-wc-payments-payment-request-button-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 13a5c71

Please sign in to comment.