PayPal

官方

PayPal Model Context Protocol 伺服器可讓您透過函式呼叫與 PayPal API 整合。此協定支援多種工具,以便與不同的 PayPal 服務互動。

你可以用 PayPal MCP 做什麼?

請讓你的助理「建立發票並寄出」,或處理其他 PayPal 工作,例如付款、爭議和運送。

  • 建立並寄出發票 — 使用帳單詳細資料和明細項目建立新發票,然後透過 create_invoicesend_invoice 寄出。
  • 寄送發票提醒 — 使用 send_invoice_reminder 為既有發票寄送提醒。
  • 處理付款 — 使用 create_orderpay_order 建立訂單並處理其付款。
  • 管理爭議 — 使用 list_disputes 列出未處理的爭議,並使用 accept_dispute_claim 接受索賠。
  • 追蹤運送 — 使用 create_shipment_trackingupdate_shipment_tracking 建立及更新運送追蹤紀錄。
  • 列出交易 — 使用 list_transactions 擷取交易紀錄,並可選擇篩選條件。

文件

PayPal Agent Toolkit

PayPal Agent Toolkit 讓熱門的代理框架(包括 OpenAI 的 Agent SDK、LangChain、Vercel 的 AI SDK 以及 Model Context Protocol (MCP))能夠透過函式呼叫與 PayPal API 整合。它支援 TypeScript,並建構於 PayPal API 與 PayPal SDK 之上。

可用工具

PayPal Agent Toolkit 提供以下工具:

發票(Invoices)

  • create_invoice:在 PayPal 系統中建立新的發票,包括收款人帳單詳細資料、明細項目、發票備註、自訂色彩主題、選用的運費,以及啟用 PAY_BY_BANK 作為付款方式的選項
  • create_recurring_series:建立定期發票系列,可依排程自動產生並寄送發票
  • list_invoices:列出發票,可選用分頁與篩選功能
  • get_invoice:擷取特定發票的詳細資料
  • send_invoice:將發票寄送給收款人
  • send_invoice_reminder:為既有發票寄送提醒通知
  • cancel_sent_invoice:取消已寄出的發票
  • generate_invoice_qr_code:為發票產生 QR Code

付款(Payments)

  • create_order:根據提供的詳細資料在 PayPal 系統中建立訂單
  • get_order:擷取訂單的詳細資料
  • pay_order:為已授權的訂單處理付款
  • create_refund:為已擷取的款項處理退款
  • get_refund:取得特定退款的詳細資料

爭議管理(Dispute Management)

  • list_disputes:擷取所有進行中爭議的摘要
  • get_dispute:擷取特定爭議的詳細資訊
  • accept_dispute_claim:接受爭議索賠

貨運追蹤(Shipment Tracking)

  • create_shipment_tracking:建立貨運追蹤紀錄
  • get_shipment_tracking:擷取貨運追蹤資訊
  • update_shipment_tracking:更新貨運追蹤資訊

目錄管理(Catalog Management)

  • create_product:在 PayPal 目錄中建立新產品
  • list_products:列出產品,可選用分頁與篩選功能
  • show_product_details:擷取特定產品的詳細資料

訂閱管理(Subscription Management)

  • create_subscription_plan:建立新的訂閱方案
  • list_subscription_plans:列出訂閱方案
  • show_subscription_plan_details:擷取特定訂閱方案的詳細資料
  • create_subscription:建立新的訂閱
  • show_subscription_details:擷取特定訂閱的詳細資料
  • update_subscription:更新既有的訂閱
  • cancel_subscription:取消進行中的訂閱

報表與洞察(Reporting and Insights)

  • 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,
        generateQRC: 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 與用戶端密鑰

  1. 建立 PayPal 開發人員帳戶

  2. 存取您的憑證

    • 在開發人員儀表板中,點選選單中的 Apps & Credentials
    • 根據您的需求在 SandboxLive 模式之間切換
  3. 建立或檢視應用程式

    • 若要建立新的應用程式,請點選 Create App
    • 為您的應用程式命名,並選擇要關聯的商業帳戶
    • 若是既有應用程式,請點選應用程式名稱以檢視詳細資料
  4. 擷取憑證

    • 應用程式建立或選取後,您會看到一個畫面,其中包含您的:
      • 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_IDCLIENT_SECRET 替換為您的實際憑證。若為正式環境,請使用 https://api-m.paypal.com 取代沙盒 URL。

使用 Postman

  1. 建立一個新的請求,目標為 https://api-m.sandbox.paypal.com/v1/oauth2/token
  2. 將方法設為 POST
  3. Authorization 下方,選取 Basic Auth 並輸入您的 Client ID 與 Client Secret
  4. Body 下方,選取 x-www-form-urlencoded 並新增一個鍵 grant_type,值為 client_credentials
  5. 傳送請求

回應

成功的回應會類似以下內容:

{
  "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"
  }
}

最佳實務

  1. 安全地儲存用戶端 ID 與用戶端密鑰
  2. 實作權杖重新整理邏輯以處理權杖到期
  3. 使用環境專屬的權杖(沙盒用於測試,正式環境用於實際交易)
  4. 避免在應用程式程式碼中硬編碼權杖

免責聲明

AI 生成的內容可能不準確或不完整。使用者有責任在依賴任何資訊前獨立驗證其正確性。PayPal 不對輸出內容的準確性提供任何保證,亦不對因使用該內容而導致的任何決策、行動或後果承擔責任。