Class: SpreeSquare::Client
- Inherits:
-
Object
- Object
- SpreeSquare::Client
show all
- Defined in:
- app/services/spree_square/client.rb
Overview
Thin wrapper around Square::Client (the current, Fern-generated SDK —
square.rb v46+). All Square API access in this extension goes through
here so credential lookup and environment selection live in one place.
Deliberately NOT using square_legacy's client.orders_api.create_order-style
API that most tutorials still show — that's the old SDK shape bundled
alongside the new one for migration purposes only.
Credential resolution, in order:
1. A SpreeSquare::Credential for the given store (self-service "Connect
to Square" via OAuth — see SpreeSquare::OauthClient) — refreshed
first if it's inside Square's recommended renewal window.
2. SQUARE_ACCESS_TOKEN from ENV — the original single-tenant path, kept
so existing sandbox/dev setups (and anyone not yet using OAuth)
don't break. Square disallows this path for real multi-merchant use
("partner developers must not request nor use personal access
tokens from the sellers who use their application") — it's a dev
convenience here, not the production story.
Defined Under Namespace
Classes: MissingCredentialsError
Class Method Summary
collapse
-
.for_store(store = Spree::Store.default) ⇒ Object
-
.instance ⇒ Object
Deliberately NOT memoized: this used to be @instance ||= new, but once a store can connect via OAuth mid-process (an admin clicking "Connect to Square" while Puma/Solid Queue keep running), caching the first resolution forever means every job in that process would keep using the stale pre-connection state until a restart.
Instance Method Summary
collapse
Constructor Details
#initialize(credential: nil) ⇒ Client
Returns a new instance of Client.
42
43
44
45
46
47
48
|
# File 'app/services/spree_square/client.rb', line 42
def initialize(credential: nil)
@credential = credential
token = resolve_token
raise MissingCredentialsError, 'No Square credential connected and SQUARE_ACCESS_TOKEN is not set' if token.blank?
@client = Square::Client.new(base_url: base_url, token: token)
end
|
Class Method Details
.for_store(store = Spree::Store.default) ⇒ Object
38
39
40
|
# File 'app/services/spree_square/client.rb', line 38
def self.for_store(store = Spree::Store.default)
new(credential: SpreeSquare::Credential.find_by(store: store))
end
|
.instance ⇒ Object
Deliberately NOT memoized: this used to be @instance ||= new, but
once a store can connect via OAuth mid-process (an admin clicking
"Connect to Square" while Puma/Solid Queue keep running), caching the
first resolution forever means every job in that process would keep
using the stale pre-connection state until a restart. The lookup this
re-does each call is one indexed SpreeSquare::Credential query — not
worth trading correctness for.
34
35
36
|
# File 'app/services/spree_square/client.rb', line 34
def self.instance
for_store
end
|
Instance Method Details
#catalog ⇒ Object
50
|
# File 'app/services/spree_square/client.rb', line 50
def catalog = @client.catalog
|
#inventory ⇒ Object
53
|
# File 'app/services/spree_square/client.rb', line 53
def inventory = @client.inventory
|
#location_id ⇒ Object
57
58
59
|
# File 'app/services/spree_square/client.rb', line 57
def location_id
fetch(:location_id, env_key: 'SQUARE_LOCATION_ID')
end
|
#locations ⇒ Object
54
|
# File 'app/services/spree_square/client.rb', line 54
def locations = @client.locations
|
#orders ⇒ Object
51
|
# File 'app/services/spree_square/client.rb', line 51
def orders = @client.orders
|
#payments ⇒ Object
52
|
# File 'app/services/spree_square/client.rb', line 52
def payments = @client.payments
|
#sandbox? ⇒ Boolean
68
69
70
|
# File 'app/services/spree_square/client.rb', line 68
def sandbox?
environment == 'sandbox'
end
|
#webhook_signature_key ⇒ Object
App-level, not per-merchant: one webhook subscription/signing key
covers every store this deployment serves, same whether a given store
authenticates via OAuth or the ENV fallback above.
64
65
66
|
# File 'app/services/spree_square/client.rb', line 64
def webhook_signature_key
fetch(:webhook_signature_key, env_key: 'SQUARE_WEBHOOK_SIGNATURE_KEY')
end
|
#webhooks ⇒ Object
55
|
# File 'app/services/spree_square/client.rb', line 55
def webhooks = @client.webhooks
|