hrd/zarinpal

zarinpal 交易请求系统

1.3.2 2023-08-17 00:21 UTC

This package is auto-updated.

Last update: 2024-09-17 02:37:26 UTC


README

Build Status StyleCI Coverage Status

zarinpal 交易请求库

用法

安装

composer require zarinpal/zarinpal

请求

use Zarinpal\Zarinpal;

$zarinpal = new Zarinpal('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX');
$zarinpal->enableSandbox(); // active sandbox mod for test env
// $zarinpal->isZarinGate(); // active zarinGate mode
$results = $zarinpal->request(
    "example.com/testVerify.php",          //required
    1000,                                  //required
    'testing',                             //required
    'me@example.com',                      //optional
    '09000000000',                         //optional
    [                          //optional
        "Wages" => [
            "zp.1.1"'=> [
                "Amount"'=> 120,
                "Description"'=> "part 1"
            ],
            "zp.2.5"'=> [
                "Amount"'=> 60,
                "Description"'=> "part 2"
            ]
        ]
    ]
);
echo json_encode($results);
if (isset($results['Authority'])) {
    file_put_contents('Authority', $results['Authority']);
    $zarinpal->redirect();
}
//it will redirect to zarinpal to do the transaction or fail and just echo the errors.
//$results['Authority'] must save somewhere to do the verification

验证

use Zarinpal\Zarinpal;

$zarinpal = new Zarinpal('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX');
$authority = file_get_contents('Authority');
echo json_encode($zarinpal->verify('OK', 1000, $authority));
//'Status'(index) going to be 'success', 'error' or 'canceled'

laravel 兼容

此包将适用于各种项目,但为了与laravel更好地集成,我添加了provider。只需将以下内容添加到"config/app.php"的provider列表中(如果你使用laravel 5.5或更高版本,则跳过此步骤):

'providers' => [
    ...
    Zarinpal\Laravel\ZarinpalServiceProvider::class
    ...
]

然后将其添加到config/services.php

'zarinpal' => [
    'merchantID' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
    'zarinGate' => false,
    'sandbox' => false,
],

现在你可以这样访问zarinpal库了

use Zarinpal\Laravel\Facade\Zarinpal;

$results = Zarinpal::request(
    "example.com/testVerify.php",          //required
    1000,                                  //required
    'testing',                             //required
    'me@example.com',                      //optional
    '09000000000',                         //optional
    [                          //optional
        "Wages" => [
            "zp.1.1" => [
                "Amount" => 120,
                "Description" => "part 1"
            ],
            "zp.2.5" => [
                "Amount" => 60,
                "Description" => "part 2"
            ]
        ]
    ]
);
// save $results['Authority'] for verifying step
Zarinpal::redirect(); // redirect user to zarinpal

// after that verify transaction by that $results['Authority']
Zarinpal::verify('OK',1000,$results['Authority']);