hypnodev/larapal

将Paypal轻松集成到Laravel的方法

v1.0.7 2023-08-10 10:38 UTC

This package is auto-updated.

Last update: 2024-09-10 12:54:45 UTC


README

Latest Version on Packagist Total Downloads

一种现代、简单且流畅的方法,允许您的客户通过PayPal进行支付。 banner

安装

通过Composer

$ composer require hypnodev/larapal

使用命令发布配置

$ php artisan vendor:publish --provider="hypnodev\Larapal\LarapalServiceProvider" 

在您的 .env 文件中添加以下密钥

PAYPAL_MODE=sandbox

PAYPAL_SANDBOX_ID=
PAYPAL_SANDBOX_SECRET=

PAYPAL_PRODUCTION_ID=
PAYPAL_PRODUCTION_SECRET=

如果您还没有Paypal API的凭证,请参考开始 - PayPal开发者

使用方法

BillableWithPaypal 特性添加到您的User模型中

<?php

namespace App;

use hypnodev\Larapal\Traits\BillableWithPaypal;
// ...

class User extends Authenticatable
{
    use Notifiable, BillableWithPaypal;
    
    // ...
}

这将向您的用户添加 chargeWithPaypalsubscribeWithPaypalgetPaypalCurrencygetShippingFields 方法。

然后您可以使用该方法向用户收费

<?php
auth()->user()->chargeWithPaypal('Charge description', [ // Array of items
    ['name' => 'pkg base', 'description' => 'base package', 'price' => 10.00, 'tax' => 2]
]);

// If your charge has shipping, you need to add an extra param with name and amount
auth()->user()->chargeWithPaypal('Charge description', [ // Array of items
    ['name' => 'pkg base', 'description' => 'base package', 'price' => 10.00, 'tax' => 2]
], [ // Shipping
    'name' => 'Courier name',
    'amount' => 100.50,
    'address' => [ // Optional, you can skip this key
        'address' => '4178 Libby Street',
        'city' => 'Hermosa Beach',
        'state' => 'CA',
        'postal_code' => '90254',
        'country' => 'USA'
    ]
]);

或者用于订阅

auth()->user()->subscribeWithPaypal('Plan id');

您可以在Paypal商户仪表板中的“App Center”下创建一个计划

如果您需要使用与配置不同的货币向用户收费,可以覆盖 getPaypalCurrency 方法

<?php

namespace App;

use hypnodev\Larapal\Traits\BillableWithPaypal;
// ...

class User extends Authenticatable
{
    use Notifiable, BillableWithPaypal;
    
    // ...
    
    /**
     * @inheritDoc
     */
    protected function getPaypalCurrency(): string
    {
        return 'USD';
    }
}

您可以使用 getShippingFields 方法设置默认的运输信息

<?php

namespace App;

use hypnodev\Larapal\Traits\BillableWithPaypal;
// ...

class User extends Authenticatable
{
    use Notifiable, BillableWithPaypal;
    
    // ...
    
    /**
     * @inheritDoc
     */
    protected function getShippingFields(): array
    {
        return [
            'address' => $this->shipping_address,
            'city' => $this->shipping_city,
            'state' => $this->shipping_state,
            'postal_code' => $this->shipping_postal_code,
            'country' => $this->shipping_country
        ];
    }
}

变更日志

请参阅变更日志,了解更多最近的变化信息。

测试

$ composer test

贡献

请参阅contributing.md 获取详细信息和待办事项列表。

安全

如果您发现任何安全相关的问题,请通过电子邮件 me@cristiancosenza.com 而不是使用问题跟踪器。

致谢

许可协议

许可协议。有关更多信息,请参阅许可文件