Module: Paymaya::Helper

Defined in:
lib/paymaya/helper.rb

Constant Summary collapse

MONEY_PARAMS =
[
  :tax, :subtotal, :discount, :shipping_fee, :amount, :value
].freeze

Class Method Summary collapse

Class Method Details

.camelify(input) ⇒ Object


27
28
29
# File 'lib/paymaya/helper.rb', line 27

def self.camelify(input)
  transform(input, :camelify, :to_camelback_keys)
end

.checkout_public_auth_headersObject


79
80
81
# File 'lib/paymaya/helper.rb', line 79

def self.checkout_public_auth_headers
  auth_headers(Paymaya.config.checkout_public_key)
end

.checkout_secret_auth_headersObject


83
84
85
# File 'lib/paymaya/helper.rb', line 83

def self.checkout_secret_auth_headers
  auth_headers(Paymaya.config.checkout_secret_key)
end

.format_amount(num, currency) ⇒ Object


107
108
109
# File 'lib/paymaya/helper.rb', line 107

def self.format_amount(num, currency)
  Money.new(num, currency, '').to_s
end

.payment_facilitator(submerchant_id:, submerchant_name:, submerchant_city:, submerchant_pos_country:, submerchant_country_code:, submerchant_state_code: nil) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/paymaya/helper.rb', line 9

def self.payment_facilitator(submerchant_id:, submerchant_name:,
  submerchant_city:, submerchant_pos_country:, submerchant_country_code:,
  submerchant_state_code: nil)
  pf = {
    smi: submerchant_id,
    smn: submerchant_name,
    mci: submerchant_city,
    mpc: submerchant_pos_country,
    mco: submerchant_country_code
  }
  pf[:mst] = submerchant_state_code unless submerchant_state_code.nil?
  pf
end

.payment_vault_public_auth_headersObject


71
72
73
# File 'lib/paymaya/helper.rb', line 71

def self.payment_vault_public_auth_headers
  auth_headers(Paymaya.config.payment_vault_public_key)
end

.payment_vault_secret_auth_headersObject


75
76
77
# File 'lib/paymaya/helper.rb', line 75

def self.payment_vault_secret_auth_headers
  auth_headers(Paymaya.config.payment_vault_secret_key)
end

.request(method, url, params, headers) ⇒ Object


95
96
97
98
99
100
101
# File 'lib/paymaya/helper.rb', line 95

def self.request(method, url, params, headers)
  response = RestClient::Request.execute(
    method: method, url: url,
    headers: headers, payload: camelify(params).to_json
  )
  snakify(JSON.parse(response))
end

.snakify(input) ⇒ Object


23
24
25
# File 'lib/paymaya/helper.rb', line 23

def self.snakify(input)
  transform(input, :snakify, :to_snake_keys)
end

.stringify_numbers(input, currency) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/paymaya/helper.rb', line 51

def self.stringify_numbers(input, currency)
  if input.is_a? Hash
    output = {}
    input.each do |k, v|
      output[k] = if (MONEY_PARAMS.include? k) && (v.is_a? Numeric)
        format_amount(v, currency)
      else
        stringify_numbers(v, currency)
      end
    end
    output
  elsif input.is_a? Array
    input.map do |elem|
      stringify_numbers(elem, currency)
    end
  else
    input.to_s
  end
end