class Satispay

Public Class Methods

new(env, security_bearer) click to toggle source
# File lib/satispay.rb, line 6
def initialize(env, security_bearer)
  @env = (env == 'prod' ? :production : :staging)
  @url = (env == 'prod' ? 'https://authservices.satispay.com' : 'https://staging.authservices.satispay.com')
  @security_bearer = security_bearer
end

Public Instance Methods

all_charges() click to toggle source

Charge requests:

# File lib/satispay.rb, line 36
def all_charges
  send_request('/online/v1/charges', :get, {})
end
all_refunds() click to toggle source

Refund requests:

# File lib/satispay.rb, line 57
def all_refunds
  send_request('/online/v1/refunds', :get, {})
end
all_users() click to toggle source

User requests:

# File lib/satispay.rb, line 20
def all_users
  send_request('/online/v1/users', :get, {})
end
check_bearer() click to toggle source

Bearer requests:

# File lib/satispay.rb, line 14
def check_bearer
  send_request('/wally-services/protocol/authenticated', :get, {})
end
create_charge(args) click to toggle source
# File lib/satispay.rb, line 40
def create_charge(args)
  args = validate_args(args, [:user_id, :currency, :amount])
  send_request('/online/v1/charges', :post, args)
end
create_user(args) click to toggle source
# File lib/satispay.rb, line 24
def create_user(args)
  args = validate_args(args, [:phone_number])
  send_request('/online/v1/users', :post, args)
end
get_charge(args) click to toggle source
# File lib/satispay.rb, line 45
def get_charge(args)
  args = validate_args(args, [:charge_id])
  send_request("/online/v1/charges/#{args[:charge_id]}", :get, {})
end
get_user(args) click to toggle source
# File lib/satispay.rb, line 29
def get_user(args)
  args = validate_args(args, [:user_id])
  send_request("/online/v1/users/#{args[:user_id]}", :get, {})
end
update_charge(args) click to toggle source
# File lib/satispay.rb, line 50
def update_charge(args)
  args = validate_args(args, [:charge_id])
  send_request("/online/v1/charges/#{args[:charge_id]}", :put, args)
end

Private Instance Methods

send_get_request(url) click to toggle source

This function send a GET request.

# File lib/satispay.rb, line 75
def send_get_request(url)
  response = RestClient::Request.execute(
    method: :get,
    url: url,
    verify_ssl: false,
    headers: {
      'Content-Type' => 'application/json',
      'Authorization' => "Bearer #{@security_bearer}"
    }
  )
end
send_post_request(url, body, method) click to toggle source

This function send a POST request.

# File lib/satispay.rb, line 88
def send_post_request(url, body, method)
  response = RestClient::Request.execute(
    method: method,
    url: url,
    verify_ssl: false,
    headers: {
      'Content-Type' => 'application/json',
      'Authorization' => "Bearer #{@security_bearer}"
    },
    payload: body.to_json
  )
end
send_request(path, method, body) click to toggle source

This function send a request to a specific url and return the response or the error.

# File lib/satispay.rb, line 65
def send_request(path, method, body)
  url = "#{@url}#{path}"
  response = (method == :get ? send_get_request(url) : send_post_request(url, body, method))
  JSON.parse(response.body) if response.body && !response.body.empty?
rescue RestClient::ExceptionWithResponse => e
  error = JSON.parse(e.response)
  raise e.response
end
validate_args(args, required) click to toggle source

This function validates an hash of arguments and raise an error if required arguments are not present.

# File lib/satispay.rb, line 103
def validate_args(args, required)
  required.each do |required_arg|
    raise 'Missing required argument' if args[required_arg] == nil
  end
  args
end