承接 shetabit/multipay 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

shetabit/multipay

最新稳定版本:v2.8.0

Composer 安装命令:

composer require shetabit/multipay

包简介

PHP Payment Gateway Integration Package

README 文档

README

PHP Payment Gateway

Software License Latest Version on Packagist Total Downloads on Packagist StyleCI Maintainability Quality Score

This is a PHP Package for Payment Gateway Integration. This package supports PHP 7.2+.

Donate me if you like this package ???? :bowtie:

For Laravel integration you can use shetabit/payment package.

This package works with multiple drivers, and you can create custom drivers if you can't find them in the current drivers list (below list).

List of contents

List of available drivers

Help me to add the gateways below by creating pull requests

  • authorize
  • 2checkout
  • braintree
  • skrill
  • payU
  • amazon payments
  • wepay
  • payoneer
  • paysimple

you can create your own custom drivers if it doesn't exist in the list, read the Create custom drivers section.

Install

Via Composer

composer require shetabit/multipay

Configure

a. Copy config/payment.php into somewhere in your project. (you can also find it in vendor/shetabit/multipay/config/payment.php path).

b. In the config file you can set the default driver to be used for all your payments and you can also change the driver at runtime.

Choose what gateway you would like to use in your application. Then make that as default driver so that you don't have to specify that everywhere. But, you can also use multiple gateways in a project.

// Eg. if you want to use zarinpal.
'default' => 'zarinpal',

Then fill the credentials for that gateway in the drivers array.

'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'),
    ],
    ...
]

c. Instantiate the Payment class and pass configs to it like the below:

    use Shetabit\Multipay\Payment;

    // load the config file from your project
    $paymentConfig = require('path/to/payment.php');

    $payment = new Payment($paymentConfig);

How to use

your Invoice holds your payment details, so initially we'll talk about Invoice class.

Working with invoices

before doing any thing you need to use Invoice class to create an invoice.

In your code, use it like the below:

// At the top of the file.
use Shetabit\Multipay\Invoice;
...

// Create new invoice.
$invoice = new Invoice;

// Set invoice amount.
$invoice->amount(1000);

// Add invoice details: There are 4 syntax available for this.
// 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');

Available methods:

  • uuid: set the invoice unique id
  • getUuid: retrieve the invoice current unique id
  • detail: attach some custom details into invoice
  • getDetails: retrieve all custom details
  • amount: set the invoice amount
  • getAmount: retrieve invoice amount
  • transactionId: set invoice payment transaction id
  • getTransactionId: retrieve payment transaction id
  • via: set a driver we use to pay the invoice
  • getDriver: retrieve the driver

Purchase invoice

In order to pay the invoice, we need the payment transactionId. We purchase the invoice to retrieve transaction id:

// At the top of the file.
use Shetabit\Multipay\Invoice;
use Shetabit\Multipay\Payment;
...

// load the config file from your project
$paymentConfig = require('path/to/payment.php');

$payment = new Payment($paymentConfig);


// 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.
	}
);

Pay invoice

After purchasing the invoice, we can redirect the user to the bank payment page:

// At the top of the file.
use Shetabit\Multipay\Invoice;
use Shetabit\Multipay\Payment;
...

// load the config file from your project
$paymentConfig = require('path/to/payment.php');

$payment = new Payment($paymentConfig);


// 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) {
    	// Store transactionId in database.
        // We need the transactionId to verify payment in the future.
	}
)->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) {
    	// Store transactionId in database.
        // We need the transactionId to verify payment in the future.
	}
)->pay()->toJson();

Verify payment

When user has completed the payment, the bank redirects them to your website, then you need to verify your payment in order to ensure the invoice has been paid.

// At the top of the file.
use Shetabit\Multipay\Payment;
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
...

// load the config file from your project
$paymentConfig = require('path/to/payment.php');

$payment = new Payment($paymentConfig);


