<?php
/***************************************************************************
*                                                                          *
* Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
*                                                                          *
* This  is  commercial  software,  only  users  who have purchased a valid *
* license  and  accept  to the terms of the  License Agreement can install *
* and use this program.                                                    *
*                                                                          *
****************************************************************************
* PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
* "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
****************************************************************************/
//
//$Id: HdfcModel.php, v 1.0 2011/06/14 20:52:11
//

require_once dirname( __FILE__ ) . '/Payu.php';

class HdfcModel extends PaymentModel{ 
//FUNCTION TO PROCESS		
	public function process($order='',$orderItems='') {
			
			$ObjCountry        	= new CountryModel();
        	$ObjState          	= new StateModel();
			$ObjCurrency		= new CurrencyModel();
			parent::listPaymentSettingsValues($order['paymentSettingsGroupID']);	
			$method					=	"DoDirectPayment";
			$version				=	'65.1';//57.0
			$payAmount				=	$order['orderTotalAmount'];
			$taxAmount				=	$order["orderTaxAmount"];
			$shippingAmount 		= 	$order["shippingAmount"];
			$serviceCharge   		= 	$order["orderOtherServiceCharge"];
			$orderPromoDiscountAmount 	= $order["orderPromoDiscountAmount"];
			$orderUserCreditAmount 		= $order["orderUserCreditAmount"];
			$masterOrderSubtotal 		= $order["orderSubtotal"];
			
			$supportedCurrencyID		= $order["purchasedCurrency"];
			
			$paymentCurrency			=	$this->settingsVars["payment_currency"];
			$hdfc_merchant_key			=	$this->settingsVars["hdfc_merchant_key"];
			$hdfc_merchant_salt			=	$this->settingsVars["hdfc_merchant_salt"];
			$hdfc_merchant_salt			=	$this->settingsVars["hdfc_merchant_salt"];
			$payment_test				=	$this->settingsVars["payment_test"]=='On'?'test':'prod';
			
			if ($paymentCurrency != '')
        	{
            	$paymentCurrency = str_replace(", ", ",", $paymentCurrency);
            	$currencyArr     = explode(",", $paymentCurrency);
        	}
			if(count($currencyArr)<1)
			{
				$this->isError 		= true;
				$this->errorMessage	= "Currency not configured , Please contact the site admin";
				return false;
			}
			else
			{
				$currencyFlag = false;
				$purchasedCurrencyDetails 	= $ObjCurrency->getCurrencyByCurrencyID($order["purchasedCurrency"]);
				$currencyCode				= $purchasedCurrencyDetails['currencyCode'];
				$systemCurrencies 			=	$ObjCurrency->listCurrencyCodes();
				if(!in_array($purchasedCurrencyDetails['currencyCode'],$currencyArr))
				{
					foreach ($systemCurrencies as $key => $currency)
        			{
						if(in_array($currency['currencyCode'],$currencyArr))
						{
							$currencyFlag = true;
							$supportedCurrencyCode 	= $currency['currencyCode'];	
							$supportedCurrencyID 	= $currency['currencyID'];
							break;
						}
					}
					if($currencyFlag==true)
					{
						$payAmount = $ObjCurrency->convertValueCustom($payAmount,$order["purchasedCurrency"],$supportedCurrencyID);
						$taxAmount = $ObjCurrency->convertValueCustom($taxAmount,$order["purchasedCurrency"],$supportedCurrencyID);
						$shippingAmount = $ObjCurrency->convertValueCustom($shippingAmount,$order["purchasedCurrency"],
						$supportedCurrencyID);
						$serviceCharge = $ObjCurrency->convertValueCustom($serviceCharge,$order["purchasedCurrency"],
						$supportedCurrencyID);
						$orderPromoDiscountAmount = $ObjCurrency->convertValueCustom($orderPromoDiscountAmount
						,$order["purchasedCurrency"],$supportedCurrencyID);
						$orderUserCreditAmount = $ObjCurrency->convertValueCustom($orderUserCreditAmount
						,$order["purchasedCurrency"],$supportedCurrencyID);
						$masterOrderSubtotal	= $ObjCurrency->convertValueCustom($masterOrderSubtotal
						,$order["purchasedCurrency"],$supportedCurrencyID);
						$currencyCode		=	$supportedCurrencyCode;
					}
					else
					{
						$this->isError 		= true;
						$this->errorMessage	= "Currency not configured , Please contact the site admin";
						return false;
					}
					
				}
			}
			
			if($payAmount<.01)
			{
				$this->isError 		= true;
				$this->errorMessage	= "Invalid amount , please use another method";
				return false;
			}
			
			$paymentAmount 	= urlencode($payAmount);
			$itemsArray  	= '';
			$itemAmount  	= 0;
			$itemsAmount 	= 0;
			$key         	= 0;
	
			$address       			 = $order['billingAddress']['billingAddress1'];
        	$address       			.= ($order['billingAddress']['billingAddress2']!='') ? ', '.
										$order['billingAddress']['billingAddress2'] : '';
										
			$countryCode             = $ObjCountry->getCountryCode($order['billingAddress']['billingCountry']);
			$languageIDs['Default']=1;
			$languageIDs['Current']=1;
			if ($order['billingAddress']['billingState'] >0)
            {
				$getStateDetails = $ObjState->getStateDetails($languageIDs, 
				$order['billingAddress']['billingState']);
				$stateCode       = $getStateDetails['stateShortName'];
            }
            else
            {
             	$stateCode = $order['billingAddress']['billingState'];
            }
			
			$billingPhone = $order['billingAddress']['billingPhone']!=''? $order['billingAddress']['billingPhone'] :'1234567890';
			
			$msg =  Payu\pay_page( array (
				'key' => $hdfc_merchant_key, 
				'txnid' => uniqid( 'mvs_' ), 
				'amount' => $order['orderTotalAmount'],
				'firstname' => $order['billingAddress']['billingFirstName'], 
				'email' => $order['billingAddress']['billingEmail'], 
				'phone' =>billingPhone,
				'productinfo' => 'Product Payment', 
				'surl' => $order['returnURL'], 
				'furl' => $order['cancelURL']), 
				$hdfc_merchant_salt , $payment_test
			);

			if($msg == '101'){
				$this->isError 		= true;
				$this->errorMessage	= "Payu Error , Status is empty";
				return false;
			}else if($msg == '102'){
				$this->isError 		= true;
				$this->errorMessage	= "Payu Error , Salt is empty";
				return false;
			}else if($msg == '103'){
				$this->isError 		= true;
				$this->errorMessage	= "Payu Error , PayU response params is empty";
				return false;
			}else if($msg == '104'){
				$this->isError 		= true;
				$this->errorMessage	= "Payu Error , Pay params is empty";
				return false;
			}
			exit;
	}
}

