Sha256: c15b322c79496954ffb3ace0650db8d91840e66918e3a6cc48c56dc23a376852

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 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]
        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)
        if payment.is_a?(Spree::PaymentSource)
          post[:apm] = { token: payment.gateway_payment_profile_id }
        else
          post[:apm] = { token: payment }
        end
        add_order_data(post, options)
        commit('process', post)
      end

      private

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nexio_activemerchant-0.3.0 lib/active_merchant/billing/gateways/nexio_apm_gateway.rb