// You need to verify the payment to ensure the invoice has been paid successfully.
// We use transaction id to verify payments
// It is a good practice to add invoice amount as well.
try {
	$receipt = $payment->amount(1000)->transactionId($transaction_id)->verify();

    // You can show payment referenceId to the user.
    echo $receipt->getReferenceId();

    // And also you can access verify receipt detail
    echo $receipt->getDetail('give-a-name')
    // Or if you want all details
    $receiptDetails = $receipt->getDetails()
    ...
} catch (InvalidPaymentException $exception) {
    /**
    	when payment is not verified, it will throw an exception.
    	We can catch the exception to handle invalid payments.
    	getMessage method, returns a suitable message that can be used in user interface.
    **/
    echo $exception->getMessage();
}

Useful methods

  • callbackUrl: can be used to change callbackUrl on the runtime.
    // At the top of the file.
    use Shetabit\Multipay\Invoice;
    use Shetabit\Multipay\Payment;
    ...
    
    // load the config file from your project
    $paymentConfig = require('path/to/payment.php');
    
    $payment = new Payment($paymentConfig);
    
    
    // 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: you can set the invoice amount directly
    // At the top of the file.
    use Shetabit\Multipay\Invoice;
    use Shetabit\Multipay\Payment;
    ...
    
    // load the config file from your project
    $paymentConfig = require('path/to/payment.php');
    
    $payment = new Payment($paymentConfig);
    
    
    // Purchase (we set invoice to null).
    $payment->callbackUrl($url)->amount(1000)->purchase(
        null,
        function($driver, $transactionId) {
        // We can store $transactionId in database.
    	}
    );
  • via: change driver on the fly
    // At the top of the file.
    use Shetabit\Multipay\Invoice;
    use Shetabit\Multipay\Payment;
    ...
    
    // load the config file from your project
    $paymentConfig = require('path/to/payment.php');
    
    $payment = new Payment($paymentConfig);
    
    
    // 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: set driver configs on the fly
    // At the top of the file.
    use Shetabit\Multipay\Invoice;
    use Shetabit\Multipay\Payment;
    ...
    
    // load the config file from your project
    $paymentConfig = require('path/to/payment.php');
    
    $payment = new Payment($paymentConfig);
    
    
    // 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.
    	}
    );

// We can also change multiple config values at the same time. // To use wages in Zarinpal, you can configure it as shown in the example below. $payment->config(['wages' => [use Zarinpal documentation for values], 'key1' => 'value1', 'key2' => 'value2'])->purchase( $invoice, function ($driver, $transactionId) { // We can store $transactionId in the database. } );

- `custom fileds`: Use custom fields of gateway (Not all gateways support this feature)
SEP gateway support up to 4 custom fields and you can set the value to a string up to 50 characters.
These custom fields are shown only when viewing reports in the user's panel.

