llabbasmkhll/laravel-zibal

为zibal的交易请求库

v1.0.0 2021-09-02 02:27 UTC

This package is auto-updated.

Last update: 2024-09-24 21:47:42 UTC


README


Logo Logo

laravel zibal

zibal的交易请求包zibal

入门指南

要本地运行副本,请按照以下简单步骤操作。

安装

  1. 您可以通过composer安装此包
    composer require llabbasmkhll/laravel-zibal 

请注意,如果您想更改商户ID,才需要执行以下步骤。如果您只想测试webservice,则无需执行这些步骤

  1. 将配置文件发布到您的项目中
    php artisan vendor:publish
  2. 在config/zibal.php中将商户值更改为您的商户ID(使用zibal进行测试)
    return [
          'merchant' => 'zibal',
    ];


用法

首先通过以下方式将包外观包含到您的文件中:

use Llabbasmkhll\LaravelZibal\Facades\Zibal;

根据zibals 官方文档,在zibal中发起交易有3个步骤

1. 请求

在此步骤中,zibal获取交易的基本信息并返回需要的trackId,用于下一步

使用此信息初始化交易请求

Zibal::init(
    1000000,            //required amount          - in rial
    'redirect',         //required callback        - can be either route name or a valid url starting with http or https
    ['key' => 'value'], //optional callback_params - will be passed to callback as query params , works only when route name passed to callback
    'description',      //optional description     - additional data , good for various reports
    123,                //optional orderId         - id of clients order (eg $invoice->id) , will be passed back to callback
    '09366217515',      //optional mobile          - clients mobile number
    ['000000000000']    //optional allowedCards    - array of allowed card numbers
)->getResponse();

这将返回一个包含resultmessagetrackId的数组

result表示请求状态如下。

您可以在初始化后添加validate()函数如下

Zibal::init( $amount, 'redirect')->validate(404)->getResponse();

这将如果结果代码不是100则将用户重定向到404页面


2. 开始

将用户重定向到zibal的支付网关进行支付

Zibal::redirect($trackId);

您可以像这样将第一步和第二步合并为一行代码

Zibal::init( $amount, 'redirect')->validate()->redirect();

这将初始化交易,如果初始化成功,则将用户重定向到zibal的支付页面,否则将用户重定向到422页面


3. 验证

您可以使用此行代码验证交易状态

Zibal::verify($trackId)->validate()->getResponse();

这将返回一个包含以下参数的数组


result表示请求状态如下。


status表示请求状态如下。



示例控制器

<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Invoice;
use Illuminate\Http\Request;
use Llabbasmkhll\LaravelZibal\Facades\Zibal;

class GatewayController extends Controller
{

    /**
     * redirect user to zibal.
     *
     * @param  \Illuminate\Http\Request  $request
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function show(Request $request)
    {
        $invoice = Invoice::find($request->input('invoice'));
        $user    = $invoice->user()->first();


        return Zibal::init(
            $invoice->amount,
            'redirect',
            [],
            $user->id,
            $invoice->id,
            $user->mobile,
        )->validate()->redirect();
    }


    /**
     * validate the transaction.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function index(Request $request)
    {
        abort_unless($request->has('trackId'), 422);

        $response = Zibal::verify($request->input('trackId'))->getResponse();

        if ($response['result'] == 100) {
            $invoice = Invoice::find($response['orderId']);
            $invoice->update(
                [
                    'status'       => 1,
                    'bank_code'    => $response['refNumber'],
                    'finalized_at' => now(),
                ]
            );
        }

        return redirect('panel');
    }

}


贡献

贡献使开源社区成为一个如此美妙的学习、灵感和创造的地方。您所做的任何贡献都将被非常欣赏。



许可证

在MIT许可证下分发。有关更多信息,请参阅LICENSE



联系方式

Abbas mkhzomi - Telegram@llabbasmkhll - llabbasmkhll@gmail.com

项目链接: https://github.com/llabbasmkhll/laravel-zibal