Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Update `Spree::Refund` with methods for decorating (#12299)

Łukasz Adamczyk6fc3d1a454bb
diff --git a/core/app/models/spree/refund.rb b/core/app/models/spree/refund.rb
index 1f69783490..8c320f49ae 100644
--- a/core/app/models/spree/refund.rb
+++ b/core/app/models/spree/refund.rb
@@ -76,13 +76,17 @@ module Spree
# return an activemerchant response object if successful or else raise an error
def process!(credit_cents)
+ refund_total_in_cents = calculate_refund_amount(credit_cents)
+
response = if payment.payment_method.payment_profiles_supported?
- payment.payment_method.credit(credit_cents, payment.source, payment.transaction_id, originator: self)
+ payment.payment_method.credit(refund_total_in_cents, payment.source, payment.transaction_id, originator: self)
else
- payment.payment_method.credit(credit_cents, payment.transaction_id, originator: self)
+ payment.payment_method.credit(refund_total_in_cents, payment.transaction_id, originator: self)
end
- unless response.success?
+ if response.success?
+ track_order_as_refunded(refund_total_in_cents)
+ else
Rails.logger.error(Spree.t(:gateway_error) + " #{response.to_yaml}")
text = response.params['message'] || response.params['response_reason_text'] || response.message
raise Core::GatewayError, text
@@ -94,6 +98,15 @@ module Spree
raise Core::GatewayError, Spree.t(:unable_to_connect_to_gateway)
end
+ def calculate_refund_amount(credit_cents)
+ # Overwrite this for more complex calculations
+ credit_cents
+ end
+
+ def track_order_as_refunded(credit_cents)
+ # You can track refunds here
+ end
+
def create_log_entry
log_entries.create!(details: @response.to_yaml)
end