blackplatinum/zarinpal

Laravel 的 Zarinpal 网关实现。

v1.0.1 2020-05-01 11:36 UTC

This package is auto-updated.

Last update: 2024-09-29 05:36:14 UTC


README

安装

使用 [Composer] 安装包

$ composer require blackplatinum/zarinpal

如何使用

在你的配置文件夹中打开 app.php 并添加以下行

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

'aliases' => [
    ...........
    'Zarinpal' => BlackPlatinum\Zarinpal\Zarinpal::class,
],

然后像这样在 .env 中添加你的商户 ID

MERCHANT_ID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

最后重启你的服务器。(你也可以忽略在 .env 中添加商户 ID,并在源代码中使用 setMerchantId() 来使用它)

如何运行

use BlackPlatinum\Zarinpal\Zarinpal;

/**
 * This is for sending your request to the bank for your payment
 */
$paymentRequest = new Zarinpal(
 'request',
    [
        'price' => 100, // Toman
        'description' => 'درگاه پرداخت زرین پال',
        'callbackUri' => 'your_uri', // Without '/'
        'orderId' => 1 // Your order id
    ], true // Enables sandbox mode
);
$result = $paymentRequest->sendPaymentInfoToGateway();
if ($result->Status == 100) {
    // The information that you have sent was out of mistakes and you are gonna
    // redirect to zarinpal gateway 
    return redirect()->to($paymentRequest->linkToGateway($result->Authority));
}

// There is something wrong about your request and you are not qualify to redirect
// to zarinpal gateway 
return redirect('your_failure_url_in_payment');

--------------------------------------------------------------------------------------------------

/**
 * This is for receiving your response from the bank about your payment request in previous code
 */
$paymentResponse = new Zarinpal(
'response',
    [
        'price' => 100, // Toman
        'authority' => $request->Authority
    ], true // Enables sandbox mode
);
$result = $paymentResponse->receivePaymentInfoFromGateway($request->Status);
if ($result) {
    // Yor payment done successfully
    $authority = $request->input('Authority');
    $status = $request->input('Status');
    $refId = $result->RefID;
    // Do the rest...
}
else {
    // Yor payment failed or you canceled payment
}

作者

许可证

此组件的所有内容均受 [MIT 许可证] 许可。