class WePay::Client

A very simple wrapper for the WePay API.

Constants

PRODUCTION_API_ENDPOINT

Production API endpoint

PRODUCTION_UI_ENDPOINT

Production UI endpoint

STAGE_API_ENDPOINT

Stage API endpoint

STAGE_UI_ENDPOINT

Stage UI endpoint

Attributes

api_endpoint[R]
api_version[R]
client_id[R]
client_secret[R]
ui_endpoint[R]

Public Class Methods

new(client_id, client_secret, use_stage = true, api_version = nil) click to toggle source
# File lib/wepay.rb, line 42
def initialize(client_id, client_secret, use_stage = true, api_version = nil)
  @client_id     = client_id.to_s
  @client_secret = client_secret.to_s
  @api_version   = api_version.to_s

  if use_stage
    @api_endpoint = STAGE_API_ENDPOINT
    @ui_endpoint  = STAGE_UI_ENDPOINT
  else
    @api_endpoint = PRODUCTION_API_ENDPOINT
    @ui_endpoint  = PRODUCTION_UI_ENDPOINT
  end
end

Public Instance Methods

call(call, access_token = false, params = {}, risk_token = false, client_ip = false) click to toggle source

Execute a call to the WePay API.

# File lib/wepay.rb, line 59
def call(call, access_token = false, params = {}, risk_token = false, client_ip = false)
  path = call.start_with?('/') ? call : call.prepend('/')
  url  = URI.parse(api_endpoint + path)

  call = Net::HTTP::Post.new(url.path, {
    'Content-Type' => 'application/json',
    'User-Agent'   => 'WePay Ruby SDK'
  })

  unless params.empty?
    call.body = params.to_json
  end

  if access_token then call.add_field('Authorization', "Bearer #{access_token}"); end
  if @api_version then call.add_field('Api-Version', @api_version); end
  if risk_token then call.add_field('WePay-Risk-Token', risk_token); end
  if client_ip then call.add_field('Client-IP', client_ip); end

  make_request(call, url)
end
oauth2_authorize_url( redirect_uri, user_email = false, user_name = false, permissions = "manage_accounts,collect_payments,view_user,send_money,preapprove_payments,manage_subscriptions", user_country = false ) click to toggle source

Returns the OAuth 2.0 URL that users should be redirected to for authorizing your API application. The `redirect_uri` must be a fully-qualified URL (e.g., `www.wepay.com`).

# File lib/wepay.rb, line 85
def oauth2_authorize_url(
  redirect_uri,
  user_email   = false,
  user_name    = false,
  permissions  = "manage_accounts,collect_payments,view_user,send_money,preapprove_payments,manage_subscriptions",
  user_country = false
)
  url = @ui_endpoint +
        '/oauth2/authorize?client_id=' + @client_id.to_s +
        '&redirect_uri=' + redirect_uri +
        '&scope=' + permissions

  url += user_name ?    '&user_name='    + CGI::escape(user_name)    : ''
  url += user_email ?   '&user_email='   + CGI::escape(user_email)   : ''
  url += user_country ? '&user_country=' + CGI::escape(user_country) : ''
end
oauth2_token(code, redirect_uri) click to toggle source

Call the `/v2/oauth2/token` endpoint to exchange an OAuth 2.0 `code` for an `access_token`.

# File lib/wepay.rb, line 105
def oauth2_token(code, redirect_uri)
  call('/oauth2/token', false, {
    'client_id'     => @client_id,
    'client_secret' => @client_secret,
    'redirect_uri'  => redirect_uri,
    'code'          => code
  })
end

Private Instance Methods

make_request(call, url) click to toggle source

Make the HTTP request to the endpoint.

# File lib/wepay.rb, line 119
def make_request(call, url)
  request              = Net::HTTP.new(url.host, url.port)
  request.read_timeout = 30
  request.use_ssl      = true

  response = request.start { |http| http.request(call) }
  JSON.parse(response.body)
end