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

Handle failed refunds #411

Merged
merged 9 commits into from
Apr 22, 2020
Merged

Handle failed refunds #411

merged 9 commits into from
Apr 22, 2020

Conversation

jrodger
Copy link
Contributor

@jrodger jrodger commented Feb 6, 2020

Fixes #312

Changes proposed in this Pull Request

  • Adds a REST endpoint for receiving webhooks forwarded from WP.com
  • Initial action is simply adding an order note when we get an update regarding a failed refund.

Local Environment Testing instructions

  • Apply server PR https://github.com/Automattic/woocommerce-payments-server/pull/159 to your local server and forward Stripe webhooks to it using the CLI: ./local/bin/listen-to-webhooks.sh
  • Check that the mocked details in functions.php - wcpay_server_remote_request match your local setup. This replaces the Jetpack call with a simple HTTP request.
  • Apply dev tools PR https://github.com/Automattic/woocommerce-payments-dev-tools/pull/4 to your local site. This allows the simple HTTP request to work without proper Jetpack authentication.
  • Make a purchase with test card 4000 0000 0000 5126 (docs)
  • Refund the purchase in wp-admin
  • The refund will succeed, but then asynchronously fail. Wait for the refund to fail, refresh the page and ensure an order note is added indicating that the refund failed

Sandbox Testing instructions

  • Apply server PR https://github.com/Automattic/woocommerce-payments-server/pull/159 to your sandbox and forward Stripe webhooks to it using the CLI
  • Make a purchase with test card 4000 0000 0000 5126 (docs)
  • Refund the purchase in wp-admin
  • The refund will succeed, but then asynchronously fail. Wait for the refund to fail, refresh the page and ensure an order note is added indicating that the refund failed

@vbelolapotkov
Copy link
Collaborator

@jrodger tried twice but wasn't able to reproduce the test (see screenshots below). Despite the server is running the correct branch I'm not sure the webhook reached my local site. Tried to set a breakpoint in webhook-controller->handle_webhook but never reached it. What's the best way to troubleshoot it?

P.s. Tried with localhost and ngrok, no difference.

Screenshot 2020-02-13 at 16 24 24

Screenshot 2020-02-13 at 16 24 47

@jrodger
Copy link
Contributor Author

jrodger commented Feb 13, 2020

🤔 We can see that the refund is failing from your first screenshot, so that's good.

That failure triggers a charge.refund.updated webhook event from Stripe to WP.com. Can you see that in your Stripe CLI? What status is WP.com returning for that event?

If that's returning 200 then we'll need to dig a little deeper. Can you try putting something like error_log( print_r( $response_array, true ) ); at the bottom of server's handle_webhook function? You can then see if anything gets logged out using something like tail --lines=100 /tmp/php-errors on your sandbox. Maybe the response code it's returning will give us a clue, or if nothing is logged then it suggests we're erroring out earlier somewhere.

@jrodger
Copy link
Contributor Author

jrodger commented Feb 13, 2020

Forgot to mention, please don't spend too long on this if there are other PRs needing review that are required for beta.

@vbelolapotkov
Copy link
Collaborator

That failure triggers a charge.refund.updated webhook event from Stripe to WP.com. Can you see that in your Stripe CLI? What status is WP.com returning for that event?

Here is the link to refund_update event:
https://dashboard.stripe.com/acct_1FzKmRFL7IKwCJxf/test/events/evt_1GBhy6FL7IKwCJxfNUgwK4Wk

According to the server response was 200 OK

If that's returning 200 then we'll need to dig a little deeper.

Will try again on Monday.

Copy link
Collaborator

@vbelolapotkov vbelolapotkov left a comment

Choose a reason for hiding this comment

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

@jrodger I was able to set up stripe CLI and reproduce testing instruction with webhooks forwarded to the sandbox. Well done!

Some general thoughts:

  1. What about tests for webhook controller class?
  2. As @RadoslavGeorgiev noticed we have a successful confirmation note first and a failure note afterwards. So it's probably a good idea to update existing messaging for order notes to avoid confusion in case of refund failure. E.g. Refund request was successfully sent as an alternative to existing success message.

if ( $order_id ) {
return wc_get_order( $order_id );
}
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just curious is returning false is better than returning NULL? AFAIK NULL is intended for informing about missing value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I much prefer having a single return type, so yeah, I prefer returning null to indicate a missing value. However, I thought I'd leave this as is since I was just moving it from another class.

It's only used in one other place, so we could take the opportunity to change it now?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's only used in one other place

Actually there are two. And this one leads to an error if false or null is returned. IMO we should fix it either here on in another PR.

so we could take the opportunity to change it now

Now I see that wc_get_order can return a boolean value, and forcing it to return null without explicit benefit feels weird. So let's leave it as is now.

@jrodger
Copy link
Contributor Author

jrodger commented Apr 7, 2020

FYI - I've updated the testing instructions so that both the local and sandbox configurations can be tested.

@vbelolapotkov vbelolapotkov self-assigned this Apr 8, 2020
Copy link
Collaborator

@vbelolapotkov vbelolapotkov left a comment

Choose a reason for hiding this comment

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

Great progress @jrodger, thank you ! 🎉 I've tested it both in sandbox mode and with a local server in docker. Changes work as expected. Left some note regarding the changes!

tests/admin/test-class-wc-rest-payments-webhook.php Outdated Show resolved Hide resolved
jrodger added 7 commits April 21, 2020 10:53
This logic is required by other parts of the code base, so extracting
it out allows it to be shared. The unit tests have been left as is for
now, but this also allows for database access to be mocked more easily.
The initial implementation just handles refund updated events and adds
an order note in the event of a failure.
This rule is generating some false positives. We can try turning it on
again if it gets fixed in a later PHPCS version. Alternatively we could
start relying on a static code analyser for more robust checks on how
we're using exceptions.
@jrodger jrodger force-pushed the add/handling-for-failed-refunds branch from 1d1ee4d to e2fcaff Compare April 21, 2020 10:25
@jrodger
Copy link
Contributor Author

jrodger commented Apr 21, 2020

Thanks for the detailed review @vbelolapotkov. I think I've addressed all your comments now. A lot of my comments boil down to "let's do this later", absolutely feel free to push back on any of those!

As @RadoslavGeorgiev noticed we have a successful confirmation note first and a failure note afterwards. So it's probably a good idea to update existing messaging for order notes to avoid confusion in case of refund failure. E.g. Refund request was successfully sent as an alternative to existing success message.

I've raised #601 to discuss this point because the more I thought about it the more work it seemed.

Copy link
Collaborator

@vbelolapotkov vbelolapotkov left a comment

Choose a reason for hiding this comment

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

Gave it a final round of tests, works as expected! Thank you @jrodger !
image

vbelolapotkov
vbelolapotkov approved these changes Apr 21, 2020
@jrodger jrodger merged commit 6e19910 into master Apr 22, 2020
@frosso frosso deleted the add/handling-for-failed-refunds branch January 16, 2024 13:57
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.

Refund displaying as successful in order details, even though refund failed
5 participants