ArefMohajerRad
ArefMohajerRad
خواندن ۱ دقیقه·۳ سال پیش

sandbox درگاه زرین پال

zarinpal_function.php:

<?php /* * ZarinPal Advanced Class * author : aref mohajer * e-mail : amohajerrad@@gmail.com */ class zarinpal { private function curl_check() { return (function_exists('curl_version')) ? true : false; } private function soap_check() { return (extension_loaded('soapx')) ? true : false; } private function error_message($code, $desc, $cb, $request=false) { if (empty($cb) && $request === true) { return &quotلینک بازگشت ( CallbackURL ) نباید خالی باشد" } if (empty($desc) && $request === true) { return &quotتوضیحات تراکنش ( Description ) نباید خالی باشد" } $error = array( &quot-1&quot => &quotاطلاعات ارسال شده ناقص است.&quot, &quot-2&quot => &quotIP و يا مرچنت كد پذيرنده صحيح نيست&quot, &quot-3&quot => &quotبا توجه به محدوديت هاي شاپرك امكان پرداخت با رقم درخواست شده ميسر نمي باشد&quot, &quot-4&quot => &quotسطح تاييد پذيرنده پايين تر از سطح نقره اي است.&quot, &quot-11&quot => &quotدرخواست مورد نظر يافت نشد.&quot, &quot-12&quot => &quotامكان ويرايش درخواست ميسر نمي باشد.&quot, &quot-21&quot => &quotهيچ نوع عمليات مالي براي اين تراكنش يافت نشد&quot, &quot-22&quot => &quotتراكنش نا موفق ميباشد&quot, &quot-33&quot => &quotرقم تراكنش با رقم پرداخت شده مطابقت ندارد&quot, &quot-34&quot => &quotسقف تقسيم تراكنش از لحاظ تعداد يا رقم عبور نموده است&quot, &quot-40&quot => &quotاجازه دسترسي به متد مربوطه وجود ندارد.&quot, &quot-41&quot => &quotاطلاعات ارسال شده مربوط به AdditionalData غيرمعتبر ميباشد.&quot, &quot-42&quot => &quotمدت زمان معتبر طول عمر شناسه پرداخت بايد بين 30 دقيه تا 45 روز مي باشد.&quot, &quot-54&quot => &quotدرخواست مورد نظر آرشيو شده است&quot, &quot100&quot => &quotعمليات با موفقيت انجام گرديده است.&quot, &quot101&quot => &quotعمليات پرداخت موفق بوده و قبلا PaymentVerification تراكنش انجام شده است.&quot, ); if (array_key_exists(&quot{$code}&quot, $error)) { return $error[&quot{$code}&quot]; } else { return &quotخطای نامشخص هنگام اتصال به درگاه زرین پال" } } private function zarinpal_node() { if ($this->curl_check() === true) { $ir_ch = curl_init(&quothttps://www.zarinpal.com/pg/services/WebGate/wsdl&quot); curl_setopt($ir_ch, CURLOPT_TIMEOUT, 1); curl_setopt($ir_ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ir_ch, CURLOPT_RETURNTRANSFER, true); curl_exec($ir_ch); $ir_info = curl_getinfo($ir_ch); curl_close($ir_ch); $de_ch = curl_init(&quothttps://de.zarinpal.com/pg/services/WebGate/wsdl&quot); curl_setopt($de_ch, CURLOPT_TIMEOUT, 1); curl_setopt($de_ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($de_ch, CURLOPT_RETURNTRANSFER, true); curl_exec($de_ch); $de_info = curl_getinfo($de_ch); curl_close($de_ch); $ir_total_time = (isset($ir_info['total_time']) && $ir_info['total_time'] > 0) ? $ir_info['total_time'] : false; $de_total_time = (isset($de_info['total_time']) && $de_info['total_time'] > 0) ? $de_info['total_time'] : false; return ($ir_total_time === false || $ir_total_time > $de_total_time) ? &quotde&quot : &quotir" } else { if (function_exists('fsockopen')) { $de_ping = $this->zarinpal_ping(&quotde.zarinpal.com&quot, 80, 1); $ir_ping = $this->zarinpal_ping(&quotwww.zarinpal.com&quot, 80, 1); $de_domain = &quothttps://de.zarinpal.com/pg/services/WebGate/wsdl" $ir_domain = &quothttps://www.zarinpal.com/pg/services/WebGate/wsdl" $ir_total_time = (isset($ir_ping) && $ir_ping > 0) ? $ir_ping : false; $de_total_time = (isset($de_ping) && $de_ping > 0) ? $de_ping : false; return ($ir_total_time === false || $ir_total_time > $de_total_time) ? &quotde&quot : &quotir" } else { $webservice = &quothttps://www.zarinpal.com/pg/services/WebGate/wsd" $headers = @get_headers($webservice); return (strpos($headers[0], '200') === false) ? &quotde&quot : &quotir" } } } private function zarinpal_ping($host, $port, $timeout) { $time_b = microtime(true); $fsockopen = @fsockopen($host, $port, $errno, $errstr, $timeout); if (!$fsockopen) { return false; } else { $time_a = microtime(true); return round((($time_a - $time_b) * 1000), 0); } } public function redirect($url) { @header('Location: '. $url); echo &quot<meta http-equiv='refresh' content='0; url={$url}' />" echo &quot.href = '{$url}';" exit; } public function request($MerchantID, $Amount, $Description=&quot&quot, $Email=&quot&quot, $Mobile=&quot&quot, $CallbackURL, $SandBox=false, $ZarinGate=false) { $ZarinGate = ($SandBox == true) ? false : $ZarinGate; if ($this->soap_check() === true) { $node = ($SandBox == true) ? &quotsandbox&quot : $this->zarinpal_node(); $upay = ($SandBox == true) ? &quotsandbox&quot : &quotwww" $client = new SoapClient(&quothttps://{$node}.zarinpal.com/pg/services/WebGate/wsdl&quot, ['encoding' => 'UTF-8']); $result = $client->PaymentRequest([ 'MerchantID' => $MerchantID, 'Amount' => $Amount, 'Description' => $Description, 'Email' => $Email, 'Mobile' => $Mobile, 'CallbackURL' => $CallbackURL, ]); $Status = (isset($result->Status) && $result->Status != &quot&quot) ? $result->Status : 0; $Authority = (isset($result->Authority) && $result->Authority != &quot&quot) ? $result->Authority : &quot" $StartPay = (isset($result->Authority) && $result->Authority != &quot&quot) ? &quothttps://{$upay}.zarinpal.com/pg/StartPay/&quot. $Authority : &quot" $StartPayUrl = (isset($ZarinGate) && $ZarinGate == true) ? &quot{$StartPay}/ZarinGate&quot : $StartPay; return array( &quotNode&quot => &quot{$node}&quot, &quotMethod&quot => &quotSOAP&quot, &quotStatus&quot => $Status, &quotMessage&quot => $this->error_message($Status, $Description, $CallbackURL, true), &quotStartPay&quot => $StartPayUrl, &quotAuthority&quot => $Authority ); } else { $node = ($SandBox == true) ? &quotsandbox&quot : &quotir" $upay = ($SandBox == true) ? &quotsandbox&quot : &quotwww" $data = array( 'MerchantID' => $MerchantID, 'Amount' => $Amount, 'Description' => $Description, 'CallbackURL' => $CallbackURL, ); $jsonData = json_encode($data); $ch = curl_init(&quothttps://{$upay}.zarinpal.com/pg/rest/WebGate/PaymentRequest.json&quot); curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v1'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($jsonData))); $result = curl_exec($ch); $err = curl_error($ch); curl_close($ch); $result = json_decode($result, true); if ($err) { $Status = 0; $Message = &quotcURL Error #:&quot . $err; $Authority = &quot" $StartPay = &quot" $StartPayUrl = &quot" } else { $Status = (isset($result[&quotStatus&quot]) && $result[&quotStatus&quot] != &quot&quot) ? $result[&quotStatus&quot] : 0; $Message = $this->error_message($Status, $Description, $CallbackURL, true); $Authority = (isset($result[&quotAuthority&quot]) && $result[&quotAuthority&quot] != &quot&quot) ? $result[&quotAuthority&quot] : &quot" $StartPay = (isset($result[&quotAuthority&quot]) && $result[&quotAuthority&quot] != &quot&quot) ? &quothttps://{$upay}.zarinpal.com/pg/StartPay/&quot. $Authority : &quot" $StartPayUrl = (isset($ZarinGate) && $ZarinGate == true) ? &quot{$StartPay}/ZarinGate&quot : $StartPay; } return array( &quotNode&quot => &quot{$node}&quot, &quotMethod&quot => &quotCURL&quot, &quotStatus&quot => $Status, &quotMessage&quot => $Message, &quotStartPay&quot => $StartPayUrl, &quotAuthority&quot => $Authority ); } } public function verify($MerchantID, $Amount, $SandBox=false, $ZarinGate=false) { $ZarinGate = ($SandBox == true) ? false : $ZarinGate; if ($this->soap_check() === true) { $au = (isset($_GET['Authority']) && $_GET['Authority'] != &quot&quot) ? $_GET['Authority'] : &quot" $node = ($SandBox == true) ? &quotsandbox&quot : $this->zarinpal_node(); $client = new SoapClient(&quothttps://{$node}.zarinpal.com/pg/services/WebGate/wsdl&quot, ['encoding' => 'UTF-8']); $result = $client->PaymentVerification([ 'MerchantID' => $MerchantID, 'Authority' => $au, 'Amount' => $Amount, ]); $Status = (isset($result->Status) && $result->Status != &quot&quot) ? $result->Status : 0; $RefID = (isset($result->RefID) && $result->RefID != &quot&quot) ? $result->RefID : &quot" $Message = $this->error_message($Status, &quot&quot, &quot&quot, false); return array( &quotNode&quot => &quot{$node}&quot, &quotMethod&quot => &quotSOAP&quot, &quotStatus&quot => $Status, &quotMessage&quot => $Message, &quotAmount&quot => $Amount, &quotRefID&quot => $RefID, &quotAuthority&quot => $au ); } else { $au = (isset($_GET['Authority']) && $_GET['Authority'] != &quot&quot) ? $_GET['Authority'] : &quot" $node = ($SandBox == true) ? &quotsandbox&quot : &quotir" $upay = ($SandBox == true) ? &quotsandbox&quot : &quotwww" $data = array('MerchantID' => $MerchantID, 'Authority' => $au, 'Amount' => $Amount); $jsonData = json_encode($data); $ch = curl_init(&quothttps://{$upay}.zarinpal.com/pg/rest/WebGate/PaymentVerification.json&quot); curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v1'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($jsonData))); $result = curl_exec($ch); $err = curl_error($ch); curl_close($ch); $result = json_decode($result, true); if ($err) { $Status = 0; $Message = &quotcURL Error #:&quot . $err; $Status = &quot" $RefID = &quot" } else { $Status = (isset($result[&quotStatus&quot]) && $result[&quotStatus&quot] != &quot&quot) ? $result[&quotStatus&quot] : 0; $RefID = (isset($result['RefID']) && $result['RefID'] != &quot&quot) ? $result['RefID'] : &quot" $Message = $this->error_message($Status, &quot&quot, &quot&quot, false); } return array( &quotNode&quot => &quot{$node}&quot, &quotMethod&quot => &quotCURL&quot, &quotStatus&quot => $Status, &quotMessage&quot => $Message, &quotAmount&quot => $Amount, &quotRefID&quot => $RefID, &quotAuthority&quot => $au ); } } }

request.php:

<?php /* * ZarinPal Advanced Class * * author : aref mohajer * e-mail : amohajerrad@gmail.com */ require_once(&quotzarinpal_function.php&quot); $MerchantID = &quotxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" $Amount = 100; $Description = &quotتراکنش زرین پال" $Email = &quot" $Mobile = &quot" $CallbackURL = &quothttps://club.sabzgasht.com/zarin/verify.php" $ZarinGate = false; $SandBox = true; $zp = new zarinpal(); $result = $zp->request($MerchantID, $Amount, $Description, $Email, $Mobile, $CallbackURL, $SandBox, $ZarinGate); if (isset($result[&quotStatus&quot]) && $result[&quotStatus&quot] == 100) { // Success and redirect to pay $zp->redirect($result[&quotStartPay&quot]); } else { // error echo &quotخطا در ایجاد تراکنش" echo &quot<br />کد خطا : &quot. $result[&quotStatus&quot]; echo &quot<br />تفسیر و علت خطا : &quot. $result[&quotMessage&quot]; }

verify.php:

<?php /* * ZarinPal Advanced Class * author : aref mohajer * e-mail : amohajerrad@gmail.com */ require_once(&quotzarinpal_function.php&quot); $MerchantID = &quotxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" $Amount = 100; $ZarinGate = false; $SandBox = true; $zp = new zarinpal(); $result = $zp->verify($MerchantID, $Amount, $SandBox, $ZarinGate); if (isset($result[&quotStatus&quot]) && $result[&quotStatus&quot] == 100) { // Success echo &quotتراکنش با موفقیت انجام شد" echo &quot<br />مبلغ : &quot. $result[&quotAmount&quot]; echo &quot<br />کد پیگیری : &quot. $result[&quotRefID&quot]; echo &quot<br />Authority : &quot. $result[&quotAuthority&quot]; } else { // error echo &quotپرداخت ناموفق" echo &quot<br />کد خطا : &quot. $result[&quotStatus&quot]; echo &quot<br />تفسیر و علت خطا : &quot. $result[&quotMessage&quot]; }
برنامه نویسبرنامه نویسیطراحی وبسورس کدزرین پال
برنامه نویس،طراح وب،عاشق کارآفرینی
شاید از این پست‌ها خوشتان بیاید