PHP Code:
<?php
error_reporting(E_ALL);
// temporal disable of multisocket
define('MODULE_PAYMENT_GOOGLECHECKOUT_MULTISOCKET', 'False');
define('GC_STATE_NEW', 100);
define('GC_STATE_PROCESSING', 101);
define('GC_STATE_DIGITAL_PROCESSED', 103);
define('GC_STATE_SHIPPED', 105);
define('GC_STATE_REFUNDED', 107);
define('GC_STATE_SHIPPED_REFUNDED', 109);
define('GC_STATE_CANCELED', 111);
chdir('./..');
$curr_dir = getcwd();
define('API_CALLBACK_ERROR_LOG', $curr_dir . "/googlecheckout/logs/response_error.log");
define('API_CALLBACK_MESSAGE_LOG', $curr_dir . "/googlecheckout/logs/response_message.log");
require_once ($curr_dir . '/googlecheckout/library/googlemerchantcalculations.php');
require_once ($curr_dir . '/googlecheckout/library/googleresult.php');
require_once ($curr_dir . '/googlecheckout/library/googlerequest.php');
require_once ($curr_dir . '/googlecheckout/library/googleresponse.php');
$Gresponse = new GoogleResponse();
//Setup the log files
$Gresponse->SetLogFiles(API_CALLBACK_ERROR_LOG, API_CALLBACK_MESSAGE_LOG, L_ALL);
// Retrieve the XML sent in the HTTP POST request to the ResponseHandler
$xml_response = isset($HTTP_RAW_POST_DATA)?
$HTTP_RAW_POST_DATA:file_get_contents("php://input");
if (get_magic_quotes_gpc()) {
$xml_response = stripslashes($xml_response);
}
list ($root, $data) = $Gresponse->GetParsedXML($xml_response);
if (isset ($data[$root]['shopping-cart']['merchant-private-data']['session-data']['VALUE'])) {
list ($sess_id, $sess_name) =
explode(";", $data[$root]['shopping-cart']['merchant-private-data']['session-data']['VALUE']);
//If session management is supported by this PHP version
if (function_exists('session_id'))
session_id($sess_id);
if (function_exists('session_name'))
session_name($sess_name);
}
include ('includes/application_top.php');
include ('includes/modules/payment/googlecheckout.php');
//BOF - define value for languages_id//define home page - added by colosports
$attributes = $db->Execute("select languages_id
from " . TABLE_LANGUAGES . "
where name = '" . $_SESSION['language'] . "'
");
$languages_id = $attributes->fields['languages_id'];
//EOF - define value for languages_id//define home page - added by colosports
// zen_session_start();
if (isset ($_SESSION['cart']) && is_object($_SESSION['cart'])) {
$cart = $_SESSION['cart'];
$cart->restore_contents();
} else {
$Gresponse->SendServerErrorStatus("Shopping cart not obtained from session.");
}
$googlepayment = new googlecheckout();
$Gresponse->SetMerchantAuthentication($googlepayment->merchantid,
$googlepayment->merchantkey);
// Check if is CGI install, if so .htaccess is needed
if (MODULE_PAYMENT_GOOGLECHECKOUT_CGI != 'True') {
$Gresponse->HttpAuthentication();
}
switch ($root) {
case "request-received": {
process_request_received_response($Gresponse);
break;
}
case "error": {
process_error_response($Gresponse);
break;
}
case "diagnosis": {
process_diagnosis_response($Gresponse);
break;
}
case "checkout-redirect": {
process_checkout_redirect($Gresponse);
break;
}
case "merchant-calculation-callback" :
{
// if (MODULE_PAYMENT_GOOGLECHECKOUT_MULTISOCKET == 'True') {
// include_once ($curr_dir . '/googlecheckout/multisocket.php');
// process_merchant_calculation_callback($Gresponse, 2.7, false);
// break;
// }
// }
// case "merchant-calculation-callback-single" :
// {
// set_time_limit(5);
process_merchant_calculation_callback_single($Gresponse);
break;
}
case "new-order-notification" :
{
// $zco_notifier->notify('NOTIFY_CHECKOUT_PROCESS_BEGIN');
/*
* 1. check if the users email exists
* 1.a if not, create the user, and log in
* 2. Check if exists as a GC user
* 2.aAdd it the the google_checkout table to match buyer_id customer_id
*
* 2. add the order to the logged user
*
*/
// Check if the order was already processed
$google_order = $db->Execute("select orders_id ".
" from " . $googlepayment->table_order . " " .
" where google_order_number = " .
$data[$root]['google-order-number']['VALUE'] );
if($google_order->RecordCount() != 0) {
// Order already processed, send ACK http 200 to avoid notification resend
$Gresponse->log->logError(sprintf(GOOGLECHECKOUT_ERR_DUPLICATED_ORDER,
$data[$root]['google-order-number']['VALUE'],
$google_order->fields['orders_id']));
$Gresponse->SendAck();
}
// Check if the email exists
$customer_exists = $db->Execute("select customers_id from " .
TABLE_CUSTOMERS . " where customers_email_address = '" .
makeSqlString($data[$root]['buyer-billing-address']['email']['VALUE']) . "'");
// Check if the GC buyer id exists
$customer_info = $db->Execute("select gct.customers_id from " .
$googlepayment->table_name . " gct " .
" inner join " .TABLE_CUSTOMERS . " tc on gct.customers_id = tc.customers_id ".
" where gct.buyer_id = " .
makeSqlString($data[$root]['buyer-id']['VALUE']));
$new_user = false;
// Ignore session to avoid mix of Cart-GC sessions/emails
// GC email is the most important one
// if ((isset($_SESSION['customer_id']) && $_SESSION['customer_id'] != '')
// || $customer_exists->RecordCount() != 0) {
if ($customer_exists->RecordCount() != 0) {
$_SESSION['customer_id'] = $customer_exists->fields['customers_id'];
}
else if($customer_info->RecordCount() != 0){
$_SESSION['customer_id'] = $customer_info->fields['customers_id'];
}
else {
list ($firstname, $lastname) =
explode(' ', makeSqlString($data[$root]['buyer-billing-address']['contact-name']['VALUE']), 2);
$sql_data_array = array (
'customers_firstname' => $firstname,
'customers_lastname' => $lastname,
'customers_email_address' => $data[$root]['buyer-billing-address']['email']['VALUE'],
'customers_nick' => '',
'customers_telephone' => $data[$root]['buyer-billing-address']['phone']['VALUE'],
'customers_fax' => $data[$root]['buyer-billing-address']['fax']['VALUE'],
'customers_default_address_id' => 0,
'customers_password' => zen_encrypt_password(makeSqlString($data[$root]['buyer-id']['VALUE'])),
'customers_newsletter' => $data[$root]['buyer-marketing-preferences']['email-allowed']['VALUE']=='true'?1:0
);
if (ACCOUNT_DOB == 'true') {
$sql_data_array['customers_dob'] = 'now()';
}
zen_db_perform(TABLE_CUSTOMERS, $sql_data_array);
$_SESSION['customer_id'] = $db->Insert_ID();
$db->Execute("insert into " . TABLE_CUSTOMERS_INFO . "
(customers_info_id, customers_info_number_of_logons,
customers_info_date_account_created)
values ('" . (int) $_SESSION['customer_id'] . "', '0', now())");
/* $db->Execute("insert into " . $googlepayment->table_name . " " .
" values ( " . $_SESSION['customer_id'] . ", " .
$data[$root]['buyer-id']['VALUE'] . ")");*/
$new_user = true;
}
// thx ZachAnderson ;)
$customer_in_gc = $db->Execute("select gct.customers_id from " .
$googlepayment->table_name . " gct " .
" where gct.buyer_id = " . makeSqlString($data[$root]['buyer-id']['VALUE']));
if($customer_in_gc->RecordCount() == 0) {
$db->Execute("insert into " . $googlepayment->table_name . " " .
" values ( " . $_SESSION['customer_id'] . ", " .
$data[$root]['buyer-id']['VALUE'] . ")");
}
// The user exists and is logged in
// Check database to see if the address exist.
$address_book = $db->Execute("select address_book_id, entry_country_id, entry_zone_id from " . TABLE_ADDRESS_BOOK . "
where customers_id = '" . $_SESSION['customer_id'] . "'
and entry_street_address = '" . makeSqlString($data[$root]['buyer-shipping-address']['address1']['VALUE']) . "'
and entry_suburb = '" . makeSqlString($data[$root]['buyer-shipping-address']['address2']['VALUE']) . "'
and entry_postcode = '" . makeSqlString($data[$root]['buyer-shipping-address']['postal-code']['VALUE']) . "'
and entry_city = '" . makeSqlString($data[$root]['buyer-shipping-address']['city']['VALUE']) . "'
");
// If not, add the addr as default one
if ($address_book->RecordCount() == 0) {
$buyer_state = $data[$root]['buyer-shipping-address']['region']['VALUE'];
$zone_answer = $db->Execute("select zone_id, zone_country_id from " .
TABLE_ZONES . " where zone_code = '" . $buyer_state . "'");
list ($firstname, $lastname) =
explode(' ', makeSqlString($data[$root]['buyer-shipping-address']['contact-name']['VALUE']), 2);
$sql_data_array = array (
'customers_id' => $_SESSION['customer_id'],
'entry_gender' => '',
'entry_company' => $data[$root]['buyer-shipping-address']['company-name']['VALUE'],
'entry_firstname' => $firstname,
'entry_lastname' => $lastname,
'entry_street_address' => $data[$root]['buyer-shipping-address']['address1']['VALUE'],
'entry_suburb' => $data[$root]['buyer-shipping-address']['address2']['VALUE'],
'entry_postcode' => $data[$root]['buyer-shipping-address']['postal-code']['VALUE'],
'entry_city' => $data[$root]['buyer-shipping-address']['city']['VALUE'],
'entry_state' => $buyer_state,
'entry_country_id' => $zone_answer->fields['zone_country_id'],
'entry_zone_id' => $zone_answer->fields['zone_id']
);
zen_db_perform(TABLE_ADDRESS_BOOK, $sql_data_array);
$address_id = $db->Insert_ID();
$db->Execute("update " . TABLE_CUSTOMERS . "
set customers_default_address_id = '" . (int) $address_id . "'
where customers_id = '" . (int) $_SESSION['customer_id'] . "'");
$_SESSION['customer_default_address_id'] = $address_id;
$_SESSION['customer_country_id'] = $zone_answer->fields['zone_country_id'];
$_SESSION['customer_zone_id'] = $zone_answer->fields['zone_id'];
} else {
$_SESSION['customer_default_address_id'] = $address_book->fields['address_book_id'];
$_SESSION['customer_country_id'] = $address_book->fields['entry_country_id'];
$_SESSION['customer_zone_id'] = $address_book->fields['entry_zone_id'];
}
$_SESSION['customer_first_name'] = $data[$root]['buyer-billing-address']['contact-name']['VALUE'];
if (isset ($data[$root]['order-adjustment']['shipping']['merchant-calculated-shipping-adjustment']['shipping-name']['VALUE'])) {
$shipping = $data[$root]['order-adjustment']['shipping']['merchant-calculated-shipping-adjustment']['shipping-name']['VALUE'];
$ship_cost = $data[$root]['order-adjustment']['shipping']['merchant-calculated-shipping-adjustment']['shipping-cost']['VALUE'];
$methods_hash = $googlepayment->getMethods();
list ($a, $method_name) = explode(': ', $shipping, 2);
$shipping_name = $methods_hash[$method_name][0];//name
$shipping_code = $methods_hash[$method_name][2];//code
} else if (isset ($data[$root]['order-adjustment']['shipping']['flat-rate-shipping-adjustment']['shipping-name']['VALUE'])) {
$shipping = $data[$root]['order-adjustment']['shipping']['flat-rate-shipping-adjustment']['shipping-name']['VALUE'];
$ship_cost = $data[$root]['order-adjustment']['shipping']['flat-rate-shipping-adjustment']['shipping-cost']['VALUE'];
$methods_hash = $googlepayment->getMethods();
list ($a, $method_name) = explode(': ', $shipping, 2);
$shipping_name = $methods_hash[$method_name][0];//name
$shipping_code = $methods_hash[$method_name][2];//code
} else if (isset ($data[$root]['order-adjustment']['shipping']['carrier-calculated-shipping-adjustment']['shipping-name']['VALUE'])) {
$shipping = $data[$root]['order-adjustment']['shipping']['carrier-calculated-shipping-adjustment']['shipping-name']['VALUE'];
$ship_cost = $data[$root]['order-adjustment']['shipping']['carrier-calculated-shipping-adjustment']['shipping-cost']['VALUE'];
$shipping_name = $shipping;
$shipping_code = 'GCCarrierCalculated';//code
} else {
$shipping = 'GC Digital Delivery';
$ship_cost = 0;
$shipping_name = $shipping;//name
$shipping_code = 'FreeGCDigital';//code
}
$tax_amt = $data[$root]['order-adjustment']['total-tax']['VALUE'];
// $order_total = $data[$root]['order-total']['VALUE'];
require (DIR_WS_CLASSES . 'order.php');
$order = new order();
// load the selected shipping module
// Set up order info
$payment_method = $googlepayment->title;
if(MODULE_PAYMENT_GOOGLECHECKOUT_MODE=='https://sandbox.google.com/checkout/'){
$payment_method .= " - <font color=red>SANDBOX</font>";
}
list ($order->customer['firstname'], $order->customer['lastname']) =
explode(' ', $data[$root]['buyer-billing-address']['contact-name']['VALUE'], 2);
$order->customer['company'] = $data[$root]['buyer-billing-address']['company-name']['VALUE'];
$order->customer['street_address'] = $data[$root]['buyer-billing-address']['address1']['VALUE'];
$order->customer['suburb'] = $data[$root]['buyer-billing-address']['address2']['VALUE'];
$order->customer['city'] = $data[$root]['buyer-billing-address']['city']['VALUE'];
$order->customer['postcode'] = $data[$root]['buyer-billing-address']['postal-code']['VALUE'];
$order->customer['state'] = $data[$root]['buyer-billing-address']['region']['VALUE'];
$order->customer['country']['title'] = $data[$root]['buyer-billing-address']['country-code']['VALUE'];
$order->customer['telephone'] = $data[$root]['buyer-billing-address']['phone']['VALUE'];
$order->customer['email_address'] = $data[$root]['buyer-billing-address']['email']['VALUE'];
$order->customer['format_id'] = 2;
list ($order->delivery['firstname'], $order->delivery['lastname']) =
explode(' ', $data[$root]['buyer-shipping-address']['contact-name']['VALUE'], 2);
$order->delivery['company'] = $data[$root]['buyer-shipping-address']['company-name']['VALUE'];
$order->delivery['street_address'] = $data[$root]['buyer-shipping-address']['address1']['VALUE'];
$order->delivery['suburb'] = $data[$root]['buyer-shipping-address']['address2']['VALUE'];
$order->delivery['city'] = $data[$root]['buyer-shipping-address']['city']['VALUE'];
$order->delivery['postcode'] = $data[$root]['buyer-shipping-address']['postal-code']['VALUE'];
$order->delivery['state'] = $data[$root]['buyer-shipping-address']['region']['VALUE'];
$order->delivery['country']['title'] = $data[$root]['buyer-shipping-address']['country-code']['VALUE'];
$order->delivery['format_id'] = 2;
list ($order->billing['firstname'], $order->billing['lastname']) =
explode(' ', $data[$root]['buyer-billing-address']['contact-name']['VALUE'], 2);
$order->billing['company'] = $data[$root]['buyer-billing-address']['company-name']['VALUE'];
$order->billing['street_address'] = $data[$root]['buyer-billing-address']['address1']['VALUE'];
$order->billing['suburb'] = $data[$root]['buyer-billing-address']['address2']['VALUE'];
$order->billing['city'] = $data[$root]['buyer-billing-address']['city']['VALUE'];
$order->billing['postcode'] = $data[$root]['buyer-billing-address']['postal-code']['VALUE'];
$order->billing['state'] = $data[$root]['buyer-billing-address']['region']['VALUE'];
$order->billing['country']['title'] = $data[$root]['buyer-billing-address']['country-code']['VALUE'];
$order->billing['format_id'] = 2;
$order->info['payment_method'] = $payment_method;
$order->info['payment_module_code'] = $googlepayment->code;
$order->info['shipping_method'] = $shipping_name;
$order->info['shipping_module_code'] = $shipping_code;
$order->info['cc_type'] = '';
$order->info['cc_owner'] = '';
$order->info['cc_number'] = '';
$order->info['cc_expires'] = '';
$order->info['order_status'] = GC_STATE_NEW;
$order->info['tax'] = $tax_amt;
$order->info['currency'] = $data[$root]['order-total']['currency'];
$order->info['currency_value'] = 1;
$_SESSION['customers_ip_address'] = $data[$root]['shopping-cart']['merchant-private-data']['ip-address']['VALUE'];
$order->info['comments'] = GOOGLECHECKOUT_STATE_NEW_ORDER_NUM .
$data[$root]['google-order-number']['VALUE'] . "\n" .
GOOGLECHECKOUT_STATE_NEW_ORDER_MC_USED .
((@$data[$root]['order-adjustment']['merchant-calculation-successful']['VALUE'] == 'true')?'True':'False') .
($new_user ? ("\n" . GOOGLECHECKOUT_STATE_NEW_ORDER_BUYER_USER .
$data[$root]['buyer-billing-address']['email']['VALUE'] . "\n" .
GOOGLECHECKOUT_STATE_NEW_ORDER_BUYER_PASS . $data[$root]['buyer-id']['VALUE']):'');
$coupons = get_arr_result(@$data[$root]['order-adjustment']['merchant-codes']['coupon-adjustment']);
// $gift_cert = get_arr_result(@$data[$root]['order-adjustment']['merchant-codes']['gift-certificate-adjustment']);
$items = get_arr_result($data[$root]['shopping-cart']['items']['item']);
// Get Coustoms OT
$ot_customs_total = 0;
$ot_customs = array ();
$order->products = array ();
foreach ($items as $item) {
if (isset ($item['merchant-private-item-data']['item']['VALUE'])) {
$order->products[] = unserialize(base64_decode($item['merchant-private-item-data']['item']['VALUE']));
} else
if ($item['merchant-private-item-data']['order_total']['VALUE']) {
$ot = unserialize(base64_decode($item['merchant-private-item-data']['order_total']['VALUE']));
$ot_customs[] = $ot;
$ot_value = $ot['value'] * (strrpos($ot['text'], '-') === false ? 1 : -1);
$ot_customs_total += $currencies->get_value($data[$root]['order-total']['currency']) * $ot_value;
} else {
// For Invoices!
// Happy BDay ropu, 07/03
$order->products[] = array (
'qty' => $item['quantity']['VALUE'],
'name' => $item['item-name']['VALUE'],
'model' => $item['item-description']['VALUE'],
'tax' => 0,
'tax_description' => @$item['tax-table-selector']['VALUE'],
'price' => $item['unit-price']['VALUE'],
'final_price' => $item['unit-price']['VALUE'],
'onetime_charges' => 0,
'weight' => 0,
'products_priced_by_attribute' => 0,
'product_is_free' => 0,
'products_discount_type' => 0,
'products_discount_type_from' => 0,
'id' => @$item['merchant-item-id']['VALUE']
);
}
}
// Update values so that order_total modules get the correct values
$order->info['total'] = $data[$root]['order-total']['VALUE'];
$order->info['subtotal'] = $data[$root]['order-total']['VALUE'] -
($ship_cost + $tax_amt) +
@$coupons[0]['applied-amount']['VALUE'] -
$ot_customs_total;
$order->info['coupon_code'] = @$coupons[0]['code']['VALUE'];
$order->info['shipping_method'] = $shipping;
$order->info['shipping_cost'] = $ship_cost;
$order->info['tax_groups']['tax'] = $tax_amt;
$order->info['currency'] = $data[$root]['order-total']['currency'];
$order->info['currency_value'] = 1;
require (DIR_WS_CLASSES . 'order_total.php');
$order_total_modules = new order_total();
// Disable OT sent as items in the GC cart
foreach ($order_total_modules->modules as $ot_code => $order_total) {
if (!in_array(substr($order_total, 0, strrpos($order_total, '.')), $googlepayment->ot_ignore)) {
unset ($order_total_modules->modules[$ot_code]);
}
}
$order_totals = $order_total_modules->process();
// Not necessary, OT already disabled
// foreach($order_totals as $ot_code => $order_total){
// if(!in_array($order_total['code'], $googlepayment->ot_ignore)){
// unset($order_totals[$ot_code]);
// }
// }
// Merge all OT
$order_totals = array_merge($order_totals, $ot_customs);
if (isset ($data[$root]['order-adjustment']['merchant-codes']['coupon-adjustment'])) {
$order_totals[] = array (
'code' => 'ot_coupon',
'title' => "<b>" . MODULE_ORDER_TOTAL_COUPON_TITLE .
" " . @$coupons[0]['code']['VALUE'] . ":</b>",
'text' => $currencies->format(@$coupons[0]['applied-amount']['VALUE']*-1,
false,@$coupons[0]['applied-amount']['currency'])
,
'value' => @$coupons[0]['applied-amount']['VALUE'],
'sort_order' => 280
);
}
Bookmarks