blognevis/zarinpal

为 Laravel 框架提供的 Zarinpal 支付(包含沙箱支持)

0.0.1 2024-08-19 04:35 UTC

This package is auto-updated.

Last update: 2024-09-19 05:13:29 UTC


README

安装它

composer require saeedpooyanfar/zarinpal

laravel 服务提供者应自动注册,如果没有,则手动注册 Zarinpal\ZarinpalServiceProvider::class 或运行

composer dump-autoload

.env 文件中设置 36 个字符的 "ZARINPAL_MERCHANTID"

...
ZARINPAL_MERCHANTID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
...

使用它

请求新的支付

<?php

...
use GuzzleHttp\Exception\RequestException;
use Zarinpal\Zarinpal;
...

...
function request(Zarinpal $zarinpal) {
    $payment = [
        'callback_url' => route('payment.verify'), // Required
        'amount'       => 5000,                    // Required
        'description'  => 'a short description',   // Required
        'metadata'     => [
            'mobile' => '0933xxx7694',       // Optional
            'email'  => 'saeedp47@gmail.com' // Optional
        ]
    ];
    try {
      $response = $zarinpal->request($payment);
      $code = $response['data']['code'];
      $message = $zarinpal->getCodeMessage($code);
      if($code === 100) {
          $authority = $response['data']['authority'];
          return $zarinpal->redirect($authority);
      }
      return 'Error,
      Code: ' . $code . ',
      Message: ' . $message;
    } catch (RequestException $exception) {
        // handle exception
    }
}
...

如果您有其他重定向方法可以使用

...
$url = $zarinpal->getRedirectUrl($authority);
...

以获取作为字符串的重定向 URL。

验证支付

<?php

...
use GuzzleHttp\Exception\RequestException;
use Illuminate\Http\Request;
use Zarinpal\Zarinpal;
...

...
function verify(Request $request, Zarinpal $zarinpal) {
    $payment = [
        'authority' => $request->input('Authority'), // $_GET['Authority']
        'amount'    => 5000
    ];
    if ($request->input('Status') !== 'OK') abort(406);
    try {
      $response = $zarinpal->verify($payment);
      $code = $response['data']['code'];
      $message = $zarinpal->getCodeMessage($code);
      if($code === 100) {
          $refId = $response['data']['ref_id'];
          return 'Payment was successful,
          RefID: ' . $refId . ',
          Message: ' . $message;
      }
      return 'Error,
      Code: ' . $code . ',
      Message: ' . $message;
    } catch (RequestException $exception) {
        // handle exception
    }
}
...

使用此库与其他框架一起使用

<?php

...
use Zarinpal\Zarinpal;
use Zarinpal\Clients\GuzzleClient; // OR SoapClient
...

...
$merchantID = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX';
$sandbox = false;
$zarinGate = false; // OR true
$zarinGatePSP = 'Asan'; // Leave this parameter blank if you don't need a custom PSP zaringate.
$client = new GuzzleClient($sandbox);
$lang = 'fa'; // OR en
$zarinpal = new Zarinpal($merchantID, $client, $lang, $sandbox, $zarinGate, $zarinGatePSP);
// object is ready, call methods now!
...

可用配置

  • ZARINPAL_LANG
    • 消息语言
    • 可能值:[fa, en]
  • ZARINPAL_ZARINGATE
    • 使用 zarringate 作为重定向 URL
    • 可能值:[0, 1]
  • ZARINPAL_ZARINGATE_PSP
    • 为 zarringate 使用自定义 PSP
    • 可能值:'Asan', 'Sep', 'Sad', 'Pec', 'Fan', 'Emz'

运行测试

# clone repo
# cd zarinpal-laravel
# composer install
cd test
php Request.php

官方文档

链接

zarinpal