shetabit/payment
关于
简单的 Laravel 支付包,支持多个驱动。
商业与金融实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- +1
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README-ZH.md
原文
Laravel 支付网关
这是一个用于整合支付网关的Laravel包。这个包需要 PHP 8.4+,并支持 Laravel 12+。
捐赠我 如果你喜欢这个包:sunglasses: :bowtie:
For PHP integration you can use shetabit/multipay package.
此软件包可用于多个驱动程序,如果在当前驱动程序列表中找不到驱动程序,则可以创建它们
目录
可用驱动列表
- aqayepardakht :heavy_check_mark:
- asanpardakht :heavy_check_mark:
- atipay :heavy_check_mark:
- azkiVam (Installment payment) :heavy_check_mark:
- behpardakht (mellat) :heavy_check_mark:
- bitpay :heavy_check_mark:
- daracard :heavy_check_mark:
- digipay :heavy_check_mark:
- etebarino (Installment payment) :heavy_check_mark:
- fanavacard :heavy_check_mark:
- gooyapay :heavy_check_mark:
- irandargah :heavy_check_mark:
- irankish :heavy_check_mark:
- jibit :heavy_check_mark:
- local :heavy_check_mark:
- minipay :heavy_check_mark:
- nextpay :heavy_check_mark:
- novinopay :heavy_check_mark:
- omidpay :heavy_check_mark:
- panapal :heavy_check_mark:
- parsian :heavy_check_mark:
- parspal :heavy_check_mark:
- pasargad :heavy_check_mark:
- payfa :heavy_check_mark:
- paypal (will be added soon in next version)
- payping :heavy_check_mark:
- paystar :heavy_check_mark:
- pna :heavy_check_mark:
- poolam :heavy_check_mark:
- rayanpay :heavy_check_mark:
- refah :heavy_check_mark:
- sadad (melli) :heavy_check_mark:
- saman :heavy_check_mark:
- sep (saman electronic payment) Keshavarzi & Saderat :heavy_check_mark:
- sepehr (saderat) :heavy_check_mark:
- sepordeh :heavy_check_mark:
- shepa :heavy_check_mark:
- sizpay :heavy_check_mark:
- snapppay :heavy_check_mark:
- stripe :heavy_check_mark:
- toman :heavy_check_mark:
- torobpay (Installment payment) :heavy_check_mark:
- vandar :heavy_check_mark:
- xendit :heavy_check_mark:
- yekpay :heavy_check_mark:
- zarinpal :heavy_check_mark:
- zibal :heavy_check_mark:
- 其他正在进行中
您可以通过pull requests 帮助我创建更多的网关
- stripe
- authorize
- 2checkout
- braintree
- skrill
- payU
- amazon payments
- wepay
- payoneer
- paysimple
如果找不到你需要的,您可以创建你自己的驱动,阅读
创建自定义驱动部分,可以了解更多
安装
通过 Composer
composer require shetabit/payment
配置
服务提供者和 Payment 别名由 Laravel 的包自动发现机制注册,因此无需修改 config/app.php(或 bootstrap/providers.php)。
运行 php artisan vendor:publish 来发布 config/payment.php 文件到你的项目中
在配置文件中,您可以将 default设置项设置为你希望的付款方式。但也可以在运行时更改驱动。
选择要在应用程序中使用的网关。然后将其设为默认驱动程序,这样就不必在任何地方都指定它。但是,您也可以在一个项目中使用多个网关。
// Eg. if you want to use zarinpal.
'default' => 'zarinpal',
然后在驱动数组中填充该网关的凭据。
'drivers' => [
'zarinpal' => [
// Fill in the credentials here.
'apiPurchaseUrl' => 'https://www.zarinpal.com/pg/rest/WebGate/PaymentRequest.json',
'apiPaymentUrl' => 'https://www.zarinpal.com/pg/StartPay/',
'apiVerificationUrl' => 'https://www.zarinpal.com/pg/rest/WebGate/PaymentVerification.json',
'merchantId' => '',
'callbackUrl' => 'http://yoursite.com/path/to',
'description' => 'payment in '.config('app.name'),
],
...
]
如何使用
您的 Invoice 包含您的付款详细信息,因此我们首先将讨论 Invoice 类。
使用费用清单进行工作
在做任何事情之前,您需要使用 Invoice 类来创建费用清单。
像下面这样,在你的代码中使用费用清单:
// At the top of the file.
use Shetabit\Multipay\Invoice;
...
// Create new invoice.
$invoice = new Invoice;
// 设置清单金额.
$invoice->amount(1000);
// 给清单添加详情: 这里展示了四种语法.
// 1
$invoice->detail(['detailName' => 'your detail goes here']);
// 2
$invoice->detail('detailName','your detail goes here');
// 3
$invoice->detail(['name1' => 'detail1','name2' => 'detail2']);
// 4
$invoice->detail('detailName1','your detail1 goes here')
->detail('detailName2','your detail2 goes here');
可用方法:
uuid: 设置一个清单的唯一idgetUuid: 获取清单的当前唯一iddetail: 给清单添加自定义信息getDetails: 获取所有的详细信息amount: 设置一个清单金额getAmount: 获取清单金额transactionId: 设置支付交易单号getTransactionId: 获取支付交易单号via: 设置我们用来支付清单的驱动getDriver: 获取驱动
获取支付清单
为了支付清单,我们需要付款交易ID:
// At the top of the file.
use Shetabit\Multipay\Invoice;
use Shetabit\Payment\Facade\Payment;
...
// Create new invoice.
$invoice = (new Invoice)->amount(1000);
// Purchase the given invoice.
Payment::purchase($invoice,function($driver, $transactionId) {
// We can store $transactionId in database.
});
// Purchase method accepts a callback function.
Payment::purchase($invoice, function($driver, $transactionId) {
// We can store $transactionId in database.
});
// You can specify callbackUrl
Payment::callbackUrl('http://yoursite.com/verify')->purchase(
$invoice,
function($driver, $transactionId) {
// We can store $transactionId in database.
}
);
支付
在获取支付单后,我们可以跳转到第三方支付机构的页面
// At the top of the file.
use Shetabit\Multipay\Invoice;
use Shetabit\Payment\Facade\Payment;
...
// Create new invoice.
$invoice = (new Invoice)->amount(1000);
// Purchase and pay the given invoice.
// You should use return statement to redirect user to the bank page.
return Payment::purchase($invoice, function($driver, $transactionId) {
// Store transactionId in database as we need it to verify payment in the future.
})->pay()->render();
// Do all things together in a single line.
return Payment::purchase(
(new Invoice)->amount(1000),
function($driver, $transactionId) {
// 把交易ID保存到数据库.
// 在接下来的付款中,我们需要验证交易ID
}
)->pay()->render();
// Retrieve json format of Redirection (in this case you can handle redirection to bank gateway)
return Payment::purchase(
(new Invoice)->amount(1000),
function($driver, $transactionId) {
// 把交易ID保存到数据库.
// 在接下来的付款中,我们需要验证交易ID
}
)->pay()->toJson();
验证付款
当用户完成付款后,支付机构会将其重定向到您的网站,然后您需要验证您的付款,以确保清单已支付。
// At the top of the file.
use Shetabit\Payment\Facade\Payment;
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
...
// 您需要验证支付机构的回传数据,以确保付款成功
// 我们需要使用交易ID来验证
// 使用交易金额来验证,也是一个很好的方法
try {
$receipt = Payment::amount(1000)->transactionId($transaction_id)->verify();
// You can show payment referenceId to the user.
echo $receipt->getReferenceId();
...
} catch (InvalidPaymentException $exception) {
/**
如果未验证付款,则会引发异常。
我们可以抓住异常处理无效付款。
getMessage方法,返回可在用户界面中使用的适当消息。
**/
echo $exception->getMessage();
}
有用的方法
callbackUrl: 使用它可以在运行时改变回调地址.// At the top of the file. use Shetabit\Multipay\Invoice; use Shetabit\Payment\Facade\Payment; ... // Create new invoice. $invoice = (new Invoice)->amount(1000); // Purchase the given invoice. Payment::callbackUrl($url)->purchase( $invoice, function($driver, $transactionId) { // We can store $transactionId in database. } );amount: 你可以设置一个清单的金额// At the top of the file. use Shetabit\Multipay\Invoice; use Shetabit\Payment\Facade\Payment; ... // Purchase (we set invoice to null). Payment::callbackUrl($url)->amount(1000)->purchase( null, function($driver, $transactionId) { // We can store $transactionId in database. } );via: 在运行中更改支付方式// At the top of the file. use Shetabit\Multipay\Invoice; use Shetabit\Payment\Facade\Payment; ... // Create new invoice. $invoice = (new Invoice)->amount(1000); // Purchase the given invoice. Payment::via('driverName')->purchase( $invoice, function($driver, $transactionId) { // We can store $transactionId in database. } );config: 在运行中更改驱动的配置信息// At the top of the file. use Shetabit\Multipay\Invoice; use Shetabit\Payment\Facade\Payment; ... // Create new invoice. $invoice = (new Invoice)->amount(1000); // Purchase the given invoice with custom driver configs. Payment::config('mechandId', 'your mechand id')->purchase( $invoice, function($driver, $transactionId) { // We can store $transactionId in database. } ); // Also we can change multiple configs at the same time. Payment::config(['key1' => 'value1', 'key2' => 'value2'])->purchase( $invoice, function($driver, $transactionId) { // We can store $transactionId in database. } );
创建自定义驱动:
首先必须在drivers数组中添加驱动程序的名称,还可以指定所需的任何配置参数。
'drivers' => [
'zarinpal' => [...],
'my_driver' => [
... // Your Config Params here.
]
]
现在您必须创建一个将用于支付清单的驱动程序映射类。
在你的驱动中,你必须继承 Shetabit\Payment\Abstracts\Driver.这个类
例如,你创建了这样一个类: App\Packages\PaymentDriver\MyDriver。
namespace App\Packages\PaymentDriver;
use Shetabit\Multipay\Abstracts\Driver;
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
use Shetabit\Multipay\{Contracts\ReceiptInterface, Invoice, Receipt, RedirectionForm};
class MyDriver extends Driver
{
// The invoice and the settings are declared by the Driver class already,
// do not redeclare them here.
public function __construct(Invoice $invoice, array|object $settings)
{
$this->invoice($invoice); // Set the invoice.
$this->settings = (object) $settings; // Set settings.
}
// Purchase the invoice, save its transactionId and finaly return it.
public function purchase() : string|int|null {
// Request for a payment transaction id.
...
$this->invoice->transactionId($transId);
return $transId;
}
// Redirect into bank using transactionId, to complete the payment.
public function pay() : RedirectionForm {
// It is better to set bankApiUrl in config/payment.php and retrieve it here:
$bankUrl = $this->settings->bankApiUrl; // bankApiUrl is the config name.
// Prepare payment url.
$payUrl = $bankUrl.$this->invoice->getTransactionId();
// Redirect to the bank. The form is rendered with blade, so it is sent
// with a CSRF field.
return $this->redirectWithForm($payUrl, [], 'GET');
}
// Verify the payment (we must verify to ensure that user has paid the invoice).
public function verify(): ReceiptInterface {
$verifyPayment = $this->settings->verifyApiUrl;
$verifyUrl = $verifyPayment.$this->invoice->getTransactionId();
...
/**
Then we send a request to $verifyUrl and if payment is not valid we throw an InvalidPaymentException with a suitable message.
**/
throw new InvalidPaymentException('a suitable message');
/**
We create a receipt for this payment if everything goes normally.
**/
return new Receipt('driverName', 'payment_receipt_number');
}
}
创建该类后,必须在 payment.php 配置文件的 map 部分中指定它。
'map' => [
...
'my_driver' => App\Packages\PaymentDriver\MyDriver::class,
]
Note:- 必须确保 map 数组的键与 drivers 数组的键相同。
事件
你可以监听两个事件
- InvoicePurchasedEvent: 在获取清单后执行
- InvoiceVerifiedEvent: 在验证交易成功后执行
两个事件都以 readonly 属性的形式提供对应的驱动和费用清单,验证事件还会提供支付回执:
public function handle(InvoiceVerifiedEvent $event): void
{
$event->receipt->getReferenceId();
$event->invoice->getTransactionId();
$event->driver->getInvoice();
}
测试
每一个 pull request 以及每一次推送到 master 分支都会由 GitHub Actions 进行检查:测试会在 PHP 8.4 和 8.5 上、
针对 Laravel 12 和 13、并且分别针对最低和最高版本的依赖运行,代码风格由 PHP_CodeSniffer 检查,源码由 PHPStan 进行静态分析,
测试覆盖率也会被统计并且必须保持在 95% 以上。
如果你的机器上已经安装了 PHP 和 Composer,可以在本地运行同样的检查:
composer install
composer test # 运行测试
composer test-coverage # 运行测试并统计覆盖率
composer check-style # 检查代码风格
composer fix-style # 尽可能自动修复代码风格
composer analyse # 运行静态分析
composer ci # 运行以上所有检查
如果你不想在机器上安装 PHP,包中自带的 Dockerfile 和 Makefile 会在容器中运行这一切:
make test # 运行测试
make coverage # 运行测试并统计覆盖率
make check-style # 检查代码风格
make fix-style # 尽可能自动修复代码风格
make analyse # 运行静态分析
make ci # 运行以上所有检查
make shell # 在容器中打开一个 shell
make help # 列出所有可用的命令
使用 make test PHP_VERSION=8.5 可以切换 PHP 版本,使用 make test-laravel LARAVEL=12 可以只测试某一个 Laravel 版本。
Change log
请查看 CHANGELOG 来获取更多关于版本更新的信息
贡献
请查看 CONTRIBUTING 和 CONDUCT 获取更多详细信息
安全
如果您发现任何与安全相关的问题,请发送电子邮件至[email protected],而不要使用issue。
信誉
License
The MIT License (MIT). Please see License File for more information.
探索相关项目
加载中…