Class: WePay::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/wepay.rb

Overview

A very simple wrapper for the WePay API.

Constant Summary

STAGE_API_ENDPOINT =

Stage API endpoint

"https://stage.wepayapi.com/v2"
STAGE_UI_ENDPOINT =

Stage UI endpoint

"https://stage.wepay.com/v2"
PRODUCTION_API_ENDPOINT =

Production API endpoint

"https://wepayapi.com/v2"
PRODUCTION_UI_ENDPOINT =

Production UI endpoint

"https://www.wepay.com/v2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, use_stage = true, api_version = nil) ⇒ Client

Returns a new instance of Client



42
43
44
45
46
47
48
49
50
51
52
53
54
# 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

Instance Attribute Details

#api_endpointObject (readonly)

Returns the value of attribute api_endpoint



36
37
38
# File 'lib/wepay.rb', line 36

def api_endpoint
  @api_endpoint
end

#api_versionObject (readonly)

Returns the value of attribute api_version



37
38
39
# File 'lib/wepay.rb', line 37

def api_version
  @api_version
end

#client_idObject (readonly)

Returns the value of attribute client_id



38
39
40
# File 'lib/wepay.rb', line 38

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret



39
40
41
# File 'lib/wepay.rb', line 39

def client_secret
  @client_secret
end

#ui_endpointObject (readonly)

Returns the value of attribute ui_endpoint



40
41
42
# File 'lib/wepay.rb', line 40

def ui_endpoint
  @ui_endpoint
end

Instance Method Details

#call(call, access_token = false, params = {}, risk_token = false, client_ip = false) ⇒ Object

Execute a call to the WePay API.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object

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., https://www.wepay.com).



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object

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



105
106
107
108
109
110
111
112
# 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