srmklive/paypal

Laravel插件,用于通过PayPal Express Checkout处理支付。可以与其他应用程序独立使用。


README

Software License Latest Version on Packagist Total Downloads StyleCI Tests Coverage Status Code Quality

重要

Laravel 11将是v3.0支持的最后一个版本。v4正在积极开发中,预计于2024年10月底发布。对于v4,以下是一些引入的更改:

  • 需要PHP 8.1及以上版本。
  • 需要Laravel 10及以上版本。
  • 集成PayPal JS SDK。
  • Symphony插件。

文档

通过以下链接可以查看该包的文档

https://laravel-paypal.readthedocs.io/en/latest/

旧文档可以在以下链接找到

https://srmklive.github.io/laravel-paypal/docs.html

使用

以下是一些访问PayPal提供者的方式

// Import the class namespaces first, before using it directly
use Srmklive\PayPal\Services\PayPal as PayPalClient;

$provider = new PayPalClient;

// Through facade. No need to import namespaces
$provider = \PayPal::setProvider();

配置文件

配置文件 paypal.php 位于 config 文件夹中。以下是发布后的内容

return [
    'mode'    => env('PAYPAL_MODE', 'sandbox'), // Can only be 'sandbox' Or 'live'. If empty or invalid, 'live' will be used.
    'sandbox' => [
        'client_id'         => env('PAYPAL_SANDBOX_CLIENT_ID', ''),
        'client_secret'     => env('PAYPAL_SANDBOX_CLIENT_SECRET', ''),
        'app_id'            => 'APP-80W284485P519543T',
    ],
    'live' => [
        'client_id'         => env('PAYPAL_LIVE_CLIENT_ID', ''),
        'client_secret'     => env('PAYPAL_LIVE_CLIENT_SECRET', ''),
        'app_id'            => env('PAYPAL_LIVE_APP_ID', ''),
    ],

    'payment_action' => env('PAYPAL_PAYMENT_ACTION', 'Sale'), // Can only be 'Sale', 'Authorization' or 'Order'
    'currency'       => env('PAYPAL_CURRENCY', 'USD'),
    'notify_url'     => env('PAYPAL_NOTIFY_URL', ''), // Change this accordingly for your application.
    'locale'         => env('PAYPAL_LOCALE', 'en_US'), // force gateway language  i.e. it_IT, es_ES, en_US ... (for express checkout only)
    'validate_ssl'   => env('PAYPAL_VALIDATE_SSL', true), // Validate SSL when creating api client.
];

覆盖PayPal API配置

您可以通过调用 setApiCredentials 方法来覆盖PayPal API配置

$config = [
    'mode'    => 'live',
    'live' => [
        'client_id'         => 'PAYPAL_LIVE_CLIENT_ID',
        'client_secret'     => 'PAYPAL_LIVE_CLIENT_SECRET',
        'app_id'            => 'PAYPAL_LIVE_APP_ID',
    ],

    'payment_action' => 'Sale',
    'currency'       => 'USD',
    'notify_url'     => 'https://your-site.com/paypal/notify',
    'locale'         => 'en_US',
    'validate_ssl'   => true,
];
$provider->setApiCredentials($config);

获取访问令牌

通过调用 setApiCredentials 方法设置PayPal API配置后。在进行任何API调用之前,您需要获取访问令牌

$provider->getAccessToken();

设置货币

默认使用的货币是 USD。如果您想更改它,您可以在调用任何相应的API方法之前,通过调用 setCurrency 方法设置不同的货币

$provider->setCurrency('EUR');

辅助方法

请注意,以下示例中 addPlanTrialPricing 的调用是可选的,在创建不带试用期的订阅时可以省略。

setReturnAndCancelUrl() 是可选的。如果您设置了URL,则必须使用真实域名。例如,localhost,project.test 不可用。

创建每日重复订阅

$response = $provider->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
            ->addPlanTrialPricing('DAY', 7)
            ->addDailyPlan('Demo Plan', 'Demo Plan', 1.50)
            ->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
            ->setupSubscription('John Doe', 'john@example.com', '2021-12-10');

创建每周重复订阅

$response = $provider->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
            ->addPlanTrialPricing('DAY', 7)
            ->addWeeklyPlan('Demo Plan', 'Demo Plan', 30)
            ->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
            ->setupSubscription('John Doe', 'john@example.com', '2021-12-10');

创建每月重复订阅

$response = $provider->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
            ->addPlanTrialPricing('DAY', 7)
            ->addMonthlyPlan('Demo Plan', 'Demo Plan', 100)
            ->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
            ->setupSubscription('John Doe', 'john@example.com', '2021-12-10');

创建每年重复订阅

$response = $provider->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
            ->addPlanTrialPricing('DAY', 7)
            ->addAnnualPlan('Demo Plan', 'Demo Plan', 600)
            ->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
            ->setupSubscription('John Doe', 'john@example.com', '2021-12-10');

创建具有自定义间隔的重复订阅

$response = $provider->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
            ->addCustomPlan('Demo Plan', 'Demo Plan', 150, 'MONTH', 3)
            ->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
            ->setupSubscription('John Doe', 'john@example.com', '2021-12-10');

通过现有产品及计费计划创建订阅

$response = $this->client->addProductById('PROD-XYAB12ABSB7868434')
    ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ')
    ->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
    ->setupSubscription('John Doe', 'john@example.com', $start_date);

支持

此版本支持Laravel 6或更高版本。

  • 如有任何问题,请在本节问题中创建一个。
  • 如果您想做出贡献
    • 从这个仓库分叉。
    • 实现您的功能。
    • 生成拉取请求。