Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update handling for PR as a country for terminal locations #10349

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/update-terminal-controller-PR-country-handling
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: update

Update handling of PR as a country in the terminal locations endpoint.
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/store'
);
$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 ) );
}
}
Loading