Class: PaysonAPI::V1::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/payson_api/v1/client.rb

Class Method Summary collapse

Class Method Details

.get_payment_details(payment_details_data) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/payson_api/v1/client.rb', line 17

def self.get_payment_details(payment_details_data)
  response = payson_request(
    PAYSON_API_PAYMENT_DETAILS_ACTION,
    payment_details_data.to_hash
  )
  response_hash = params_to_hash(response.body)
  PaysonAPI::V1::Responses::PaymentDetails.new(response_hash)
end

.hash_to_params(hash) ⇒ Object



43
44
45
# File 'lib/payson_api/v1/client.rb', line 43

def self.hash_to_params(hash)
  hash.map { |k, v| "#{k}=#{URI.encode_www_form_component(v.to_s)}" }.join('&')
end

.initiate_payment(payment_data) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/payson_api/v1/client.rb', line 8

def self.initiate_payment(payment_data)
  response = payson_request(
    PAYSON_API_PAY_ACTION,
    payment_data.to_hash
  )
  response_hash = params_to_hash(response.body)
  PaysonAPI::V1::Responses::Payment.new(response_hash)
end

.params_to_hash(params) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/payson_api/v1/client.rb', line 47

def self.params_to_hash(params)
  {}.tap do |hash|
    params.split('&').each do |param|
      key, val = param.split('=')
      hash[key] = URI.decode_www_form_component(val)
    end
  end
end

.payson_request(action, data) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/payson_api/v1/client.rb', line 56

def self.payson_request(action, data)
  path = format('/%<api_version>s/%<action>s/', api_version: PAYSON_API_VERSION, action: action)
  url = PaysonAPI::V1.api_base_url + path
  headers = {
    'PAYSON-SECURITY-USERID' => PaysonAPI::V1.config.api_user_id,
    'PAYSON-SECURITY-PASSWORD' => PaysonAPI::V1.config.api_password,
    'Content-Type' => 'application/x-www-form-urlencoded'
  }
  post(url, data, headers)
end

.post(url, data, headers) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/payson_api/v1/client.rb', line 67

def self.post(url, data, headers)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  req = Net::HTTP::Post.new(uri.request_uri)
  req.body = data.is_a?(Hash) ? hash_to_params(data) : data
  headers.each do |name, value|
    req[name] = value
  end
  http.request(req)
end

.update_payment(payment_update_data) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/payson_api/v1/client.rb', line 26

def self.update_payment(payment_update_data)
  response = payson_request(
    PAYSON_API_PAYMENT_DETAILS_ACTION,
    payment_update_data.to_hash
  )
  response_hash = params_to_hash(response.body)
  PaysonAPI::V1::Responses::PaymentUpdate.new(response_hash)
end

.validate_ipn(ipn_data) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/payson_api/v1/client.rb', line 35

def self.validate_ipn(ipn_data)
  response = payson_request(
    PAYSON_API_PAYMENT_VALIDATE_ACTION,
    ipn_data.to_s
  )
  PaysonAPI::V1::Responses::Validate.new(response.body)
end