Class: Spree::Payment::Cancellation
- Inherits:
-
Object
- Object
- Spree::Payment::Cancellation
- 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
-
#reason ⇒ Object
readonly
Returns the value of attribute reason.
Instance Method Summary collapse
-
#cancel(payment) ⇒ Object
Cancels a payment.
-
#initialize(reason: DEFAULT_REASON) ⇒ Cancellation
constructor
A new instance of Cancellation.
Constructor Details
#initialize(reason: DEFAULT_REASON) ⇒ Cancellation
Returns a new instance of Cancellation.
17 18 19 |
# File 'app/models/spree/payment/cancellation.rb', line 17 def initialize(reason: DEFAULT_REASON) @reason = reason end |
Instance Attribute Details
#reason ⇒ Object (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.
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 |