<?php

require_once ROOT_PATH.'/zipmarket/apps/payments/models/payments_abstract.class.php';

class mellat extends payments_abstract {
	
	public function generateForm($order_data = array()) {
		global $langArray, $currency, $config, $languageURL;
		require_once("nusoap.php");
		$client = new nusoap_client('https://bpm.shaparak.ir/pgwchannel/services/pgw?wsdl');
		$namespace='http://interfaces.core.sw.bps.com/';
		$terminalId = $this->getMeta('mellat_terminalid');
		$userName = $this->getMeta('mellat_usernamet');
		$userPassword = $this->getMeta('mellat_passwordt');
		$orderId=rand(1, 9999999999999);
		$amount = $order_data['price'] * 10;
		$date =  date("ymd");
		$time =  date("His");
		$localDate = $date;
		$localTime = $time;
		$additionalData = '';
		$callBackUrl = 'http://' . $config['domain'] . '/' . $languageURL . 'payments/mellat/?oID='.(int)$order_data['id'];
		$payerId = '0';
		$parameters = array(
			'terminalId' => $terminalId,
			'userName' => $userName,
			'userPassword' => $userPassword,
			'orderId' => $orderId,
			'amount' => $amount,
			'localDate' => $localDate,
			'localTime' => $localTime,
			'additionalData' => $additionalData,
			'callBackUrl' => $callBackUrl,
			'payerId' => $payerId);

		// Call the SOAP method
		$result = $client->call('bpPayRequest', $parameters, $namespace);
		
		// Check for a fault
		if ($client->fault) {
			echo $this ->CheckBPMStatus($_POST['ResCode']);
		} 
		else {
			// Check for errors
			
			$resultStr  = $result;

			$err = $client->getError();
			if ($err) {
				$this -> msg['class'] = 'error';
        $this -> msg['message'] = $err;
			} 
			else {
				// Display the result

				$res = explode (',',$resultStr);

				
				$ResCode = $res[0];
				
				if ($ResCode == "0") {
					
$form  = '<form class="form-for-pay" action="https://bpm.shaparak.ir/pgwchannel/startpay.mellat" method="post" id="mellat_form_submit">'."\n";

		$form .= '<input type="hidden"  name="RefId" value="'.$res[1].'" />
		<button id="mellat-purchase-button" class="btn-icon purchase" type="submit">خرید کنید</button>'."\n";
		$form .= '</form>'."\n";		
				} 
				else {
				$this -> msg['class'] = 'error';
				$this -> msg['message'] = "اتصال به بانک با مشکل مواجه شد. مشکل را با مدیر سایت در میان بگذارید";
				// log error in app
					// Update table, log the error
					// Show proper message to user
				}
			}// end Display the result
		}// end Check for errors
		if($this -> msg['class']== 'error')
			$form=$this -> msg['message'];
		return $form;
	}
	


