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

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)

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



15
16
17
# File 'app/models/spree/payment/cancellation.rb', line 15

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

Instance Attribute Details

#reasonObject (readonly)

Returns the value of attribute reason



11
12
13
# File 'app/models/spree/payment/cancellation.rb', line 11

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:



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

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