wepay 0.0.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/wepay.rb +122 -71
- metadata +10 -10
- data/README +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19b8acb8a744b06fbacbfa8700b3df2678c90223
|
4
|
+
data.tar.gz: 09f4d706b306d796ae6ed9e27e0579108257b88b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca6156e6ad955560cab2544ae3e090dfe7d18cc368b96dc23ecf787e606f1622558ec4615872aedbd60ccdf820ad1022700dbeaf94bfa8d733c8e89a9a11e310
|
7
|
+
data.tar.gz: 84eaceea88f95d1cb7e6a10e19d6c30959b94c11aa590633bab773fa0264f63c74309e585dce0a9644d39ab000bbf57c9f1402cfdb3e4415ea1d2599a0d444fd
|
data/lib/wepay.rb
CHANGED
@@ -1,76 +1,127 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
##
|
2
|
+
# Copyright (c) 2015 WePay.
|
3
|
+
#
|
4
|
+
# http://opensource.org/licenses/Apache2.0
|
5
|
+
##
|
6
|
+
|
7
|
+
require 'cgi'
|
3
8
|
require 'json'
|
4
9
|
require 'net/http'
|
5
10
|
require 'net/https'
|
6
|
-
require '
|
11
|
+
require 'rubygems'
|
12
|
+
require 'uri'
|
13
|
+
|
14
|
+
##
|
15
|
+
# The root WePay namespace.
|
16
|
+
##
|
17
|
+
module WePay
|
18
|
+
|
19
|
+
##
|
20
|
+
# A very simple wrapper for the WePay API.
|
21
|
+
##
|
22
|
+
class Client
|
23
|
+
|
24
|
+
# Stage API endpoint
|
25
|
+
STAGE_API_ENDPOINT = "https://stage.wepayapi.com/v2"
|
26
|
+
|
27
|
+
# Stage UI endpoint
|
28
|
+
STAGE_UI_ENDPOINT = "https://stage.wepay.com/v2"
|
29
|
+
|
30
|
+
# Production API endpoint
|
31
|
+
PRODUCTION_API_ENDPOINT = "https://wepayapi.com/v2"
|
32
|
+
|
33
|
+
# Production UI endpoint
|
34
|
+
PRODUCTION_UI_ENDPOINT = "https://www.wepay.com/v2"
|
35
|
+
|
36
|
+
attr_reader :api_endpoint
|
37
|
+
attr_reader :api_version
|
38
|
+
attr_reader :client_id
|
39
|
+
attr_reader :client_secret
|
40
|
+
attr_reader :ui_endpoint
|
41
|
+
|
42
|
+
##
|
43
|
+
# Initializes the API application.
|
44
|
+
##
|
45
|
+
def initialize(client_id, client_secret, use_stage = true, api_version = nil)
|
46
|
+
@client_id = client_id.to_s
|
47
|
+
@client_secret = client_secret.to_s
|
48
|
+
@api_version = api_version.to_s
|
49
|
+
|
50
|
+
if use_stage
|
51
|
+
@api_endpoint = STAGE_API_ENDPOINT
|
52
|
+
@ui_endpoint = STAGE_UI_ENDPOINT
|
53
|
+
else
|
54
|
+
@api_endpoint = PRODUCTION_API_ENDPOINT
|
55
|
+
@ui_endpoint = PRODUCTION_UI_ENDPOINT
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
##
|
60
|
+
# Execute a call to the WePay API.
|
61
|
+
##
|
62
|
+
def call(call, access_token = false, params = {})
|
63
|
+
url = URI.parse(@api_endpoint + (call[0,1] == '/' ? call : "/#{ call }"))
|
64
|
+
call = Net::HTTP::Post.new(url.path, initheader = {
|
65
|
+
'Content-Type' => 'application/json',
|
66
|
+
'User-Agent' => 'WePay Ruby SDK'
|
67
|
+
})
|
68
|
+
|
69
|
+
unless params.empty?{}
|
70
|
+
params = params.merge({
|
71
|
+
"client_id" => @client_id,
|
72
|
+
"client_secret" => @client_secret
|
73
|
+
})
|
74
|
+
end
|
75
|
+
|
76
|
+
if access_token then call.add_field('Authorization: Bearer', access_token); end
|
77
|
+
if @api_version then call.add_field('Api-Version', @api_version); end
|
78
|
+
|
79
|
+
make_request(call, url)
|
80
|
+
end
|
81
|
+
|
82
|
+
##
|
83
|
+
# Returns the OAuth 2.0 URL that users should be redirected to for
|
84
|
+
# authorizing your API application. The `redirect_uri` must be a
|
85
|
+
# fully-qualified URL (e.g., `https://www.wepay.com`).
|
86
|
+
##
|
87
|
+
def oauth2_authorize_url(
|
88
|
+
redirect_uri,
|
89
|
+
user_email = false,
|
90
|
+
user_name = false,
|
91
|
+
permissions = "manage_accounts,collect_payments,view_user,send_money,preapprove_payments,manage_subscriptions"
|
92
|
+
)
|
93
|
+
url = @ui_endpoint
|
94
|
+
+ '/oauth2/authorize?client_id=' + @client_id.to_s
|
95
|
+
+ '&redirect_uri=' + redirect_uri
|
96
|
+
+ '&scope=' + permissions
|
97
|
+
|
98
|
+
url += user_name ? '&user_name=' + CGI::escape(user_name) : ''
|
99
|
+
url += user_email ? '&user_email=' + CGI::escape(user_email) : ''
|
100
|
+
end
|
101
|
+
|
102
|
+
##
|
103
|
+
# Call the `/v2/oauth2/token` endpoint to exchange an OAuth 2.0 `code` for an `access_token`.
|
104
|
+
##
|
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
|
+
end
|
113
|
+
|
114
|
+
private
|
7
115
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
# initializes the API application, api_endpoint should be something like 'https://stage.wepay.com/v2'
|
21
|
-
def initialize(_client_id, _client_secret, _use_stage = true, _use_ssl = true, _api_version = nil)
|
22
|
-
@client_id = _client_id
|
23
|
-
@client_secret = _client_secret
|
24
|
-
if _use_stage
|
25
|
-
@api_endpoint = STAGE_API_ENDPOINT
|
26
|
-
@ui_endpoint = STAGE_UI_ENDPOINT
|
27
|
-
else
|
28
|
-
@api_endpoint = PRODUCTION_API_ENDPOINT
|
29
|
-
@ui_endpoint = PRODUCTION_UI_ENDPOINT
|
30
|
-
end
|
31
|
-
@use_ssl = _use_ssl
|
32
|
-
@api_version = _api_version
|
33
|
-
end
|
34
|
-
|
35
|
-
# make a call to the WePay API
|
36
|
-
def call(call, access_token = false, params = false)
|
37
|
-
# get the url
|
38
|
-
url = URI.parse(@api_endpoint + call)
|
39
|
-
# construct the call data and access token
|
40
|
-
call = Net::HTTP::Post.new(url.path, initheader = {'Content-Type' =>'application/json', 'User-Agent' => 'WePay Ruby SDK'})
|
41
|
-
if params
|
42
|
-
call.body = params.to_json
|
43
|
-
end
|
44
|
-
if access_token
|
45
|
-
call.add_field('Authorization: Bearer', access_token);
|
46
|
-
end
|
47
|
-
|
48
|
-
# send Api Version header
|
49
|
-
if @api_version
|
50
|
-
call.add_field('Api-Version', @api_version);
|
51
|
-
end
|
52
|
-
|
53
|
-
# create the request object
|
54
|
-
request = Net::HTTP.new(url.host, url.port)
|
55
|
-
request.read_timeout = 30
|
56
|
-
request.use_ssl = @use_ssl
|
57
|
-
# make the call
|
58
|
-
response = request.start {|http| http.request(call) }
|
59
|
-
# returns JSON response as ruby hash
|
60
|
-
JSON.parse(response.body)
|
61
|
-
end
|
62
|
-
|
63
|
-
# this function returns the URL that you send the user to to authorize your API application
|
64
|
-
# the redirect_uri must be a full uri (ex https://www.wepay.com)
|
65
|
-
def oauth2_authorize_url(redirect_uri, user_email = false, user_name = false, permissions = "manage_accounts,view_balance,collect_payments,view_user,send_money,preapprove_payments,manage_subscriptions")
|
66
|
-
url = @ui_endpoint + '/oauth2/authorize?client_id=' + @client_id.to_s + '&redirect_uri=' + redirect_uri + '&scope=' + permissions
|
67
|
-
url += user_name ? '&user_name=' + CGI::escape(user_name) : ''
|
68
|
-
url += user_email ? '&user_email=' + CGI::escape(user_email) : ''
|
69
|
-
end
|
70
|
-
|
71
|
-
#this function will make a call to the /v2/oauth2/token endpoint to exchange a code for an access_token
|
72
|
-
def oauth2_token(code, redirect_uri)
|
73
|
-
call('/oauth2/token', false, {'client_id' => @client_id, 'client_secret' => @client_secret, 'redirect_uri' => redirect_uri, 'code' => code })
|
74
|
-
end
|
75
|
-
|
116
|
+
##
|
117
|
+
# Make the HTTP request to the endpoint.
|
118
|
+
##
|
119
|
+
def make_request(call, url)
|
120
|
+
request = Net::HTTP.new(url.host, url.port)
|
121
|
+
request.read_timeout = 30
|
122
|
+
request.use_ssl = true
|
123
|
+
response = request.start {|http| http.request(call) }
|
124
|
+
JSON.parse(response.body)
|
125
|
+
end
|
126
|
+
end
|
76
127
|
end
|
metadata
CHANGED
@@ -1,25 +1,25 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wepay
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- WePay
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: The WePay Ruby
|
13
|
+
description: The WePay SDK for Ruby lets you easily make WePay API calls from Ruby.
|
14
14
|
email: api@wepay.com
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
19
|
- lib/wepay.rb
|
20
|
-
-
|
21
|
-
|
22
|
-
|
20
|
+
homepage: https://github.com/wepay/Ruby-SDK
|
21
|
+
licenses:
|
22
|
+
- Apache-2.0
|
23
23
|
metadata: {}
|
24
24
|
post_install_message:
|
25
25
|
rdoc_options: []
|
@@ -27,18 +27,18 @@ require_paths:
|
|
27
27
|
- lib
|
28
28
|
required_ruby_version: !ruby/object:Gem::Requirement
|
29
29
|
requirements:
|
30
|
-
- -
|
30
|
+
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '0'
|
33
33
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
34
|
requirements:
|
35
|
-
- -
|
35
|
+
- - ">="
|
36
36
|
- !ruby/object:Gem::Version
|
37
37
|
version: '0'
|
38
38
|
requirements: []
|
39
39
|
rubyforge_project:
|
40
|
-
rubygems_version: 2.
|
40
|
+
rubygems_version: 2.4.5
|
41
41
|
signing_key:
|
42
42
|
specification_version: 4
|
43
|
-
summary:
|
43
|
+
summary: WePay SDK for Ruby
|
44
44
|
test_files: []
|
data/README
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
WePay Ruby SDK
|
2
|
-
|
3
|
-
To use this SDK:
|
4
|
-
|
5
|
-
# creates a wepay object you can use to make calls.
|
6
|
-
# Set use_stage to true for test/stage, false for production.
|
7
|
-
wepay = WePay.new(client_id, client_secret, use_stage)
|
8
|
-
|
9
|
-
# To set an [API-Version](https://www.wepay.com/developer/reference/versioning) in the header with your call request, use:
|
10
|
-
wepay = WePay.new(client_id, client_secret, use_stage, true, api_version)
|
11
|
-
|
12
|
-
# get the authorization url
|
13
|
-
# you send the user to this url to authorize the application, they will return to your redirect with a code in the get parameters
|
14
|
-
redirect_uri = "http://myexamplesite.com/wepay"
|
15
|
-
url = wepay.oauth2_authorize_url(redirect_uri)
|
16
|
-
redirect_to(url)
|
17
|
-
|
18
|
-
# once you have the code you can get an access token
|
19
|
-
response = wepay.oauth2_token(code, redirect_uri)
|
20
|
-
access_token = response['access_token']
|
21
|
-
|
22
|
-
|
23
|
-
# makes a call to the /user endpoint (which requires no parameters)
|
24
|
-
response = wepay.call('/user', access_token)
|
25
|
-
|
26
|
-
# you can also open a payment account for the user
|
27
|
-
response = wepay.call('/account/create', access_token, {:name => "test account", "description" => "this is only a test" })
|
28
|
-
|
29
|
-
|
30
|
-
# switching to production
|
31
|
-
When you want to switch to production, change the _use_stage parameter on the WePay constructer to false.
|
32
|
-
|
33
|
-
|
34
|
-
check out our developer docs at https://stage.wepay.com/developer for more info
|
35
|
-
|
36
|
-
or email api@wepay.com if you have any questions.
|