omalizadeh/laravel-multi-payment

基于驱动程序的laravel支付包,支持通过多个网关进行在线支付

v3.1.0 2023-10-23 07:25 UTC

README

Latest Stable Version Tests Total Downloads License

Laravel在线支付网关包

这是一个支持多驱动程序的laravel网关支付包。每个驱动程序都可以有多个配置。支持laravel v8.0+,需要php v8.1+

喜欢这个包,请给它加星标!

波斯语文档

支持的网关

安装与配置

使用composer安装

  composer require omalizadeh/laravel-multi-payment

发布主配置文件

  php artisan vendor:publish --tag=multipayment-config

基于标签发布网关配置文件,例如

  • zarinpal-config
  • mellat-config
  • saman-config
  • pasargad-config
  • novin-config

例如

  php artisan vendor:publish --tag=zarinpal-config

您还可以发布网关重定向的视图文件并进行自定义

  php artisan vendor:publish --tag=multipayment-view

在主配置文件multipayment.php中,您可以指定默认驱动程序。例如,zarinpal.second的值表示将使用配置在zarinpal配置文件中second键部分的zarinpal网关。还有一个选项,可以从伊朗托曼货币(IRR)自动转换到伊朗里亚尔(IRR)以及反向转换。

     /**
     * set default gateway
     * 
     * valid pattern --> GATEWAY_NAME.GATEWAY_CONFIG_KEY 
     */
    'default_gateway' => env('DEFAULT_PAYMENT_GATEWAY', 'zarinpal.second'),

    /**
     *  set to false if your in-app currency is IRR
     */
    'convert_to_rials' => true

在每个网关配置文件中,您可以指定多个凭据,因此您可以为您的应用程序从同一提供者拥有多个网关。

     /**
     *  gateway configurations
     */
    'first' => [
        'merchant_id'  => '',
        'callback_url' => 'https://yoursite.com/path/to',
        'mode'        => 'normal', // Supported values: normal, sandbox, zaringate
        'description' => 'payment using zarinpal',
    ],
    'second' => [
        'merchant_id'  => '',
        'callback_url' => 'https://yoursite.com/path/to',
        'mode'        => 'sandbox',
        'description' => 'payment using zarinpal',
    ]

用法

网关支付有两个主要阶段。第一个是购买(通过调用网关API的transaction_id/token开始处理)和打开带有接收数据的网关支付网页。第二个是验证(检查支付是否成功)。

购买

Inovice对象包含支付数据。首先,您创建一个发票,设置金额和其他信息,然后通过将发票传递给PaymentGateway外观启动支付流程。您可以使用外观上的setProvider方法在支付前更改网关。

    // On top...
    use Omalizadeh\MultiPayment\Facades\PaymentGateway;

    ////

    $invoice = new Invoice(10000);
    $invoice->setPhoneNumber("989123456789");
    
    return PaymentGateway::purchase($invoice, function (string $transactionId) {
        // Save transaction_id and do stuff...
    })->view();

验证

在支付网关将您的应用程序重定向后,您必须创建一个发票并设置其transaction_id和金额。然后使用PaymentGateway验证发票的支付是否成功。

    try {
        // Get amount & transaction_id from database or gateway request
        $invoice = new Invoice($amount, $transactionId);
        $receipt = PaymentGateway::verify($invoice);
        // Save receipt data and return response
        //
    } catch (PaymentAlreadyVerifiedException $exception) {
        // Optional: Handle repeated verification request
    } catch (PaymentFailedException $exception) {
        // Handle exception for failed payments
        return $exception->getMessage();
    }

其他功能

未经验证的支付

还有一个方法(目前仅由zarinpal支持)可以获取成功的未经验证支付列表。使用PaymentGateway外观上的unverifiedPayments方法来使用此功能。

退款

使用refund方法,您可以退款成功的支付给客户。