|
1
|
|
|
## |
|
2
|
|
|
# Copyright (c) 2012-2016 WePay. |
|
3
|
|
|
# |
|
4
|
|
|
# http://opensource.org/licenses/Apache2.0 |
|
5
|
|
|
## |
|
6
|
|
|
|
|
7
|
1 |
|
require 'cgi' |
|
8
|
1 |
|
require 'json' |
|
9
|
1 |
|
require 'net/http' |
|
10
|
1 |
|
require 'net/https' |
|
11
|
1 |
|
require 'rubygems' |
|
12
|
1 |
|
require 'uri' |
|
13
|
|
|
|
|
14
|
|
|
## |
|
15
|
|
|
# The root WePay namespace. |
|
16
|
|
|
## |
|
17
|
1 |
|
module WePay |
|
18
|
|
|
|
|
19
|
|
|
## |
|
20
|
|
|
# A very simple wrapper for the WePay API. |
|
21
|
|
|
## |
|
22
|
1 |
|
class Client |
|
23
|
|
|
|
|
24
|
|
|
# Stage API endpoint |
|
25
|
1 |
|
STAGE_API_ENDPOINT = "https://stage.wepayapi.com/v2" |
|
26
|
|
|
|
|
27
|
|
|
# Stage UI endpoint |
|
28
|
1 |
|
STAGE_UI_ENDPOINT = "https://stage.wepay.com/v2" |
|
29
|
|
|
|
|
30
|
|
|
# Production API endpoint |
|
31
|
1 |
|
PRODUCTION_API_ENDPOINT = "https://wepayapi.com/v2" |
|
32
|
|
|
|
|
33
|
|
|
# Production UI endpoint |
|
34
|
1 |
|
PRODUCTION_UI_ENDPOINT = "https://www.wepay.com/v2" |
|
35
|
|
|
|
|
36
|
1 |
|
attr_reader :api_endpoint |
|
37
|
1 |
|
attr_reader :api_version |
|
38
|
1 |
|
attr_reader :client_id |
|
39
|
1 |
|
attr_reader :client_secret |
|
40
|
1 |
|
attr_reader :ui_endpoint |
|
41
|
|
|
|
|
42
|
1 |
|
def initialize(client_id, client_secret, use_stage = true, api_version = nil) |
|
43
|
18 |
|
@client_id = client_id.to_s |
|
44
|
18 |
|
@client_secret = client_secret.to_s |
|
45
|
18 |
|
@api_version = api_version.to_s |
|
46
|
|
|
|
|
47
|
18 |
|
if use_stage |
|
48
|
14 |
|
@api_endpoint = STAGE_API_ENDPOINT |
|
49
|
14 |
|
@ui_endpoint = STAGE_UI_ENDPOINT |
|
50
|
|
|
else |
|
51
|
4 |
|
@api_endpoint = PRODUCTION_API_ENDPOINT |
|
52
|
4 |
|
@ui_endpoint = PRODUCTION_UI_ENDPOINT |
|
53
|
|
|
end |
|
54
|
|
|
end |
|
55
|
|
|
|
|
56
|
|
|
## |
|
57
|
|
|
# Execute a call to the WePay API. |
|
58
|
|
|
## |
|
59
|
1 |
|
def call(call, access_token = false, params = {}, risk_token = false, client_ip = false) |
|
60
|
7 |
|
path = call.start_with?('/') ? call : call.prepend('/') |
|
61
|
7 |
|
url = URI.parse(api_endpoint + path) |
|
62
|
|
|
|
|
63
|
7 |
|
call = Net::HTTP::Post.new(url.path, { |
|
64
|
|
|
'Content-Type' => 'application/json', |
|
65
|
|
|
'User-Agent' => 'WePay Ruby SDK' |
|
66
|
|
|
}) |
|
67
|
|
|
|
|
68
|
7 |
|
unless params.empty? |
|
69
|
2 |
|
call.body = params.to_json |
|
70
|
|
|
end |
|
71
|
|
|
|
|
72
|
7 |
|
if access_token then call.add_field('Authorization', "Bearer #{access_token}"); end |
|
73
|
7 |
|
if @api_version then call.add_field('Api-Version', @api_version); end |
|
74
|
|
|
if risk_token then call.add_field('WePay-Risk-Token', risk_token); end |
|
75
|
7 |
|
if client_ip then call.add_field('Client-IP', client_ip); end |
|
76
|
|
|
|
|
77
|
|
|
make_request(call, url) |
|
78
|
|
|
end |
|
79
|
|
|
|
|
80
|
|
|
## |
|
81
|
|
|
# Returns the OAuth 2.0 URL that users should be redirected to for |
|
82
|
|
|
# authorizing your API application. The `redirect_uri` must be a |
|
83
|
1 |
|
# fully-qualified URL (e.g., `https://www.wepay.com`). |
|
84
|
|
|
## |
|
85
|
|
|
def oauth2_authorize_url( |
|
86
|
|
|
redirect_uri, |
|
87
|
|
|
user_email = false, |
|
88
|
|
|
user_name = false, |
|
89
|
|
|
permissions = "manage_accounts,collect_payments,view_user,send_money,preapprove_payments,manage_subscriptions", |
|
90
|
2 |
|
user_country = false |
|
91
|
|
|
) |
|
92
|
|
|
url = @ui_endpoint + |
|
93
|
|
|
'/oauth2/authorize?client_id=' + @client_id.to_s + |
|
94
|
|
|
'&redirect_uri=' + redirect_uri + |
|
95
|
2 |
|
'&scope=' + permissions |
|
96
|
2 |
|
|
|
97
|
2 |
|
url += user_name ? '&user_name=' + CGI::escape(user_name) : '' |
|
98
|
|
|
url += user_email ? '&user_email=' + CGI::escape(user_email) : '' |
|
99
|
|
|
url += user_country ? '&user_country=' + CGI::escape(user_country) : '' |
|
|
|
|
|
|
100
|
|
|
end |
|
101
|
|
|
|
|
102
|
|
|
## |
|
103
|
1 |
|
# Call the `/v2/oauth2/token` endpoint to exchange an OAuth 2.0 `code` for an `access_token`. |
|
104
|
1 |
|
## |
|
105
|
|
|
def oauth2_token(code, redirect_uri) |
|
106
|
|
|
call('/oauth2/token', false, { |
|
107
|
|
|
'client_id' => @client_id, |
|
108
|
|
|
'client_secret' => @client_secret, |
|
109
|
|
|
'redirect_uri' => redirect_uri, |
|
110
|
|
|
'code' => code |
|
111
|
|
|
}) |
|
112
|
1 |
|
end |
|
113
|
|
|
|
|
114
|
|
|
private |
|
115
|
|
|
|
|
116
|
|
|
## |
|
117
|
1 |
|
# Make the HTTP request to the endpoint. |
|
118
|
7 |
|
## |
|
119
|
7 |
|
def make_request(call, url) |
|
120
|
7 |
|
request = Net::HTTP.new(url.host, url.port) |
|
121
|
|
|
request.read_timeout = 30 |
|
122
|
7 |
|
request.use_ssl = true |
|
123
|
7 |
|
|
|
124
|
|
|
response = request.start { |http| http.request(call) } |
|
125
|
|
|
JSON.parse(response.body) |
|
126
|
|
|
end |
|
127
|
|
|
end |
|
128
|
|
|
end |
|
129
|
|
|
|