Class: WepayRails::Payments::Gateway

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Api::CheckoutMethods
Defined in:
lib/wepay-rails.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Api::CheckoutMethods

#apply_security_token, #checkout_redirect_uri, #ipn_callback_uri, #lookup_checkout, #perform_checkout

Constructor Details

#initialize(*args) ⇒ Gateway

Pass in the wepay access token that we got after the oauth handshake and use it for ongoing comunique with Wepay. This also relies heavily on there being a wepay.yml file in your rails config directory - it must look like this:



49
50
51
52
53
# File 'lib/wepay-rails.rb', line 49

def initialize(*args)
  @wepay_config = WepayRails::Configuration.settings
  @access_token = args.first || @wepay_config[:access_token]
  @base_uri     = @wepay_config[:wepay_api_uri] || "https://www.wepay.com/v2"
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



43
44
45
# File 'lib/wepay-rails.rb', line 43

def access_token
  @access_token
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’]



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/wepay-rails.rb', line 85

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 do |k, v|
    "#{k.to_s}=#{v}"
  end.join('&')

  "#{@base_uri}/oauth2/authorize?#{query}"
end

#call_api(api_path, params = {}) ⇒ Object



108
109
110
111
# File 'lib/wepay-rails.rb', line 108

def call_api(api_path, params={})
  response = self.class.post("#{@base_uri}#{api_path}", {:headers => wepay_auth_header}.merge!({:body => params}))
  JSON.parse(response.body)
end

#configurationObject



104
105
106
# File 'lib/wepay-rails.rb', line 104

def configuration
  @wepay_config
end

#get_access_token(auth_code, redirect_uri) ⇒ Object

Fetch the access token from wepay for the auth code



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/wepay-rails.rb', line 56

def get_access_token(auth_code, redirect_uri)

  params = {
    :client_id     => @wepay_config[:client_id],
    :client_secret => @wepay_config[:client_secret],
    :redirect_uri  => redirect_uri,
    :code          => auth_code
  }

  response = self.class.post("#{@base_uri}/oauth2/token", {:body => params})
  json = JSON.parse(response.body)

  if json.has_key?("error")
    if json.has_key?("error_description")
      if ['invalid code parameter','the code has expired','this access_token has been revoked'].include?(json['error_description'])
        raise WepayRails::Exceptions::ExpiredTokenError.new("Token either expired, revoked or invalid: #{json["error_description"]}")
      end
      raise WepayRails::Exceptions::AccessTokenError.new(json["error_description"])
    end
  end

  raise WepayRails::Exceptions::AccessTokenError.new("A problem occurred trying to get the access token: #{json.inspect}") unless json.has_key?("access_token")

  @access_token = json["access_token"]
end

#wepay_auth_headerObject



97
98
99
100
101
102
# File 'lib/wepay-rails.rb', line 97

def wepay_auth_header
  unless @access_token
    raise WepayRails::Exceptions::AccessTokenError.new("No access token available")
  end
  {'Authorization' => "Bearer: #{@access_token}"}
end