From db5219a084f1614e11f7b85d8fc2f472f8577a8d Mon Sep 17 00:00:00 2001 From: deepakpathania Date: Tue, 11 Feb 2025 13:12:16 +0530 Subject: [PATCH 1/3] Update handling for PR as a country for terminal locations --- ...payments-terminal-locations-controller.php | 6 ++ ...payments-terminal-locations-controller.php | 56 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/includes/admin/class-wc-rest-payments-terminal-locations-controller.php b/includes/admin/class-wc-rest-payments-terminal-locations-controller.php index c0f0f7754ee..7368d3e0c7d 100644 --- a/includes/admin/class-wc-rest-payments-terminal-locations-controller.php +++ b/includes/admin/class-wc-rest-payments-terminal-locations-controller.php @@ -123,6 +123,12 @@ public function get_store_location( $request ) { ] ); + // Special handling for Puerto Rico - treat as US state rather than country. + if ( 'PR' === $location_address['country'] ) { + $location_address['country'] = 'US'; + $location_address['state'] = 'PR'; + } + // If address is not populated, emit an error and specify the URL where this can be done. // See also https://tosbourn.com/list-of-countries-without-a-postcode/ when launching in new countries. $is_address_populated = isset( $location_address['country'], $location_address['city'], $location_address['postal_code'], $location_address['line1'] ); diff --git a/tests/unit/admin/test-class-wc-rest-payments-terminal-locations-controller.php b/tests/unit/admin/test-class-wc-rest-payments-terminal-locations-controller.php index ff2cb975be2..914bb00e647 100644 --- a/tests/unit/admin/test-class-wc-rest-payments-terminal-locations-controller.php +++ b/tests/unit/admin/test-class-wc-rest-payments-terminal-locations-controller.php @@ -427,4 +427,60 @@ public function test_fetching_all_uses_cache_for_existing_locations() { $result = $this->controller->get_all_locations( $request ); $this->assertEquals( [ $this->location ], $result->get_data() ); } + + public function test_handles_puerto_rico_as_us_state() { + delete_transient( Controller::STORE_LOCATIONS_TRANSIENT_KEY ); + + // Set the store location for Puerto Rico. + update_option( 'woocommerce_store_city', 'San Juan' ); + update_option( 'woocommerce_default_country', 'PR' ); + update_option( 'woocommerce_store_address', 'Calle Normandie' ); + update_option( 'woocommerce_store_postcode', '00907' ); + + $request = $this->mock_wcpay_request( Get_Request::class ); + + $request->expects( $this->once() ) + ->method( 'set_api' ) + ->with( WC_Payments_API_Client::TERMINAL_LOCATIONS_API ); + $request->expects( $this->once() ) + ->method( 'format_response' ) + ->willReturn( [] ); + + // Verify that create_terminal_location is called with US as country and PR as state. + $this->mock_api_client + ->expects( $this->once() ) + ->method( 'create_terminal_location' ) + ->with( + $this->location['display_name'], + [ + 'city' => 'San Juan', + 'country' => 'US', + 'line1' => 'Calle Normandie', + 'postal_code' => '00907', + 'state' => 'PR', + ] + ) + ->willReturn( $this->location ); + + // Setup the request. + $request = new WP_REST_Request( + 'GET', + '/wc/v3/payments/terminal/locations' + ); + $request->set_header( 'Content-Type', 'application/json' ); + + // Mock the second request that fetches all locations. + $get_locations_request = $this->mock_wcpay_request( Get_Request::class ); + $get_locations_request->expects( $this->once() ) + ->method( 'set_api' ) + ->with( WC_Payments_API_Client::TERMINAL_LOCATIONS_API ); + $get_locations_request->expects( $this->once() ) + ->method( 'format_response' ) + ->willReturn( [ $this->location ] ); + + $response = $this->controller->get_store_location( $request ); + $this->assertInstanceOf( WP_REST_Response::class, $response ); + $this->assertEquals( $this->location, $response->get_data() ); + $this->assertSame( [ $this->location ], get_transient( Controller::STORE_LOCATIONS_TRANSIENT_KEY ) ); + } } From 396e91fb4bbb88014f4b9cc52ce7dad1f193bb80 Mon Sep 17 00:00:00 2001 From: deepakpathania Date: Tue, 11 Feb 2025 13:13:31 +0530 Subject: [PATCH 2/3] Add changelog --- changelog/update-terminal-controller-PR-country-handling | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog/update-terminal-controller-PR-country-handling diff --git a/changelog/update-terminal-controller-PR-country-handling b/changelog/update-terminal-controller-PR-country-handling new file mode 100644 index 00000000000..591b8ffdc6a --- /dev/null +++ b/changelog/update-terminal-controller-PR-country-handling @@ -0,0 +1,4 @@ +Significance: minor +Type: update + +Update handling of PR as a country in the terminal locations endpoint. From a6fa06657fd7150099e577b0a3fd890a1a90b068 Mon Sep 17 00:00:00 2001 From: deepakpathania Date: Tue, 11 Feb 2025 16:42:28 +0530 Subject: [PATCH 3/3] Update the route in terminal test --- ...est-class-wc-rest-payments-terminal-locations-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/admin/test-class-wc-rest-payments-terminal-locations-controller.php b/tests/unit/admin/test-class-wc-rest-payments-terminal-locations-controller.php index 914bb00e647..39ce70734ef 100644 --- a/tests/unit/admin/test-class-wc-rest-payments-terminal-locations-controller.php +++ b/tests/unit/admin/test-class-wc-rest-payments-terminal-locations-controller.php @@ -465,7 +465,7 @@ public function test_handles_puerto_rico_as_us_state() { // Setup the request. $request = new WP_REST_Request( 'GET', - '/wc/v3/payments/terminal/locations' + '/wc/v3/payments/terminal/locations/store' ); $request->set_header( 'Content-Type', 'application/json' );