Sha256: c11950b81e1f8f21746c0a452baf420489943c5fe2de4609ad43c83e299b3ac6

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

require 'active_support/core_ext/hash'

module Datatrans::Web
  class Transaction
    include Datatrans::Common

    attr_accessor :request
    attr_reader :response, :params

    def initialize(params)
      raise 'Please define Datatrans.sign_key or set it to false!' unless Datatrans.sign_key == false || Datatrans.sign_key.present?

      params = params.to_hash
      params.symbolize_keys!
      params.reverse_merge!(:reqtype => 'NOA', :useAlias => 'yes', :hiddenMode => 'yes')
      @params = params
    end

    def signature
      sign(Datatrans.merchant_id, params[:amount], params[:currency], params[:refno])
    end

    def authorize
      @response = AuthorizeResponse.new(params)
      @response.successful?
    end

    def method_missing(method, *args, &block)
      if response.respond_to? method.to_sym
        response.send(method)
      else
        super
      end
    end
  end

  module ViewHelper
    def datatrans_notification_request_hidden_fields(transaction)
      fields = [
        hidden_field_tag(:merchantId, Datatrans.merchant_id),
        hidden_field_tag(:hiddenMode, transaction.params[:hiddenMode]),
        hidden_field_tag(:reqtype, transaction.params[:reqtype]),
        hidden_field_tag(:amount, transaction.params[:amount]),
        hidden_field_tag(:currency, transaction.params[:currency]),
        hidden_field_tag(:useAlias, transaction.params[:useAlias]),
        hidden_field_tag(:sign, transaction.signature),
        hidden_field_tag(:refno, transaction.params[:refno]),
        hidden_field_tag(:uppCustomerDetails, transaction.params[:uppCustomerDetails]),
      ]

      [:uppCustomerName, :uppCustomerEmail].each do |field_name|
        if transaction.params[field_name].present?
          fields << hidden_field_tag(field_name, transaction.params[field_name])
        end
      end

      fields.join.html_safe
    end
  end
end

require 'datatrans/web/transaction/authorize'

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
datatrans-2.4.0 lib/datatrans/web/transaction.rb
datatrans-2.3.1 lib/datatrans/web/transaction.rb