Sha256: 1396bac5147e2cf8e495e12add4916ecfd33727596227814ad7c9338b08db073

Contents?: true

Size: 1.83 KB

Versions: 6

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module SolidusNexio
  class AlternativePaymentMethod < SolidusSupport.payment_method_parent_class
    include NexioPaymentCommons

    preference(:server, :string, default: 'test')
    preference(:merchant_id, :string, default: nil)
    preference(:auth_token, :string, default: nil)
    preference(:payment_method, :string, default: nil)
    preference(:save_token, :boolean, default: false)
    preference(:customer_redirect, :boolean, default: false)
    preference(:token_domain_presence, :string, default: nil)

    def partial_name
      'nexio_apm'
    end
    alias method_type partial_name

    def generate_token(options)
      order = Spree::Order.find_by(number: options.dig(:order, :number))
      return unless order

      amount = order.respond_to?(:order_total_after_store_credit) ? order.order_total_after_store_credit : order.total
      params = {
        payment_method: preferred_payment_method,
        save_token: preferred_save_token,
        is_auth_only: !auto_capture?
      }.merge!(description_payload(order, options), request_domain_payload(options), options)

      gateway.generate_token(amount.to_money.cents, params)
    end

    def payment_source_class
      ApmSource
    end

    private

    def gateway_class
      ActiveMerchant::Billing::NexioApmGateway
    end

    def request_domain_payload(options)
      request_domain = options.delete(:request_domain)
      return {} unless request_domain && preferred_token_domain_presence.present?

      {
        payload: {
          data: {
            preferred_token_domain_presence.to_sym => request_domain
          }
        }
      }
    end

    def description_payload(_order, _options)
      {
        payload: {
          data: {
            description: "eComm #{preferred_payment_method} payment"
          }
        }
      }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
solidus_nexio-0.7.8 app/models/solidus_nexio/alternative_payment_method.rb
solidus_nexio-0.7.7 app/models/solidus_nexio/alternative_payment_method.rb
solidus_nexio-0.7.6 app/models/solidus_nexio/alternative_payment_method.rb
solidus_nexio-0.7.5 app/models/solidus_nexio/alternative_payment_method.rb
solidus_nexio-0.7.4 app/models/solidus_nexio/alternative_payment_method.rb
solidus_nexio-0.7.3 app/models/solidus_nexio/alternative_payment_method.rb