Skip to content

Commit 474ed6c

Browse files
Mayishamalithsen
authored andcommitted
Track charge completed via webhooks in order note (#4322)
* track charge completed via webhooks in order note * add changelog * piggyback webhook flag with response data instead of adding a new function paaram * fix mocks
1 parent 69515f1 commit 474ed6c

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
*** Changelog ***
22

3+
= 9.5.3 - xxxx-xx-xx =
4+
* Tweak - Track charge completed via webhooks in order notes
5+
36
= 9.5.2 - 2025-05-22 =
47
* Add - Implement custom database cache for persistent caching with in-memory optimization.
58
* Update - Remove feature that flags 401s and proactively blocks subsequent API calls until the store has reauthenticated.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,9 @@ public function process_response( $response, $order ) {
611611

612612
/* translators: transaction id */
613613
$message = sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $response->id );
614+
if ( isset( $response->is_webhook_response ) ) {
615+
$message .= ' (via webhook)';
616+
}
614617
$order->add_order_note( $message );
615618
}
616619
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ public function process_webhook_payment( $notification, $retry = true ) {
357357

358358
do_action( 'wc_gateway_stripe_process_webhook_payment', $response, $order );
359359

360+
$response->is_webhook_response = true;
360361
$this->process_response( $response, $order );
361362

362363
} catch ( WC_Stripe_Exception $e ) {
@@ -509,7 +510,7 @@ public function process_webhook_capture( $notification ) {
509510
$order->payment_complete( $notification->data->object->id );
510511

511512
/* translators: transaction id */
512-
$order->add_order_note( sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $notification->data->object->id ) );
513+
$order->add_order_note( sprintf( __( 'Stripe charge complete (Charge ID: %s) (via webhook)', 'woocommerce-gateway-stripe' ), $notification->data->object->id ) );
513514
}
514515

515516
if ( is_callable( [ $order, 'save' ] ) ) {
@@ -589,7 +590,7 @@ public function process_webhook_charge_succeeded( $notification ) {
589590
$order->payment_complete( $charge->id );
590591

591592
/* translators: transaction id */
592-
$order->add_order_note( sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $charge->id ) );
593+
$order->add_order_note( sprintf( __( 'Stripe charge complete (Charge ID: %s) (via webhook)', 'woocommerce-gateway-stripe' ), $charge->id ) );
593594
}
594595

595596
if ( is_callable( [ $order, 'save' ] ) ) {
@@ -1048,6 +1049,7 @@ public function process_payment_intent( $notification ) {
10481049

10491050
do_action( 'wc_gateway_stripe_process_payment', $charge, $order );
10501051

1052+
$charge->is_webhook_response = true;
10511053
$this->process_response( $charge, $order );
10521054
} else {
10531055
WC_Stripe_Logger::log( "Processing $notification->type ($intent->id) asynchronously for order $order_id." );
@@ -1235,6 +1237,7 @@ protected function handle_deferred_payment_intent_succeeded( $order, $intent_id
12351237
WC_Stripe_Logger::log( "Processing Stripe PaymentIntent {$intent_id} for order {$order->get_id()} via deferred webhook." );
12361238

12371239
do_action( 'wc_gateway_stripe_process_payment', $charge, $order );
1240+
$charge->is_webhook_response = true;
12381241
$this->process_response( $charge, $order );
12391242
}
12401243

readme.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,8 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
110110

111111
== Changelog ==
112112

113-
= 9.5.2 - 2025-05-22 =
114-
* Add - Implement custom database cache for persistent caching with in-memory optimization.
115-
* Update - Remove feature that flags 401s and proactively blocks subsequent API calls until the store has reauthenticated.
116-
* Fix - Disable payment settings sync when we receive unsupported payment method configurations.
117-
* Fix - Ensure that we use current Stripe API keys after settings updates
118-
* Fix - Fix initial enabled payment methods migration to the Stripe Payment Methods Configuration API
113+
= 9.5.3 - xxxx-xx-xx =
114+
115+
* Tweak - Track charge completed via webhooks in order notes
119116

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

tests/phpunit/test-wc-stripe-webhook-handler.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,17 @@ public function test_process_of_successful_payment_intent_deferred_webhook() {
185185
// Expect the get latest charge from intent to be called.
186186
$this->mock_webhook_handler->expects( $this->once() )
187187
->method( 'get_latest_charge_from_intent' )
188-
->willReturn( self::MOCK_PAYMENT_INTENT['charges']['data'][0] );
188+
->willReturn( (object) self::MOCK_PAYMENT_INTENT['charges']['data'][0] );
189189

190190
// Expect the process response to be called with the charge and order.
191+
$charge_param = (object) array_merge(
192+
self::MOCK_PAYMENT_INTENT['charges']['data'][0],
193+
[ 'is_webhook_response' => true ]
194+
);
191195
$this->mock_webhook_handler->expects( $this->once() )
192196
->method( 'process_response' )
193197
->with(
194-
self::MOCK_PAYMENT_INTENT['charges']['data'][0],
198+
$charge_param,
195199
$this->callback(
196200
function ( $passed_order ) use ( $order ) {
197201
return $passed_order instanceof WC_Order && $order->get_id() === $passed_order->get_id();
@@ -422,6 +426,9 @@ public function test_process_payment_intent(
422426
[ &$mock_action_process_payment_intent_incomplete, 'action' ]
423427
);
424428

429+
$this->mock_webhook_handler->method( 'get_latest_charge_from_intent' )
430+
->willReturn( (object) self::MOCK_PAYMENT_INTENT['charges']['data'][0] );
431+
425432
$order = WC_Helper_Order::create_order();
426433
$order->set_status( $order_status );
427434
if ( $order_locked ) {

0 commit comments

Comments
 (0)