install it:
composer require saeedpooyanfar/zarinpal
laravel service provider should register automatically, if not, register Zarinpal\ZarinpalServiceProvider::class
manually or run:
composer dump-autoload
set 36 chars "ZARINPAL_MERCHANTID" in .env
file:
...
ZARINPAL_MERCHANTID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
...
request new payment:
<?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
}
}
...
If you have other redirection methods you can use:
...
$url = $zarinpal->getRedirectUrl($authority);
...
to get the redirect url as a string.
verify the payment:
<?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!
...
# clone repo
# cd zarinpal-laravel
# composer install
cd test
php Request.php