PayPal
官方PayPal Model Context Protocol 伺服器可讓您透過函式呼叫與 PayPal API 整合。此協定支援多種工具,以便與不同的 PayPal 服務互動。
你可以用 PayPal MCP 做什麼?
- 建立並寄送發票 — 要求草擬含明細項目的發票,然後透過
create_invoice和send_invoice寄出或安排提醒。 - 管理付款與退款 — 使用
create_order建立訂單、擷取款項,或透過create_refund對已擷取的款項發出退款。 - 處理爭議 — 直接透過
list_disputes和accept_dispute_claim列出未處理的爭議、檢視詳細資料,或接受索賠。 - 追蹤貨運 — 使用
create_shipment_tracking為訂單建立追蹤紀錄,並透過get_shipment_tracking取得其狀態。 - 管理訂閱 — 透過
create_subscription_plan和cancel_subscription設定訂閱方案、建立訂閱,或取消有效的訂閱。 - 取得商家洞察 — 使用
list_transactions和get_merchant_insights擷取交易歷史或商業分析。
託管 MCP 伺服器
npx add-mcp 'https://mcp.paypal.com/mcp'可安裝到 Claude Code、Codex、Cursor 等客戶端
文件
PayPal Agent 工具套件
PayPal Agent 工具套件讓熱門的代理框架(包括 OpenAI 的 Agent SDK、LangChain、Vercel 的 AI SDK 以及 Model Context Protocol (MCP))能夠透過函式呼叫與 PayPal API 整合。它支援 TypeScript,並建構於 PayPal API 與 PayPal SDK 之上。
可用工具
PayPal Agent 工具套件提供以下工具:
發票
-
create_invoice:在 PayPal 系統中建立新發票,包括收款人帳單詳細資料、明細項目、發票備註、自訂色彩主題、可選的運費,以及啟用 PAY_BY_BANK 作為付款方式的選項 -
list_invoices:列出發票,可選擇分頁與篩選 -
get_invoice:擷取特定發票的詳細資料 -
send_invoice:將發票傳送給收款人 -
send_invoice_reminder:為現有發票傳送提醒 -
cancel_sent_invoice:取消已傳送的發票 -
generate_invoice_qr_code:為發票產生 QR Code -
delete_invoice:永久刪除草稿或排定的發票 -
generate_invoice_number:產生商家可用的下一個發票編號 -
search_invoicing:搜尋發票或週期性發票系列 -
update_invoicing:更新現有發票或週期性發票系列(完整取代) -
cancel_invoice_auto_reminder:取消發票所有排定的自動提醒 -
record_payment_for_invoice:針對發票記錄外部或手動付款 -
record_refund_for_invoice:針對發票記錄退款 -
create_conditional_rules_for_invoice:為發票建立條件規則,例如提前付款折扣或自動取消日期 -
create_recurring_series:建立週期性發票系列,可依排程自動產生並傳送發票 -
activate_recurring_series:啟用草稿的週期性發票系列 -
get_recurring_series:擷取特定週期性發票系列的詳細資料 -
cancel_recurring_series:取消啟用中的週期性發票系列 -
delete_recurring_series:刪除草稿的週期性發票系列
付款
create_order:根據提供的詳細資料在 PayPal 系統中建立訂單get_order:擷取訂單的詳細資料pay_order:處理已授權訂單的付款create_refund:處理已擷取付款的退款get_refund:取得特定退款的詳細資料
爭議管理
list_disputes:擷取所有未解決爭議的摘要get_dispute:擷取特定爭議的詳細資訊accept_dispute_claim:接受爭議索賠
貨運追蹤
create_shipment_tracking:建立貨運追蹤記錄get_shipment_tracking:擷取貨運追蹤資訊update_shipment_tracking:更新貨運追蹤資訊
目錄管理
create_product:在 PayPal 目錄中建立新產品list_products:列出產品,可選擇分頁與篩選show_product_details:擷取特定產品的詳細資料
訂閱管理
create_subscription_plan:建立新的訂閱方案list_subscription_plans:列出訂閱方案show_subscription_plan_details:擷取特定訂閱方案的詳細資料create_subscription:建立新的訂閱show_subscription_details:擷取特定訂閱的詳細資料update_subscription:更新現有訂閱cancel_subscription:取消啟用中的訂閱
報告與洞察
list_transactions:列出交易,可選擇分頁與篩選get_merchant_insights:擷取商家的商業智慧指標與分析
TypeScript
安裝
除非您想要修改套件,否則不需要此原始碼。如果您只是想要使用此套件,請執行:
npm install @paypal/agent-toolkit
需求
- Node 18+
使用方式
此函式庫需要使用您帳戶的用戶端 ID 與密鑰進行設定,這些資訊可在您的 PayPal Developer Dashboard 中取得。
此工具套件可與 Vercel 的 AI SDK 搭配使用,並可作為工具清單傳入。更多詳細資訊,請參閱我們的 範例。
import { PayPalAgentToolkit } from '@paypal/agent-toolkit/ai-sdk';
const paypalToolkit = new PayPalAgentToolkit({
clientId: process.env.PAYPAL_CLIENT_ID,
clientSecret: process.env.PAYPAL_CLIENT_SECRET,
configuration: {
actions: {
invoices: {
create: true,
createRecurringSeries: true,
list: true,
send: true,
sendReminder: true,
cancel: true,
delete: true,
generateQRC: true,
generateInvoiceNumber: true,
search: true,
update: true,
cancelReminders: true,
recordPayment: true,
recordRefund: true,
createConditionalRules: true,
},
products: { create: true, list: true, update: true },
subscriptionPlans: { create: true, list: true, show: true },
shipment: { create: true, show: true, cancel: true },
orders: { create: true, get: true },
disputes: { list: true, get: true },
},
},
});
若要使用沙盒模式,請在您的設定中新增 context。
configuration: {
context: {
sandbox: true,
}
}
初始化工作流程
import { PayPalWorkflows, ALL_TOOLS_ENABLED } from '@paypal/agent-toolkit/ai-sdk';
const paypalWorkflows = new PayPalWorkflows({
clientId: process.env.PAYPAL_CLIENT_ID,
clientSecret: process.env.PAYPAL_CLIENT_SECRET,
configuration: {
actions: ALL_TOOLS_ENABLED,
},
});
使用方式
使用工具套件
const llm: LanguageModelV1 = getModel(); // The model to be used with ai-sdk
const { text: response } = await generateText({
model: llm,
tools: paypalToolkit.getTools(),
maxSteps: 10,
prompt: `Create an order for $50 for custom handcrafted item and get the payment link.`,
});
環境變數
可以使用以下環境變數:
PAYPAL_ACCESS_TOKEN:您的 PayPal 存取權杖PAYPAL_ENVIRONMENT:設為SANDBOX以使用沙盒模式,設為PRODUCTION以使用正式環境(預設為SANDBOX模式)
本指南說明如何為 PayPal API 整合產生存取權杖,包括如何找到您的用戶端 ID 與用戶端密鑰。
先決條件
- PayPal 開發人員帳戶(用於沙盒)
- PayPal 商業帳戶(用於正式環境)
尋找您的用戶端 ID 與用戶端密鑰
-
建立 PayPal 開發人員帳戶:
- 前往 PayPal Developer Dashboard
- 使用您的 PayPal 憑證註冊或登入
-
存取您的憑證:
- 在開發人員儀表板中,點擊選單中的 Apps & Credentials
- 根據您的需求在 Sandbox 與 Live 模式之間切換
-
建立或檢視應用程式:
- 若要建立新應用程式,請點擊 Create App
- 為您的應用程式命名,並選擇要關聯的商業帳戶
- 對於現有應用程式,請點擊應用程式名稱以檢視詳細資料
-
擷取憑證:
- 建立或選取應用程式後,您會看到一個畫面,其中包含您的:
- Client ID:您應用程式的公開識別碼
- Client Secret:私密金鑰(點擊 "Show" 後顯示)
- 請安全地儲存這些憑證,因為產生存取權杖時需要用到
- 建立或選取應用程式後,您會看到一個畫面,其中包含您的:
產生存取權杖
使用 cURL
curl -v https://api-m.sandbox.paypal.com/v1/oauth2/token \\
-H \"Accept: application/json\" \\
-H \"Accept-Language: en_US\" \\
-u \"CLIENT_ID:CLIENT_SECRET\" \\
-d \"grant_type=client_credentials\"
將 CLIENT_ID 和 CLIENT_SECRET 替換為您的實際憑證。若為正式環境,請使用 https://api-m.paypal.com 取代沙盒 URL。
使用 Postman
- 建立一個新的請求,傳送至
https://api-m.sandbox.paypal.com/v1/oauth2/token - 將方法設為 POST
- 在 Authorization 下,選取 Basic Auth 並輸入您的 Client ID 與 Client Secret
- 在 Body 下,選取 x-www-form-urlencoded 並新增一個鍵
grant_type,值為client_credentials - 傳送請求
回應
成功的回應看起來會像這樣:
{
"scope": "...",
"access_token": "Your Access Token",
"token_type": "Bearer",
"app_id": "APP-80W284485P519543T",
"expires_in": 32400,
"nonce": "..."
}
複製 access_token 的值,以便在您的 Claude Desktop 整合中使用。
權杖詳細資料
- 沙盒權杖:有效期限為 3-8 小時
- 正式環境權杖:有效期限為 8 小時
- 建議在到期前實作權杖重新整理邏輯
在 Claude Desktop 中使用權杖
取得存取權杖後,請更新 Claude Desktop 連接器設定中的 PAYPAL_ACCESS_TOKEN 值:
{
"env": {
"PAYPAL_ACCESS_TOKEN": "YOUR_NEW_ACCESS_TOKEN",
"PAYPAL_ENVIRONMENT": "SANDBOX"
}
}
最佳實務
- 安全地儲存用戶端 ID 與用戶端密鑰
- 實作權杖重新整理邏輯以處理權杖到期
- 使用環境專屬的權杖(沙盒用於測試,正式環境用於真實交易)
- 避免在應用程式程式碼中硬編碼權杖
免責聲明
AI 生成的內容可能不準確或不完整。使用者有責任在依賴任何資訊前獨立驗證其正確性。PayPal 不對輸出內容的準確性提供任何保證,且不對因使用該內容而導致的任何決策、行動或後果承擔責任。