From 85f82e8382d802967f99efcdd06f56e14a5953e5 Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Fri, 22 Jan 2021 13:24:27 +0100 Subject: [PATCH] Do not automatically perform! after redund creation After solidusio/solidus#3641 it's requested to create and perform refunds in two different operations. This commit aligns the code to the new standard. Co-authored-by: Rainer Dema --- .../payment_method/stripe_credit_card.rb | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/app/models/spree/payment_method/stripe_credit_card.rb b/app/models/spree/payment_method/stripe_credit_card.rb index 7e219d76..5566606d 100644 --- a/app/models/spree/payment_method/stripe_credit_card.rb +++ b/app/models/spree/payment_method/stripe_credit_card.rb @@ -87,10 +87,13 @@ def payment_intents_refund_reason def try_void(payment) if v3_intents? && payment.completed? - payment.refunds.create!( - amount: payment.credit_allowed, - reason: payment_intents_refund_reason - ).response + refund = perform_refund(payment) + + if refund.respnd_to?(:perform_response) + refund.perform_response + else + refund.response + end else void(payment.response_code, nil, nil) end @@ -190,6 +193,24 @@ def update_source!(source) source.cc_type = CARD_TYPE_MAPPING[source.cc_type] if CARD_TYPE_MAPPING.include?(source.cc_type) source end + + def perform_refund(payment) + refund = payment.refunds.build( + amount: payment.credit_allowed, + reason: payment_intents_refund_reason + ) + + if refund.respond_to?(:perform_after_create) + refund.perform_after_create = false + refund.save! + refund.perform! + else + refund.save! + refund.perform! if Gem::Requirement.new('>= 3.0.0.alpha').satisfied_by?(Spree.solidus_gem_version) + end + + refund + end end end end