<?php

$itemID = get_id(2);

require_once ROOT_PATH.'/zipmarket/apps/items/models/orders.class.php';
$cms = new orders();

$OrderID = isset($_REQUEST['oID'])   ? $_REQUEST['oID']   : '';

$order_info = $cms->get($OrderID);
		require_once("nusoap.php");
		$client = new nusoap_client('https://bpm.shaparak.ir/pgwchannel/services/pgw?wsdl');
        $namespace='http://interfaces.core.sw.bps.com/';
		$terminalId = $meta['mellat_terminalid'];
		$userName = $meta['mellat_usernamet'];
		$userPassword = $meta['mellat_passwordt'];
		if($_POST['ResCode']!=0){
		$msg['status'] = 0;
		$msg['message']=CheckBPMStatus($_POST['ResCode']);
		}
		if($order_info != '' AND $_POST['ResCode']==0){
        $err = $client->getError();
					if ($err)
					{
						$msg['status'] = 0;
						$msg['reversal'] = 1;
						$msg['message'] = $err;
					}
					else
					{
					  	$orderId 				= $_POST[SaleOrderId];
					  	$verifySaleOrderId 		= $_POST[SaleOrderId];
					  	$verifySaleReferenceId 	= $_POST[SaleReferenceId];
					  	
						$parameters = array(
							'terminalId' => $terminalId,
							'userName' => $userName,
							'userPassword' => $userPassword,
							'orderId' => $orderId,
							'saleOrderId' => $verifySaleOrderId,
							'saleReferenceId' => $verifySaleReferenceId);

						// Call the SOAP method
						$result = $client->call('bpVerifyRequest', $parameters, $namespace);

						// Check for a fault
						if ($client->fault)
						{
							$msg['status'] = 0;
							$msg['reversal'] = 1;
							foreach ($result as $error)
								$msg['message'] .= $error;
						}
						else
						{
							$err = $client->getError();
							if ($err)
							{
								$msg['status'] = 0;
								$msg['reversal'] = 1;
								$msg['message'] = $err;
							}
							else 
							{
								if($result == 0)
								{
									// Display the result
									// Update Table, Save Verify Status 
									// Note: Successful Verify means complete successful sale was done.
									$inquirySaleOrderId = $_POST[SaleOrderId];
									$inquirySaleReferenceId = $_POST[SaleReferenceId];

									// Check for an error
									$err = $client->getError();
									if ($err)
									{
										$msg['status'] = 0;
										$msg['reversal'] = 1;
										$msg['message'] = $err;
									}
									else
									{
										$parameters = array(
											'terminalId' => $terminalId,
											'userName' => $userName,
											'userPassword' => $userPassword,
											'orderId' => $orderId,
											'saleOrderId' => $inquirySaleOrderId,
											'saleReferenceId' => $inquirySaleReferenceId);

										// Call the SOAP method
										$result = $client->call('bpInquiryRequest', $parameters, $namespace);
										if($result == 0)
										{
											// Check for a fault
											if ($client->fault)
											{
												$msg['status'] = 0;
												$msg['reversal'] = 1;
												foreach ($result as $error)
													$msg['message'] .= $error;
											}
											else
											{
												$err = $client->getError();
												if ($err)
												{
													$msg['status'] = 0;
													$msg['reversal'] = 1;
													$msg['message'] = $err;
												}
												else
												{
													$msg['status'] = 1;
												}// end Display the result
											}// end Check for errors
										}
										else
										{
											$msg['status'] = 0;
											$msg['reversal'] = 0;
											if (function_exists('CheckBPMStatus'))
												$msg['message'] = CheckBPMStatus($result);
											else
												$msg['message'] = $result;
										}
									}
								}
								else
								{
									$msg['status'] = 0;
									$msg['reversal'] = 0;
									if (function_exists('CheckBPMStatus'))
										$msg['message'] = CheckBPMStatus($result);
									else
										$msg['message'] = $result;
								}
							}// end Display the result
						}// end Check for errors
					}
			if ($msg['status'] == 1)
			{
				// Update Table, Save Inquiry Status 
				// Note: Successful Inquiry means complete successful sale was done.
				$settleSaleOrderId 		= $_POST[SaleOrderId];
				$settleSaleReferenceId 	= $_POST[SaleReferenceId];
				// Check for an error
				$err = $client->getError();
				if ($err) 
				{
					$msg['status'] = 0;
					$msg['message'] = $err;
				}
			  	else
			  	{
					$parameters = array(
						'terminalId' => $terminalId,
						'userName' => $userName,
						'userPassword' => $userPassword,
						'orderId' => $orderId,
						'saleOrderId' => $settleSaleOrderId,
						'saleReferenceId' => $settleSaleReferenceId);
					
					// Call the SOAP method
					$result = $client->call('bpSettleRequest', $parameters, $namespace);
					
					if($result == 0)
					{
						// Check for a fault
						if ($client->fault)
						{
							$msg['status'] = 0;
							foreach ($result as $error)
								$msg['message'] .= $error;
						}
						else
						{
							$err = $client->getError();
							if ($err)
							{
								$msg['reversal'] = 1;
								$msg['status'] = 0;
								$msg['message'] = $err;
							} 
							else 
							{
								// Update Table, Save Settle Status 
								// Note: Successful Settle means that sale is settled.
								$cms->orderIsPay($OrderID);
							}// end Display the result
						}// end Check for errors
					}
					else
					{
						$msg['status'] = 0;
						$msg['reversal'] = 1;
						if (function_exists('CheckBPMStatus'))
							$msg['message'] = $this ->CheckBPMStatus($result);
						else
							$msg['message'] = $result;
					}
				}
			}
			//------------- در غیر اینصورت وجه برگشت داده شود
			if ($msg['reversal'] == 1)
			{
				$orderId 					= $_POST[SaleOrderId];
			  	$reversalSaleOrderId 		= $_POST[SaleOrderId];
			  	$reversalSaleReferenceId 	= $_POST[SaleReferenceId];

				// Check for an error
				$err = $client->getError();
				if ($err)
				{
					$msg['status'] = 0;
					$msg['message'] = $err;
				}
				else
				{
					$parameters = array(
						'terminalId' => $terminalId,
						'userName' => $userName,
						'userPassword' => $userPassword,
						'orderId' => $orderId,
						'saleOrderId' => $reversalSaleOrderId,
						'saleReferenceId' => $reversalSaleReferenceId);
					// Call the SOAP method
					$result = $client->call('bpReversalRequest', $parameters, $namespace);
					// Check for a fault
					if ($client->fault)
					{
						$msg['status'] = 0;
						foreach ($result as $error)
							$msg['message'] .= $error;
					}
					else
					{
						$err = $client->getError();
						if ($err) {
							// Display the error
							$msg['status'] = 0;
							$msg['message'] = $err;
						}
						else
						{
							// Update Table, Save Reversal Status 
							// Note: Successful Reversal means that sale is reversed.
							if($result == 0)
							{
								$msg['message'] .= '<br />در موقع پرداخت مشکلی به وجود آمد٬ مبلغ پرداخت شده به حساب شما برگشت داده شد.';
							}
							else
							{
								$msg['message'] .= '<br />در موقع پرداخت مشکلی به وجود آمد.';
							}
						}// end Display the result
					}// end Check for errors
				}
			}
			
refresh('http://' . $config['domain'] . '/' . $languageURL . 'users/downloads/');
			
}


                exit;
				
