<?php

require_once 'includes/master.inc.php';
// check for some required variables in the request
if (!isset($_REQUEST['payment_status']) || !isset($_REQUEST['business'])) {
    die;
}
// make sure payment has completed and it's for the correct PayPal account
if ($_REQUEST['payment_status'] == "Completed" && strtolower($_REQUEST['business']) == SITE_CONFIG_PAYPAL_PAYMENTS_EMAIL_ADDRESS) {
    // load order using custom payment tracker hash
    $paymentTracker = $_REQUEST['custom'];
    $order = OrderPeer::loadByPaymentTracker($paymentTracker);
    if ($order) {
        $extendedDays = $order->days;
        $userId = $order->user_id;
        // log in payment_log
        $paypal_vars = "";
        foreach ($_REQUEST as $k => $v) {
            $paypal_vars .= $k . " => " . $v . "\n";
        }
        $dbInsert = new DBObject("payment_log", array("user_id", "date_created", "amount", "currency_code", "from_email", "to_email", "description", "request_log"));
        $dbInsert->user_id = $userId;
        $dbInsert->date_created = date("Y-m-d H:i:s", time());
        $dbInsert->amount = $_REQUEST['mc_gross'];
        $dbInsert->currency_code = $_REQUEST['mc_currency'];
        $dbInsert->from_email = $_REQUEST['payer_email'];
        $dbInsert->to_email = $_REQUEST['business'];
        $dbInsert->description = $extendedDays . ' days extension';
        $dbInsert->request_log = $paypal_vars;
        $dbInsert->insert();
        // make sure the amount paid matched what we expect