Class: Spree::Payment::Cancellation

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/payment/cancellation.rb

Overview

Payment cancellation handler

Cancels a payment by trying to void first and if that fails creating a refund about the full amount instead.

Constant Summary collapse

DEFAULT_REASON =
'Order canceled'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reason: DEFAULT_REASON) ⇒ Cancellation

Returns a new instance of Cancellation.

Parameters:

  • reason (String) (defaults to: DEFAULT_REASON)

    (DEFAULT_REASON) - The reason used to create the Spree::RefundReason



17
18
19
# File 'app/models/spree/payment/cancellation.rb', line 17

def initialize(reason: DEFAULT_REASON)
  @reason = reason
end

Instance Attribute Details

#reasonObject (readonly)

Returns the value of attribute reason.



13
14
15
# File 'app/models/spree/payment/cancellation.rb', line 13

def reason
  @reason
end

Instance Method Details

#cancel(payment) ⇒ Object

Cancels a payment

Tries to void the payment by asking the payment method to try a void, if that fails create a refund about the allowed credit amount instead.

Parameters:



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/spree/payment/cancellation.rb', line 28

def cancel(payment)
  # For payment methods already implemeting `try_void`
  if try_void_available?(payment.payment_method)
    if response = payment.payment_method.try_void(payment)
      payment.send(:handle_void_response, response)
    else
      payment.refunds.create!(amount: payment.credit_allowed, reason: refund_reason)
    end
  else
    # For payment methods not yet implemeting `try_void`
    deprecated_behavior(payment)
  end
end