@@ -402,7 +402,8 @@ public function process_webhook_capture( $notification ) {
402
402
if ( $ this ->is_partial_capture ( $ notification ) ) {
403
403
$ partial_amount = $ this ->get_partial_amount_to_charge ( $ notification );
404
404
$ 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 );
406
407
/* translators: partial captured amount */
407
408
$ order ->add_order_note ( sprintf ( __ ( 'This charge was partially captured via Stripe Dashboard in the amount of: %s ' , 'woocommerce-gateway-stripe ' ), $ partial_amount ) );
408
409
} else {
@@ -554,11 +555,12 @@ public function process_webhook_refund( $notification ) {
554
555
$ order_id = $ order ->get_id ();
555
556
556
557
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 ;
562
564
563
565
if ( ! in_array ( strtolower ( $ currency ), WC_Stripe_Helper::no_decimal_currencies (), true ) ) {
564
566
$ raw_amount /= 100 ;
@@ -580,7 +582,7 @@ public function process_webhook_refund( $notification ) {
580
582
}
581
583
582
584
// 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 ) {
584
586
return ;
585
587
}
586
588
@@ -600,14 +602,14 @@ public function process_webhook_refund( $notification ) {
600
602
WC_Stripe_Logger::log ( $ refund ->get_error_message () );
601
603
}
602
604
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 );
604
606
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 );
607
609
}
608
610
609
611
/* 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 ) );
611
613
}
612
614
}
613
615
}
@@ -768,6 +770,25 @@ public function is_partial_capture( $notification ) {
768
770
return 0 < $ notification ->data ->object ->amount_refunded ;
769
771
}
770
772
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
+
771
792
/**
772
793
* Gets the amount refunded.
773
794
*
@@ -777,10 +798,11 @@ public function is_partial_capture( $notification ) {
777
798
*/
778
799
public function get_refund_amount ( $ notification ) {
779
800
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 ;
781
803
782
804
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 ;
784
806
}
785
807
786
808
return $ amount ;
0 commit comments