<?php
require_once 'modules/admin/models/GatewayPlugin.php';

/**
* @package Plugins
*/
class PluginGooglecheckout extends GatewayPlugin
{

    function getVariables() 
    {
    	/* Specification
    		  itemkey     - used to identify variable in your other functions
    		  type 		  - text,textarea,yesno,password
    		  description - description of the variable, displayed in ClientExec
    	*/
    
        $variables = array (
                    /*T*/"Plugin Name"/*/T*/ => array (
    			   						"type"          =>"hidden",
    									"description"   =>/*T*/"How CE sees this plugin (not to be confused with the Signup Name)"/*/T*/,
    									"value"         =>/*T*/"Google Checkout"/*/T*/
    								   ),
                    /*T*/"In Signup"/*/T*/ => array (
    			   						"type"          =>"yesno",
    									"description"   =>/*T*/"Select YES if you want to include this payment processor in the signup script."/*/T*/,
    									"value"         =>"0"
    								   ),
    	            /*T*/"One-Time Payments"/*/T*/ => array (
                                        "type"          =>"yesno",
                                        "description"   =>/*T*/"Select YES if you want to allow customers to make one-time payments through this payment processor."/*/T*/,
                                        "value"         =>"0"
                                       ),
                    /*T*/"Merchant ID"/*/T*/ => array (
    			   						"type"          =>"text",
    									"description"   =>/*T*/"ID used to identify you to Google Checkout.<br>NOTE: This ID is required if you have selected Google Checkout as a payment gateway for any of your clients."/*/T*/,
    									"value"         =>""
    								   ),
                    /*T*/"Merchant Key"/*/T*/ => array (
    			   						"type"          =>"text",
    									"description"   =>/*T*/"key used to identify you to Google Checkout.<br>NOTE: This key is required if you have selected Google Checkout as a payment gateway for any of your clients."/*/T*/,
    									"value"         =>""
    								   ),
                    /*T*/"Google Checkout Sandbox"/*/T*/ => array (
    				                    "type"          =>"yesno",
    				                    "description"   =>/*T*/"Select YES if you want to set Google Checkout into Sandbox for testing. (<b>Note:</b> You must set to NO before accepting actual payments through this processor.)<p><p><b>Important:</b> Before using this plugin you must set the following on your Google Checkout Account, even if it is the Sandbox Account:<p><li>On <i><u>Settings -> Integration -> API callback URL</u></i> set the full Callback URL stated above.(<b>Note: </b>If you are going to use the Production Account, you must have installed an HTTPS connection secured by 128-bit SSL v3 or TLS connection, and your Callback URL must start with <b>https</b> instead of http</li><li>On <i><u>Settings -> Preferences -> Order processing preferences</u></i> select the option <i>Automatically authorize and charge the buyer's credit card.</i></li><li>On <i><u>Settings -> Profile ->  Public business website</u></i> write the full Callback URL stated above.</li>"/*/T*/,
    				                    "value"         =>"0"
    				                    ),
                    /*T*/"Signup Name"/*/T*/ => array (
    			   						"type"          =>"text",
    									"description"   =>/*T*/"Select the name to display in the signup process for this payment type. Example: eCheck or Credit Card."/*/T*/,
    									"value"         =>/*T*/"Google Checkout"/*/T*/
    								   ),
                  /*T*/"Payment Description"/*/T*/ => array (
                                        "type"          =>"text",
                                        "description"   =>/*T*/"(Optional) Enter the description for this payment processor to be shown to customers. <br /><b>Example:</b> Save time.  Checkout securely.&lt;br /&gt;&amp;nbsp; Pay without sharing your financial information."/*/T*/,
                                        "value"         =>""
                                       ),
                    /*T*/"Auto Payment"/*/T*/ => array (
    			   						"type"          =>"hidden",
    									"description"   =>/*T*/"No description"/*/T*/,
    									"value"         =>"0"
    								   ),
                   /*T*/"Check CVV2"/*/T*/ => array (
                                        "type"          =>"hidden",
                                        "description"   =>/*T*/"Select YES if you want to accept CVV2 for this plugin."/*/T*/,
                                        "value"         =>"0"
                                       )
    
        );
    	return $variables;
    }
    
