Class: WepayRails::Payments::Gateway
- Inherits:
-
Object
- Object
- WepayRails::Payments::Gateway
- Includes:
- HTTParty, Api::AccountMethods, Api::ChargeMethods, Api::CheckoutMethods, Api::PreapprovalMethods
- Defined in:
- lib/wepay-rails.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#account_id ⇒ Object
Returns the value of attribute account_id.
Instance Method Summary collapse
-
#auth_code_url(redirect_uri, params = {}) ⇒ Object
Get the auth code url that will be used to fetch the auth code for the customer arguments are the redirect_uri and an array of permissions that your application needs ex.
- #call_api(api_path, params = {}, timeout = 30000) ⇒ Object
- #configuration ⇒ Object
-
#get_access_token(auth_code, redirect_uri, callback_uri = nil) ⇒ Object
Fetch the access token from wepay for the auth code.
-
#initialize(*args) ⇒ Gateway
constructor
Pass in the wepay access token that we got after the oauth handshake and use it for ongoing communique with Wepay.
- #raise_if_response_error(json) ⇒ Object
- #symbolize_response(response) ⇒ Object
- #wepay_auth_header ⇒ Object
Methods included from Api::ChargeMethods
#apply_security_token, #charge_redirect_uri, #checkout_redirect_uri, #ipn_callback_uri, #lookup_checkout, #lookup_preapproval, #perform_charge
Methods included from Api::PreapprovalMethods
#apply_security_token, #ipn_callback_uri, #lookup_preapproval, #perform_preapproval, #preapproval_redirect_uri
Methods included from Api::CheckoutMethods
#apply_security_token, #checkout_redirect_uri, #ipn_callback_uri, #lookup_checkout, #lookup_preapproval, #perform_checkout
Methods included from Api::AccountMethods
#create_account, #delete_account, #find_account, #get_account, #get_account_balance, #modify_account
Constructor Details
#initialize(*args) ⇒ Gateway
Pass in the wepay access token that we got after the oauth handshake and use it for ongoing communique with Wepay. This also relies heavily on there being a wepay.yml file in your rails config directory - it must look like this:
77 78 79 80 81 82 83 |
# File 'lib/wepay-rails.rb', line 77 def initialize(*args) @wepay_config = WepayRails::Configuration.settings || {:scope => []} @access_token = args.first || @wepay_config[:access_token] @account_id = args.first || @wepay_config[:account_id] @ui_endpoint = @wepay_config[:wepay_ui_endpoint] || "https://www.wepay.com/v2" @api_endpoint = @wepay_config[:wepay_api_endpoint] || "https://wepayapi.com/v2" end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
70 71 72 |
# File 'lib/wepay-rails.rb', line 70 def access_token @access_token end |
#account_id ⇒ Object
Returns the value of attribute account_id.
71 72 73 |
# File 'lib/wepay-rails.rb', line 71 def account_id @account_id end |
Instance Method Details
#auth_code_url(redirect_uri, params = {}) ⇒ Object
Get the auth code url that will be used to fetch the auth code for the customer arguments are the redirect_uri and an array of permissions that your application needs ex. [‘manage_accounts’,‘collect_payments’,‘view_balance’,‘view_user’]
88 89 90 91 92 93 94 95 |
# File 'lib/wepay-rails.rb', line 88 def auth_code_url(redirect_uri, params = {}) params[:client_id] ||= @wepay_config[:client_id] params[:scope] ||= @wepay_config[:scope].join(',') params[:redirect_uri] = redirect_uri query = params.map { |k, v| "#{k.to_s}=#{v}" }.join('&') "#{@ui_endpoint}/oauth2/authorize?#{query}" end |
#call_api(api_path, params = {}, timeout = 30000) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/wepay-rails.rb', line 126 def call_api(api_path, params={}, timeout=30000) begin self.class.default_timeout(timeout) response = self.class.post("#{@api_endpoint}#{api_path}", {:headers => wepay_auth_header, :body => params}) json = symbolize_response(response.body) rescue Errno, JSON::ParserError => e raise WepayRails::Exceptions::WepayApiError.new("The request to WePay timed out. This might mean you sent an invalid request or WePay is having issues.") rescue => e raise e if e.class.to_s =~ /WepayRails/ raise WepayRails::Exceptions::WepayApiError.new("There was an error while trying to connect with WePay - #{e.inspect}") end if response.success? return json elsif response.code == 401 raise WepayRails::Exceptions::ExpiredTokenError.new("Token either expired, revoked or invalid: #{json.inspect}.") else raise WepayRails::Exceptions::WepayApiError.new("The API request failed with error code ##{response.code}: #{json.inspect}.") end end |
#configuration ⇒ Object
102 103 104 |
# File 'lib/wepay-rails.rb', line 102 def configuration @wepay_config end |
#get_access_token(auth_code, redirect_uri, callback_uri = nil) ⇒ Object
Fetch the access token from wepay for the auth code
147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/wepay-rails.rb', line 147 def get_access_token(auth_code, redirect_uri, callback_uri = nil) params = { :client_id => @wepay_config[:client_id], :client_secret => @wepay_config[:client_secret], :redirect_uri => redirect_uri, :code => auth_code, :callback_uri => callback_uri # Optional field in which you will receive IPNs with the user_id # when the user revokes an access_token or is deleted. } json = call_api("/oauth2/token", params) raise WepayRails::Exceptions::AccessTokenError.new("A problem occurred trying to get the access token: #{json.inspect}") unless json.has_key?(:access_token) @account_id = json[:user_id] @access_token = json[:access_token] end |
#raise_if_response_error(json) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/wepay-rails.rb', line 106 def raise_if_response_error(json) if json.has_key?(:error) && json.has_key?(:error_description) if ['invalid code parameter','the code has expired','this access_token has been revoked', 'a valid access_token is required'].include?(json[:error_description]) raise WepayRails::Exceptions::ExpiredTokenError.new("Token either expired, revoked or invalid: #{json[:error_description]}") else raise WepayRails::Exceptions::WepayApiError.new(json[:error_description]) end end end |
#symbolize_response(response) ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/wepay-rails.rb', line 116 def symbolize_response(response) json = JSON.parse(response) if json.kind_of? Hash json.symbolize_keys! and raise_if_response_error(json) elsif json.kind_of? Array json.each{|h| h.symbolize_keys!} end json end |
#wepay_auth_header ⇒ Object
97 98 99 100 |
# File 'lib/wepay-rails.rb', line 97 def wepay_auth_header @access_token.blank? ? {'User-Agent' => "WepayRails"} : {'User-Agent' => "WepayRails", 'Authorization' => "Bearer: #{@access_token}"} end |