class GoPay::Client
Public Class Methods
new(config)
click to toggle source
# File lib/gopay/client.rb, line 5 def initialize(config) @config = config end
Public Instance Methods
request(method, path, body_parameters: {})
click to toggle source
# File lib/gopay/client.rb, line 9 def request(method, path, body_parameters: {}) token = token get_token_scope(method, path) content_type = get_content_type(path) body_parameters = content_type == 'application/json' ? body_parameters.to_json : body_parameters begin response = RestClient::Request.execute(method: method, url: @config[:gate]+path, payload: body_parameters, headers: { "Accept" => "application/json", "Content-Type" => content_type, "Authorization" => "Bearer #{token}" }) rescue RestClient::ExceptionWithResponse => e raise Error.handle_gopay_error(e.response) end unless response.code == 200 raise Error.handle_gopay_error(response) end JSON.parse response.body end
Private Instance Methods
get_content_type(path)
click to toggle source
# File lib/gopay/client.rb, line 36 def get_content_type(path) if (path == '/api/payments/payment') || (path =~ /create-recurrence/) 'application/json' else 'application/x-www-form-urlencoded' end end
get_token_scope(method, path)
click to toggle source
# File lib/gopay/client.rb, line 28 def get_token_scope(method, path) if method == :post && path == '/api/payments/payment' 'payment-create' else 'payment-all' end end
token(scope = 'payment-create')
click to toggle source
payment-create - for new payment payment-all - for testing state etc
# File lib/gopay/client.rb, line 46 def token(scope = 'payment-create') response = RestClient.post( "#{@config[:gate]}/api/oauth2/token", { grant_type: 'client_credentials', scope: scope, }, { "Accept" => "application/json", "Content-Type" => "application/x-www-form-urlencoded", "Authorization" => "Basic #{Base64.encode64(@config[:client_id] + ':' + @config[:client_secret])}" }) JSON.parse(response.body)["access_token"] end