Skip to content

Commit 84d1b4c

Browse files
authored
Fix wc_gateway_stripe_process_payment action not called for sync payments (#4485)
* Deprecate wc_gateway_stripe_process_redirect_payment * Deprecate wc_gateway_stripe_process_webhook_payment * Add new filter wc_gateway_stripe_process_payment_charge
1 parent d43e1be commit 84d1b4c

10 files changed

+116
-13
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Fix - Set default values for custom field options
2323
* Fix - Enforce rate limiter for failed add payment method attempts
2424
* Update - Add the number of pending webhooks to the Account status section
25+
* Update - Deprecate `wc_gateway_stripe_process_payment`, `wc_gateway_stripe_process_redirect_payment` and `wc_gateway_stripe_process_webhook_payment` actions in favour of `wc_gateway_stripe_process_payment_charge`
2526

2627
= 9.6.0 - 2025-07-07 =
2728
* Fix - Register Express Checkout script before use to restore buttons on “order-pay” pages

includes/abstracts/abstract-wc-stripe-payment-gateway.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,12 @@ public function generate_payment_request( $order, $prepared_payment_method ) {
534534
}
535535

536536
/**
537-
* Store extra meta data for an order from a Stripe Response.
537+
* Store extra meta data for an order from a Stripe charge response.
538+
*
539+
* @param object $response The Charge response from Stripe.
540+
* @param WC_Order $order The Order object.
541+
*
542+
* @return object The Charge response from Stripe.
538543
*
539544
* @throws WC_Stripe_Exception
540545
*/
@@ -548,6 +553,14 @@ public function process_response( $response, $order ) {
548553
throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
549554
}
550555

556+
/**
557+
* Allow third-party code to add custom logic before processing the charge data from Stripe.
558+
*
559+
* @param object $response The Charge response from Stripe.
560+
* @param WC_Order $order The Order object.
561+
*/
562+
do_action( 'wc_gateway_stripe_process_payment_charge', $response, $order );
563+
551564
$order_id = $order->get_id();
552565
$captured = ( isset( $response->captured ) && $response->captured ) ? 'yes' : 'no';
553566

includes/class-wc-stripe-order-handler.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,13 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er
219219
return;
220220
}
221221

222-
do_action( 'wc_gateway_stripe_process_redirect_payment', $response, $order );
222+
do_action_deprecated(
223+
'wc_gateway_stripe_process_redirect_payment',
224+
[ $response, $order ],
225+
'9.7.0',
226+
'wc_gateway_stripe_process_payment_charge',
227+
'The wc_gateway_stripe_process_redirect_payment action is deprecated. Use wc_gateway_stripe_process_payment_charge instead.'
228+
);
223229

224230
$this->process_response( $response, $order );
225231

includes/class-wc-stripe-webhook-handler.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,13 @@ public function process_webhook_payment( $notification, $retry = true ) {
360360
return;
361361
}
362362

363-
do_action( 'wc_gateway_stripe_process_webhook_payment', $response, $order );
363+
do_action_deprecated(
364+
'wc_gateway_stripe_process_webhook_payment',
365+
[ $response, $order ],
366+
'9.7.0',
367+
'wc_gateway_stripe_process_payment_charge',
368+
'The wc_gateway_stripe_process_webhook_payment action is deprecated. Use wc_gateway_stripe_process_payment_charge instead.'
369+
);
364370

