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
# File 'app/models/spree/payment/cancellation.rb', line 28

def cancel(payment)
  if response = payment.payment_method.try_void(payment)
    payment.handle_void_response(response)
  else
    payment.refunds.create!(amount: payment.credit_allowed, reason: refund_reason).perform!
  end
end