|
| 1 | +module Spree |
| 2 | + class Refund < Spree::Base |
| 3 | + belongs_to :payment, inverse_of: :refunds |
| 4 | + belongs_to :return_authorization # optional |
| 5 | + |
| 6 | + has_many :log_entries, as: :source |
| 7 | + |
| 8 | + validates :payment, presence: true |
| 9 | + validates :transaction_id, presence: true |
| 10 | + validates :amount, presence: true, numericality: {greater_than: 0} |
| 11 | + |
| 12 | + # attempts to perform the refund |
| 13 | + # if successful it returns the refund, otherwise it raises |
| 14 | + def self.perform!(payment, amount, return_authorization=nil) |
| 15 | + check_amount(amount) |
| 16 | + check_environment(payment) |
| 17 | + |
| 18 | + credit_cents = Spree::Money.new(amount.to_f, currency: payment.currency).money.cents |
| 19 | + |
| 20 | + response = process!(payment, credit_cents) |
| 21 | + |
| 22 | + refund = create!({ |
| 23 | + payment: payment, |
| 24 | + return_authorization: return_authorization, |
| 25 | + transaction_id: response.authorization, |
| 26 | + amount: amount, |
| 27 | + }) |
| 28 | + |
| 29 | + refund.log_entries.create!(details: response.to_yaml) |
| 30 | + |
| 31 | + refund |
| 32 | + end |
| 33 | + |
| 34 | + # return an activemerchant response object if successful or else raise an error |
| 35 | + def self.process!(payment, credit_cents) |
| 36 | + response = if payment.payment_method.payment_profiles_supported? |
| 37 | + payment.payment_method.credit(credit_cents, payment.source, payment.transaction_id, {}) |
| 38 | + else |
| 39 | + payment.payment_method.credit(credit_cents, payment.transaction_id, {}) |
| 40 | + end |
| 41 | + |
| 42 | + if !response.success? |
| 43 | + logger.error(Spree.t(:gateway_error) + " #{response.to_yaml}") |
| 44 | + text = response.params['message'] || response.params['response_reason_text'] || response.message |
| 45 | + raise Core::GatewayError.new(text) |
| 46 | + end |
| 47 | + |
| 48 | + response |
| 49 | + rescue ActiveMerchant::ConnectionError => e |
| 50 | + logger.error(Spree.t(:gateway_error) + " #{e.inspect}") |
| 51 | + raise Core::GatewayError.new(Spree.t(:unable_to_connect_to_gateway)) |
| 52 | + end |
| 53 | + |
| 54 | + # Saftey check to make sure we're not accidentally performing operations on a live gateway. |
| 55 | + # Ex. When testing in staging environment with a copy of production data. |
| 56 | + def self.check_environment(payment) |
| 57 | + return if payment.payment_method.environment == Rails.env |
| 58 | + message = Spree.t(:gateway_config_unavailable) + " - #{Rails.env}" |
| 59 | + raise Core::GatewayError.new(message) |
| 60 | + end |
| 61 | + |
| 62 | + def self.check_amount(amount) |
| 63 | + unless amount > 0 |
| 64 | + raise Core::GatewayError.new(Spree.t(:refund_amount_must_be_greater_than_zero)) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def money |
| 69 | + Spree::Money.new(amount, { currency: payment.currency }) |
| 70 | + end |
| 71 | + alias display_amount money |
| 72 | + |
| 73 | + end |
| 74 | +end |
0 commit comments