Skip to content

Commit

Permalink
Destroy and rebuild refund on gateway error
Browse files Browse the repository at this point in the history
If we don't destroy unsuccessful refund on payment gateway errors
we'll end up having a refund associated to the payment without
the actual refund processed.

Since at the moment refunds have no states to indicate whether
is successful or not, the system is considering it as done and
doesn't allow to perform a new one since the credit allowed amount
for the payment becomes 0:

payment.credit_allowed = payment total - refund amount

and this formula is true also when the refund has not been succesful.

This comes from our past behavior of creating and calling perform! on
refunds at the same time. Records weren't created if the refund failed
to perform.

A more clean solution would be considering in that formula only
refunds that are completed but at the moment there's no way to
do that. This way we can also keep track of the failed attempts
to perform refunds, saving all the information we get back from the
gateway.
  • Loading branch information
kennyadsl committed Nov 5, 2020
1 parent a6abe8c commit 70dcce9
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions backend/app/controllers/spree/admin/refunds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ def build_resource
end
end

def rebuild_refund_on_perform_failure
failed_refund_attributes = @refund.attributes
@refund.destroy!
@refund = build_resource
@refund.attributes = failed_refund_attributes
end

def spree_core_gateway_error(error)
rebuild_refund_on_perform_failure
flash[:error] = error.message
render :new
end
Expand Down

0 comments on commit 70dcce9

Please sign in to comment.