Sha256: 14227c6efde4216f1766ab214ffee4bbe48c837ee452a695ed075dd8a934ac22

Contents?: true

Size: 1.18 KB

Versions: 67

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Spree
  class Payment
    # Payment cancellation handler
    #
    # Cancels a payment by trying to void first and if that fails
    # creating a refund about the full amount instead.
    #
    class Cancellation
      DEFAULT_REASON = 'Order canceled'.freeze

      attr_reader :reason

      # @param reason [String] (DEFAULT_REASON) -
      #   The reason used to create the Spree::RefundReason
      def initialize(reason: DEFAULT_REASON)
        @reason = reason
      end

      # 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.
      #
      # @param payment [Spree::Payment] - the payment that should be canceled
      #
      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

      private

      def refund_reason
        Spree::RefundReason.where(name: reason).first_or_create
      end
    end
  end
end

Version data entries

67 entries across 67 versions & 1 rubygems

Version Path
solidus_core-4.4.2 app/models/spree/payment/cancellation.rb
solidus_core-4.4.1 app/models/spree/payment/cancellation.rb
solidus_core-4.4.0 app/models/spree/payment/cancellation.rb
solidus_core-4.3.4 app/models/spree/payment/cancellation.rb
solidus_core-4.2.4 app/models/spree/payment/cancellation.rb
solidus_core-4.1.5 app/models/spree/payment/cancellation.rb
solidus_core-4.3.3 app/models/spree/payment/cancellation.rb
solidus_core-4.3.2 app/models/spree/payment/cancellation.rb
solidus_core-4.1.4 app/models/spree/payment/cancellation.rb
solidus_core-4.3.1 app/models/spree/payment/cancellation.rb
solidus_core-4.3.0 app/models/spree/payment/cancellation.rb
solidus_core-4.2.3 app/models/spree/payment/cancellation.rb
solidus_core-4.1.3 app/models/spree/payment/cancellation.rb
solidus_core-4.0.4 app/models/spree/payment/cancellation.rb
solidus_core-3.4.6 app/models/spree/payment/cancellation.rb
solidus_core-4.0.3 app/models/spree/payment/cancellation.rb
solidus_core-4.1.2 app/models/spree/payment/cancellation.rb
solidus_core-4.2.2 app/models/spree/payment/cancellation.rb
solidus_core-3.4.5 app/models/spree/payment/cancellation.rb
solidus_core-4.2.1 app/models/spree/payment/cancellation.rb