Class: SpreeSquare::Credential

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree_square/credential.rb

Overview

A Square OAuth connection for one Spree::Store. Replaces the SQUARE_ACCESS_TOKEN env var with a per-store, self-service "Connect to Square" flow (see SpreeSquare::OauthClient and Spree::Admin::SquareOauthController) — the access-token mechanism Square requires multi-merchant apps to move away from before listing on the App Marketplace (personal access tokens are explicitly disallowed there).

access_token/refresh_token are encrypted at rest (ActiveRecord

Encryption — see config/initializers/active_record_encryption.rb); Square tokens expire every 30 days and Square recommends refreshing every 7 or fewer, hence REFRESH_BUFFER below.

Constant Summary collapse

REFRESH_BUFFER =
7.days

Instance Method Summary collapse

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/models/spree_square/credential.rb', line 29

def expired?
  expires_at.present? && expires_at <= Time.current
end

#needs_refresh?Boolean

True once we're inside Square's recommended refresh window — checked before every API call (see SpreeSquare::Client#ensure_fresh!) rather than on a schedule, so a credential that's gone briefly unused still gets refreshed the moment it's needed again.

Returns:

  • (Boolean)


37
38
39
# File 'app/models/spree_square/credential.rb', line 37

def needs_refresh?
  expires_at.present? && expires_at <= REFRESH_BUFFER.from_now
end

#sandbox?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/models/spree_square/credential.rb', line 25

def sandbox?
  square_environment == 'sandbox'
end