{
  "slug": "Running-Zarinpal-Gateway-on-Laravel-framework-ac9d84674ec6",
  "title": "Running Zarinpal Gateway on Laravel framework",
  "subtitle": "one of the most common things using in a web developer is a gateway for making money from customers.",
  "excerpt": "one of the most common things using in a web developer is a gateway for making money from customers.",
  "date": "2020-09-23",
  "tags": [
    "Laravel",
    "Payments"
  ],
  "readingTime": "6 min",
  "url": "https://medium.com/@mobinshaterian/running-zarinpal-gateway-on-laravel-framework-ac9d84674ec6",
  "hero": "https://cdn-images-1.medium.com/max/1200/1*KetcZbRwwWLucBlkUOMd6g.png",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Running Zarinpal Gateway version4 on Laravel framework version 5.5"
    },
    {
      "type": "paragraph",
      "html": "one of the most common things using in a web developer is a gateway for making money from customers."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/1200/1*KetcZbRwwWLucBlkUOMd6g.png",
      "alt": "Running Zarinpal Gateway on Laravel framework",
      "caption": "",
      "width": 1285,
      "height": 694
    },
    {
      "type": "paragraph",
      "html": "Zarinpal is a famous gateway in Persian guys but the biggest problem of this gateway is lake document when releasing new API."
    },
    {
      "type": "paragraph",
      "html": "for implement Laravel by using Zarinpal, I Found the <code>Shetabit</code> composer."
    },
    {
      "type": "code",
      "lang": "text",
      "code": "composer require shetabit/payment"
    },
    {
      "type": "paragraph",
      "html": "but this composer isn’t compatible with the new version of Zarinpal and my Laravel version. so I’m using a traditional way to connect Zarinpal into Laravel."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/1200/1*NuRKcngQdCgC_aCP3uBmBQ.png",
      "alt": "Running Zarinpal Gateway on Laravel framework",
      "caption": "",
      "width": 1835,
      "height": 757
    },
    {
      "type": "quote",
      "html": "“needing fast way connection the payment to the gateway”, My boss screams at me. therefore ignoring the composer and deploy traditionally."
    },
    {
      "type": "paragraph",
      "html": "I’m submit Shetabit githaub issues and they answer me at below link. but unfortunately I don’t have time to check them."
    },
    {
      "type": "heading",
      "level": 3,
      "text": "stay with me to build payment in Laravel v5.5 with Zarinpal Gateway API version4"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Database"
    },
    {
      "type": "paragraph",
      "html": "3 tables are needed in this project. package price, invoice, and status payment need to store in the database and using as different roles."
    },
    {
      "type": "paragraph",
      "html": "php artisan make:migration packageprices"
    },
    {
      "type": "paragraph",
      "html": "add the structure of the database in up part"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "Schema::create('packageprices', function (Blueprint $table) {"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->increments('id');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('name');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->integer('price')->unsigned();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->integer('oldprice')->unsigned();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('hour');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->timestamps();"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "});"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "Schema::create('transactions', function (Blueprint $table) {"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->increments('id');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('trackingCode');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->integer('price')->unsigned();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->integer('user_id')->unsigned();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->integer('packageprice_id')->unsigned();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->integer('statustransaction_id')->unsigned();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('bankName')->nullable();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('Authority')->nullable();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('zarinpalStatus')->nullable();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('zarinpalErrorMessage')->nullable();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('zarinpalError')->nullable();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('zarinpalRefID')->nullable();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('zarinpalStatusFinal')->nullable();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('zarinpalErrorMessageFinal')->nullable();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('zarinpalErrorFinal')->nullable();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('zarinpalFinalcardhash')->nullable();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('zarinpalFinalcardpan')->nullable();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('zarinpalFinalfeetype')->nullable();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('zarinpalfee')->nullable();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->timestamps();"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "});"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "Schema::create('statustransactionses', function (Blueprint $table) {"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->increments('id');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->string('name');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$table->timestamps();"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "});"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Migrate"
    },
    {
      "type": "paragraph",
      "html": "php artisan migrate"
    },
    {
      "type": "paragraph",
      "html": "and for back to previwe step"
    },
    {
      "type": "paragraph",
      "html": "php artisan migrate:rollback"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Model"
    },
    {
      "type": "paragraph",
      "html": "make 3 models base on commands below:"
    },
    {
      "type": "paragraph",
      "html": "php artisan make:model Packageprice — migration"
    },
    {
      "type": "paragraph",
      "html": "php artisan make:model Statustransactionse — migration"
    },
    {
      "type": "paragraph",
      "html": "php artisan make:model Transaction — migration"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Controller"
    },
    {
      "type": "paragraph",
      "html": "in this project on need only one controller"
    },
    {
      "type": "paragraph",
      "html": "php artisan make:controller PaymentController — resource"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Initial request with Zarinpal"
    },
    {
      "type": "paragraph",
      "html": "for making connections to Zarinpal need to send a request to get the Authority code in the controller."
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$error = array("
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-1\" => \"اطلاعات ارسال شده ناقص است.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-2\" => \"IP و يا مرچنت كد پذيرنده صحيح نيست\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-3\" => \"با توجه به محدوديت هاي شاپرك امكان پرداخت با رقم درخواست شده ميسر نمي باشد\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-4\" => \"سطح تاييد پذيرنده پايين تر از سطح نقره اي است.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-11\" => \"درخواست مورد نظر يافت نشد.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-12\" => \"امكان ويرايش درخواست ميسر نمي باشد.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-21\" => \"هيچ نوع عمليات مالي براي اين تراكنش يافت نشد\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-22\" => \"تراكنش نا موفق ميباشد\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-33\" => \"رقم تراكنش با رقم پرداخت شده مطابقت ندارد\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-34\" => \"سقف تقسيم تراكنش از لحاظ تعداد يا رقم عبور نموده است\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-40\" => \"اجازه دسترسي به متد مربوطه وجود ندارد.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-41\" => \"اطلاعات ارسال شده مربوط به AdditionalData غيرمعتبر ميباشد.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-42\" => \"مدت زمان معتبر طول عمر شناسه پرداخت بايد بين 30 دقيه تا 45 روز مي باشد.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-54\" => \"درخواست مورد نظر آرشيو شده است\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"100\" => \"عمليات با موفقيت انجام گرديده است.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"101\" => \"عمليات پرداخت موفق بوده و قبلا PaymentVerification تراكنش انجام شده است.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": ");"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$uuid = uniqid();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction = Transaction::create(["
    },
    {
      "type": "code",
      "lang": "text",
      "code": "'trackingCode' => $uuid,"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "'price' => $pakageprice->price,"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "'user_id' => $user->id,"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "'packageprice_id' => $pakageprice->id,"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "'statustransaction_id' => 1,"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "'bankName' => 'زرین پال'"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "]);"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "try {"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$data = array('merchant_id' => 'Your code',"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "'amount' => $transaction->price * 10,"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "'callback_url' => 'url',"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "'mobile' => $user->mobile,"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "'email' => $user->email,"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "'description' => $uuid"
    },
    {
      "type": "code",
      "lang": "text",
      "code": ");"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$jsonData = json_encode($data);"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/request.json');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v4');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "curl_setopt($ch, CURLOPT_HTTPHEADER, array("
    },
    {
      "type": "code",
      "lang": "text",
      "code": "'Content-Type: application/json',"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "'Content-Length: ' . strlen($jsonData)"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "));"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$result = curl_exec($ch);"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$err = curl_error($ch);"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$result = json_decode($result, true);"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "curl_close($ch);"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$code = $result['data']['code'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$errorMessage = \"\";"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "if (array_key_exists(\"{$code}\", $error)) {"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$errorMessage = $error[\"{$code}\"];"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "} else {"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$errorMessage = \"خطای نامشخص هنگام اتصال به درگاه زرین پال\";"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->Authority = $result['data']['authority'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalStatus = $result['data']['message'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalErrorMessage = $errorMessage;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalError = $err;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->save();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "if ($err) {"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->Authority = $result['data']['authority'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalStatus = $result['data']['message'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalErrorMessage = $errorMessage;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalError = $err;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->save();"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "if($transaction AND !empty($result['data'][\"authority\"]) ){"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "return Redirect::to(\"\nhttps://www.zarinpal.com/pg/StartPay/\" . $result['data'][\"authority\"]);"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}catch (Exception $ex) {"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->Authority = NULL;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalStatus = 'خطا در ارتباط اولیه';"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalErrorMessage = 'خطا در ارتباط اولیه';"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalError = 'error';"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->save();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$request->session()->flash('warning', 'خطا در ارتباط با زرین پال');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "return redirect()->back();"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "paragraph",
      "html": "after this code user is going to zaripal gateway."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/1200/1*dnAjWNuS-FRHYPFk8JPOjw.png",
      "alt": "Running Zarinpal Gateway on Laravel framework",
      "caption": "",
      "width": 1298,
      "height": 858
    },
    {
      "type": "paragraph",
      "html": "next step Zarinpal callback to your callback address. so create call back address in the router.php and payment must confirm in the controller. if payment not confirms, the money will come back to the customer."
    },
    {
      "type": "paragraph",
      "html": "callback method in controller:"
    },
    {
      "type": "paragraph",
      "html": "get Autority by get method."
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$Authority = NULL;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$Status = NULL;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "if (isset($_GET['Authority']) OR isset($_GET['Status'])) {"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$Authority = $_GET['Authority'];\n$Status = $_GET['Status'];"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "paragraph",
      "html": "if your using nginx set below configure to get data."
    },
    {
      "type": "code",
      "lang": "nginx",
      "code": "location / {"
    },
    {
      "type": "code",
      "lang": "nginx",
      "code": "try_files $uri $uri/ /index.php?$args;"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "paragraph",
      "html": "in controller verify the zarinpal transaction"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "if (!empty($Authority) AND !empty($Status)) {"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "if($Status == 'OK')"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "{"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$errorMessagesZarinpal = array("
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-1\" => \"اطلاعات ارسال شده ناقص است.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-2\" => \"IP و يا مرچنت كد پذيرنده صحيح نيست\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-3\" => \"با توجه به محدوديت هاي شاپرك امكان پرداخت با رقم درخواست شده ميسر نمي باشد\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-4\" => \"سطح تاييد پذيرنده پايين تر از سطح نقره اي است.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-11\" => \"درخواست مورد نظر يافت نشد.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-12\" => \"امكان ويرايش درخواست ميسر نمي باشد.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-21\" => \"هيچ نوع عمليات مالي براي اين تراكنش يافت نشد\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-22\" => \"تراكنش نا موفق ميباشد\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-33\" => \"رقم تراكنش با رقم پرداخت شده مطابقت ندارد\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-34\" => \"سقف تقسيم تراكنش از لحاظ تعداد يا رقم عبور نموده است\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-40\" => \"اجازه دسترسي به متد مربوطه وجود ندارد.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-41\" => \"اطلاعات ارسال شده مربوط به AdditionalData غيرمعتبر ميباشد.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-42\" => \"مدت زمان معتبر طول عمر شناسه پرداخت بايد بين 30 دقيه تا 45 روز مي باشد.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"-54\" => \"درخواست مورد نظر آرشيو شده است\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"100\" => \"عمليات با موفقيت انجام گرديده است.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"101\" => \"عمليات پرداخت موفق بوده و قبلا PaymentVerification تراكنش انجام شده است.\","
    },
    {
      "type": "code",
      "lang": "text",
      "code": ");"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction = Transaction::where('Authority', $Authority)"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "->first();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "if(empty($transaction->id))"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "{"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$error = True;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$message = \"خطا تراکنشی یافت نشد \";"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "return view('/callback')->with('message', $message)->with('error' , $error);"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "if($transaction->statustransaction_id != 1)"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "{"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$error = True;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$message = \"خطا این تراکنش قبلا در سیستم تعیین تکلیف شده است \";"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "return view('/callback')->with('message', $message)->with('error' , $error);"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$data = array("
    },
    {
      "type": "code",
      "lang": "text",
      "code": "'merchant_id' => 'Your Code',"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "'authority' => $Authority,"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "'amount' => $transaction->price * 10,"
    },
    {
      "type": "code",
      "lang": "text",
      "code": ");"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$jsonData = json_encode($data);"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/verify.json');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api V4');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "curl_setopt($ch, CURLOPT_HTTPHEADER, array("
    },
    {
      "type": "code",
      "lang": "text",
      "code": "'Content-Type: application/json',"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "'Content-Length: ' . strlen($jsonData)"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "));"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$result = curl_exec($ch);"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$err = curl_error($ch);"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "curl_close($ch);"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$result = json_decode($result, true);"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "if(isset($result['errors']['code']))"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "{"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalErrorMessageFinal = $result['errors']['code'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalErrorFinal = $result['errors']['message'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->statustransaction_id = 2;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->save();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$error = True;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$message = \".خطا از سمت زرین پال. \".\"عملیات با شکست روبه رو شد\".$result['errors']['message'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "return view('callback')->with('message', $message)->with('error' , $error);"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "else"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "{"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$code = $result['data']['code'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$errorMessage = \"\";"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "if (array_key_exists(\"{$code}\", $errorMessagesZarinpal)) {"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$errorMessage = $errorMessagesZarinpal[\"{$code}\"];"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "} else {"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$errorMessage = \"خطای نامشخص هنگام اتصال به درگاه زرین پال\";"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "if (isset($result['data']['code']) AND $result['data']['code'] == '100')"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "{"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalErrorMessageFinal = $result['data']['code'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalStatusFinal = $result['data']['message'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->statustransaction_id = 3;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalRefID = $result['data']['ref_id'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalFinalcardhash = $result['data']['card_hash'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalFinalcardpan =$result['data']['card_pan'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalFinalfeetype = $result['data']['fee_type'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->zarinpalfee =$result['data']['fee'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$transaction->save();"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$error = False;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$message = \"پرداخت با موفقیت انجام شد. شد پیگیری\".$result['data']['ref_id'].\"\n    \";"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$message.= \"  -  \";"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$message.= $packageprice->hour.\" ساعت حساب کاربری شما شارژ شد\";"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "return view('/callback')->with('message', $message)->with('error' , $error);"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "else if (isset($result['data']['code']) AND $result['data']['code'] == '101')"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "{"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$error = True;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$message = \"تراکنش شما در سیستم قبلا ثبت شده است و کد پیگیری :\".$result['data']['ref_id'];"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "return view('/customer/payment/paymentcallbakzarinpall')->with('message', $message)->with('error' ,\n$error);"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "else{ //Status is NOK"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$error = True;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$message = \"زرین پال اعلام می کند که تراکنش شما موفقیت آمیز نبوده است \";"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "return view('callback')->with('message', $message)->with('error' , $error);"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}else // no Authority get"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "{"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$error = True;"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "$message = \"خطا ورود به صورت غیر مجاز \";"
    },
    {
      "type": "code",
      "lang": "php",
      "code": "return view('/callback')->with('message', $message)->with('error' , $error);"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "}"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Zarinal Api Response"
    },
    {
      "type": "paragraph",
      "html": "error in connection"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "array:2 [▼"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"data\" => []"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"errors\" => array:3 [▼"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"code\" => -51"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"message\" => \"Session is not valid, session is not active paid try.\""
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"validations\" => []"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "]"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "]"
    },
    {
      "type": "paragraph",
      "html": "successful API response"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "array:2 [▼"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"data\" => array:7 [▼"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"code\" => 100"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"message\" => \"Paid\""
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"card_hash\" => \"code\""
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"card_pan\" => \"card\""
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"ref_id\" => integer"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"fee_type\" => \"Merchant\""
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"fee\" => 0"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "]"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "\"errors\" => []"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "]"
    },
    {
      "type": "paragraph",
      "html": "I would like to use these line by object oriented Class but composer class has problem with this version of Zarinpal Gateway."
    }
  ]
}
