Skip to content

Commit c258212

Browse files
authored
Fix refunds on Stripe API 2022-11-15 (#2499)
* Fix refunds on Stripe API 2022-11-15 * Add changelog entry * Add comments to `get_refund_object`
1 parent 08c5149 commit c258212

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

changelog.txt

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

33
= 7.1.0 - 2022-xx-xx =
44
* Fix - Expand charges object from incoming webhooks using Stripe API version 2022-11-15.
5+
* Fix - Expand refunds on charge object from incoming webhooks using Stripe API version 2022-11-15.
56

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

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,16 +648,17 @@ public function get_source_object( $source_id = '' ) {
648648
*
649649
* @since x.x.x
650650
* @param string $charge_id The charge ID to get charge object for.
651+
* @param array $params The parameters to pass to the request.
651652
*
652653
* @throws WC_Stripe_Exception Error while retrieving charge object.
653654
* @return string|object
654655
*/
655-
public function get_charge_object( $charge_id = '' ) {
656+
public function get_charge_object( $charge_id = '', $params = [] ) {
656657
if ( empty( $charge_id ) ) {
657658
return '';
658659
}
659660

660-
$charge_object = WC_Stripe_API::retrieve( 'charges/' . $charge_id );
661+
$charge_object = WC_Stripe_API::request( $params, 'charges/' . $charge_id, 'GET' );
661662

662663
if ( ! empty( $charge_object->error ) ) {
663664
throw new WC_Stripe_Exception( print_r( $charge_object, true ), $charge_object->error->message );

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

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ public function process_webhook_capture( $notification ) {
402402
if ( $this->is_partial_capture( $notification ) ) {
403403
$partial_amount = $this->get_partial_amount_to_charge( $notification );
404404
$order->set_total( $partial_amount );
405-
$this->update_fees( $order, $notification->data->object->refunds->data[0]->balance_transaction );
405+
$refund_object = $this->get_refund_object( $notification );
406+
$this->update_fees( $order, $refund_object->balance_transaction );
406407
/* translators: partial captured amount */
407408
$order->add_order_note( sprintf( __( 'This charge was partially captured via Stripe Dashboard in the amount of: %s', 'woocommerce-gateway-stripe' ), $partial_amount ) );
408409
} else {
@@ -554,11 +555,12 @@ public function process_webhook_refund( $notification ) {
554555
$order_id = $order->get_id();
555556

556557
if ( 'stripe' === $order->get_payment_method() ) {
557-
$charge = $order->get_transaction_id();
558-
$captured = $order->get_meta( '_stripe_charge_captured' );
559-
$refund_id = $order->get_meta( '_stripe_refund_id' );
560-
$currency = $order->get_currency();
561-
$raw_amount = $notification->data->object->refunds->data[0]->amount;
558+
$charge = $order->get_transaction_id();
559+
$captured = $order->get_meta( '_stripe_charge_captured' );
560+
$refund_id = $order->get_meta( '_stripe_refund_id' );
561+
$currency = $order->get_currency();
562+
$refund_object = $this->get_refund_object( $notification );
563+
$raw_amount = $refund_object->amount;
562564

563565
if ( ! in_array( strtolower( $currency ), WC_Stripe_Helper::no_decimal_currencies(), true ) ) {
564566
$raw_amount /= 100;
@@ -580,7 +582,7 @@ public function process_webhook_refund( $notification ) {
580582
}
581583

582584
// If the refund ID matches, don't continue to prevent double refunding.
583-
if ( $notification->data->object->refunds->data[0]->id === $refund_id ) {
585+
if ( $refund_object->id === $refund_id ) {
584586
return;
585587
}
586588

@@ -600,14 +602,14 @@ public function process_webhook_refund( $notification ) {
600602
WC_Stripe_Logger::log( $refund->get_error_message() );
601603
}
602604

603-
$order->update_meta_data( '_stripe_refund_id', $notification->data->object->refunds->data[0]->id );
605+
$order->update_meta_data( '_stripe_refund_id', $refund_object->id );
604606

605-
if ( isset( $notification->data->object->refunds->data[0]->balance_transaction ) ) {
606-
$this->update_fees( $order, $notification->data->object->refunds->data[0]->balance_transaction );
607+
if ( isset( $refund_object->balance_transaction ) ) {
608+
$this->update_fees( $order, $refund_object->balance_transaction );
607609
}
608610

609611
/* translators: 1) amount (including currency symbol) 2) transaction id 3) refund message */
610-
$order->add_order_note( sprintf( __( 'Refunded %1$s - Refund ID: %2$s - %3$s', 'woocommerce-gateway-stripe' ), $amount, $notification->data->object->refunds->data[0]->id, $reason ) );
612+
$order->add_order_note( sprintf( __( 'Refunded %1$s - Refund ID: %2$s - %3$s', 'woocommerce-gateway-stripe' ), $amount, $refund_object->id, $reason ) );
611613
}
612614
}
613615
}
@@ -768,6 +770,25 @@ public function is_partial_capture( $notification ) {
768770
return 0 < $notification->data->object->amount_refunded;
769771
}
770772

773+
/**
774+
* Gets the first refund object from charge notification.
775+
*
776+
* @since x.x.x
777+
* @param object $notification
778+
*
779+
* @return object
780+
*/
781+
public function get_refund_object( $notification ) {
782+
// Since API version 2022-11-15, the Charge object no longer expands `refunds` by default.
783+
// We can remove this once we drop support for API versions prior to 2022-11-15.
784+
if ( ! empty( $notification->data->object->refunds->data[0] ) ) {
785+
return $notification->data->object->refunds->data[0];
786+
}
787+
788+
$charge = $this->get_charge_object( $notification->data->object->id, [ 'expand' => [ 'refunds' ] ] );
789+
return $charge->refunds->data[0];
790+
}
791+
771792
/**
772793
* Gets the amount refunded.
773794
*
@@ -777,10 +798,11 @@ public function is_partial_capture( $notification ) {
777798
*/
778799
public function get_refund_amount( $notification ) {
779800
if ( $this->is_partial_capture( $notification ) ) {
780-
$amount = $notification->data->object->refunds->data[0]->amount / 100;
801+
$refund_object = $this->get_refund_object( $notification );
802+
$amount = $refund_object->amount / 100;
781803

782804
if ( in_array( strtolower( $notification->data->object->currency ), WC_Stripe_Helper::no_decimal_currencies() ) ) {
783-
$amount = $notification->data->object->refunds->data[0]->amount;
805+
$amount = $refund_object->amount;
784806
}
785807

786808
return $amount;

readme.txt

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

131131
= 7.1.0 - 2022-xx-xx =
132132
* Fix - Expand charges object from incoming webhooks using Stripe API version 2022-11-15.
133+
* Fix - Expand refunds on charge object from incoming webhooks using Stripe API version 2022-11-15.
133134

134135

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

0 commit comments

Comments
 (0)