Skip to content

Commit 08c5149

Browse files
authored
Fix compatibility issue with incoming webhooks in Stripe API version 2022-11-15 (#2493)
* Fix compatibility issue with Stripe API version 2022-11-15 * Fix docblock * Add changelog entry
1 parent 1833465 commit 08c5149

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
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
= 7.1.0 - 2022-xx-xx =
4+
* Fix - Expand charges object from incoming webhooks using Stripe API version 2022-11-15.
45

56
= 7.0.1 - 2022-11-11 =
67
* Fix - Issue where subscription renewal payments were being charged twice no longer present.

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,10 +621,13 @@ public function get_owner_details( $order ) {
621621
}
622622

623623
/**
624-
* Get source object by source id.
624+
* Get source object by source ID.
625625
*
626626
* @since 4.0.3
627627
* @param string $source_id The source ID to get source object for.
628+
*
629+
* @throws WC_Stripe_Exception Error while retrieving source object.
630+
* @return string|object
628631
*/
629632
public function get_source_object( $source_id = '' ) {
630633
if ( empty( $source_id ) ) {
@@ -640,6 +643,48 @@ public function get_source_object( $source_id = '' ) {
640643
return $source_object;
641644
}
642645

646+
/**
647+
* Get charge object by charge ID.
648+
*
649+
* @since x.x.x
650+
* @param string $charge_id The charge ID to get charge object for.
651+
*
652+
* @throws WC_Stripe_Exception Error while retrieving charge object.
653+
* @return string|object
654+
*/
655+
public function get_charge_object( $charge_id = '' ) {
656+
if ( empty( $charge_id ) ) {
657+
return '';
658+
}
659+
660+
$charge_object = WC_Stripe_API::retrieve( 'charges/' . $charge_id );
661+
662+
if ( ! empty( $charge_object->error ) ) {
663+
throw new WC_Stripe_Exception( print_r( $charge_object, true ), $charge_object->error->message );
664+
}
665+
666+
return $charge_object;
667+
}
668+
669+
/**
670+
* Get latest charge object from payment intent.
671+
*
672+
* Since API version 2022-11-15, the `charges` property was replaced with `latest_charge`.
673+
* We can remove this method once we drop support for API versions prior to 2022-11-15.
674+
*
675+
* @since x.x.x
676+
* @param object $intent Stripe API Payment Intent object response.
677+
*
678+
* @return object
679+
*/
680+
public function get_latest_charge_from_intent( $intent ) {
681+
if ( ! empty( $intent->charges->data ) ) {
682+
return end( $intent->charges->data );
683+
} else {
684+
return $this->get_charge_object( $intent->latest_charge );
685+
}
686+
}
687+
643688
/**
644689
* Checks if card is a prepaid card.
645690
*

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,8 @@ public function process_payment_intent_success( $notification ) {
845845
break;
846846
case 'payment_intent.succeeded':
847847
case 'payment_intent.amount_capturable_updated':
848-
$charge = end( $intent->charges->data );
848+
$charge = $this->get_latest_charge_from_intent( $intent );
849+
849850
WC_Stripe_Logger::log( "Stripe PaymentIntent $intent->id succeeded for order $order_id" );
850851

851852
do_action( 'wc_gateway_stripe_process_payment', $charge, $order );

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ If you get stuck, you can ask for help in the Plugin Forum.
129129
== Changelog ==
130130

131131
= 7.1.0 - 2022-xx-xx =
132+
* Fix - Expand charges object from incoming webhooks using Stripe API version 2022-11-15.
132133

133134

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

0 commit comments

Comments
 (0)