	public function generateDepositForm($order_data = array()) {
		global $langArray, $currency, $config, $languageURL;
		require_once("nusoap.php");
		$client = new nusoap_client('https://bpm.shaparak.ir/pgwchannel/services/pgw?wsdl');
		$namespace='http://interfaces.core.sw.bps.com/';
		$terminalId = $this->getMeta('mellat_terminalid');
		$userName = $this->getMeta('mellat_usernamet');
		$userPassword = $this->getMeta('mellat_passwordt');
		$orderId=rand(1, 9999999999999);
		$amount = $order_data['deposit'] * 10;
		$date =  date("ymd");
		$time =  date("His");
		$localDate = $date;
		$localTime = $time;
		$additionalData = '';
		$callBackUrl = 'http://' . $config['domain'] . '/' . $languageURL . 'payments/deposit_mellat/?oID='.(int)$order_data['id'];
		$payerId = '0';
		$parameters = array(
			'terminalId' => $terminalId,
			'userName' => $userName,
			'userPassword' => $userPassword,
			'orderId' => $orderId,
			'amount' => $amount,
			'localDate' => $localDate,
			'localTime' => $localTime,
			'additionalData' => $additionalData,
			'callBackUrl' => $callBackUrl,
			'payerId' => $payerId);

		// Call the SOAP method
		$result = $client->call('bpPayRequest', $parameters, $namespace);
		
		// Check for a fault
		if ($client->fault) {
			echo $this ->CheckBPMStatus($_POST['ResCode']);
		} 
		else {
			// Check for errors
			
			$resultStr  = $result;

			$err = $client->getError();
			if ($err) {
				$this -> msg['class'] = 'error';
        $this -> msg['message'] = $err;
			} 
			else {
				// Display the result

				$res = explode (',',$resultStr);

				
				$ResCode = $res[0];
				
				if ($ResCode == "0") {
					
$form  = '<form class="form-for-pay" action="https://bpm.shaparak.ir/pgwchannel/startpay.mellat" method="post" id="mellat_form_submit">'."\n";

		$form .= '<input type="hidden"  name="RefId" value="'.$res[1].'" />
		<button id="mellat-purchase-button" class="btn-icon purchase" type="submit">خرید کنید</button>'."\n";
		$form .= '</form>'."\n";		
				} 
				else {
				$this -> msg['class'] = 'error';
				$this -> msg['message'] = "اتصال به بانک با مشکل مواجه شد. مشکل را با مدیر سایت در میان بگذارید";
				// log error in app
					// Update table, log the error
					// Show proper message to user
				}
			}// end Display the result
		}// end Check for errors
		if($this -> msg['class']== 'error')
			$form=$this -> msg['message'];
		return $form;
	}
	
	private function western_to_persian($str) {
	$alphabet = array (
		'Û°' => '۰', 'Û±' => '۱', 'Û²' => '۲', 'Û³' => '۳', 'Û´' => '۴', 'Ûµ' => '۵', 'Û¶' => '۶', 'Û·' => '۷', 'Û¸' => '۸',
		'Û¹' => '۹', 'Ø¢' => 'آ', 'Ø§' => 'ا', 'Ø£' => 'أ', 'Ø¥' => 'إ', 'Ø¤' => 'ؤ', 'Ø¦' => 'ئ', 'Ø¡' => 'ء', 'Ø¨' => 'ب',
		'Ù¾' => 'پ', 'Øª' => 'ت', 'Ø«' => 'ث', 'Ø¬' => 'ج', 'Ú†' => 'چ', 'Ø­' => 'ح', 'Ø®' => 'خ', 'Ø¯' => 'د', 'Ø°' => 'ذ',
		'Ø±' => 'ر', 'Ø²' => 'ز', 'Ú˜' => 'ژ', 'Ø³' => 'س', 'Ø´' => 'ش', 'Øµ' => 'ص', 'Ø¶' => 'ض', 'Ø·' => 'ط', 'Ø¸' => 'ظ',
		'Ø¹' => 'ع', 'Øº' => 'غ', 'Ù' => 'ف', 'Ù‚' => 'ق', 'Ú©' => 'ک', 'Ú¯' => 'گ', 'Ù„' => 'ل', 'Ù…' => 'م', 'Ù†' => 'ن',
		'Ùˆ' => 'و', 'Ù‡' => 'ه', 'ÛŒ' => 'ی', 'ÙŠ' => 'ي', 'Û€' => 'ۀ', 'Ø©' => 'ة', 'ÙŽ' => 'َ', 'Ù' => 'ُ', 'Ù' => 'ِ',
		'Ù‘' => 'ّ', 'Ù‹' => 'ً', 'ÙŒ' => 'ٌ', 'Ù' => 'ٍ', 'ØŒ' => '،', 'Ø›' => '؛', ',' => ',', 'ØŸ' => '؟'
	);

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

	return $str;
	}
	private 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;
	}
	
}

?>