@alphabite/medusa-paypal

中文文档
0.2.6发布于 2026/04/17MITTypeScriptESM + CJS近 30 天下载 3078

Alphabite's Medusa Paypal Plugin

原始使用文档
文档目录18 节
  1. 🔢 Version Compatibility
  2. Instructions
  3. 📚 Table of Contents
  4. 🎯 Core Features
  5. 🧱 Compatibility
  6. 🛠 Common Use Cases
  7. 📖 Documentation
  8. 1. Install the Plugin
  9. 2. Register the Plugin
  10. ⚙️ Plugin Options
  11. ✅ Compatibility
  12. 🚀 Next Steps
  13. 💻 Front End Integration
  14. 1. Install Dependencies
  15. 2. Environment Variables
  16. 3. Implementation Overview
  17. 4. Code Example
  18. 5. Payment Flow Details

📝 PayPal Plugin for Medusa

Discord Documentation

The Alphabite PayPal Plugin integrates PayPal payment processing into your Medusa store. It handles various payment flows, including capturing payments, managing refunds, and ensuring robust error handling.


🔢 Version Compatibility

Pick the plugin version that matches your Medusa backend version:

Medusa VersionPlugin VersionInstall Command
>= 2.13.*latestnpm install @alphabite/medusa-paypal
< 2.13.*0.2.5npm install @alphabite/[email protected]

Instructions

  • Medusa 2.13.* and above: Install the latest version of the plugin. This is the actively maintained release and is required for compatibility with the latest Medusa APIs.
  • Medusa below 2.13.*: Pin the plugin to version 0.2.5. Later plugin versions rely on APIs introduced in Medusa 2.13 and will not work on older backends.

Before installing, verify your Medusa version:

npm list @medusajs/medusa

Then install the matching plugin version:

# Medusa >= 2.13.* (latest)
npm install @alphabite/medusa-paypal

# Medusa < 2.13.* (pinned)
npm install @alphabite/[email protected]

⚠️ Use the same plugin version across local, staging, and production to avoid runtime errors.


📝 PayPal Plugin for Medusa

Discord Documentation

The Alphabite PayPal Plugin integrates PayPal payment processing into your Medusa store. It handles various payment flows, including capturing payments, managing refunds, and ensuring robust error handling.


📚 Table of Contents


🎯 Core Features

  • ✅ Seamless PayPal payment integration
  • 🔄 Handles various PayPal error states
  • 💰 Supports refunds directly from Medusa Admin
  • 🛒 Creates new order IDs for each payment attempt within the same payment intent
  • 📦 Optional inclusion of shipping and customer data in PayPal orders

