Skip to content

Commit b1b5c8f

Browse files
authored
Fix webhook order retrieval by intent charges (#3704)
* Fix webhook order retrieval by intent charges * Changelog and readme entries * Unit tests
1 parent 4b8fd57 commit b1b5c8f

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** Changelog ***
22

33
= 9.1.1 - xxxx-xx-xx =
4+
* Fix - Fixes the webhook order retrieval by intent charges. The processed event is an object, not an array.
45
* Fix - Payment request button fails to display when the legacy checkout experience is enabled.
56
* Fix - Resolves the payment element loading issue in the legacy checkout experience.
67

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,8 +1317,8 @@ private function get_order_from_intent( $intent ) {
13171317
// Try to retrieve from the charges array.
13181318
if ( ! empty( $intent->charges ) ) {
13191319
$charge = $intent->charges[0] ?? [];
1320-
$order_id = $charge['metadata']['order_id'] ?? null;
1321-
return wc_get_order( $order_id );
1320+
$order_id = $charge->metadata->order_id ?? null;
1321+
return $order_id ? wc_get_order( $order_id ) : false;
13221322
}
13231323

13241324
// Fall back to finding the order via the intent ID.

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
111111
== Changelog ==
112112

113113
= 9.1.1 - xxxx-xx-xx =
114+
* Fix - Fixes the webhook order retrieval by intent charges. The processed event is an object, not an array.
114115
* Fix - Payment request button fails to display when the legacy checkout experience is enabled.
115116
* Fix - Resolves the payment element loading issue in the legacy checkout experience.
116117

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,21 +433,27 @@ public function test_process_payment_intent(
433433
$order->save_meta_data();
434434
$order->save();
435435

436-
$notification = (object) [
436+
$notification = [
437437
'type' => $event_type,
438-
'data' => (object) [
439-
'object' => (object) [
438+
'data' => [
439+
'object' => [
440440
'id' => 'pi_mock',
441-
'metadata' => (object) [
442-
'order_id' => $order->get_id(),
441+
'charges' => [
442+
[
443+
'metadata' => [
444+
'order_id' => $order->get_id(),
445+
],
446+
],
443447
],
444-
'last_payment_error' => (object) [
448+
'last_payment_error' => [
445449
'message' => 'Your card was declined. You can call your bank for details.',
446450
],
447451
],
448452
],
449453
];
450454

455+
$notification = json_decode( wp_json_encode( $notification ) );
456+
451457
$this->mock_webhook_handler->process_payment_intent( $notification );
452458

453459
$final_order = wc_get_order( $order->get_id() );

0 commit comments

Comments
 (0)