Skip to content

Commit

Permalink
Filtering billing details from order based on checkout fields (#8459)
Browse files Browse the repository at this point in the history
Co-authored-by: Samir Merchant <[email protected]>
  • Loading branch information
gpressutto5 and FangedParakeet authored Mar 26, 2024
1 parent 024f00b commit f284f74
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-subscription-billing-fields
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Fixed billing address error for subscription without some billing details
41 changes: 27 additions & 14 deletions includes/class-wc-payments-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,28 +338,41 @@ public static function map_search_orders_to_charge_ids( $search ) {
}

/**
* Extract the billing details from the WC order
* Extract the billing details from the WC order.
* It only returns the fields that are present in the billing section of the checkout.
*
* @param WC_Order $order Order to extract the billing details from.
*
* @return array
*/
public static function get_billing_details_from_order( $order ) {
$billing_details = [
'address' => [
'city' => $order->get_billing_city(),
'country' => $order->get_billing_country(),
'line1' => $order->get_billing_address_1(),
'line2' => $order->get_billing_address_2(),
'postal_code' => $order->get_billing_postcode(),
'state' => $order->get_billing_state(),
],
'email' => $order->get_billing_email(),
'name' => trim( $order->get_formatted_billing_full_name() ),
'phone' => $order->get_billing_phone(),
$billing_fields = array_keys( WC()->checkout()->get_checkout_fields( 'billing' ) );
$address_field_to_key = [
'billing_city' => 'city',
'billing_country' => 'country',
'billing_address_1' => 'line1',
'billing_address_2' => 'line2',
'billing_postcode' => 'postal_code',
'billing_state' => 'state',
];
$field_to_key = [
'billing_email' => 'email',
'billing_phone' => 'phone',
];
$billing_details = [ 'address' => [] ];
foreach ( $billing_fields as $field ) {
if ( isset( $address_field_to_key[ $field ] ) ) {
$billing_details['address'][ $address_field_to_key[ $field ] ] = $order->{"get_{$field}"}();
} elseif ( isset( $field_to_key[ $field ] ) ) {
$billing_details[ $field_to_key[ $field ] ] = $order->{"get_{$field}"}();
}
}

if ( in_array( 'billing_first_name', $billing_fields, true ) && in_array( 'billing_last_name', $billing_fields, true ) ) {
$billing_details['name'] = trim( $order->get_formatted_billing_full_name() );
}

return array_filter( $billing_details );
return $billing_details;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,16 +340,16 @@ function ( $data ): bool {
'test_mode' => false,
'billing_details' => [
'address' => [
'city' => $order->get_billing_city(),
'country' => $order->get_billing_country(),
'line1' => $order->get_billing_address_1(),
'line2' => $order->get_billing_address_2(),
'postal_code' => $order->get_billing_postcode(),
'city' => $order->get_billing_city(),
'state' => $order->get_billing_state(),
'postal_code' => $order->get_billing_postcode(),
],
'phone' => $order->get_billing_phone(),
'email' => $order->get_billing_email(),
'name' => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
'phone' => $order->get_billing_phone(),
],
]
),
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/test-class-wc-payments-customer-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,37 @@ public function test_update_payment_method_with_billing_details_from_order() {
$this->customer_service->update_payment_method_with_billing_details_from_order( 'pm_mock', $order );
}

public function test_update_payment_method_with_billing_details_from_checkout_fields() {
$fields = wc()->checkout()->checkout_fields;
unset( $fields['billing']['billing_company'] );
unset( $fields['billing']['billing_country'] );
unset( $fields['billing']['billing_address_1'] );
unset( $fields['billing']['billing_address_2'] );
unset( $fields['billing']['billing_city'] );
unset( $fields['billing']['billing_state'] );
unset( $fields['billing']['billing_phone'] );
wc()->checkout()->checkout_fields = $fields;
$this->mock_api_client
->expects( $this->once() )
->method( 'update_payment_method' )
->with(
'pm_mock',
[
'billing_details' => [
'address' => [
'postal_code' => '12345',
],
'email' => '[email protected]',
'name' => 'Jeroen Sormani',
],
]
);

$order = WC_Helper_Order::create_order();

$this->customer_service->update_payment_method_with_billing_details_from_order( 'pm_mock', $order );
}

public function test_get_payment_methods_for_customer_not_throw_resource_missing_code_exception() {
$this->mock_api_client->expects( $this->once() )
->method( 'get_payment_methods' )
Expand Down

0 comments on commit f284f74

Please sign in to comment.