<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";
include "../includes/dwolla.php";
if(isset( $_SESSION["sess_artistid"]) and $_SESSION["sess_artistid"]!= ""){
	$userid = $_SESSION["sess_artistid"];
}

$sql = "SELECT * FROM ".$tableprefix."settings ";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
$txtPaypalEmail = $row["paypalemail"];
$txtPaypalSandbox = $row["enablepaypalsandbox"];

$gift_coupon_flag = 0;
$paypal_check = 0;

$error_description = '';
// first, check for any errors
if (array_key_exists("error", $_GET)) {
	// find out what happened:
	$error_description = $_GET['error_description']; // "User Cancelled"
	//exit($error_description);
}
$status = $_GET['status'];


 $flag_to_continue=false;

if(($_GET['amount']!='' && $_GET['signature']!='') || $error_description!='' ){


       $checkoutId = $_GET['checkoutId'];
       $amount = $_GET['amount'];
       $amount_compare = $_GET['amount'];
       $signature = $_GET['signature'];
       if($error_description!=''){
            header("Location:failure.php?error=".$error_description);
            exit;
       }
       $signatureValid = verifyGatewaySignature($signature, $checkoutId, $amount);
       if (!$signatureValid) {
            header("Location:failure.php?error=bad signature");
            exit;
       }


}
else
    {

      if(isset($_SESSION['SESS_pay_amount']) && $_SESSION['SESS_pay_amount'] != "")
   {
       if($_SESSION['SESS_pay_amount'] == trim($_GET['amount']))
       {
       $flag_to_continue =true;
       }
   }
}



$amount_compare=trim($_GET['amount']);

$tx_token   = $_GET['transaction'];
/*******************************/

//$currency_attributes = displayCurrencyType();

//$totaltopay = number_format(($currency_attributes[1] * getCartTotal($userid)),2,".","");

//$amount_compare = number_format(($currency_attributes[1] * $_GET['amt']),2,".","");

/*---------checking if there is any difference in amounts------*/



if(isset($_SESSION['SESS_pay_amount']) && $_SESSION['SESS_pay_amount'] != "")
{
	if($_SESSION['SESS_pay_amount'] == trim($_GET['amt']))
	{
		$paypal_check = 0;
		
	}
}


if($paypal_check==0)
{
	if(function_exists('session_register')){
            session_unregister($_SESSION['SESS_pay_amount']);
        }
        else{
            
            unset($_SESSION['SESS_pay_amount']);
        }
}


if(isset($_SESSION['SESS_pay_amount']) && $_SESSION['SESS_pay_amount'] != "")
{
	if($_SESSION['SESS_pay_amount'] != $amount_compare)
	{
		$paypal_check = 1;
	}
}


	$select_Plan = "SELECT plan_Period
								   FROM ".$tableprefix."sellerPlans
								   WHERE plan_Id = ".trim($_SESSION['membership']['plan_Id']);
	$result_Plan = mysql_query($select_Plan) or die(mysql_error());
	$result_Plan = mysql_fetch_assoc($result_Plan);	
	if($result_Plan['plan_Period']=="M")
	$noOfDays=30;
	if($result_Plan['plan_Period']=="Y")
	$noOfDays=365;
	

$_SESSION['SES_MEMPLAN']=$_SESSION['membership']['plan_Id'];

if($paypal_check==0)
{ // check if paypal is success

		 $sql = "UPDATE " .$tableprefix. "artists SET sellerPlanID =".$_SESSION['membership']['plan_Id'].",plan_expired='N'  WHERE artist_id =".$_SESSION['sess_artistid'];
		mysql_query($sql) or die(mysql_error());
		$orderid = mysql_insert_id();
		
		$fromDate= date("Y-m-d");
		$endDate= date("Y-m-d",strtotime("+".$noOfDays." day"));


		$sql = "INSERT INTO " .$tableprefix. "artists_membershipDetails (artist_id, artist_memStartDate, artist_memEndDate,plan_Id,plan_SubscriptionCharge,transaction_id) VALUES
				(".$_SESSION['sess_artistid'].",'".$fromDate."','".$endDate."',".$_SESSION['membership']['plan_Id'].",".$_SESSION['membership']['plan_SubscriptionCharge'].",'".$tx_token."')";
		mysql_query($sql) or die(mysql_error());
		$orderid = mysql_insert_id();
		
			unset($_SESSION['membership']);
			unset($_SESSION['SESS_pay_amount']);
			unset($_SESSION['SESS_insertQry']);
			
			$_SESSION['PAY_MESSAGE']=MSG_PROC_SUC;
			
			header("location:artistmain.php");
			
			exit;
		
		
}		
else
{
		header("location:failure.php");
		exit;
}




?>															