class Epa::Json::Client
Constants
- API_URL
- DEFAULTS
Attributes
config[RW]
logger[RW]
token[RW]
Public Class Methods
new(username, password, options = {})
click to toggle source
# File lib/epa/json/client.rb, line 19 def initialize(username, password, options = {}) @username = username @password = password @config = DEFAULTS.merge options @logger = Logger.new @config[:log] ? STDOUT : nil end
Public Instance Methods
balance(currency = 'USD', ep_id = nil)
click to toggle source
# File lib/epa/json/client.rb, line 39 def balance(currency = 'USD', ep_id = nil) response = user_info wallet = if ep_id response["ewallets"].detect { |w| w["ePid"] == ep_id } else response["ewallets"].first end raise ApiError, 'wallet not found' unless wallet wallet["balances"].detect { |b| b["currency"] == currency.downcase }["currentBalance"] end
call_json_api(path, method = 'get', payload = "", headers = {})
click to toggle source
# File lib/epa/json/client.rb, line 97 def call_json_api(path, method = 'get', payload = "", headers = {}) uri = URI(API_URL) uri.path = path http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = "Net::HTTP::#{method.downcase.camelize}".constantize.new(uri.request_uri, headers) request.body = payload logger.info "Request url: #{uri}, payload: #{payload}" # Send the request response = http.request(request) json_response = JSON.parse(response.body) logger.info "Response: #{json_response.inspect}" json_response end
get_token()
click to toggle source
# File lib/epa/json/client.rb, line 26 def get_token data_hash = { grant_type: 'password', username: @username, password: @password } secret = Base64.encode64("#{config[:api_name]}:#{config[:api_secret]}") response = call_json_api '/token', 'POST', data_hash.to_query, 'Authorization' => "Basic #{secret}" raise AuthorizationError, response["error_description"] unless response["access_token"] response["access_token"] end
internal_payment(payload)
click to toggle source
# File lib/epa/json/client.rb, line 81 def internal_payment(payload) call_json_api '/v1/InternalPayment/', 'PUT', payload.to_json, headers end
transaction_history(options)
click to toggle source
# File lib/epa/json/client.rb, line 85 def transaction_history(options) payload = { from: (options[:from] || 1.day.ago).to_i, till: (options[:to] || Time.now).to_i, take: options[:take] || 20, skip: options[:skip] || 0 } response = call_json_api '/v1/transactions', 'POST', payload.to_json, headers response["transactions"] end
transfer_funds(options = {})
click to toggle source
# File lib/epa/json/client.rb, line 50 def transfer_funds(options = {}) payload = { confirmation: nil, mode: "ValidateAndExecute", sourcePurse: options[:from], transfers: [ { amount: options[:amount], currency: options[:currency] || 'USD', details: options[:details], paymentReceiver: options[:to] } ] } response = internal_payment payload raise PaymentError, response["errorMsgs"].join(', ') unless response["confirmationMessage"] payload[:confirmation] = { code: guess_code(options[:secret_code], response["confirmationMessage"]), sessionId: response["confirmationSessionId"] } result_response = internal_payment payload result_response["transfers"].first["transactionId"] end
user_info()
click to toggle source
# File lib/epa/json/client.rb, line 77 def user_info call_json_api '/v1/user', 'GET', "", headers end
Private Instance Methods
guess_code(code, message)
click to toggle source
# File lib/epa/json/client.rb, line 126 def guess_code(code, message) numbers = message.scan(/\d\,\d\,\d/).first numbers.split(/\,/).map { |i| code[i.to_i - 1] }.join end
headers()
click to toggle source
# File lib/epa/json/client.rb, line 117 def headers @token ||= get_token { 'Authorization' => "Bearer #{@token}", 'Accept' => 'application/json', 'Content-Type' => 'application/json' } end