mollie / spree-mollie-gateway

Mollie payments for Spree Commerce.
https://www.mollie.com
16 stars 23 forks source link

Payment refunds should not update order. #31

Closed vernondegoede closed 5 years ago

vernondegoede commented 5 years ago

We received a bug report last week about order confirmation emails being delivered twice when a merchant refunds a payment within Mollie.

The issue At this moment, every time the Mollie webhook is called, the method update_by_mollie_status! is called. This will check what the current status of the payment is, and update the status of the Spree::Order / Spree::Payment accordingly. If the status of the Mollie payment is paid, we'll finalize the order and send the order confirmation.

This worked fine when we were still using the Mollie v1 API. However, since we're now using the v2 API, refunded is not a separate payment status anymore. Therefore, the order will be finalized again for every webhook call that happens after a payment refund.

Proposed solution To fix this, we should only finalize the order (which takes care of sending the order confirmation email) and set the completed_at to the current date + time if the order has not been paid for yet but the Mollie payment is paid. I added a test for this as well.

vernondegoede commented 5 years ago

Example log (./logs/mollie.log) after this change (which indicates that this works):

D, [2018-10-22T10:40:16.670939 #57543] DEBUG -- : Payment tr_czrueWVBWz created for order R078346786-PFX8VLDM

# User is redirected to Mollie checkout
D, [2018-10-22T10:40:16.778027 #57543] DEBUG -- : For order R078346786 redirect user to payment URL: https://www.mollie.com/paymentscreen/testmode/?method=creditcard&token=5hjntx
D, [2018-10-22T10:40:24.398401 #57543] DEBUG -- : Webhook called for payment tr_czrueWVBWz
D, [2018-10-22T10:40:24.523236 #57543] DEBUG -- : Checking Mollie payment status. Mollie payment has status paid
D, [2018-10-22T10:40:25.305417 #57543] DEBUG -- : Payment is paid and will transition to completed. Order will be finalized.
D, [2018-10-22T10:40:26.083063 #57543] DEBUG -- : Checking Mollie payment status. Mollie payment has status paid
D, [2018-10-22T10:40:26.083226 #57543] DEBUG -- : Payment is already completed. Not updating the payment status within Spree.

# User is redirected from Mollie checkout to Spree again
D, [2018-10-22T10:40:26.084751 #57543] DEBUG -- : Redirect URL visited for order R078346786-PFX8VLDM
D, [2018-10-22T10:41:53.460584 #57543] DEBUG -- : Webhook called for payment tr_7EVmhAaFTV
D, [2018-10-22T10:41:53.590312 #57543] DEBUG -- : Checking Mollie payment status. Mollie payment has status paid

# After partial refund for this payment
D, [2018-10-22T10:41:53.590408 #57543] DEBUG -- : Payment is refunded. Not updating the payment status within Spree.

# After canceling order from Spree admin
D, [2018-10-22T10:48:03.593397 #57543] DEBUG -- : Starting cancelation for tr_7EVmhAaFTV
D, [2018-10-22T10:48:03.719638 #57543] DEBUG -- : Molie payment tr_7EVmhAaFTV is not cancelable. Skipping any further updates.

# After another partial refund
D, [2018-10-22T10:48:45.320350 #57543] DEBUG -- : Webhook called for payment tr_7EVmhAaFTV
D, [2018-10-22T10:48:45.443716 #57543] DEBUG -- : Checking Mollie payment status. Mollie payment has status paid
D, [2018-10-22T10:48:45.443819 #57543] DEBUG -- : Payment is refunded. Not updating the payment status within Spree.