litvinjuan / laravel-payments
Laravel 的支付包
Requires
- php: >=7.2
- ext-json: *
- konekt/enum-eloquent: ^1.3
- moneyphp/money: ^3.2
- nesbot/carbon: ^2.24
README
此包提供特性和接口,让您轻松创建和存储支付,并提供简化的界面与支付网关交互。
安装
您可以通过 composer 安装此包
composer require litvinjuan/laravel-payments
如果您使用的是低于 5.5 版本的 Laravel,您需要将以下内容添加到您的 config/app.php
文件中
'providers' => [ litvinjuan\LaravelPayments\LaravelPaymentsServiceProvider::class, ]
接下来,您需要发布配置和迁移文件。在您的项目根目录中运行以下命令
php artisan vendor:publish --provider="litvinjuan\LaravelPayments\LaravelPaymentsServiceProvider"
最后,运行迁移以创建您的支付表
php artisan migrate
使用方法
您需要创建一个实现了 Payable
接口和 HasPayment
特性的模型。这是您的用户将要支付的对象模型(例如订单、产品)。
Payable
接口有 3 个方法需要您自己实现:getPayablePrice()
应返回应支付的总金额,getPayableDescription()
应返回用于银行对账单和未来参考的支付描述,payer()
应返回应付款人的引用。
以下是一个示例
<?php namespace App; use Illuminate\Database\Eloquent\Model; use litvinjuan\LaravelPayments\Payments\HasPayment; use litvinjuan\LaravelPayments\Payments\Payable; use litvinjuan\LaravelPayments\Payments\Payer; use litvinjuan\LaravelPayments\Payments\Payment; use Money\Money; /** * @property int $id * @property Money $total * @property User $buyer * @property Payment $payment */ class Order extends Model implements Payable { use HasPayment; public function getPayablePrice(): Money { return $this->total; } public function getPayableDescription(): string { return 'This is a dummy bank statement'; } public function payer(): Payer { return $this->buyer; } }
您还需要创建一个实现了 Payer
接口和 CanPay
特性的模型。这是代表付款人的模型(提示:这通常是您的用户模型)。
Payer
接口有 1 个方法需要您自己实现:getPayerEmail()
应返回付款人的电子邮件地址。
以下是一个示例
<?php namespace App; use Illuminate\Database\Eloquent\Model; use litvinjuan\LaravelPayments\Payments\CanPay; use litvinjuan\LaravelPayments\Payments\Payer; use litvinjuan\LaravelPayments\Payments\Payment; /** * @property int $id * @property string $email * @property-read Payment[] $payments */ class User extends Model implements Payer { use CanPay; public function getPayerEmail(): string { return $this->email; } }
每次您需要购买时,都像平常一样创建您的 Payable 模型。
$order = new Order(); $order->total = new Money(2900, 'USD'); $order->save();
一旦您有了 Payable 实例,您就可以调用 createPayment
方法。
$payment = order->createPayment();
要与网关交互,您首先需要获取一个 Gateway 实例
$gateway = GatewayFactory::make($payment);
这将根据支付项的 gateway_name 属性返回一个 Gateway 实例。
然后我们可以检查该网关是否支持我们正在尝试进行的调用
if (! $gateway->supportsPurchase()) { throw InvalidRequestException::notSupported(); } if (! $gateway->supportsRefund()) { throw InvalidRequestException::notSupported(); } // ... // See full list of calls below
目前支持以下 10 种调用
* Authorize // Authorize an amount on the customer's card * CompleteAuthorize // Handle return from off-site authorization * Capture // Capture a previously authorized amount * Purchase // Automatically authorize and purchase an amount on the customer's card * CompletePurchase // Handle return from off-site authorization * GetPayment // Get information about a specific payment * PaymentNotification // Process a received payment notification * Refund // Refund a previously charged amount to the customer's card * Void // Void a payment * ValidatePayment // Validates whether a payment was successful or not
注意:在批准客户的购买之前,建议您使用 ValidatePayment
调用。
注意:个别网关可能不支持所有调用,因此您应始终检查相应的 supports() 方法。
更新日志
有关最近更改的更多信息,请参阅 更新日志。
安全
如果您发现任何安全问题,请通过电子邮件 litvinjuan@gmail.com 而不是使用问题跟踪器。
致谢
支持我们
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。