Class: OffsitePayments::Integrations::Allpay::Helper

Inherits:
Helper
  • Object
show all
Defined in:
lib/offsite_payments/integrations/allpay.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, account, options = {}) ⇒ Helper

Returns a new instance of Helper.



136
137
138
139
140
# File 'lib/offsite_payments/integrations/allpay.rb', line 136

def initialize(order, , options = {})
  super
  add_field 'MerchantID', OffsitePayments::Integrations::Allpay.merchant_id
  add_field 'PaymentType', OffsitePayments::Integrations::Allpay::PAYMENT_TYPE
end

Class Method Details

.url_encode(text) ⇒ Object

Allpay .NET url encoding Code based from CGI.escape() Some special characters (e.g. “()*!”) are not escaped on Allpay server when they generate their check sum value, causing CheckMacValue Error.

TODO: The following characters still cause CheckMacValue error:

'<', "\n", "\r", '&'


165
166
167
168
169
170
171
172
# File 'lib/offsite_payments/integrations/allpay.rb', line 165

def self.url_encode(text)
  text = text.dup
  text.gsub!(/([^ a-zA-Z0-9\(\)\!\*_.-]+)/) do
    '%' + $1.unpack('H2' * $1.bytesize).join('%')
  end
  text.tr!(' ', '+')
  text
end

Instance Method Details

#encrypted_dataObject



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/offsite_payments/integrations/allpay.rb', line 146

def encrypted_data
  raw_data = @fields.sort.map{|field, value|
    # utf8, authenticity_token, commit are generated from form helper, needed to skip
    "#{field}=#{value}" if field!='utf8' && field!='authenticity_token' && field!='commit'
  }.join('&')

  hash_raw_data = "HashKey=#{OffsitePayments::Integrations::Allpay.hash_key}&#{raw_data}&HashIV=#{OffsitePayments::Integrations::Allpay.hash_iv}"
  url_encode_data = self.class.url_encode(hash_raw_data)
  url_encode_data.downcase!

  add_field 'CheckMacValue', Digest::MD5.hexdigest(url_encode_data).upcase
end

#merchant_trade_date(date) ⇒ Object



142
143
144
# File 'lib/offsite_payments/integrations/allpay.rb', line 142

def merchant_trade_date(date)
  add_field 'MerchantTradeDate', date.strftime('%Y/%m/%d %H:%M:%S')
end