365371
$response->is_webhook_response = true;
366372
$this->process_response( $response, $order );
@@ -1080,7 +1086,13 @@ public function process_payment_intent( $notification ) {
10801086
if ( $is_voucher_payment || $is_wallet_payment || ( ! $process_webhook_async && ! $is_awaiting_action ) ) {
10811087
$charge = $this->get_latest_charge_from_intent( $intent );
10821088

1083-
do_action( 'wc_gateway_stripe_process_payment', $charge, $order );
1089+
do_action_deprecated(
1090+
'wc_gateway_stripe_process_payment',
1091+
[ $charge, $order ],
1092+
'9.7.0',
1093+
'wc_gateway_stripe_process_payment_charge',
1094+
'The wc_gateway_stripe_process_payment action is deprecated. Use wc_gateway_stripe_process_payment_charge instead.'
1095+
);
10841096

10851097
$charge->is_webhook_response = true;
10861098
$this->process_response( $charge, $order );
@@ -1286,7 +1298,14 @@ protected function handle_deferred_payment_intent_succeeded( $order, $intent_id
12861298

12871299
WC_Stripe_Logger::log( "Processing Stripe PaymentIntent {$intent_id} for order {$order->get_id()} via deferred webhook." );
12881300

1289-
do_action( 'wc_gateway_stripe_process_payment', $charge, $order );
1301+
do_action_deprecated(
1302+
'wc_gateway_stripe_process_payment',
1303+
[ $charge, $order ],
1304+
'9.7.0',
1305+
'wc_gateway_stripe_process_payment_charge',
1306+
'The wc_gateway_stripe_process_payment action is deprecated. Use wc_gateway_stripe_process_payment_charge instead.'
1307+
);
1308+
12901309
$charge->is_webhook_response = true;
12911310
$this->process_response( $charge, $order );
12921311
}

includes/compat/trait-wc-stripe-subscriptions.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,13 @@ public function process_subscription_payment( $amount, $renewal_order, $retry =
571571
do_action( 'wc_gateway_stripe_process_payment_subscription_charge_attempt_delayed', $response, $renewal_order );
572572
} else {
573573
// The charge was successfully captured
574-
do_action( 'wc_gateway_stripe_process_payment', $response, $renewal_order );
574+
do_action_deprecated(
575+
'wc_gateway_stripe_process_payment',
576+
[ $response, $renewal_order ],
577+
'9.7.0',
578+
'wc_gateway_stripe_process_payment_charge',
579+
'The wc_gateway_stripe_process_payment action is deprecated. Use wc_gateway_stripe_process_payment_charge instead.'
580+
);
575581

576582
// Use the last charge within the intent or the full response body in case of SEPA.
577583
$latest_charge = $this->get_latest_charge_from_intent( $response );

includes/payment-methods/class-wc-gateway-stripe-sepa.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,13 @@ public function process_payment( $order_id, $retry = true, $force_save_source =
373373
throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
374374
}
375375

376-
do_action( 'wc_gateway_stripe_process_payment', $response, $order );
376+
do_action_deprecated(
377+
'wc_gateway_stripe_process_payment',
378+
[ $response, $order ],
379+
'9.7.0',
380+
'wc_gateway_stripe_process_payment_charge',
381+
'The wc_gateway_stripe_process_payment action is deprecated. Use wc_gateway_stripe_process_payment_charge instead.'
382+
);
377383

378384
// Process valid response.
379385
$this->process_response( $response, $order );

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,6 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
132132
* Fix - Set default values for custom field options
133133
* Fix - Enforce rate limiter for failed add payment method attempts
134134
* Update - Add the number of pending webhooks to the Account status section
135+
* Update - Deprecate `wc_gateway_stripe_process_payment`, `wc_gateway_stripe_process_redirect_payment` and `wc_gateway_stripe_process_webhook_payment` actions in favour of `wc_gateway_stripe_process_payment_charge`
135136

136137
[See changelog for full details across versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).

tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,45 @@ public function test_process_response_updates_order_by_charge_status() {
14591459
$this->assertMatchesRegularExpression( '/Payment processing failed./', $exception->getLocalizedMessage() );
14601460
}
14611461

1462+
/**
1463+
* Test that the wc_gateway_stripe_process_payment_charge action is triggered when process_response() is called for synchronous payment paths.
1464+
*/
1465+
public function test_process_response_triggers_wc_gateway_stripe_process_payment_charge_action() {
1466+
$payment_method_id = 'pm_mock';
1467+
$customer_id = 'cus_mock';
1468+
$order = WC_Helper_Order::create_order();
1469+
$order_id = $order->get_id();
1470+
1471+
$payment_method_mock = self::MOCK_CARD_PAYMENT_METHOD_TEMPLATE;
1472+
$payment_method_mock['id'] = $payment_method_id;
1473+
$payment_method_mock['customer'] = $customer_id;
1474+
$payment_method_mock['card']['exp_year'] = intval( gmdate( 'Y' ) ) + 1;
1475+
1476+
$charge_mock = self::MOCK_CARD_PAYMENT_INTENT_TEMPLATE['charges']['data'][0];
1477+
$charge_mock['payment_method_details'] = $payment_method_mock;
1478+
$charge_mock['captured'] = true;
1479+
$charge_mock['status'] = 'succeeded';
1480+
$charge_mock['id'] = 'ch_mock_success';
1481+
1482+
$mock_action_process_payment = new MockAction();
1483+
add_action(
1484+
'wc_gateway_stripe_process_payment_charge',
1485+
[ &$mock_action_process_payment, 'action' ]
1486+
);
1487+
1488+
$this->mock_gateway->process_response( $this->array_to_object( $charge_mock ), wc_get_order( $order_id ) );
1489+
1490+
$final_order = wc_get_order( $order_id );
1491+
1492+
// Test the action was called only once.
1493+
$this->assertEquals( 1, $mock_action_process_payment->get_call_count() );
1494+
1495+
// Test the order was processed successfully.
1496+
$this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() );
1497+
$this->assertEquals( 'yes', $final_order->get_meta( '_stripe_charge_captured', true ) );
1498+
$this->assertEquals( $charge_mock['id'], $final_order->get_transaction_id() );
1499+
}
1500+
14621501
/**
14631502
* TESTS FOR SAVED PAYMENTS.
14641503
*/
@@ -2034,7 +2073,7 @@ public function test_subscription_renewal_is_successful() {
20342073
// by hooking into it.
20352074
$mock_action_process_payment = new MockAction();
20362075
add_action(
2037-
'wc_gateway_stripe_process_payment',
2076+
'wc_gateway_stripe_process_payment_charge',
20382077
[ &$mock_action_process_payment, 'action' ]
20392078
);
20402079

@@ -2083,7 +2122,7 @@ public function test_subscription_renewal_is_successful() {
20832122
// Assert: Our hook was called once.
20842123
$this->assertEquals( 1, $mock_action_process_payment->get_call_count() );
20852124
// Assert: Only our hook was called.
2086-
$this->assertEquals( [ 'wc_gateway_stripe_process_payment' ], $mock_action_process_payment->get_tags() );
2125+
$this->assertEquals( [ 'wc_gateway_stripe_process_payment_charge' ], $mock_action_process_payment->get_tags() );
20872126
}
20882127

20892128
/**

tests/phpunit/WC_Stripe_Subscription_Renewal_Test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function test_renewal_successful() {
224224
// by hooking into it.
225225
$mock_action_process_payment = new MockAction();
226226
add_action(
227-
'wc_gateway_stripe_process_payment',
227+
'wc_gateway_stripe_process_payment_charge',
228228
[ &$mock_action_process_payment, 'action' ]
229229
);
230230

@@ -256,7 +256,7 @@ public function test_renewal_successful() {
256256
$this->assertEquals( 1, $mock_action_process_payment->get_call_count() );
257257

258258
// Assert: Only our hook was called.
259-
$this->assertEquals( [ 'wc_gateway_stripe_process_payment' ], $mock_action_process_payment->get_tags() );
259+
$this->assertEquals( [ 'wc_gateway_stripe_process_payment_charge' ], $mock_action_process_payment->get_tags() );
260260

261261
// Clean up.
262262
remove_filter( 'pre_http_request', [ $this, 'pre_http_request_response_success' ] );

tests/phpunit/WC_Stripe_Webhook_Handler_Test.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ private function mock_webhook_handler( $exclude_methods = [] ) {
7575
$this->mock_webhook_handler = $this->getMockBuilder( WC_Stripe_Webhook_Handler::class )
7676
->setMethods( $methods )
7777
->getMock();
78+
79+
// Set process_response mock to use the real method.
80+
// We need to mock this because several tests check that it's not called or called a specific number of times.
81+
$this->mock_webhook_handler->expects( $this->any() )
82+
->method( 'process_response' )
83+
->willReturnCallback(
84+
function ( $response, $order ) {
85+
// Call the real method
86+
$real_handler = new WC_Stripe_Webhook_Handler();
87+
return $real_handler->process_response( $response, $order );
88+
}
89+
);
7890
}
7991

8092
/**
@@ -427,7 +439,7 @@ public function test_process_payment_intent(
427439
) {
428440
$mock_action_process_payment = new MockAction();
429441
add_action(
430-
'wc_gateway_stripe_process_payment',
442+
'wc_gateway_stripe_process_payment_charge',
431443
[ &$mock_action_process_payment, 'action' ]
432444
);
433445

@@ -590,7 +602,7 @@ public function provide_test_process_payment_intent() {
590602
'order locked' => false,
591603
'payment type' => WC_Stripe_Payment_Methods::BOLETO,
592604
'order status final' => false,
593-
'expected status' => OrderStatus::PENDING,
605+
'expected status' => OrderStatus::PROCESSING,
594606
'expected note' => '',
595607
'expected process payment calls' => 1,
596608
'expected process payment intent incomplete calls' => 0,

0 commit comments

Comments
 (0)