Skip to content
Closed
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
186 changes: 186 additions & 0 deletions tests/phpunit/WC_Stripe_Customer_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,190 @@ public function test_validate_create_customer_request(
$this->assertFalse( $was_exception_thrown, 'No exception was thrown when no exception was expected' );
}
}

/**
* Test that update_or_create_customer retrieves billing details from the order object
* when called in the context of get_customer_id_for_order().
*/
public function test_update_or_create_customer_retrieves_billing_details_from_order() {
$billing_data = [
'email' => 'order-test@example.com',
'first_name' => 'Order',
'last_name' => 'Tester',
'address_1' => '456 Order St',
'city' => 'Order City',
'state' => 'NY',
'postcode' => '54321',
'country' => 'US',
];

// Create mock order with specific billing data.
$mock_order = $this->getMockBuilder( \WC_Order::class )
->disableOriginalConstructor()
->onlyMethods(
[
'get_billing_address_1',
'get_billing_address_2',
'get_billing_city',
'get_billing_country',
'get_billing_email',
'get_billing_first_name',
'get_billing_last_name',
'get_billing_postcode',
'get_billing_state',
]
)
->getMock();

$mock_order->method( 'get_billing_address_1' )->willReturn( $billing_data['address_1'] );
$mock_order->method( 'get_billing_address_2' )->willReturn( '' );
$mock_order->method( 'get_billing_city' )->willReturn( $billing_data['city'] );
$mock_order->method( 'get_billing_country' )->willReturn( $billing_data['country'] );
$mock_order->method( 'get_billing_email' )->willReturn( $billing_data['email'] );
$mock_order->method( 'get_billing_first_name' )->willReturn( $billing_data['first_name'] );
$mock_order->method( 'get_billing_last_name' )->willReturn( $billing_data['last_name'] );
$mock_order->method( 'get_billing_postcode' )->willReturn( $billing_data['postcode'] );
$mock_order->method( 'get_billing_state' )->willReturn( $billing_data['state'] );

// Capture the API request arguments.
$captured_request_body = null;

$mock_customer_search_call = function ( $return_value, $parsed_args, $url ) {
if ( ! str_starts_with( $url, 'https://api.stripe.com/v1/customers/search' ) ) {
return $return_value;
}

return [
'response' => 200,
'headers' => [ 'Content-Type' => 'application/json' ],
'body' => json_encode(
[
'data' => [],
]
),
];
};
add_filter( 'pre_http_request', $mock_customer_search_call, 10, 3 );

$mock_create_customer_call = function ( $return_value, $parsed_args, $url ) use ( &$captured_request_body ) {
if ( 'https://api.stripe.com/v1/customers' !== $url ) {
return $return_value;
}

// Capture the request body to verify billing details.
$captured_request_body = $parsed_args['body'];

return [
'response' => 200,
'headers' => [ 'Content-Type' => 'application/json' ],
'body' => json_encode(
[
'id' => 'cus_test_123',
]
),
];
};
add_filter( 'pre_http_request', $mock_create_customer_call, 10, 3 );

$customer = new \WC_Stripe_Customer();

try {
// Call update_or_create_customer with order object (simulating get_customer_id_for_order context).
$customer_id = $customer->update_or_create_customer( [], null, $mock_order );

// Verify customer was created successfully.
$this->assertEquals( 'cus_test_123', $customer_id );

// Verify billing details were correctly retrieved from the order.
$this->assertNotNull( $captured_request_body, 'Customer create request was not captured' );
$this->assertStringContainsString( 'email=' . urlencode( $billing_data['email'] ), $captured_request_body );
$this->assertStringContainsString( 'name=' . urlencode( $billing_data['first_name'] . ' ' . $billing_data['last_name'] ), $captured_request_body );
$this->assertStringContainsString( urlencode( $billing_data['address_1'] ), $captured_request_body );
$this->assertStringContainsString( urlencode( $billing_data['city'] ), $captured_request_body );
$this->assertStringContainsString( urlencode( $billing_data['state'] ), $captured_request_body );
$this->assertStringContainsString( urlencode( $billing_data['postcode'] ), $captured_request_body );
$this->assertStringContainsString( urlencode( $billing_data['country'] ), $captured_request_body );
} finally {
remove_filter( 'pre_http_request', $mock_customer_search_call, 10 );
remove_filter( 'pre_http_request', $mock_create_customer_call, 10 );
}
}

/**
* Test that update_or_create_customer with pay_for_order context only requires email.
*/
public function test_update_or_create_customer_with_pay_for_order_context() {
$billing_data = [
'email' => 'payfororder@example.com',
];

// Create mock order with minimal billing data (only email).
$mock_order = $this->getMockBuilder( \WC_Order::class )
->disableOriginalConstructor()
->onlyMethods(
[
'get_billing_address_1',
'get_billing_address_2',
'get_billing_city',
'get_billing_country',
'get_billing_email',
'get_billing_first_name',
'get_billing_last_name',
'get_billing_postcode',
'get_billing_state',
]
)
->getMock();

$mock_order->method( 'get_billing_address_1' )->willReturn( '' );
$mock_order->method( 'get_billing_address_2' )->willReturn( '' );
$mock_order->method( 'get_billing_city' )->willReturn( '' );
$mock_order->method( 'get_billing_country' )->willReturn( '' );
$mock_order->method( 'get_billing_email' )->willReturn( $billing_data['email'] );
$mock_order->method( 'get_billing_first_name' )->willReturn( '' );
$mock_order->method( 'get_billing_last_name' )->willReturn( '' );
$mock_order->method( 'get_billing_postcode' )->willReturn( '' );
$mock_order->method( 'get_billing_state' )->willReturn( '' );

$captured_request_body = null;

$mock_create_customer_call = function ( $return_value, $parsed_args, $url ) use ( &$captured_request_body ) {
if ( 'https://api.stripe.com/v1/customers' !== $url ) {
return $return_value;
}

$captured_request_body = $parsed_args['body'];

return [
'response' => 200,
'headers' => [ 'Content-Type' => 'application/json' ],
'body' => json_encode(
[
'id' => 'cus_pay_for_order_123',
]
),
];
};
add_filter( 'pre_http_request', $mock_create_customer_call, 10, 3 );

$customer = new \WC_Stripe_Customer();

try {
// Call update_or_create_customer with pay_for_order context (simulating get_customer_id_for_order).
$customer_id = $customer->update_or_create_customer(
[],
\WC_Stripe_Customer::CUSTOMER_CONTEXT_PAY_FOR_ORDER,
$mock_order
);

// Verify customer was created successfully with minimal details.
$this->assertEquals( 'cus_pay_for_order_123', $customer_id );

// Verify email was included in the request.
$this->assertNotNull( $captured_request_body, 'Customer create request was not captured' );
$this->assertStringContainsString( 'email=' . urlencode( $billing_data['email'] ), $captured_request_body );
} finally {
remove_filter( 'pre_http_request', $mock_create_customer_call, 10 );
}
}
}