<?php
// Include the configuration file
require_once 'config.php';

// Include the database connection file
include_once 'dbConnect.php';

// Include the PayPal API library
require_once 'PaypalCheckout.class.php';
$paypal = new PaypalCheckout;

$response = array('status' => 0, 'msg' => 'Transaction Failed!');
//$_POST['order_id'] = "16592635Y99164746";
//$_POST['paypal_order_check'] = 1;
if(!empty($_POST['paypal_order_check']) && !empty($_POST['order_id'])){
    // Validate and get order details with PayPal API
    try { 
        $order = $paypal->validate($_POST['order_id']);
		//echo "<pre>";
		//print_r($order);
//		$purchase_unit = $order['purchase_units'][0];
//		$amount_value = $purchase_unit['amount']['value'];
//		$cents = number_format(($amount_value * 100),0,"","");
//		echo (int)$cents;
//		exit;
    } catch(Exception $e) { 
        $api_error = $e->getMessage(); 
    }
	
	if(!empty($order)){
		$order_id = $order['id'];
		$intent = $order['intent'];
		$order_status = $order['status'];
        $order_time = date("Y-m-d H:i:s", strtotime($order['create_time']));

        if(!empty($order['purchase_units'][0])){
			$purchase_unit = $order['purchase_units'][0];

			$item_number = $purchase_unit['custom_id'];
			$pattern = ",";
			$pieces = explode($pattern,$item_number, 3);
			// 3 is how many custom fields there are
			$custom1 = $pieces[0]; // Member User ID
			$custom2 = $pieces[1]; // Old Challenge ID or Total Votes
			$custom3 = $pieces[2]; // Member Email
			
			// https://stackoverflow.com/questions/44275586/how-to-send-multiple-variables-through-paypal-ipn
			// https://stackoverflow.com/questions/11631926/paypal-ipn-process-more-than-one-custom-variable
            $item_name = $purchase_unit['description'];
            
			if(!empty($purchase_unit['amount'])){
				$currency_code = $purchase_unit['amount']['currency_code'];
				$amount_value = $purchase_unit['amount']['value']; // replaced $itemPrice
				$amountCents = number_format(($amount_value * 100),0,"","");
			}

			if(!empty($purchase_unit['payments']['captures'][0])){
				$payment_capture = $purchase_unit['payments']['captures'][0];
				$transaction_id = $payment_capture['id'];
				$payment_status = $payment_capture['status'];
			}

            if(!empty($purchase_unit['payee'])){
                $payee = $purchase_unit['payee'];
                $payee_email_address = $payee['email_address'];
                $merchant_id = $payee['merchant_id'];
            }
		}

        $payment_source = '';
		if(!empty($order['payment_source'])){
            foreach($order['payment_source'] as $key=>$value){
                $payment_source = $key;
            }
		}

		if(!empty($order['payer'])){
			$payer = $order['payer'];
			$payer_id = $payer['payer_id'];
			$payer_name = $payer['name'];
			$payer_given_name = !empty($payer_name['given_name'])?$payer_name['given_name']:'';
			$payer_surname = !empty($payer_name['surname'])?$payer_name['surname']:'';
            $payer_full_name = trim($payer_given_name.' '.$payer_surname);
            $payer_full_name = htmlspecialchars($payer_full_name);

			$payer_email_address = $payer['email_address'];
			$payer_address = $payer['address'];
			$payer_country_code = !empty($payer_address['country_code'])?$payer_address['country_code']:'';
		} else {
			$payer_full_name = $order['payment_source']['apple_pay']['name'];
		}

		if (!empty($order_id) && $order_status == 'COMPLETED') {
			// Insert transaction data into the database
			//$vt_product = "CrowdKodeCredits"; // was EntryPass
//			$custom1 = "4";
//			$custom2 = "300";
//			$custom3 = "sillystats@gmail.com";
//			$amount_value = 1;
			//$sqlQ2 = "INSERT INTO project_cofund (user_id, project_id, comment, rating, prevamt, escrowPrev, escrowAmt) VALUES (?,?,?,?,?,?,?)";
			//$stmt2 = $db->prepare($sqlQ2);
			//$stmt2->bind_param("sssssss", $custom1, $custom2, $vt_product, $amountCents, $amountCents, $amountCents, $amountCents);
			//$insert2 = $stmt2->execute();
			
			// INCREMENT THE chip_ins by + 1, which is Total Players
			//$sqlQ3 =  "UPDATE projects SET chip_ins=chip_ins+1 WHERE id = $custom2";
			//$db->query($sqlQ3);
//			if ($db->query($sqlQ3)) {
//				return true;
//			} else {
//				return false;
//			}
			$rewards = $custom2 * 10;
			// UPDATE THE USER'S CREDITS/VOTES BALANCE
			$sqlQ3 =  "UPDATE users SET credits=credits+$custom2, reward=reward+$rewards WHERE id = $custom1";
			$db->query($sqlQ3);
//			if ($db->query($sqlQ3)) {
//				return true;
//			} else {
//				return false;
//			}
			
            // Check if any transaction data exists already with the same TXN ID
            $sqlQ = "SELECT id FROM transactions WHERE transaction_id = ?";
            $stmt = $db->prepare($sqlQ); 
            $stmt->bind_param("s", $transaction_id);
            $stmt->execute();
            $stmt->bind_result($row_id);
            $stmt->fetch();
            
            $payment_id = 0;
            if(!empty($row_id)){
                $payment_id = $row_id;
            }else{
                // Insert transaction data into the database
				$sqlQ = "INSERT INTO transactions (item_number,memID,chID,memEmail,item_name,item_price,item_price_currency,payer_id,payer_name,payer_email,payer_country,merchant_id,merchant_email,order_id,transaction_id,paid_amount,paid_amount_currency,payment_source,payment_status,created,modified) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())";
                $stmt = $db->prepare($sqlQ);
				$stmt->bind_param("sssssdsssssssssdssss", $item_number, $custom1, $custom2, $custom3, $item_name, $amount_value, $currency, $payer_id, $payer_full_name, $payer_email_address, $payer_country_code, $merchant_id, $payee_email_address, $order_id, $transaction_id, $amount_value, $currency_code, $payment_source, $payment_status, $order_time);
                $insert = $stmt->execute();
                
                if($insert){
                    $payment_id = $stmt->insert_id;
                }
            }

			if (!empty($payment_id)) {
				//////////////// ORIGINAL CODE ////////////////
                $ref_id_enc = base64_encode($transaction_id);
                $response = array('status' => 1, 'msg' => 'Transaction Completed!', 'ref_id' => $ref_id_enc);
            }
		}
	}else{
        $response['msg'] = $api_error;
    }
}
echo json_encode($response);