Sha256: 06e060b88c2d99e1dcc063ff876b10dae3c3ca9e23cc397237c7d7cf685e4364

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

require_relative './nexio_base_gateway'

module ActiveMerchant
  module Billing
    class NexioApmGateway < NexioBaseGateway
      self.display_name = 'Nexio AMP'
      self.base_path = '/apm/v3'
      self.abstract_class = false

      OneTimeToken = Struct.new(:token, :iframe_url, :redirect_urls, :button_urls)

      def generate_token(money, options = {})
        post = build_payload(options)
        add_invoice(post, money, options)
        post[:data][:paymentMethod] = options[:payment_method] if options[:payment_method].present?
        add_order_data(post, options)
        post[:customerRedirectUrl] = options[:callback_url] if options[:callback_url].present?
        post[:processingOptions][:saveRecurringToken] = true if options[:save_token]
        post[:isAuthOnly] = true if options[:is_auth_only]
        resp = commit('token', post)
        return unless resp.success?

        OneTimeToken.new(
          resp.params['token'],
          resp.params['expressIFrameUrl'],
          map_urls(resp.params['redirectUrls']),
          map_urls(resp.params['buttonIFrameUrls'])
        )
      end

      def purchase(money, payment, options = {})
        post = build_payload(options)
        add_invoice(post, money, options)
        post[:apm] = { token: payment.is_a?(Spree::PaymentSource) ? payment.gateway_payment_profile_id : payment }
        add_order_data(post, options)
        post[:isAuthOnly] = true if options[:is_auth_only]
        commit('process', post)
      end

      def authorize(money, payment, options = {})
        purchase(money, payment, options.merge(is_auth_only: true))
      end

      private

      def map_urls(list)
        list.each_with_object({}) { |item, acc| acc[item['paymentMethod']] = item['url'] }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nexio_activemerchant-0.4.1 lib/active_merchant/billing/gateways/nexio_apm_gateway.rb
nexio_activemerchant-0.4.0 lib/active_merchant/billing/gateways/nexio_apm_gateway.rb
nexio_activemerchant-0.3.1 lib/active_merchant/billing/gateways/nexio_apm_gateway.rb