🧱 Compatibility

  • Backend: Medusa v2+
  • Frontend: Framework-agnostic (integrates with PayPal's SDK)
  • Admin: Refund functionality integrated into Medusa Admin

🛠 Common Use Cases

  • Accepting PayPal payments for products and services
  • Managing payment captures and refunds efficiently
  • Ensuring robust payment processing with comprehensive error handling

📖 Documentation

For complete documentation, visit our PayPal Plugin Documentation.



📦 Installation

This guide walks you through installing and configuring the Alphabite PayPal Plugin in your Medusa backend.


1. Install the Plugin

Install the package via npm:

npm install @alphabite/medusa-paypal

2. Register the Plugin

Add the plugin to your medusa.config.ts or medusa-config.js:

{
  plugins: [
    {
      resolve: "@alphabite/medusa-paypal",
      options: {
        clientId: process.env.PAYPAL_CLIENT_ID,
        clientSecret: process.env.PAYPAL_CLIENT_SECRET,
        isSandbox: process.env.PAYPAL_IS_SANDBOX === "true",
        webhookId: process.env.PAYPAL_WEBHOOK_ID,
        includeShippingData: false,
        includeCustomerData: false,
      },
    },
  ],
  modules:[
    {
      resolve: "@medusajs/medusa/payment",
      options: {
        providers: [
          {
            resolve: "./src/modules/paypal",
            id: "paypal",
            options: {
              clientId: process.env.PAYPAL_CLIENT_ID,
              clientSecret: process.env.PAYPAL_CLIENT_SECRET,
              isSandbox: process.env.PAYPAL_SANDBOX === "true",
              webhookId: process.env.PAYPAL_WEBHOOK_ID,
              includeShippingData: false,
              includeCustomerData: false,
            },
          },
        ],
      },
    },
  ]
};

⚙️ Plugin Options

The following options can be passed to the PayPal plugin in your medusa-config.js or medusa.config.ts file:

OptionTypeDefaultDescription
clientIdstringRequired. Your PayPal API client ID.
clientSecretstringRequired. Your PayPal API client secret.
isSandboxbooleantrueWhether to use the PayPal Sandbox environment for testing.
webhookIdstringOptional. Your PayPal webhook ID. If provided, enables confirmation of payment captures.
includeShippingDatabooleanfalseOptional. If true, shipping data from the storefront order will be added to the PayPal order.
includeCustomerDatabooleanfalseOptional. If true, customer data from the storefront order will be added to the PayPal order.

✅ Compatibility

  • Requires Medusa v2
  • Compatible with both JS and TypeScript projects

🚀 Next Steps

👉 Configuration Guide 👉 Join our Discord Community for faster support


💻 Front End Integration

This guide explains how to integrate the PayPal payment flow into your Next.js storefront using @paypal/react-paypal-js.

1. Install Dependencies

Install the official PayPal React SDK:

npm install @paypal/react-paypal-js

2. Environment Variables

Ensure your public PayPal Client ID is available in your environment variables:

NEXT_PUBLIC_PAYPAL_CLIENT_ID=your_paypal_client_id

3. Implementation Overview

The integration involves wrapping your payment form with PayPalScriptProvider and PayPalCardFieldsProvider.

Key Components:

  • PayPalScriptProvider: Loads the PayPal SDK script.
  • PayPalCardFieldsProvider: Manages the state and callbacks for the card fields.
  • PayPalCardFieldsForm: Renders the secure input fields.

4. Code Example

Here is a simplified structure of how to implement the PayPal payment component, based on shose-storefront/src/collections/form/checkout/paypal.tsx:

import {
  PayPalCardFieldsForm,
  PayPalCardFieldsProvider,
  PayPalScriptProvider,
  usePayPalCardFields,
} from "@paypal/react-paypal-js";
import { useState, useEffect } from "react";
// Import your internal hooks/utilities (e.g., sdk, usePlaceOrder)

export const PayPalPayment = ({ cart, onPaymentCompleted }) => {
  const [clientToken, setClientToken] = useState(null);

  // 1. Fetch Client Token from your backend
  useEffect(() => {
    const fetchClientToken = async () => {
      const response = await sdk.client.fetch("/store/paypal/client-token", {
        method: "POST",
      });
      setClientToken(response.clientToken);
    };
    fetchClientToken();
  }, []);

  // 2. Define Callbacks
  const createOrder = async () => {
    // Logic to initiate payment session and return order_id
    // e.g., sdk.store.payment.initiatePaymentSession(cart, { provider_id: "pp_paypal_paypal" })
    return order_id;
  };

  const onApprove = async (data) => {
    // Logic to finalize order after PayPal approval
    await placeOrder();
    onPaymentCompleted();
  };

  if (!clientToken) return <div>Loading...</div>;

  return (
    <PayPalScriptProvider
      options={{
        clientId: process.env.NEXT_PUBLIC_PAYPAL_CLIENT_ID,
        components: "card-fields",
        currency: "EUR",
        intent: "capture",
        dataClientToken: clientToken,
      }}
    >
      <PayPalCardFieldsProvider
        createOrder={createOrder}
        onApprove={onApprove}
        style={{
          input: {
            "font-size": "14px",
            "font-family": "Inter, sans-serif",
            color: "#111827",
          },
        }}
      >
        <PayPalCardFieldsForm />
        <SubmitButton />
      </PayPalCardFieldsProvider>
    </PayPalScriptProvider>
  );
};

const SubmitButton = () => {
  const { cardFieldsForm } = usePayPalCardFields();

  const handleClick = async () => {
    if (cardFieldsForm) {
      await cardFieldsForm.submit();
    }
  };

  return <button onClick={handleClick}>Pay Now</button>;
};

5. Payment Flow Details

  1. Client Token: You must fetch a client token from your Medusa backend (/store/paypal/client-token) and pass it to PayPalScriptProvider. This authorizes the client to perform actions on behalf of your account.
  2. Create Order: The createOrder callback is triggered when the user attempts to pay. It should initialize a payment session in Medusa and return the PayPal Order ID.
  3. On Approve: The onApprove callback runs after PayPal successfully authorizes the payment. Use this to complete the order in Medusa (e.g., placeOrder).

版本与发布记录

@alphabite/medusa-paypal 共发布 12 个版本,首次发布于 2025/06/12;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.2.6latest2026/04/176 个月前
0.2.52026/02/039 个月前
0.2.42025/09/091.2 年前
0.2.32025/09/091.2 年前
0.2.22025/09/091.2 年前
0.2.12025/08/281.2 年前

关于 @alphabite/medusa-paypal 的常见问题

@alphabite/medusa-paypal 是什么?

Alphabite's Medusa Paypal Plugin

如何安装 @alphabite/medusa-paypal?

在项目目录运行 npm install @alphabite/medusa-paypal,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@medusajs/admin-sdk(2.13.6)、@medusajs/cli(2.13.6)、@medusajs/framework(2.13.6)、@medusajs/icons(2.13.6)、@medusajs/medusa(2.13.6)、@medusajs/test-utils(2.13.6)、@medusajs/ui(4.0.4)、@mikro-orm/cli(6.4.3)、@mikro-orm/core(6.4.3)、@mikro-orm/knex(6.4.3)、@mikro-orm/migrations(6.4.3)、@mikro-orm/postgresql(6.4.3)、awilix(^8.0.1)、pg(^8.13.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@alphabite/medusa-paypal 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@alphabite/medusa-paypal 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@alphabite/medusa-paypal 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@alphabite/medusa-paypal 可以商用吗?

@alphabite/medusa-paypal 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@alphabite/medusa-paypal 的下载量有多少?

根据 npm 官方下载统计,@alphabite/medusa-paypal 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,078 次,日均约 103 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@alphabite/medusa-paypal 还在维护吗?

最新版本 0.2.6 发布于 2026/04/17(6 个月前);至今共发布 12 个版本,其中过去 12 个月发布了 2 个;首次发布于 2025/06/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。