function western_to_persian($str) {
	$alphabet = array (
		'Û°' => '۰', 'Û±' => '۱', 'Û²' => '۲', 'Û³' => '۳', 'Û´' => '۴', 'Ûµ' => '۵', 'Û¶' => '۶', 'Û·' => '۷', 'Û¸' => '۸',
		'Û¹' => '۹', 'Ø¢' => 'آ', 'Ø§' => 'ا', 'Ø£' => 'أ', 'Ø¥' => 'إ', 'Ø¤' => 'ؤ', 'Ø¦' => 'ئ', 'Ø¡' => 'ء', 'Ø¨' => 'ب',
		'Ù¾' => 'پ', 'Øª' => 'ت', 'Ø«' => 'ث', 'Ø¬' => 'ج', 'Ú†' => 'چ', 'Ø­' => 'ح', 'Ø®' => 'خ', 'Ø¯' => 'د', 'Ø°' => 'ذ',
		'Ø±' => 'ر', 'Ø²' => 'ز', 'Ú˜' => 'ژ', 'Ø³' => 'س', 'Ø´' => 'ش', 'Øµ' => 'ص', 'Ø¶' => 'ض', 'Ø·' => 'ط', 'Ø¸' => 'ظ',
		'Ø¹' => 'ع', 'Øº' => 'غ', 'Ù' => 'ف', 'Ù‚' => 'ق', 'Ú©' => 'ک', 'Ú¯' => 'گ', 'Ù„' => 'ل', 'Ù…' => 'م', 'Ù†' => 'ن',
		'Ùˆ' => 'و', 'Ù‡' => 'ه', 'ÛŒ' => 'ی', 'ÙŠ' => 'ي', 'Û€' => 'ۀ', 'Ø©' => 'ة', 'ÙŽ' => 'َ', 'Ù' => 'ُ', 'Ù' => 'ِ',
		'Ù‘' => 'ّ', 'Ù‹' => 'ً', 'ÙŒ' => 'ٌ', 'Ù' => 'ٍ', 'ØŒ' => '،', 'Ø›' => '؛', ',' => ',', 'ØŸ' => '؟'
	);

	foreach($alphabet as $western => $fa)
		$str = str_replace($western, $fa, $str);

	return $str;
	}
	function CheckBPMStatus($ecode)	{
		               //$tmess=western_to_persian("Ø´Ø±Ø­ Ø®Ø·Ø§:");
		               switch ($ecode)
					     {
						  case 0:
					        $tmess=$this ->western_to_persian("ØªØ±Ø§Ú©Ù†Ø´ Ø¨Ø§ Ù…ÙˆÙÙ‚ÙŠØª Ø§Ù†Ø¬Ø§Ù… Ø´Ø¯");
						    break;
						  case 11:
					        $tmess=$this ->western_to_persian("Ø´Ù…Ø§Ø±Ù‡ Ú©Ø§Ø±Øª Ù…Ø¹ØªØ¨Ø± Ù†ÙŠØ³Øª");
						    break;
						  case 12:
					        $tmess= $this ->western_to_persian("Ù…ÙˆØ¬ÙˆØ¯ÙŠ Ú©Ø§ÙÙŠ Ù†ÙŠØ³Øª");
						    break;
						  case 13:
					        $tmess= $this ->western_to_persian("Ø±Ù…Ø² Ø¯ÙˆÙ… Ø´Ù…Ø§ ØµØ­ÙŠØ­ Ù†ÙŠØ³Øª");
						    break;
						  case 14:
					        $tmess= $this ->western_to_persian("Ø¯ÙØ¹Ø§Øª Ù…Ø¬Ø§Ø² ÙˆØ±ÙˆØ¯ Ø±Ù…Ø² Ø¨ÙŠØ´ Ø§Ø² Ø­Ø¯ Ø§Ø³Øª");
						    break;
						  case 15:
					        $tmess= $this ->western_to_persian("Ú©Ø§Ø±Øª Ù…Ø¹ØªØ¨Ø± Ù†ÙŠØ³Øª");
						    break;
						  case 16:
					        $tmess= $this ->western_to_persian("Ø¯ÙØ¹Ø§Øª Ø¨Ø±Ø¯Ø§Ø´Øª ÙˆØ¬Ù‡ Ø¨ÙŠØ´ Ø§Ø² Ø­Ø¯ Ù…Ø¬Ø§Ø² Ø§Ø³Øª");
						    break;
						  case 17:
					        $tmess= $this ->western_to_persian("Ú©Ø§Ø±Ø¨Ø± Ø§Ø² Ø§Ù†Ø¬Ø§Ù… ØªØ±Ø§Ú©Ù†Ø´ Ù…Ù†ØµØ±Ù Ø´Ø¯Ù‡ Ø§Ø³Øª");
						    break;
						  case 18:
					        $tmess= $this ->western_to_persian("ØªØ§Ø±ÙŠØ® Ø§Ù†Ù‚Ø¶Ø§ÙŠ Ú©Ø§Ø±Øª Ú¯Ø°Ø´ØªÙ‡ Ø§Ø³Øª");
						    break;
						  case 19:
					        $tmess= $this ->western_to_persian("Ù…Ø¨Ù„Øº Ø¨Ø±Ø¯Ø§Ø´Øª ÙˆØ¬Ù‡ Ø¨ÙŠØ´ Ø§Ø² Ø­Ø¯ Ù…Ø¬Ø§Ø² Ø§Ø³Øª");
						    break;
						  case 111:
					        $tmess= $this ->western_to_persian("ØµØ§Ø¯Ø± Ú©Ù†Ù†Ø¯Ù‡ Ú©Ø§Ø±Øª Ù†Ø§Ù…Ø¹ØªØ¨Ø± Ø§Ø³Øª");
						    break;
						  case 112:
					        $tmess= $this ->western_to_persian("Ø®Ø·Ø§ÙŠ Ø³ÙˆÙŠÙŠÚ† ØµØ§Ø¯Ø± Ú©Ù†Ù†Ø¯Ù‡ Ú©Ø§Ø±Øª");
						    break;
						  case 113:
					        $tmess= $this ->western_to_persian("Ù¾Ø§Ø³Ø®ÙŠ Ø§Ø² ØµØ§Ø¯Ø± Ú©Ù†Ù†Ø¯Ù‡ Ú©Ø§Ø±Øª Ø¯Ø±ÙŠØ§ÙØª Ù†Ø´Ø¯");
						    break;
						  case 114:
					        $tmess= $this ->western_to_persian("Ø¯Ø§Ø±Ù†Ø¯Ù‡ Ú©Ø§Ø±Øª Ù…Ø¬Ø§Ø² Ø¨Ù‡ Ø§Ù†Ø¬Ø§Ù… Ø§ÙŠÙ† ØªØ±Ø§Ú©Ù†Ø´ Ù†Ù…ÙŠ Ø¨Ø§Ø´Ø¯");
						    break;
						  case 21:
					        $tmess= $this ->western_to_persian("Ù¾Ø°ÙŠØ±Ù†Ø¯Ù‡ Ù…Ø¹ØªØ¨Ø± Ù†ÙŠØ³Øª");
						    break;
						  case 23:
					        $tmess= $this ->western_to_persian("Ø®Ø·Ø§ÙŠ Ø§Ù…Ù†ÙŠØªÙŠ Ø±Ø® Ø¯Ø§Ø¯Ù‡ Ø§Ø³Øª");
						    break;
						  case 24:
					        $tmess= $this ->western_to_persian("Ø§Ø·Ù„Ø§Ø¹Ø§Øª Ú©Ø§Ø±Ø¨Ø±ÙŠ Ù¾Ø°ÙŠØ±Ù†Ø¯Ù‡ Ù…Ø¹ØªØ¨Ø± Ù†ÙŠØ³Øª");
						    break;
						  case 25:
					        $tmess= $this ->western_to_persian("Ù…Ø¨Ù„Øº Ù†Ø§Ù…Ø¹ØªØ¨Ø± Ø§Ø³Øª");
						    break;
						  case 31:
					        $tmess= $this ->western_to_persian("Ù¾Ø§Ø³Ø® Ù†Ø§Ù…Ø¹ØªØ¨Ø± Ø§Ø³Øª");
						    break;
						  case 32:
					        $tmess= $this ->western_to_persian("ÙØ±Ù…Øª Ø§Ø·Ù„Ø§Ø¹Ø§Øª ÙˆØ§Ø±Ø¯ Ø´Ø¯Ù‡ ØµØ­ÙŠØ­ Ù†ÙŠØ³Øª");
						    break;
						  case 33:
					        $tmess=$this ->western_to_persian("Ø­Ø³Ø§Ø¨ Ù†Ø§Ù…Ø¹ØªØ¨Ø± Ø§Ø³Øª");
						    break;
						  case 34:
					        $tmess= $this ->western_to_persian("Ø®Ø·Ø§ÙŠ Ø³ÙŠØ³ØªÙ…ÙŠ");
						    break;
						  case 35:
					        $tmess= $this ->western_to_persian("ØªØ§Ø±ÙŠØ® Ù†Ø§Ù…Ø¹ØªØ¨Ø± Ø§Ø³Øª");
						    break;
						  case 41:
					        $tmess= $this ->western_to_persian("Ø´Ù…Ø§Ø±Ù‡ Ø¯Ø±Ø®ÙˆØ§Ø³Øª ØªÚ©Ø±Ø§Ø±ÙŠ Ø§Ø³Øª");
						    break;
						  case 42:
					        $tmess= $this ->western_to_persian("ØªØ±Ø§Ú©Ù†Ø´ Sale ÙŠØ§ÙØª Ù†Ø´Ø¯");
						    break;
						  case 43:
					        $tmess= $this ->western_to_persian("Ù‚Ø¨Ù„Ø§ Ø¯Ø±Ø®ÙˆØ§Ø³Øª Verify Ø¯Ø§Ø¯Ù‡ Ø´Ø¯Ù‡ Ø§Ø³Øª");
						    break;
						  case 44:
					        $tmess= $this ->western_to_persian("Ø¯Ø±Ø®ÙˆØ§Ø³Øª Verify ÙŠØ§ÙØª Ù†Ø´Ø¯");
						    break;
						  case 45:
					        $tmess= $this ->western_to_persian("ØªØ±Ø§Ú©Ù†Ø´ Settle Ø´Ø¯Ù‡ Ø§Ø³Øª");
						    break;
						  case 46:
					        $tmess= $this ->western_to_persian("ØªØ±Ø§Ú©Ù†Ø´ Settle Ù†Ø´Ø¯Ù‡ Ø§Ø³Øª");
						    break;
						  case 47:
					        $tmess= $this ->western_to_persian("ØªØ±Ø§Ú©Ù†Ø´ Settle ÙŠØ§ÙØª Ù†Ø´Ø¯");
						    break;
						  case 48:
					        $tmess= $this ->western_to_persian("ØªØ±Ø§Ú©Ù†Ø´ Reverse Ø´Ø¯Ù‡ Ø§Ø³Øª");
						    break;
						  case 49:
					        $tmess= $this ->western_to_persian("ØªØ±Ø§Ú©Ù†Ø´ Refund ÙŠØ§ÙØª Ù†Ø´Ø¯");
						    break;
						  case 412:
					        $tmess= $this ->western_to_persian("Ø´Ù†Ø§Ø³Ù‡ Ù‚Ø¨Ø¶ Ù†Ø§Ø¯Ø±Ø³Øª Ø§Ø³Øª");
						    break;
						  case 413:
					        $tmess= $this ->western_to_persian("Ø´Ù†Ø§Ø³Ù‡ Ù¾Ø±Ø¯Ø§Ø®Øª Ù†Ø§Ø¯Ø±Ø³Øª Ø§Ø³Øª");
						    break;
						  case 414:
					        $tmess= $this ->western_to_persian("Ø³Ø§Ø²Ù…Ø§Ù† ØµØ§Ø¯Ø± Ú©Ù†Ù†Ø¯Ù‡ Ù‚Ø¨Ø¶ Ù…Ø¹ØªØ¨Ø± Ù†ÙŠØ³Øª");
						    break;
						  case 415:
					        $tmess= $this ->western_to_persian("Ø²Ù…Ø§Ù† Ø¬Ù„Ø³Ù‡ Ú©Ø§Ø±ÙŠ Ø¨Ù‡ Ù¾Ø§ÙŠØ§Ù† Ø±Ø³ÙŠØ¯Ù‡ Ø§Ø³Øª");
						    break;
						  case 416:
					        $tmess= $this ->western_to_persian("Ø®Ø·Ø§ Ø¯Ø± Ø«Ø¨Øª Ø§Ø·Ù„Ø§Ø¹Ø§Øª");
						    break;
						  case 417:
					        $tmess= $this ->western_to_persian("Ø´Ù†Ø§Ø³Ù‡ Ù¾Ø±Ø¯Ø§Ø®Øª Ú©Ù†Ù†Ø¯Ù‡ Ù†Ø§Ù…Ø¹ØªØ¨Ø± Ø§Ø³Øª");
						    break;
						  case 418:
					        $tmess= $this ->western_to_persian("Ø§Ø´Ú©Ø§Ù„ Ø¯Ø± ØªØ¹Ø±ÙŠÙ Ø§Ø·Ù„Ø§Ø¹Ø§Øª Ù…Ø´ØªØ±ÙŠ");
						    break;
						  case 419:
					        $tmess= $this ->western_to_persian("ØªØ¹Ø¯Ø§Ø¯ Ø¯ÙØ¹Ø§Øª ÙˆØ±ÙˆØ¯ Ø§Ø·Ù„Ø§Ø¹Ø§Øª Ø¨ÙŠØ´ Ø§Ø² Ø­Ø¯ Ù…Ø¬Ø§Ø² Ø§Ø³Øª");
						    break;
						  case 421:
					        $tmess= $this ->western_to_persian("IP Ù…Ø¹ØªØ¨Ø± Ù†ÙŠØ³Øª");
						    break;
						  case 51:
					        $tmess= $this ->western_to_persian("ØªØ±Ø§Ú©Ù†Ø´ ØªÚ©Ø±Ø§Ø±ÙŠ Ø§Ø³Øª");
						    break;
						  case 54:
					        $tmess= $this ->western_to_persian("ØªØ±Ø§Ú©Ù†Ø´ Ù…Ø±Ø¬Ø¹ Ù…ÙˆØ¬ÙˆØ¯ Ù†ÙŠØ³Øª");
						    break;
						  case 55:
					        $tmess= $this ->western_to_persian("ØªØ±Ø§Ú©Ù†Ø´ Ù†Ø§Ù…Ø¹ØªØ¨Ø± Ø§Ø³Øª");
						    break;
						  case 61:
					        $tmess= $this ->western_to_persian("Ø®Ø·Ø§ Ø¯Ø± ÙˆØ§Ø±ÙŠØ²");
						    break;
						 }
		return $tmess;
	}