    function singlepayment($params) 
    {
    	require_once 'plugins/gateways/googlecheckout/googlefunctions.php';
        require_once 'newedge/classes/NE_Network.php';
        
        // EWW
        $GLOBALS['logger'] =& $this->logger;
    
    	$tInvoice = new Invoice($this->user, $params["invoiceNumber"]);
    	$merchantId = trim($params["plugin_googlecheckout_Merchant ID"]);
    	$merchantKey = trim($params["plugin_googlecheckout_Merchant Key"]);
    	$GLOBALS["merchant_key"] = $merchantKey;
    	$schemaUrl = "http://checkout.google.com/schema/2";
    	
    	// Specify item data and create an item to include in the order
    	$item_name = $tInvoice->getDescription();
    	$item_description = $tInvoice->getDescription();
    	$quantity = "1";
    	
    	//  REPLACED WITH THE LINE BELOW  //
    	//$unit_price = $tInvoice->getPrice();
    	//  REPLACED WITH THE LINE BELOW  //
    	
    	//  ADDED FOR: VARIABLE_PAYMENTS - Balance Due  //
    	$unit_price = $tInvoice->getBalanceDue();
    	//  ADDED FOR: VARIABLE_PAYMENTS - Balance Due  //
    	
    	$merchant_private_item_data = $params["invoiceNumber"];
    	
    	$currency = $params['currencytype'];
    	if(!in_array($currency, array("GBP", "USD"))){
    	    $currency = "USD";
    	}
    	
    	$xmlCart = "	
    					<?xml version=\"1.0\" encoding=\"UTF-8\"?>
    					<checkout-shopping-cart xmlns = \"$schemaUrl\">
    							<shopping-cart>
    							      <items>
    							          <item>
    							              <item-name>$item_name</item-name>
    							              <item-description>$item_description</item-description>
    							              <quantity>$quantity</quantity>
    							              <unit-price currency=\"$currency\">$unit_price</unit-price>
    							              <merchant-private-item-data>
    							                <item-note>$merchant_private_item_data</item-note>
    							            </merchant-private-item-data>
    							          </item>				          
    							      </items>
    							</shopping-cart>
    							<checkout-flow-support>
    									<merchant-checkout-flow-support/>
    								</checkout-flow-support>
    					</checkout-shopping-cart>";
    	
    	$signature = CalcHmacSha1($xmlCart);
    
    	$b64_cart = base64_encode($xmlCart);
    	$b64_signature = base64_encode($signature);
    		
    	// Start building the URL that will be used to send customers to GC for payment.
    	if($params["plugin_googlecheckout_Google Checkout Sandbox"] == '1'){
    		
    		$base_url = "https://sandbox.google.com/checkout/cws/v2/Merchant/";
    	}
    	else{
    		$base_url = "https://checkout.google.com/cws/v2/Merchant/";
    	}
    		
    	$base_url .= $merchantId; 
    	$checkoutUrl = $base_url . "/checkout";
        $checkoutDiagnoseUrl = $base_url . "/checkout/diagnose";
        $requestUrl = $base_url . "/request";
        $requestDiagnoseUrl = $base_url . "/request/diagnose";
        
        //http Autentication
    	$merchantIdKey = $merchantId.':'.$merchantKey;
    	$b64_merchantIdKey = base64_encode($merchantIdKey);
    	$httpAutentication = array("Authorization: Basic $b64_merchantIdKey", 'Content-Type: application/xml', 'Accept: application/xml');

      try{
          $transmit_response = NE_Network::curlRequest($this->settings, $requestUrl, $xmlCart, $httpAutentication, true, false);
      }catch(Exception $ex){
          $this->logger->log(2, "PluginGooglecheckout exception on curlRequest: \n ".$ex->getMessage());
      }
      
      if(is_a($transmit_response, 'NE_Error')){
          $this->logger->log(2, "PluginGooglecheckout error response from curlRequest: \n ".$transmit_response->getMessage());
      }else{
          //$this->logger->log(2, "PluginGooglecheckout response from curlRequest: \n ".print_r($transmit_response, true));
      }
      
    	ProcessXmlData($transmit_response, $params);
    }
}
?>
