Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fatal error when canceling uncaptured orders due to non-expanded refunds in Stripe API response #3377

Merged

Conversation

james-allan
Copy link
Contributor

Fixes #3371

Changes proposed in this Pull Request:

When we bumped the Stripe API version in 8.5.0 to 2024-06-20 we introduced the potential for a fatal error when cancelling uncaptured orders.

In version 2022-11-15, Stripe changed the default response for charges to no longer automatically expand the refunds property.

Screenshot 2024-08-22 at 8 15 08 PM
refunds no longer expanded. Source: https://docs.stripe.com/upgrades#2022-11-15

This caused a fatal error when attempting to cancel an uncaptured charge because we expected the charge object to have refund data. This PR fixes that by making sure we fetch the charge and expand the refunds array before attempting to access it.

Testing instructions

  1. Go to WooCommerce > Settings > Payments > Stripe > Settings
  2. Enable the "issue an authorization on checkout, and capture later" option
  3. Purchase a product using Stripe
  4. Attempt to cancel the order.
    • On develop you will receive the following error.
    • On this branch no error should occur.
PHP Fatal error:  Uncaught Error: Attempt to modify property "data" on null in /wp-content/plugins/woocommerce-gateway-stripe/includes/abstracts/abstract-wc-stripe-payment-gateway.php:1130
Stack trace:
#0 /wp-content/plugins/woocommerce-gateway-stripe/includes/class-wc-stripe-order-handler.php(348): WC_Stripe_Payment_Gateway->process_refund(8772)
#1 /wp-includes/class-wp-hook.php(326): WC_Stripe_Order_Handler->cancel_payment(8772)
#2 /wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#3 /wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#4 /wp-content/plugins/woocommerce/includes/class-wc-order.php(416): do_action('woocommerce_ord...', 8772, Object(Automattic\WooCommerce\Admin\Overrides\Order), Array)
#5 /wp-content/plugins/woocommerce/includes/class-wc-order.php(257): WC_Order->status_transition()
#6 /wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-data.php(768): WC_Order->save()
#7 /wp-includes/class-wp-hook.php(326): WC_Meta_Box_Order_Data::save(8772)
#8 /wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#9 /wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#10 /wp-content/plugins/woocommerce/src/Internal/Admin/Orders/Edit.php(313): do_action('woocommerce_pro...', 8772, Object(Automattic\WooCommerce\Admin\Overrides\Order))

  • Covered with tests (or have a good reason not to test in description ☝️)
  • Added changelog entry in both changelog.txt and readme.txt (or does not apply)
  • Tested on mobile (or does not apply)

Post merge

@james-allan james-allan marked this pull request as ready for review August 23, 2024 02:25
@james-allan james-allan requested a review from Mayisha August 23, 2024 06:10
Copy link
Contributor

@Mayisha Mayisha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @james-allan. The changes look good to me 🎉

I can not reproduce the fatal error. But I have confirmed that in the develop branch the $response is null for me in the previous code,
$response = end( $charge->refunds->data );

In this branch, I see the correct data in $response with the updated code

{"id":"re_3PsKL8CBKj0yxC0Z0NDMDq1J","object":"refund","amount":3800,"balance_transaction":null,"charge":"ch_3PsKL8CBKj0yxC0Z0aSOpBIm","created":1724745702,"currency":"eur","destination_details":{"card":{"reference":"676489","reference_status":"available","reference_type":"system_trace_audit_number","type":"reversal"},"type":"card"},"metadata":{},"payment_intent":"pi_3PsKL8CBKj0yxC0Z0sGDowhm","reason":null,"receipt_number":null,"source_transfer_reversal":null,"status":"succeeded","transfer_reversal":null}

@james-allan james-allan merged commit 36ea8ad into develop Aug 28, 2024
33 of 35 checks passed
@james-allan james-allan deleted the fix/3371-fix-fatal-error-on-refunding-uncaputured-order branch August 28, 2024 06:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Critical error when cancelling an order while authorize and capture is enabled
2 participants