PHP code example of saeedpooyanfar / zarinpal

1. Go to this page and download the library: Download saeedpooyanfar/zarinpal library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

saeedpooyanfar / zarinpal example snippets




...
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'  => '[email protected]' // 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);
...



...
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
    }
}
...



...
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!
...
shell
composer dump-autoload
bash
# clone repo
# cd zarinpal-laravel
# composer install
cd test
php Request.php