```php
// At the top of the file.
use Shetabit\Multipay\Invoice;
...


// Create new invoice.
$invoice = (new Invoice)->amount(1000);

// Use invoice bag to store custom field values.
$invoice->detail([
            'ResNum1' => $order->orderId,
            'ResNum2' => $customer->verifiedCode,
            'ResNum3' => $someValue,
            'ResNum4' => $someOtherValue,
            ]);

Create custom drivers:

First you have to add the name of your driver, in the drivers array and also you can specify any config parameters you want.

'drivers' => [
    'zarinpal' => [...],
    'my_driver' => [
        ... // Your Config Params here.
    ]
]

Now you have to create a Driver Map Class that will be used to pay invoices. In your driver, You just have to extend Shetabit\Multipay\Abstracts\Driver.

Eg. You created a class: App\Packages\Multipay\Driver\MyDriver.

namespace App\Packages\Multipay\Driver;

use Shetabit\Multipay\Abstracts\Driver;
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
use Shetabit\Multipay\{Contracts\ReceiptInterface, Invoice, RedirectionForm, Receipt};

class MyDriver extends Driver
{
    protected $invoice; // Invoice.

    protected $settings; // Driver settings.

    public function __construct(Invoice $invoice, $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() {
        // 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.
        $url = $payUrl;
        $inputs = [];
        $method = 'GET';

        return $this->redirectWithForm($url, $inputs, $method);
    }
  
    // 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');
    }
}

Once you create that class you have to specify it in the payment.php config file map section.

'map' => [
    ...
    'my_driver' => App\Packages\Multipay\Driver\MyDriver::class,
]

Note: You have to make sure that the key of the map array is identical to the key of the drivers array.

Events:

Notice 1: event listeners will be registered globaly for all payments.

Notice 2: if you want your listeners work correctly, you must subcribe them before the target event dispatches.

Its better to subcribe events in your app's entry point or main service provider, so events will be subcribed before any events dispatches.

You can listen for 3 events:

  1. purchase
  2. pay
  3. verify.
  • purchase: Occurs when an invoice is purchased (after purchasing invoice is done successfully).
// add purchase event listener
Payment::addPurchaseListener(function($driver, $invoice) {
    echo $driver;
    echo $invoice;
});
  • pay: Occurs when an invoice is prepared to pay.
// add pay event listener
Payment::addPayListener(function($driver, $invoice) {
    echo 'first listener';
});

// we can add multiple listeners
Payment::addPayListener(function($driver, $invoice) {
    echo 'second listener';
});
  • verify: Occurs when an invoice is verified successfully.
// we can add multiple listeners and also remove them!!!

$firstListener = function($driver, $invoice) {
    echo 'first listener';
};

$secondListener = function($driver, $invoice) {
    echo 'second listener';
};

Payment::addVerifyListener($firstListener);
Payment::addVerifyListener($secondListener);

// remove first listener
Payment::removeVerifyListener($firstListener);

// if we call remove listener without any arguments, it will remove all listeners
Payment::removeVerifyListener(); // remove all verify listeners :D

Local driver

Local driver can simulate payment flow of a real gateway for development purpose.

Payment can be initiated like any other driver

$invoice = (new Invoice)->amount(10000);
$payment->via('local')->purchase($invoice, function($driver, $transactionId) {
    // a fake transaction ID is generated and returned.
})->pay()->render();

Calling render() method will render a HTML form with Accept and Cancel buttons, which simulate corresponding action of real payment gateway. and redirects to the specified callback url. transactionId parameter will allways be available in the returned query url.

Payment can be verified after receiving the callback request.

$receipt = $payment->via('local')->verify();

In case of succesful payment, $receipt will contains the following parameters

[
'orderId' => // fake order number 
'traceNo' => // fake trace number (this should be stored in databse)
'referenceNo' => // generated transaction ID in `purchase` method callback
'cardNo' => // fake last four digits of card 
]

In case of canceled payment, PurchaseFailedException will be thrown to simulate the failed verification of gateway.

Driver functionalities can be configured via Invoice detail bag.

  • available parameters
$invoice->detail([
    // setting this value will cause `purchase` method to throw an `PurchaseFailedException` 
    // to simulate when a gateway can not initialize the payment.
        'failedPurchase' => 'custom message to decribe the error',

    // Setting this parameter will be shown in payment form.
        'orderId' => 4444,
]);
  • appearance

Appearance of payment form can be customized via config parameter of local driver in payment.php file.

'local' => [
    // default callback url of the driver
    'callbackUrl' => '/callback',

    // main title of the form
    'title' => 'Test gateway',
  
    // a description to show under the title for more clarification
    'description' => 'This gateway is for using in development environments only.',
  
    // custom label to show as order No.
    'orderLabel' => 'Order No.',
  
    // custom label to show as payable amount
    'amountLabel' => 'Payable amount',
  
    // custom label of successful payment button
    'payButton' => 'Successful Payment',
  
    // custom label of cancel payment button
    'cancelButton' => 'Cancel Payment',
],

Change log

Please see CHANGELOG for more information on what has been changed recently.

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email khanzadimahdi@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

shetabit/multipay 适用场景与选型建议

shetabit/multipay 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 335.38k 次下载、GitHub Stars 达 290, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「payment」 「pay」 「gateway」 「paypal」 「Bank」 「payment gateway」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 shetabit/multipay 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 shetabit/multipay 我们能提供哪些服务?
定制开发 / 二次开发

基于 shetabit/multipay 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 335.38k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 290
  • 点击次数: 35
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 290
  • Watchers: 7
  • Forks: 147
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04