Skip to content

Commit ea01bfa

Browse files
Fix invalid IP address during mandate data creation (#4292)
* Fix invalid IP address during mandate data creation --------- Co-authored-by: daledupreez <[email protected]>
1 parent 0496af5 commit ea01bfa

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Update - Add support for customer order notes and express checkout
2929
* Dev - Minor fix to e2e setup code
3030
* Dev - Make PHP error log from Docker container available in docker/logs/php/error.log
31+
* Fix - Fix invalid IP address error encountered during mandate data creation.
3132

3233
= 9.4.1 - 2025-04-17 =
3334
* Dev - Forces rollback of version 9.4.0.

includes/class-wc-stripe-helper.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,11 +1600,11 @@ public static function is_webhook_url( $url, $webhook_url = '' ) {
16001600
$url_parts = wp_parse_url( $url );
16011601
$webhook_url_parts = wp_parse_url( $webhook_url );
16021602

1603-
$url_host = $url_parts['host'] ?? '';
1604-
$url_path = $url_parts['path'] ?? '';
1605-
$url_query = $url_parts['query'] ?? '';
1606-
$webhook_host = $webhook_url_parts['host'] ?? '';
1607-
$webhook_path = $webhook_url_parts['path'] ?? '';
1603+
$url_host = $url_parts['host'] ?? '';
1604+
$url_path = $url_parts['path'] ?? '';
1605+
$url_query = $url_parts['query'] ?? '';
1606+
$webhook_host = $webhook_url_parts['host'] ?? '';
1607+
$webhook_path = $webhook_url_parts['path'] ?? '';
16081608
$webhook_query = $webhook_url_parts['query'] ?? '';
16091609

16101610
if ( $url_host !== $webhook_host || $url_path !== $webhook_path ) {
@@ -1624,7 +1624,7 @@ public static function is_webhook_url( $url, $webhook_url = '' ) {
16241624
return false;
16251625
}
16261626

1627-
$url_query_parts = [];
1627+
$url_query_parts = [];
16281628
$webhook_query_parts = [];
16291629

16301630
parse_str( $url_query, $url_query_parts );
@@ -1727,6 +1727,14 @@ public static function get_klarna_preferred_locale( $store_locale, $billing_coun
17271727
*/
17281728
public static function add_mandate_data( $request ) {
17291729
$ip_address = WC_Geolocation::get_ip_address();
1730+
1731+
// Handle cases where WC_Geolocation::get_ip_address() returns multiple, comma-separated IP addresses.
1732+
// This will be addressed upstream in WooCommerce 9.9.0 as of (https://github.com/woocommerce/woocommerce/pull/57284).
1733+
// TODO: Remove this block when WooCommerce 9.9.0 is released.
1734+
if ( str_contains( $ip_address, ',' ) ) {
1735+
$ip_address = trim( current( preg_split( '/,/', $ip_address ) ) );
1736+
}
1737+
17301738
self::maybe_log_ip_issues( $ip_address );
17311739

17321740
$request['mandate_data'] = [

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,6 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
138138
* Update - Add support for customer order notes and express checkout
139139
* Dev - Minor fix to e2e setup code
140140
* Dev - Make PHP error log from Docker container available in docker/logs/php/error.log
141+
* Fix - Fix invalid IP address error encountered during mandate data creation.
141142

142143
[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).

tests/phpunit/test-wc-stripe-helper.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,9 @@ public function provide_is_wallet_payment_method(): array {
394394
* @return void
395395
*/
396396
public function test_handle_main_stripe_settings() {
397-
WC_Stripe_Helper::update_main_stripe_settings( [ 'test' => 'test' ] );
397+
WC_Stripe_Helper::update_main_stripe_settings( [ 'test' => 'abc' ] );
398398
$current_settings = WC_Stripe_Helper::get_stripe_settings();
399-
$this->assertSame( [ 'test' => 'test' ], $current_settings );
399+
$this->assertSame( $current_settings['test'], 'abc' );
400400

401401
WC_Stripe_Helper::delete_main_stripe_settings();
402402
$current_settings = WC_Stripe_Helper::get_stripe_settings();
@@ -507,4 +507,43 @@ public function test_add_stripe_methods_in_woocommerce_gateway_order() {
507507
$this->assertTrue( $gateway_order['stripe'] < $gateway_order['stripe_affirm'] );
508508
$this->assertTrue( $gateway_order['stripe_affirm'] < $gateway_order['cheque'] );
509509
}
510+
511+
/**
512+
* Test for `add_mandate_data`.
513+
*
514+
* @param string $server_variable_key The key of the server variable to set.
515+
* @param string $server_variable_value The value to set the server variable to.
516+
* @param string $expected_ip_address The expected IP address.
517+
* @dataProvider provider_test_add_mandate_data
518+
* @return void
519+
*/
520+
public function test_add_mandate_data( $server_variable_key, $server_variable_value, $expected_ip_address ) {
521+
unset( $_SERVER['REMOTE_ADDR'] );
522+
unset( $_SERVER['HTTP_X_REAL_IP'] );
523+
unset( $_SERVER['HTTP_X_FORWARDED_FOR'] );
524+
525+
$_SERVER[ $server_variable_key ] = $server_variable_value;
526+
$request = WC_Stripe_Helper::add_mandate_data( [] );
527+
$this->assertTrue( isset( $request['mandate_data']['customer_acceptance']['online']['ip_address'] ) );
528+
$ip_address = $request['mandate_data']['customer_acceptance']['online']['ip_address'];
529+
$this->assertSame( $expected_ip_address, $ip_address );
530+
}
531+
532+
/**
533+
* Data provider for `test_add_mandate_data`.
534+
*
535+
* @return array
536+
*/
537+
public function provider_test_add_mandate_data() {
538+
return [
539+
[ 'REMOTE_ADDR', '192.168.1.1', '192.168.1.1' ],
540+
[ 'REMOTE_ADDR', '192.168.1.1, 192.168.1.2, 192.168.1.3', '192.168.1.1' ],
541+
[ 'HTTP_X_REAL_IP', '192.168.1.1', '192.168.1.1' ],
542+
[ 'HTTP_X_REAL_IP', '192.168.1.1, 192.168.1.2, 192.168.1.3', '192.168.1.1' ],
543+
[ 'HTTP_X_FORWARDED_FOR', '192.168.1.1, 192.168.1.2, 192.168.1.3', '192.168.1.1' ],
544+
[ 'HTTP_X_FORWARDED_FOR', '192.168.1.1', '192.168.1.1' ],
545+
[ 'HTTP_X_REAL_IP', 'invalid-ip-address', 'invalid-ip-address' ],
546+
[ 'HTTP_X_REAL_IP', '', '' ],
547+
];
548+
}
510549
}

0 commit comments

Comments
 (0)