wepay 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +32 -0
  2. data/lib/wepay.rb +69 -0
  3. metadata +46 -0
data/README ADDED
@@ -0,0 +1,32 @@
1
+ WePay Ruby SDK
2
+
3
+ To use this SDK:
4
+
5
+ # creates a wepay object you can use to make calls
6
+ wepay = WePay.new(client_id, client_secret)
7
+
8
+ # get the authorization url
9
+ # you send the user to this url to authorize the application, they will return to your redirect with a code in the get parameters
10
+ redirect_uri = "http://myexamplesite.com/wepay"
11
+ url = wepay.oauth2_authorize_url(redirect_uri)
12
+ redirect_to(url)
13
+
14
+ # once you have the code you can get an access token
15
+ response = wepay.oauth2_token(code, redirect_uri)
16
+ access_token = response['access_token']
17
+
18
+
19
+ # makes a call to the /user endpoint (which requires no parameters)
20
+ response = wepay.call('/user', access_token)
21
+
22
+ # you can also open a payment account for the user
23
+ response = wepay.call('/account/create', access_token, {:name => "test account", "description" => "this is only a test" })
24
+
25
+
26
+ # switching to production
27
+ When you want to switch to production, change the _use_stage parameter on the WePay constructer to false.
28
+
29
+
30
+ check out our developer docs at https://stage.wepay.com/developer for more info
31
+
32
+ or email api@wepay.com if you have any questions.
data/lib/wepay.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'rubygems'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'net/https'
6
+ require 'cgi'
7
+
8
+ =begin
9
+ helps you make API calls to the WePay API v2
10
+ =end
11
+
12
+ class WePay
13
+
14
+ STAGE_API_ENDPOINT = "https://stage.wepayapi.com/v2"
15
+ STAGE_UI_ENDPOINT = "https://stage.wepay.com/v2"
16
+
17
+ PRODUCTION_API_ENDPOINT = "https://wepayapi.com/v2"
18
+ PRODUCTION_UI_ENDPOINT = "https://www.wepay.com/v2"
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)
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
+ end
33
+
34
+ # make a call to the WePay API
35
+ def call(call, access_token = false, params = false)
36
+ # get the url
37
+ url = URI.parse(@api_endpoint + call)
38
+ p url
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
+ # create the request object
48
+ request = Net::HTTP.new(url.host, url.port)
49
+ request.use_ssl = @use_ssl
50
+ # make the call
51
+ response = request.start {|http| http.request(call) }
52
+ # returns JSON response as ruby hash
53
+ JSON.parse(response.body)
54
+ end
55
+
56
+ # this function returns the URL that you send the user to to authorize your API application
57
+ # the redirect_uri must be a full uri (ex https://www.wepay.com)
58
+ def oauth2_authorize_url(redirect_uri, user_email = false, user_name = false, permissions = "manage_accounts,view_balance,collect_payments,refund_payments,view_user")
59
+ url = @ui_endpoint + '/oauth2/authorize?client_id=' + @client_id.to_s + '&redirect_uri=' + redirect_uri + '&scope=' + permissions
60
+ url += user_name ? '&user_name=' + CGI::escape(user_name) : ''
61
+ url += user_email ? '&user_email=' + CGI::escape(user_email) : ''
62
+ end
63
+
64
+ #this function will make a call to the /v2/oauth2/token endpoint to exchange a code for an access_token
65
+ def oauth2_token(code, redirect_uri)
66
+ call('/oauth2/token', false, {'client_id' => @client_id, 'client_secret' => @client_secret, 'redirect_uri' => redirect_uri, 'code' => code })
67
+ end
68
+
69
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wepay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - WePay
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-11 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: The WePay Ruby SDK lets you easily make WePay API calls from ruby.
15
+ email: api@wepay.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/wepay.rb
21
+ - README
22
+ homepage: https://stage.wepay.com/developer
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.10
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Ruby SDK for the WePay API
46
+ test_files: []