<?php
/**
 * Copyright (C) 2006 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Please refer to the Google Checkout PHP Sample Code Documentation
 * for requirements and guidelines on how to use the sample code.
 * 
 * This sample code is a simple listener for the responses that the 
 * Google Checkout server sends to the merchants. When a response is 
 * received, it logs the message to a local file and processes the message.
 *
 */

include "../../../config.inc.php";
$this_config = $plugin_config['payment']['google_checkout'];

require_once('gCheckout.php');

if (!isset($_SERVER['PHP_AUTH_USER'])) { // HTTP Authentication
    header('WWW-Authenticate: Basic realm="aMember Google Checkout"');
    header('HTTP/1.0 401 Unauthorized');
    $err = "Error. Google Checkout ResponseHandler - username is not entered";
    $db->log_error($err);
    exit();
}

// checking name and password
if( ($_SERVER['PHP_AUTH_USER'] != $this_config["merchant_id"])  
 || ($_SERVER['PHP_AUTH_PW']  != $this_config["merchant_key"])){
    header('WWW-Authenticate: Basic realm="aMember Google Checkout"');
    header('HTTP/1.0 401 Unauthorized');
    $err = "Error. Google Checkout ResponseHandler - Incorrect username or password entered";
    $db->log_error($err . " (".$_SERVER['PHP_AUTH_USER'].":".$_SERVER['PHP_AUTH_PW'].")");
    exit();
}


$checkout_xml_schema = 'http://checkout.google.com/schema/2';
$gCheckout = new gCheckout($this_config["merchant_id"], $this_config["merchant_key"], $cart_expires = '', 
                           $checkout_xml_schema, $this_config["currency"], $this_config["debug"]);


// Retrieve the XML sent in the HTTP POST request to the ResponseHandler
$xml_response = $HTTP_RAW_POST_DATA;

// Get rid of PHP's magical escaping of quotes 
if (get_magic_quotes_gpc()) {
    $xml_response = stripslashes($xml_response);
}

// Log the XML received in the HTTP POST request
//$gCheckout->LogMessage($xml_response, $debug_only_msg = true);

/*
 * Call the ProcessXmlData function. The ProcessXmlData will route 
 * the XML data to the function that handles the particular type
 * of XML message contained in the POST request.
 */
$gCheckout->ProcessXmlData($xml_response);

/// resend postback if necessary
if ($this_config['resend_postback'])
    foreach (preg_split('/\s+/', $this_config['resend_postback']) as $url){
        $url = trim($url);
        $purl = parse_url($url);
        if ($url == '' || $_SERVER['PHP_SELF'] == $purl['path']) continue;
        $response = $gCheckout->GetCurlResponse($xml_response, $url, $this_config['merchant_id'], $this_config['merchant_key'], $add_headers = true);
        $logfile = '';
        $gCheckout->LogMessage("Google Checkout Resend Postback to: " . $url, $debug_only_msg = false);
    }

?>