<?php
/*
 * ipn_success.php
 *
 * PHP Toolkit for PayPal v0.51
 * http://www.paypal.com/pdn
 *
 * Copyright (c) 2004 PayPal Inc
 *
 * Released under Common Public License 1.0
 * http://opensource.org/licenses/cpl.php
 *
 */
//include file - not accessible directly
chdir('../../');
include("includes/myconnect.php");
require("includes/currencies.php");
$currencies = new currencies();

include_once('paypal_standard/includes/global_config.inc.php'); 
create_csv_file("uploadedimages/ipn_success.txt",$_REQUEST);

if (isset($_REQUEST['invoice']) && is_numeric($_REQUEST['invoice']) && ($_REQUEST['invoice'] > 0)) {
//log successful transaction to file or database
//$_REQUEST['invoice'] = 30;
//$_REQUEST["custom"] = 2;
//$_REQUEST["payment_status"] = "Completed";
//$_REQUEST['mc_gross'] = 51;
//$_REQUEST['mc_currency'] = "MYR";

$orderID = $_REQUEST['invoice'];
$userid = $_REQUEST["custom"];
$total = $_REQUEST['mc_gross'];

$invoiceID = str_pad($orderID, 4, "0", STR_PAD_LEFT);

$order_query = mysql_query("select orders_status, currency, currency_value from dmc_orders where orders_id = $orderID and customers_id = $userid limit 1");
$order_total = mysql_num_rows($order_query);
if ($order_total > 0) {
$order=mysql_fetch_object($order_query);

$currency = $order->currency;
$currency_value = $order->currency_value;

$total_query = mysql_query("select value from dmc_orders_total where orders_id = '$orderID' and class = 'ot_total' limit 1");
$total_value = mysql_fetch_object($total_query);

$level_query = mysql_query("select * from dmc_referrer where ref_mem_id = '$userid'") or die(mysql_error());
$level = mysql_fetch_array($level_query);

$comment = $_REQUEST["payment_status"] . " (" . ucwords($_REQUEST['payer_status']) . "; Transaction ID: " .$_REQUEST['txn_id'] . "; " . $currencies->format_ceil($_REQUEST['mc_gross'], false, $currency) . ")";

if ($_REQUEST["payment_status"]=="Completed") {
$order_status_id = 6;

if ($_REQUEST['item_name']=="Business Card") {
$update2 =  mysql_query("Update dmc_business set period=365 where buss_id=$orderID") or die(mysql_error());
}

$insert1 = mysql_query("insert into dmc_transactions (uid,amount,description,date_submitted,currency,currency_value) values ($userid,$total,'Money added through paypal','".date("YmdHis",time())."', '$currency', '$currency_value')") or die(mysql_error());

$insert2 = mysql_query("insert into dmc_transactions (uid,amount,description,date_submitted,currency,currency_value) values ($userid,-$total,'Paid for Invoice $invoiceID','".date("YmdHis",time())."', '$currency', '$currency_value')") or die(mysql_error());

if ($level['first_lvl']!=0) {
$fisrtCom = $currencies->price_Xcode($total * 0.1);
$insert3 = mysql_query("insert into dmc_transactions (uid,amount,description,date_submitted,commision,currency,currency_value) values (".$level['first_lvl'].",$fisrtCom,'Commision for Invoice $invoiceID','".date("YmdHis",time())."',$userid, '$currency', '$currency_value')") or die(mysql_error());
}

if ($level['second_lvl']!=0) {
$secondCom = $currencies->price_Xcode($fisrtCom * 0.1);
$insert4 = mysql_query("insert into dmc_transactions (uid,amount,description,date_submitted,commision,currency,currency_value) values (".$level['second_lvl'].",$secondCom,'Commision for Invoice $invoiceID','".date("YmdHis",time())."',$userid, '$currency', '$currency_value')") or die(mysql_error());
}

}

if ($_REQUEST["payment_status"]=="Pending"){
$order_status_id = 5;
$comment .= "; " . $_REQUEST['pending_reason'];
} elseif (($_REQUEST['payment_status'] == 'Reversed') || ($_REQUEST['payment_status'] == 'Refunded')) {
if ($total < 0) {
$total = abs($total);
}
$order_status_id = 5;
$comment .= "; " . $_REQUEST['reason_code'];
$insert1 = mysql_query("insert into dmc_transactions (uid,amount,description,date_submitted,currency,currency_value) values ($userid,$total,'".ucwords($_REQUEST['reason_code'])." for Invoice $invoiceID','".date("YmdHis",time())."', '$currency', '$currency_value')") or die(mysql_error());

$insert2 = mysql_query("insert into dmc_transactions (uid,amount,description,date_submitted,currency,currency_value) values ($userid,-$total,'Money ".$_REQUEST['reason_code']." through paypal','".date("YmdHis",time())."', '$currency', '$currency_value')") or die(mysql_error());

if ($level['first_lvl']!=0) {
$fisrtCom = $currencies->price_Xcode($total * (COM_FIRST_LVL/100));
$insert3 = mysql_query("insert into dmc_transactions (uid,amount,description,date_submitted,commision,currency,currency_value) values (".$level['first_lvl'].",-$fisrtCom,'Commision ".$_REQUEST['reason_code']." for Invoice $invoiceID','".date("YmdHis",time())."',$userid, '$currency', '$currency_value')") or die(mysql_error());
}

if ($level['second_lvl']!=0) {
$secondCom = $currencies->price_Xcode($fisrtCom * (COM_SCND_LVL/100));
$insert4 = mysql_query("insert into dmc_transactions (uid,amount,description,date_submitted,commision,currency,currency_value) values (".$level['second_lvl'].",-$secondCom,'Commision ".$_REQUEST['reason_code']." for Invoice $invoiceID','".date("YmdHis",time())."',$userid, '$currency', '$currency_value')") or die(mysql_error());
}
}

if ($total != $currencies->format_Xcode_ceil($total_value->value,true,$currency,$currency_value)) {
$order_status_id = 5;
$comment .= '; PayPal transaction value (' . $total . ') does not match order value (' . $currencies->format_Xcode_ceil($total_value->value,true,$currency,$currency_value) . ')';
}

$update =  mysql_query("Update dmc_orders set orders_status=$order_status_id, last_modified = now() where orders_id=$orderID") or die(mysql_error());

$sql5="INSERT INTO dmc_orders_status_history (orders_id, orders_status_id, date_added, customer_notified, comments) values ('$orderID', '$order_status_id', now(), '0','PayPal IPN Verified [$comment]')";
$myquery5 = mysql_query($sql5) or die(mysql_error());

 }
}
else
{
	die('This page is not directly accessible');
}
?>