Class: WePay

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

Overview

helps you make API calls to the WePay API v2

Constant Summary collapse

STAGE_API_ENDPOINT =
"https://stage.wepayapi.com/v2"
STAGE_UI_ENDPOINT =
"https://stage.wepay.com/v2"
PRODUCTION_API_ENDPOINT =
"https://wepayapi.com/v2"
PRODUCTION_UI_ENDPOINT =
"https://www.wepay.com/v2"

Instance Method Summary collapse

Constructor Details

#initialize(_client_id, _client_secret, _use_stage = true, _use_ssl = true) ⇒ WePay

initializes the API application, api_endpoint should be something like ‘stage.wepay.com/v2



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wepay.rb', line 21

def initialize(_client_id, _client_secret, _use_stage = true, _use_ssl = true)
	@client_id = _client_id
	@client_secret = _client_secret
	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
	@use_ssl = _use_ssl
end

Instance Method Details

#call(call, access_token = false, params = false) ⇒ Object

make a call to the WePay API



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wepay.rb', line 35

def call(call, access_token = false, params = false)
	# get the url
	url = URI.parse(@api_endpoint + call)
	# construct the call data and access token
	call = Net::HTTP::Post.new(url.path, initheader = {'Content-Type' =>'application/json', 'User-Agent' => 'WePay Ruby SDK'})
	if params
		call.body = params.to_json
	end
	if access_token
		call.add_field('Authorization: Bearer', access_token);
	end
	# create the request object
	request = Net::HTTP.new(url.host, url.port)
	request.read_timeout = 30
	request.use_ssl = @use_ssl
	# make the call
	response = request.start {|http| http.request(call) }
	# returns JSON response as ruby hash
	JSON.parse(response.body)
end

#oauth2_authorize_url(redirect_uri, user_email = false, user_name = false, permissions = "manage_accounts,view_balance,collect_payments,refund_payments,view_user") ⇒ Object

this function returns the URL that you send the user to to authorize your API application the redirect_uri must be a full uri (ex www.wepay.com)



58
59
60
61
62
# File 'lib/wepay.rb', line 58

def oauth2_authorize_url(redirect_uri, user_email = false, user_name = false, permissions = "manage_accounts,view_balance,collect_payments,refund_payments,view_user")
	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

this function will make a call to the /v2/oauth2/token endpoint to exchange a code for an access_token



65
66
67
# File 'lib/wepay.rb', line 65

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