Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Enforce valid states from the API (#4844)
Browse files Browse the repository at this point in the history
* Enforce valid states from the API

* Remove paypal from test
  • Loading branch information
mikejolley authored Oct 19, 2021
1 parent 3f824b0 commit 35c5161
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
12 changes: 10 additions & 2 deletions src/StoreApi/Schemas/BillingAddressSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ public function validate_callback( $address, $request, $param ) {
*/
public function get_item_response( $address ) {
if ( ( $address instanceof \WC_Customer || $address instanceof \WC_Order ) ) {
$billing_country = $address->get_billing_country();
$billing_state = $address->get_billing_state();
$valid_states = wc()->countries->get_states( $billing_country );

if ( ! empty( $billing_state ) && count( $valid_states ) && ! in_array( $billing_state, $valid_states, true ) ) {
$billing_state = '';
}

return (object) $this->prepare_html_response(
[
'first_name' => $address->get_billing_first_name(),
Expand All @@ -101,9 +109,9 @@ public function get_item_response( $address ) {
'address_1' => $address->get_billing_address_1(),
'address_2' => $address->get_billing_address_2(),
'city' => $address->get_billing_city(),
'state' => $address->get_billing_state(),
'state' => $billing_state,
'postcode' => $address->get_billing_postcode(),
'country' => $address->get_billing_country(),
'country' => $billing_country,
'email' => $address->get_billing_email(),
'phone' => $address->get_billing_phone(),
]
Expand Down
12 changes: 10 additions & 2 deletions src/StoreApi/Schemas/ShippingAddressSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public function get_item_response( $address ) {
$shipping_phone = $address->get_meta( $address instanceof \WC_Customer ? 'shipping_phone' : '_shipping_phone', true );
}

$shipping_country = $address->get_shipping_country();
$shipping_state = $address->get_shipping_state();
$valid_states = wc()->countries->get_states( $shipping_country );

if ( ! empty( $shipping_state ) && count( $valid_states ) && ! in_array( $shipping_state, $valid_states, true ) ) {
$shipping_state = '';
}

return (object) $this->prepare_html_response(
[
'first_name' => $address->get_shipping_first_name(),
Expand All @@ -50,9 +58,9 @@ public function get_item_response( $address ) {
'address_1' => $address->get_shipping_address_1(),
'address_2' => $address->get_shipping_address_2(),
'city' => $address->get_shipping_city(),
'state' => $address->get_shipping_state(),
'state' => $shipping_state,
'postcode' => $address->get_shipping_postcode(),
'country' => $address->get_shipping_country(),
'country' => $shipping_country,
'phone' => $shipping_phone,
]
);
Expand Down
6 changes: 0 additions & 6 deletions tests/e2e/specs/frontend/checkout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,6 @@ describe( `${ block.name } Block (frontend)`, () => {

await scrollTo( '.wc-block-components-radio-control__input' );

await expect( page ).toClick(
'.wc-block-components-payment-method-label',
{
alt: 'PayPal',
}
);
await expect( page ).toClick(
'.wc-block-components-payment-method-label',
{
Expand Down

0 comments on commit 35c5161

Please sign in to comment.