Stripe CLI Windows 安裝與登入
Stripe 官方命令列工具,可在終端機操作 Stripe API、把 webhook 轉發到本機測試,這邊自動抓 GitHub 上的最新版本安裝
兩個選項擇一,整段貼到 PowerShell 執行
選項 A:C:\App\stripe-cli(系統變數)
以系統管理員身分執行
先確認權限,沒有顯示錯誤再往下
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw "請以系統管理員身分執行 PowerShell" }
下載並安裝
# 設定安裝目錄
$StripeDir = "C:\App\stripe-cli"
# 取得最新版本資訊
$Release = Invoke-RestMethod https://api.github.com/repos/stripe/stripe-cli/releases/latest
$Asset = $Release.assets | Where-Object name -like "*_windows_x86_64.zip"
# 下載 stripe
$Zip = "$env:TEMP\stripe.zip"
Invoke-WebRequest -Uri $Asset.browser_download_url -OutFile $Zip
# 驗證檔案
if ((Get-FileHash $Zip -Algorithm SHA256).Hash -ne ($Asset.digest -replace "^sha256:")) { throw "SHA256 不符" }
# 解壓縮到安裝目錄(覆蓋舊版)
Expand-Archive $Zip $StripeDir -Force
Remove-Item $Zip
# 加入系統環境變數
$MachinePath = [Environment]::GetEnvironmentVariable("Path", "Machine")
if (($MachinePath -split ";") -notcontains $StripeDir) {
[Environment]::SetEnvironmentVariable("Path", "$MachinePath;$StripeDir", "Machine")
}
選項 B:C:\Users\<你>\.local\bin(使用者變數)
一般權限即可
# 設定安裝目錄
$StripeDir = "$env:USERPROFILE\.local\bin"
# 取得最新版本資訊
$Release = Invoke-RestMethod https://api.github.com/repos/stripe/stripe-cli/releases/latest
$Asset = $Release.assets | Where-Object name -like "*_windows_x86_64.zip"
# 下載 stripe
$Zip = "$env:TEMP\stripe.zip"
Invoke-WebRequest -Uri $Asset.browser_download_url -OutFile $Zip
# 驗證檔案
if ((Get-FileHash $Zip -Algorithm SHA256).Hash -ne ($Asset.digest -replace "^sha256:")) { throw "SHA256 不符" }
# 解壓縮到安裝目錄(覆蓋舊版)
Expand-Archive $Zip $StripeDir -Force
Remove-Item $Zip
# 加入使用者環境變數
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
if (($UserPath -split ";") -notcontains $StripeDir) {
[Environment]::SetEnvironmentVariable("Path", "$UserPath;$StripeDir", "User")
}
確認安裝
裝完接著執行,重新載入環境變數並顯示安裝位置
# 重新載入環境變數
$env:Path = (($env:Path -split ";") + ([Environment]::GetEnvironmentVariable("Path", "Machine") -split ";") + ([Environment]::GetEnvironmentVariable("Path", "User") -split ";") | Where-Object { $_ } | Select-Object -Unique) -join ";"
# 顯示安裝位置
Write-Host "stripe 位置:$((Get-Command stripe).Source)"
確認版本
stripe --version
登入
登入 Stripe(瀏覽器會開啟 Dashboard,確認畫面上的配對碼和終端機一致,可一次勾選多個帳號、沙盒或正式環境,再按審核)
stripe login
已登入時再跑一次 stripe login,按 Enter 開啟瀏覽器,可以授權更多帳號、沙盒或正式環境(live)
Stripe 新版建議測試只用沙盒就好,授權時不要勾選沙盒底下的「測試模式」,那是舊式的測試環境
確認環境
列出已授權的帳號,active 是目前使用中的
stripe whoami --format json | ConvertFrom-Json -PipelineVariable me |
ForEach-Object authorized_accounts |
Select-Object name, id,
@{ n = "modes"; e = { $_.modes -join ", " } },
@{ n = "status"; e = { if ($_.id -eq $me.account_id) { "active" } } }
更新
重跑選項 A 或 B 即可,會抓最新版覆蓋