Skip to content

Conversation

htdat
Copy link
Member

@htdat htdat commented Mar 20, 2025

Fixes #1195

Changes proposed in this Pull Request

Changes based on this spike #1195 (comment):

  • Modify add_note_and_metadata_for_refund: change its name and add one param $is_pending to add_note_and_metadata_for_created_refund so that it can handle both pending and succeeded note.
  • No need to change on the timeline refund, i.e. WC_REST_Payments_Refunds_Controller::process_refund, because it's eventually tunneld to gateway process_refund with wc_create_refund.
  • Update webhook handling so that it can handle both pending and succeeded. The most notable change is on process_webhook_refund_updated where I moved the handled the failed refund into Order_Service.

Testing instructions

  • Use https://docs.stripe.com/testing?testing-method=card-numbers#refunds to test for the relevant flows. (If testing with a local dev server, make sure to enable webhook listening).
  • Trigger pending -> succeeded refund status in all flows: from Stripe dash, WP Admin order admin page, and Transaction Timeline.
  • Trigger succeeded -> failed refund status in the same flows.
  • Expect: all respective pending, succeeded, failed notes are added into the order.

  • Run npm run changelog to add a changelog file, choose patch to leave it empty if the change is not significant. You can add multiple changelog files in one PR by running this command a few times.
  • Covered with tests (or have a good reason not to test in description ☝️)
  • Tested on mobile (or does not apply)

Post merge

Important

Add documents for this new link https://woocommerce.com/document/woopayments/managing-money/#pending-refunds
Update: asked here p1743136317673289-slack-C03EE4CPPK5

@botwoo
Copy link
Collaborator

botwoo commented Mar 20, 2025

Test the build

Option 1. Jetpack Beta

  • Install and activate Jetpack Beta.
  • Use this build by searching for PR number 10600 or branch name fix/1195-pending-refund in your-test.site/wp-admin/admin.php?page=jetpack-beta&plugin=woocommerce-payments

Option 2. Jurassic Ninja - available for logged-in A12s

🚀 Launch a JN site with this branch 🚀

ℹ️ Install this Tampermonkey script to get more options.


Build info:

  • Latest commit: d27e300
  • Build time: 2025-03-26 04:27:10 UTC

Note: the build is updated when a new commit is pushed to this PR.

Copy link
Contributor

github-actions bot commented Mar 20, 2025

Size Change: 0 B

Total Size: 1.27 MB

