<?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: OgoneModel.php, v 1.1 2012/12/02 12:52:11
//
class RazorpayModel extends PaymentModel
{
    public function process($order = '',$orderItems = '')
    {
        $ObjCurrency		= new CurrencyModel();
        parent::listPaymentSettingsValues($order['paymentSettingsGroupID']);
        $payAmount       = round($order['orderTotalAmount'],2);
        $taxAmount       = $order["orderTaxAmount"];
        $serviceCharge   = $order["orderOtherServiceCharge"];
        $shippingAmount = $order["shippingAmount"];
			
        $paymentCurrency=$this->settingsVars["payment_currency"];
        $payment_gateway_url=$this->settingsVars["payment_gateway_url"];
        $payment_test=$this->settingsVars["payment_test"];
        $payment_key_secret=$this->settingsVars["payment_key_secret"];
        $payment_key_id=$this->settingsVars["payment_key_id"];
        $transaction_type=$this->settingsVars["transaction_type"];
        $payment_method_name=$this->settingsVars["payment_method_name"];
        
        
        
        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);
					$currencyCode		=	$supportedCurrencyCode;
				}
				else
				{
					$this->isError 		= true;
					$this->errorMessage	= "Currency not configured , Please contact the site admin";
					return false;
				}
				
			}
		}
     
        
        $keyId =$payment_key_id;
        $keySecret =$payment_key_secret;
        $displayCurrency = $currencyCode; 
                
        $orderData = array(
            'receipt'         => $order['masterOrderID'],
            'amount'          => $payAmount*100, //  rupees in paise
            'currency'        => $displayCurrency,
            'payment_capture' => 1 // auto capture
        );
       
        
        include 'razorpay/pay.php';
        
       return $data ;   
    
    }
    public function verify($post,$orderID,$paymentSettingsGroupID){
        
        parent::listPaymentSettingsValues($paymentSettingsGroupID);
       
        $keyId =$this->settingsVars["payment_key_id"];
        $keySecret =$this->settingsVars["payment_key_secret"];
      
        
        
        $_POST['razorpay_order_id']=$orderID;
        $_POST['razorpay_payment_id']=$post['razorpay_payment_id'];
        $_POST['razorpay_signature']=$post['razorpay_signature'];
        
        include 'razorpay/verify.php';
        
        return $success;
        
    }
    public function getPay($razorpay_payment_id,$paymentSettingsGroupID){
        parent::listPaymentSettingsValues($paymentSettingsGroupID);
       
        $keyId =$this->settingsVars["payment_key_id"];
        $keySecret =$this->settingsVars["payment_key_secret"];
        
        include 'razorpay/payments.php';
        
           return $razorpayOrder['status'];
    }
}