<?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"];
}
if(checkArtistExpired($userid)){
	header("Location:seller_plan_upgrade.php?sellerid=".$userid);
	exit;
}
$pids = $_GET['pid'];

$productListingFee = getSettingsValue('product_listing_fee');
$prdCount = 0;
if($pids != '')
{
    //$pids = explode($pids, ',');
    if(!empty($pids))
    {
        //$pids = array_unique($pids);
        $sql = "SELECT product_id, product_name
                  FROM ".$tableprefix."products p
                 WHERE p.product_id IN (". mysql_real_escape_string($pids) .")
            AND p.product_artist_id = " . $userid;

        $resultsP = mysql_query($sql) or die(mysql_error());
        $prdCount = mysql_num_rows($resultsP);
    }
}
//die("her1e");
if(!($productListingFee > 0) || !($prdCount > 0))
{
    header("Location:mycatalog.php");
    exit;
}



$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=true;
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_pay_listing_fee.php?pid=".$pids."&error=".$error_description);
            exit;
       }
       $signatureValid = verifyGatewaySignature($signature, $checkoutId, $amount);
       
       if (!$signatureValid) {
            header("Location:failure_pay_listing_fee.php?pid=".$pids."&error=bad signature");
            exit;
       }


}

    
 

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

$tx_token   = $_GET['transaction'];





$productListingFeeNew = round($productListingFee * $prdCount);

if($flag_to_continue && ($amount_compare == $productListingFeeNew))
{
   
    $sql = "UPDATE ".$tableprefix."products
               SET payment_status = 'COMPLETED'
             WHERE product_id IN (". mysql_real_escape_string($pids) .")
               AND product_artist_id = " . $userid;
        $resultsP = mysql_query($sql) or die(mysql_error());

        $pids = explode(',', $pids);
        if(!empty($pids))
        {
            foreach($pids as $id)
            {
                    $sql = "INSERT INTO ".$tableprefix."product_listing_fee_payment
                                    SET payment_status = 'COMPLETED',
                                        product_id = '". mysql_real_escape_string($id) ."',
                                        total_amount = '". mysql_real_escape_string($productListingFee) ."',
                                        amount_paid= '". mysql_real_escape_string($productListingFee) ."',
                                        payment_method = 'DWOLLA'";
                    $resultsP = mysql_query($sql) or die(mysql_error());
            }
        }


        header("Location:myproducts.php?message=".MSG_PAYMNT_COMP_SUC);
}else{

						header("Location:failure_pay_listing_fee.php?pid=".$pids);
						/*
					   echo "<script>alert(\"Invalid request.Please try again later\");</script>";
					   echo "<script>location.href='index.php';</script>";*/
					   exit;


}