ℹ️ View Unchanged
Filename Size
release/woocommerce-payments/assets/css/admin.css 1.41 kB
release/woocommerce-payments/assets/css/admin.rtl.css 1.42 kB
release/woocommerce-payments/assets/css/success.css 1.05 kB
release/woocommerce-payments/assets/css/success.rtl.css 1.05 kB
release/woocommerce-payments/dist/blocks-checkout-rtl.css 2.74 kB
release/woocommerce-payments/dist/blocks-checkout.css 2.74 kB
release/woocommerce-payments/dist/blocks-checkout.js 56.2 kB
release/woocommerce-payments/dist/cart-block.js 17.3 kB
release/woocommerce-payments/dist/cart.js 5.73 kB
release/woocommerce-payments/dist/checkout-rtl.css 1.28 kB
release/woocommerce-payments/dist/checkout.css 1.28 kB
release/woocommerce-payments/dist/checkout.js 34.7 kB
release/woocommerce-payments/dist/express-checkout-rtl.css 236 B
release/woocommerce-payments/dist/express-checkout.css 236 B
release/woocommerce-payments/dist/express-checkout.js 15.9 kB
release/woocommerce-payments/dist/frontend-tracks.js 854 B
release/woocommerce-payments/dist/index-rtl.css 19.1 kB
release/woocommerce-payments/dist/index.css 19.1 kB
release/woocommerce-payments/dist/index.js 239 kB
release/woocommerce-payments/dist/multi-currency-analytics.js 1.08 kB
release/woocommerce-payments/dist/multi-currency-rtl.css 4.29 kB
release/woocommerce-payments/dist/multi-currency-switcher-block.js 60.8 kB
release/woocommerce-payments/dist/multi-currency.css 4.29 kB
release/woocommerce-payments/dist/multi-currency.js 58.2 kB
release/woocommerce-payments/dist/order-rtl.css 740 B
release/woocommerce-payments/dist/order.css 740 B
release/woocommerce-payments/dist/order.js 42.3 kB
release/woocommerce-payments/dist/payment-gateways-rtl.css 1.34 kB
release/woocommerce-payments/dist/payment-gateways.css 1.34 kB
release/woocommerce-payments/dist/payment-gateways.js 39.1 kB
release/woocommerce-payments/dist/plugins-page-rtl.css 375 B
release/woocommerce-payments/dist/plugins-page.css 375 B
release/woocommerce-payments/dist/plugins-page.js 20.1 kB
release/woocommerce-payments/dist/product-details-rtl.css 433 B
release/woocommerce-payments/dist/product-details.css 436 B
release/woocommerce-payments/dist/product-details.js 12.7 kB
release/woocommerce-payments/dist/settings-rtl.css 11.4 kB
release/woocommerce-payments/dist/settings.css 11.3 kB
release/woocommerce-payments/dist/settings.js 223 kB
release/woocommerce-payments/dist/subscription-edit-page.js 703 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal-rtl.css 524 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal.css 524 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal.js 20.2 kB
release/woocommerce-payments/dist/subscription-product-onboarding-toast.js 730 B
release/woocommerce-payments/dist/subscriptions-empty-state-rtl.css 120 B
release/woocommerce-payments/dist/subscriptions-empty-state.css 120 B
release/woocommerce-payments/dist/subscriptions-empty-state.js 19.3 kB
release/woocommerce-payments/dist/success.js 6.03 kB
release/woocommerce-payments/dist/tokenized-express-checkout-rtl.css 236 B
release/woocommerce-payments/dist/tokenized-express-checkout.css 236 B
release/woocommerce-payments/dist/tokenized-express-checkout.js 16.9 kB
release/woocommerce-payments/dist/tos-rtl.css 235 B
release/woocommerce-payments/dist/tos.css 235 B
release/woocommerce-payments/dist/tos.js 21.8 kB
release/woocommerce-payments/dist/woopay-direct-checkout.js 6.13 kB
release/woocommerce-payments/dist/woopay-express-button.js 23.5 kB
release/woocommerce-payments/dist/woopay-rtl.css 4.31 kB
release/woocommerce-payments/dist/woopay.css 4.28 kB
release/woocommerce-payments/dist/woopay.js 71.1 kB
release/woocommerce-payments/includes/subscriptions/assets/css/plugin-page.css 625 B
release/woocommerce-payments/includes/subscriptions/assets/js/plugin-page.js 814 B
release/woocommerce-payments/vendor/automattic/jetpack-assets/build/i18n-loader.js 2.46 kB
release/woocommerce-payments/vendor/automattic/jetpack-assets/build/jetpack-script-data.js 957 B
release/woocommerce-payments/vendor/automattic/jetpack-assets/src/js/i18n-loader.js 1.02 kB
release/woocommerce-payments/vendor/automattic/jetpack-assets/src/js/script-data.js 69 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/babel.config.js 163 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/identity-crisis.css 2.47 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/identity-crisis.js 14.3 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/identity-crisis.rtl.css 2.47 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-connection.css 10.1 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-connection.js 29.7 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-connection.rtl.css 10.1 kB
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-admin-create-user.css 198 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-admin-create-user.js 280 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-admin-create-user.rtl.css 198 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-login.css 625 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-login.js 333 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-login.rtl.css 626 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-sso-users.js 417 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/jetpack-users-connection.js 161 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/tracks-ajax.js 521 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/dist/tracks-callables.js 585 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/src/sso/jetpack-sso-admin-create-user.css 215 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/src/sso/jetpack-sso-admin-create-user.js 521 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/src/sso/jetpack-sso-login.css 721 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/src/sso/jetpack-sso-login.js 412 B
release/woocommerce-payments/vendor/automattic/jetpack-connection/src/sso/jetpack-sso-users.js 625 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/about.css 1.04 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin-empty-state.css 294 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin-order-statuses.css 408 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin.css 3.59 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/checkout.css 301 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/modal.css 746 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/view-subscription.css 574 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/wcs-upgrade.css 414 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/admin-pointers.js 543 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/admin.js 9.4 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/jstz.js 6.78 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/jstz.min.js 3.84 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/meta-boxes-coupon.js 545 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/meta-boxes-subscription.js 2.52 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/moment.js 22.2 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/moment.min.js 11.7 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/payment-method-restrictions.js 1.29 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/wcs-meta-boxes-order.js 507 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/payment-methods.js 358 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/single-product.js 428 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/view-subscription.js 1.38 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/wcs-cart.js 782 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/modal.js 1.09 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/wcs-upgrade.js 1.26 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/build/index.css 391 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/build/index.js 3.04 kB

compressed-size-action

@htdat htdat self-assigned this Mar 20, 2025
@htdat htdat requested a review from a team March 21, 2025 10:17
@htdat
Copy link
Member Author

htdat commented Mar 21, 2025

FYI ready for the initial review. The unit tests are still in progress, but I would love to have initial feedback meanwhile.

@mgascam
Copy link
Contributor

mgascam commented Mar 24, 2025

@htdat I noticed the following error while testing. It saw it when I tried to visit the order details page, after a checkout:

