saeedpooyanfar / zarinpal
Zarinpal 为 Laravel 框架提供的支付解决方案(支持沙箱模式)
v5.0.1
2021-05-20 20:08 UTC
Requires
- php: ^7.4|^8.0
- ext-json: *
- guzzlehttp/guzzle: ^7.2.0
This package is auto-updated.
Last update: 2024-09-19 23:30:08 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