epayments_client 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed82dd84528bda8dc6a0792b3a502eb928c0de51
4
+ data.tar.gz: b099d1c89c0267230b44a01f4c19bb8367463f12
5
+ SHA512:
6
+ metadata.gz: 84c9a62d909159bc534e3bb2de8157dd5d7d70c1845489579b3ca5892d658b27d27322e40afbeeb28a05827d166c78e0926af78a9345120bbc3af76be845f735
7
+ data.tar.gz: 2e9f3a09b3cc47bba5170a1bf86c653427ae15b5edf069044516d8cd5ea6488c269b1b69ca4fe15af66690a39a5093934f0b9e3feb3c498ec24a8623cd4c75da
@@ -0,0 +1,133 @@
1
+ require 'net/https'
2
+ require 'logger'
3
+ require 'base64'
4
+ require 'active_support/all'
5
+
6
+ module Epa
7
+ module Json
8
+ class Client
9
+
10
+ DEFAULTS = {
11
+ api_secret: 'mYn6hrkg0ILqrtIp8KSD',
12
+ api_name: 'epayments'
13
+ }
14
+
15
+ API_URL = 'https://api.epayments.com'
16
+
17
+ attr_accessor :token, :config, :logger
18
+
19
+ def initialize(username, password, options = {})
20
+ @username = username
21
+ @password = password
22
+ @config = DEFAULTS.merge options
23
+ @logger = Logger.new @config[:log] ? STDOUT : nil
24
+ end
25
+
26
+ def get_token
27
+ data_hash = {
28
+ grant_type: 'password',
29
+ username: @username,
30
+ password: @password
31
+ }
32
+ secret = Base64.encode64("#{config[:api_name]}:#{config[:api_secret]}")
33
+
34
+ response = call_json_api '/token', 'POST', data_hash.to_query, 'Authorization' => "Basic #{secret}"
35
+ raise AuthorizationError, response["error_description"] unless response["access_token"]
36
+ response["access_token"]
37
+ end
38
+
39
+ def balance(currency = 'USD', ep_id = nil)
40
+ response = user_info
41
+ wallet = if ep_id
42
+ response["ewallets"].detect { |w| w["ePid"] == ep_id }
43
+ else
44
+ response["ewallets"].first
45
+ end
46
+ raise ApiError, 'wallet not found' unless wallet
47
+ wallet["balances"].detect { |b| b["currency"] == currency.downcase }["currentBalance"]
48
+ end
49
+
50
+ def transfer_funds(options = {})
51
+ payload = {
52
+ confirmation: nil,
53
+ mode: "ValidateAndExecute",
54
+ sourcePurse: options[:from],
55
+ transfers: [
56
+ {
57
+ amount: options[:amount],
58
+ currency: options[:currency] || 'USD',
59
+ details: options[:details],
60
+ paymentReceiver: options[:to]
61
+ }
62
+ ]
63
+ }
64
+
65
+ response = internal_payment payload
66
+ raise PaymentError, response["errorMsgs"].join(', ') unless response["confirmationMessage"]
67
+
68
+ payload[:confirmation] = {
69
+ code: guess_code(options[:secret_code], response["confirmationMessage"]),
70
+ sessionId: response["confirmationSessionId"]
71
+ }
72
+
73
+ result_response = internal_payment payload
74
+ result_response["transfers"].first["transactionId"]
75
+ end
76
+
77
+ def user_info
78
+ call_json_api '/v1/user', 'GET', "", headers
79
+ end
80
+
81
+ def internal_payment(payload)
82
+ call_json_api '/v1/InternalPayment/', 'PUT', payload.to_json, headers
83
+ end
84
+
85
+ def transaction_history(options)
86
+ payload = {
87
+ from: (options[:from] || 1.day.ago).to_i,
88
+ till: (options[:to] || Time.now).to_i,
89
+ take: options[:take] || 20,
90
+ skip: options[:skip] || 0
91
+ }
92
+
93
+ response = call_json_api '/v1/transactions', 'POST', payload.to_json, headers
94
+ response["transactions"]
95
+ end
96
+
97
+ private
98
+
99
+ def headers
100
+ @token ||= get_token
101
+ {
102
+ 'Authorization' => "Bearer #{@token}",
103
+ 'Accept' => 'application/json',
104
+ 'Content-Type' => 'application/json'
105
+ }
106
+ end
107
+
108
+ def call_json_api(path, method = 'get', payload = "", headers = {})
109
+ uri = URI(API_URL)
110
+ uri.path = path
111
+ http = Net::HTTP.new(uri.host, uri.port)
112
+ http.use_ssl = true
113
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
114
+ request = "Net::HTTP::#{method.downcase.camelize}".constantize.new(uri.request_uri, headers)
115
+ request.body = payload
116
+ logger.info "Request url: #{uri}, payload: #{payload}"
117
+
118
+ # Send the request
119
+ response = http.request(request)
120
+ json_response = JSON.parse(response.body)
121
+ logger.info "Response: #{json_response.inspect}"
122
+
123
+ json_response
124
+ end
125
+
126
+ def guess_code(code, message)
127
+ numbers = message.scan(/\d\,\d\,\d/).first
128
+ numbers.split(/\,/).map { |i| code[i.to_i - 1] }.join
129
+ end
130
+
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,13 @@
1
+ module Epa
2
+ module Json
3
+ class ApiError < StandardError
4
+ end
5
+
6
+ class PaymentError < ApiError
7
+ end
8
+
9
+ class AuthorizationError < ApiError
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,2 @@
1
+ require 'epa/json/client'
2
+ require 'epa/json/exceptions'
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: epayments_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Artem Harmaty
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Ruby wrapper for epayments JSON API
28
+ email: harmaty@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/epa/json/client.rb
34
+ - lib/epa/json/exceptions.rb
35
+ - lib/epayments_client.rb
36
+ homepage: https://github.com/harmaty/epayments_client
37
+ licenses: []
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.4.6
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Epayments client
59
+ test_files: []