name: fbp-square-oauth description: Implement seller/shop-scoped Square OAuth connection flows in FBP apps, including app-wide Square credentials, shop token storage, authorize/callback URLs, token refresh, and OAuth verification.
fbp-square-oauth
trigger conditions
- ショップごとに Square OAuth 連携を追加する
shopに Square seller token / location / merchant ID を保存する- Square Developers の Application ID / Secret と FBP
settingを整理する - OAuth callback 後に JSON ではなく通常画面へ戻したい
- Sandbox OAuth の 400 / 白画面 / redirect_uri 問題を調査する
related skills
- DB項目を追加/削除する場合は
fbp-dbも使う。 - ショップ一覧ボタンや管理画面を触る場合は
fbp-original-screen/fbp-dialogも使う。 - 決済実行そのものは
fbp-square-paymentを使う。この skill は OAuth 連携までを扱う。
data design
- Square Application ID / Secret はショップ固有ではなくアプリ共通設定に置く。
shopには seller account から得た OAuth 結果だけを保存する。- 推奨
shopfields:square_location_idsquare_access_tokensquare_refresh_tokensquare_token_expires_atsquare_merchant_id
shop.square_application_idは原則不要。既存にある場合は整理候補。setting.square_application_secretが無い場合は追加し、設定画面ではpassword入力 + 空欄保存時は既存値保持 +設定済みマスク表示にする。
workflow
- 公式 Square OAuth docs を確認する。特に Sandbox/Production の URL と
session差分は変わりうるため、必要なら公式だけを参照する。 settingに app-wide credentials を用意する。square_application_idsquare_application_secret- 既存の
square_access_tokenは従来のテスト/直接決済用として混同しない。
shopに seller token fields を用意し、手入力フォームから token 類は外す。- ショップ一覧に
SQUARE連携ボタンを追加する。 square_connect()で authorize URL にリダイレクトする。square_oauth_callback()でstateを検証し、codeを/oauth2/tokenに交換する。- token 取得後
/v2/locationsを呼び、最初の active location ID を保存する。 - callback 完了後は直接管理画面 class を描画せず、
__AUTO_LOAD_MAIN_AREAをセットしてbase*pageへ戻す。 - 公開決済では注文の
shop_idに紐づく shop token/location をset_square()に渡す。期限が近い場合は refresh token で更新する。
Square URL rules
- Sandbox authorize base:
https://connect.squareupsandbox.com/oauth2/authorize - Production authorize base:
https://connect.squareup.com/oauth2/authorize - Sandbox token base:
https://connect.squareupsandbox.com/oauth2/token - Production token base:
https://connect.squareup.com/oauth2/token - Sandbox では
session=falseを付けない。Sandbox はsession=truedefault のみ。 - Production では
session=falseを付ける。 scopeは space-separated string を URL encode する。+区切りで問題ない。- callback URL は Square Developers に登録した Redirect URL と完全一致させる。
- FBP の
*URL が Square 側で通らない場合があるため、OAuth callback は次の標準形式を優先する。https://.../fbp/app.php?class=shop_original_management&function=square_oauth_callback
Sandbox checklist
- Square Developer Console で対象 app の environment が Sandbox になっている。
- Sandbox OAuth Redirect URL に callback URL を登録している。
- Seller 用 Sandbox test account を作成している。
- OAuth開始前に Developer Console の Sandbox test accounts から対象 seller を
Open in Square Dashboardで開いている。 - 400のHTMLに
first launch the seller test account from the Developer Consoleが出る場合、seller test account 未起動。
developer handoff
開発者へ手順を伝える場合は、次を短く案内する。
Square Developer Console settings
- Square Developer Console で対象 Application を開く。
- Sandbox / Production の利用環境を確認する。
- OAuth redirect URL に FBP callback URL を完全一致で登録する。
- 推奨形式:
https://<domain>/<app>/fbp/app.php?class=shop_original_management&function=square_oauth_callback *を含む FBP URL は Square 側で通らない場合があるため避ける。
- 推奨形式:
- Sandbox では Seller test account を作成し、初回OAuth前に
Open in Square Dashboardで一度開く。 - Application ID と Application Secret を控える。Access token はOAuth連携用の事前設定には使わない。
FBP settings
- FBP管理画面の setting に次を設定する。
square_application_idsquare_application_secret
square_application_secretはマスク表示し、空欄保存では既存値を保持する。- 既存の
square_access_tokenはアプリ共通の直接決済/テスト用として扱い、ショップOAuth token と混同しない。
Test flow
- Sandbox環境でショップ管理一覧を開く。
- 対象ショップの
SQUARE連携を押す。 - Square認可画面へ遷移することを確認する。
- Sandbox seller で許可する。
- FBP管理画面へ戻り、JSONが直接表示されないことを確認する。
shopにsquare_access_token,square_refresh_token,square_token_expires_at,square_merchant_id,square_location_idが保存されたことを確認する。- 公開決済側で注文の
shop_idに紐づく shop token/location が使われることを確認する。
sample code
- Read
references/sample-shop-square-oauth.mdwhen implementing a shop-scoped OAuth flow.
verification
php -lfor changed PHP files.- Sync source to the target runtime using the environment's normal procedure.
app_call <shop_management_class> runsucceeds.setting.pageshowsApplication Secretas masked/blank input, not raw secret.- OAuth authorize request reaches Square without 400.
- Callback stores
square_access_token,square_refresh_token,square_token_expires_at,square_merchant_id, andsquare_location_id. - After callback, browser returns to normal FBP page, not raw JSON.