Fatal error: Uncaught Error: Class "WCPay\Constants\Refund_Status" not found in /var/www/html/wp-content/plugins/woocommerce-payments/includes/class-wc-payment-gateway-wcpay.php:2327 Stack trace: #0 /var/www/html/wp-content/plugins/woocommerce-payments/includes/admin/class-wc-payments-admin.php(833): WC_Payment_Gateway_WCPay->has_refund_failed(Object(Automattic\WooCommerce\Admin\Overrides\Order)) #1 /var/www/html/wp-includes/class-wp-hook.php(324): WC_Payments_Admin->enqueue_payments_scripts('woocommerce_pag...') #2 /var/www/html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array) #3 /var/www/html/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #4 /var/www/html/wp-admin/admin-header.php(118): do_action('admin_enqueue_s...', 'woocommerce_pag...') #5 /var/www/html/wp-admin/admin.php(239): require_once('/var/www/html/w...') #6 {main} thrown in /var/www/html/wp-content/plugins/woocommerce-payments/includes/class-wc-payment-gateway-wcpay.php on line 2327

I'm a bit confused, since the class should be available, given the call to use WCPay\Constants\Refund_Status;. Also we are already using other constants in the same file 🤔 Sharing in case it rings a bell.

Copy link
Contributor

@mgascam mgascam left a comment

Choose a reason for hiding this comment

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

I think we are missing to include the new constant file for the refund status:

diff --git a/includes/class-wc-payments.php b/includes/class-wc-payments.php
index 41759213d..321b1f1b4 100644
--- a/includes/class-wc-payments.php
+++ b/includes/class-wc-payments.php
@@ -473,6 +473,7 @@ class WC_Payments {
                include_once __DIR__ . '/exceptions/class-invalid-address-exception.php';
                include_once __DIR__ . '/constants/class-base-constant.php';
                include_once __DIR__ . '/constants/class-country-code.php';
+               include_once __DIR__ . '/constants/class-refund-status.php';
                include_once __DIR__ . '/constants/class-country-test-cards.php';
                include_once __DIR__ . '/constants/class-currency-code.php';
                include_once __DIR__ . '/constants/class-fraud-meta-box-type.php';

@htdat
Copy link
Member Author

htdat commented Mar 25, 2025

@mgascam - yeah, I missed adding the file into the include list. The PR is ready to review. I've added tests covering for the webhook processing change; but I will still need to add tests for changes in Order Service.

In the meantime, you can review the code too :D

Edit: with the most recent commit, all tests have been created and updated for changes.

@htdat htdat requested a review from a team March 25, 2025 14:56
@mgascam
Copy link
Contributor

mgascam commented Mar 25, 2025

Hi @htdat I wanted to share one scenario I found while testing the PR. I did the checkout with the Asynchronous success card. The thing is my test account did not have balance to process the refund, so it was pending and I canceled it in the Stripe dashboard. This is the payment for your reference: https://dashboard.stripe.com/test/connect/accounts/acct_1PxRhsR4vt8oOW8F/payments/pi_3R6dCwR4vt8oOW8F1rjgCeW9. If you have a look, you can see that the last triggered event was refund.failed. The status of this event is "canceled", but I don't think we are handling it. As a result, things are out of sync and the last order note in my test store is:
Screenshot 2025-03-25 at 20 46 40

Sorry It's a bit late for me to debug it and confirm my theory. I'll do it tomorrow but just wanted to share with you in case you can also have a look 🙏

@htdat htdat force-pushed the fix/1195-pending-refund branch from aec38a9 to d27e300 Compare March 26, 2025 04:23
@htdat
Copy link
Member Author

htdat commented Mar 26, 2025

Thanks @mgascam. I handled that canceled status here in this commit 0028acf

Basically, the canceled status is only possible when refunding from Stripe Dash. It's not possible from WooPayments client side as we have not supported the canceled endpoint for refunds yet. Given these facts, the chance to see the canceled status is very low but anyway, we could support it without much effort.

Copy link
Contributor

@mgascam mgascam left a comment

Choose a reason for hiding this comment

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

Thanks for addressing my comments @htdat. The code changes look good to me. I appreciate the streamlined process as well of the addition of the refund status constant. I left a comment regarding the public docs url to double-check with you. I also saw an opportunity to enhance the refund failure reason in the order notes, but we can tackle it in a follow up issue.

Manual tests worked as intended for me. I tested both the success and failure flows from the Stripe dash, the Order admin and the timeline and did not find any issues. Great work!

@htdat htdat added this pull request to the merge queue Mar 27, 2025
Merged via the queue into develop with commit fba4cf5 Mar 27, 2025
28 checks passed
@htdat htdat deleted the fix/1195-pending-refund branch March 27, 2025 02:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect handling of pending refund
3 participants