Class: Effective::QbOauthController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/effective/qb_oauth_controller.rb

Instance Method Summary collapse

Instance Method Details

#authorizeObject



12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/effective/qb_oauth_controller.rb', line 12

def authorize
  grant_url = client.auth_code.authorize_url(
    redirect_uri: redirect_uri,
    response_type: 'code',
    state: SecureRandom.hex(12),
    scope: 'com.intuit.quickbooks.accounting'
  )

  redirect_to(grant_url)
end

#callbackObject

This matches the QuickBooks Redirect URI and we have to set it up ahead of time.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/effective/qb_oauth_controller.rb', line 24

def callback
  return unless params[:code].present? && params[:realmId].present? && params[:state].present?

  token = client.auth_code.get_token(params[:code], redirect_uri: redirect_uri)
  return unless token

  realm = Effective::QbRealm.all.first_or_initialize

  realm.update!(
    realm_id: params[:realmId],
    access_token: token.token,
    refresh_token: token.refresh_token,
    access_token_expires_at: Time.at(token.expires_at),
    refresh_token_expires_at: (Time.at(token.expires_at) + 100.days)
  )

  flash[:success] = 'Successfully connected with QuickBooks Online'

  redirect_to(effective_qb_online.admin_quickbooks_path)
end

#revokeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/effective/qb_oauth_controller.rb', line 45

def revoke
  realm = EffectiveQbOnline.api.realm
  return unless realm

  # Instantiate the token
  token = OAuth2::AccessToken.new(client, realm.access_token, refresh_token: realm.refresh_token)

  # Revoke
  response = token.post('/o/oauth2/revoke', params: { token: realm.refresh_token })

  if response.status == 200
    flash[:success] = 'Successfully revoked from QuickBooks Online'
    realm.destroy!
  else
    flash[:danger] = 'Unable to revoke'
  end

  redirect_to(effective_qb_online.admin_quickbooks_path)
end