<?php
/*********************************************************
Basic IPN script to be used  on your server to receive IPN POSTed (back) 
from AsianPay payments.

This script is provided "as is" with NO WARRANTY OF ANY KIND, INCLUDING
THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE


**********************************************************/

include("../../../includes/ini.inc.php");
include("../../../includes/global.inc.php");
require_once("../../../classes/db.inc.php");
$db = new db();
include_once("../../../includes/functions.inc.php");
$config = fetchDbConfig("config");
include_once("../../../language/".$config['defaultLang']."/lang.inc.php");
include("../../../includes/currencyVars.inc.php");

$module = fetchDbConfig("AsianPay");

$secret_code = "a3219edea91c414172fbccc407895262";
$asianpayid = "payments@asianpay.com";

// Get the IP address of the server calling your script

$IP = $_SERVER['REMOTE_ADDR'];


// Verify which is the kind of transaction ( Buy Now - Subscriptions - Donation)
$TransactionType = $_POST['type'];

/*********************************************

For Buy Now Single Or Advanced and Shopping Cart

**********************************************/

//IPN for Buy Now Buttons
if($TransactionType=="Purchase")
{



// Collect and assign variables to all the variables sent from AsianPay IPn POSTed (back).


$merchant_send = $_POST['senderid'];  //  Unique AsianPay ID for the sender
$merchant_receive = $_POST['receiverid']; // Unique AsianPay ID for the receiver
$payer_name = $_POST['sendername']; // Full name ( business anme ) of the sender
$payee_name = $_POST['receivername'];// Full name ( business name) of the receiver


$amount = $_POST['total']; // gross amount of this transaction
$total_fees = $_POST['total_fees']; // amount of fees charged to this transactins

$net_amount = $amount - $total_fees; // Generate the amount that has been credited to the receiver AsianPay account

/*******************************************************************
If you are using our advanced Buy Now buttons you maybe need to receive also these fields
*/

$prod_price = $_POST['prod_price']; // product price 
$ship_price = $_POST['ship_price'];  // shipping costs if any
$tax = $_POST['tax'];                     // tax amount if any

/***********************/

// Unique product ID generated from AsianPay when you created the code or from you if using the customized form ( prod_code)
$itemid = $_POST['productid']; 

$itemcode = $_POST['prodcode'];  // received only if using the standard form. not present in the advanced form.
$itemname = $_POST['prodname']; // Name/Title of the product as settled from you in your form
$payee_email = $_POST['paidto'];  // receiver email as registered in asianpay database
$payer_email = $_POST['paidby'];  //sender email as registered in AsianPay database
$TransactionID = $_POST['batch'];  // unique transaction ID generated from AsianPay

 // member status ( unverified or certified ) - You can use this information to diversify your actions related to this transaction
$Member = $_POST['memberstatus']; 

$TransactionStatus = $_POST['status'];  // transaction status:  0 = pending  1 = complete
$TransactionTime = $_POST['time'];   // timer the transaction occurred
$secret = $_POST['secretcode'];  // Your secret code as registered in Asianpay database.

/**********************************************
If the IPn is coming from the shopping cart you can use the above for your cart order id and the list_item to get the list of the products
**********************************************/
if (isset($_POST['cart_order_id'])) {
$cart_order_id = $_POST['cart_order_id']; }

if (isset($_POST['list_item'])) {
$list_item = $_POST['list_item']; }

/// We break down the list of items in the email message. Yu can do our own choice to fit your database needs.

/************************************************
All your custom fields are collected if present in the POST
************************************************/

if (isset($_POST['custom_1'])) {
$custom_1 = $_POST['custom_1']; }

if (isset($_POST['custom_2'])) {
$custom_2 = $_POST['custom_2']; }

if (isset($_POST['custom_3'])) {
$custom_3 = $_POST['custom_3']; }

if (isset($_POST['custom_4'])) {
$custom_4 = $_POST['custom_4']; }

if (isset($_POST['custom_5'])) {
$custom_5 = $_POST['custom_5']; }

if (isset($_POST['custom_6'])) {
$custom_6 = $_POST['custom_6']; }

if (isset($_POST['custom_7'])) {
$custom_7 = $_POST['custom_7']; }

if (isset($_POST['custom_8'])) {
$custom_8 = $_POST['custom_8']; }

if (isset($_POST['custom_9'])) {
$custom_9 = $_POST['custom_9']; }

if (isset($_POST['custom_10'])) {
$custom_10 = $_POST['custom_10']; }


//make all the check you need to verify that the transaction is correct
// but at least the followings
// Verify Secret Code
// Verify that the receiver email ( your email ) is the correct one

if(($secret==$secret_code)&&($payee_email==$asianpayid))
{


// At this point you can also verify if the transaction is complete or pending and if the member is certified or not
$summary = $db->select("SELECT prod_total, comments FROM ".$glob['dbprefix']."CubeCart_order_sum WHERE cart_order_id = "
.$db->mySQLsafe($_POST['productid']));

if($_POST['status']!=="1"){
				$fail = TRUE;

$ppComment = "";
				
				switch($_POST['status']){
					
					case "0":
					$ppComment = "Pending - The payment is pending";
					break;
					
					default:
					$ppComment = "Unspecified Error.";
					break;
					
				}
				
				if(empty($summary[0]['comment'])){
					$updateComment['comments'] = "'AsianPay Comment: ".$ppComment."'";
				} else {
					$updateComment['comments'] = "'".$summary[0]['comments']."\r\n\r\nAsianPay Comment: ".$ppComment."'";
				}
				$update = $db->update($glob['dbprefix']."CubeCart_order_sum", $updateComment,"cart_order_id=".$db->mySQLSafe($_POST['productid']));
				
			}

// check that transaction has not been previously processed
			$Trans_ID = $db->select("SELECT * FROM ".$glob['dbprefix']."CubeCart_order_sum WHERE sec_order_id = ".$db->mySQLsafe($_POST['batch']));
			
			
			if($Trans_ID == TRUE){
				
				$fail = TRUE;
				
			} else {
				
				$updateOrderTxn['sec_order_id'] = $db->mySQLSafe($_POST['batch']);
				
				$update = $db->update($glob['dbprefix']."CubeCart_order_sum", $updateOrderTxn,"cart_order_id=".$db->mySQLSafe($_POST['productid']));
			
			}

if($fail==FALSE){
				$cart_order_id = $_POST['productid'];
				include("../../../includes/orderSuccess.inc.php");


}
}
else
{

// if the transaction is fraudolent or not correct do something to notify yourself of the problem



}
?>