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

Initializes the API application.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wepay.rb', line 45

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 = {}) ⇒ Object

Execute a call to the WePay API.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/wepay.rb', line 62

def call(call, access_token = false, params = {})
  url = URI.parse(@api_endpoint + (call[0,1] == '/' ? call : "/#{ call }"))
  call = Net::HTTP::Post.new(url.path, initheader = {
    '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

  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") ⇒ 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).



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/wepay.rb', line 84

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"
)
  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) : ''
end

#oauth2_token(code, redirect_uri) ⇒ Object

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



102
103
104
105
106
107
108
109
# File 'lib/wepay.rb', line 102

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