Class: Payeezy::Transactions

Inherits:
Object
  • Object
show all
Defined in:
lib/payeezy.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Transactions

Returns a new instance of Transactions.



11
12
13
14
15
16
# File 'lib/payeezy.rb', line 11

def initialize(options = {})
  @url = options[:url]
  @apikey = options[:apikey]
  @apisecret = options[:apisecret]
  @token = options[:token]
end

Instance Method Details

#bin_to_hex(s) ⇒ Object



28
29
30
# File 'lib/payeezy.rb', line 28

def bin_to_hex(s)
  s.unpack('H*').first
end

#call_rest(url, data, headers) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/payeezy.rb', line 55

def call_rest(url, data, headers)
  rest_resource = RestClient::Resource.new(url)
  raw_response = response = {}
  begin
    raw_response = rest_resource.post data, headers
    response = parse(raw_response)
  rescue => e
    raw_response = e.response
    response = response_error(raw_response)
  rescue JSON::ParserError
    response = json_error(raw_response)
  end

  response
end

#commit(action, params) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/payeezy.rb', line 45

def commit(action, params)
  url = @url
  if action == :capture || action == :void || action == :refund || action == :split
    url = url + '/' + params[:transaction_id]
    params.delete(:transaction_id)
  end
  params[:transaction_type] = action
  call_rest(url, post_data(params), headers(post_data(params)))
end

#generate_hmac(nonce, current_timestamp, payload) ⇒ Object



22
23
24
25
26
# File 'lib/payeezy.rb', line 22

def generate_hmac(nonce, current_timestamp, payload)
  message = @apikey + nonce.to_s + current_timestamp.to_s + @token + payload
  hash = Base64.strict_encode64(bin_to_hex(OpenSSL::HMAC.digest('sha256', @apisecret, message)))
  hash
end

#handle_message(response, success) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/payeezy.rb', line 71

def handle_message(response, success)
  if success
    response['transaction_status']
  elsif response.key?('Error')
    response['Error'].map { |_, messages| messages }.join('. ')
  else
    response.inspect
  end
end

#headers(payload) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/payeezy.rb', line 32

def headers(payload)
  nonce = (SecureRandom.random_number *10000000000)
  current_timestamp = (Time.now.to_f*1000).to_i
  {
      'Content-Type' => 'application/json',
      'apikey' => @apikey,
      'token' => @token,
      'nonce' => nonce,
      'timestamp' => current_timestamp,
      'Authorization' => generate_hmac(nonce, current_timestamp, payload)
  }
end

#json_error(raw_response) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/payeezy.rb', line 97

def json_error(raw_response)
  msg = "Payeezy has returned an invalid response: [#{raw_response.inspect}]"
  {
      'Error' => {
          'messages' => msg
      }
  }
end

#parse(body) ⇒ Object



89
90
91
# File 'lib/payeezy.rb', line 89

def parse(body)
  JSON.parse(body)
end

#post_data(params) ⇒ Object



93
94
95
# File 'lib/payeezy.rb', line 93

def post_data(params)
  params.to_json
end

#response_error(raw_response) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/payeezy.rb', line 81

def response_error(raw_response)
  begin
    parse(raw_response)
  rescue JSON::ParserError
    json_error(raw_response)
  end
end

#transact(action, payload) ⇒ Object



18
19
20
# File 'lib/payeezy.rb', line 18

def transact(action, payload)
  commit(action, payload)
end