function nzsc_googleResponse() { global $wpdb, $user_ID; $merchant_id = get_option('google_id'); $merchant_key = get_option('google_key'); $server_type = get_option('google_server_type'); $currency = get_option('google_cur'); define('RESPONSE_HANDLER_ERROR_LOG_FILE', 'library/googleerror.log'); define('RESPONSE_HANDLER_LOG_FILE', 'library/googlemessage.log'); if (stristr($_SERVER['HTTP_USER_AGENT'], "Google Checkout Notification Agent")) { $Gresponse = new GoogleResponse($merchant_id, $merchant_key); $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); $message = "<pre>" . print_r($user_marketing_preference, 1) . "</pre>"; $sessionid = mt_rand(100, 999) . time(); if ($root == "new-order-notification") { $_SESSION['nzshpcrt_cart'] = ''; $cart_items = $data['new-order-notification']['shopping-cart']['items']; $user_marketing_preference = $data['new-order-notification']['buyer-marketing-preferences']['email-allowed']['VALUE']; $shipping_name = $data['new-order-notification']['buyer-shipping-address']['contact-name']['VALUE']; $shipping_name = explode(" ", $shipping_name); $shipping_firstname = $shipping_name[0]; $shipping_lastname = $shipping_name[count($shipping_name) - 1]; $shipping_country = $data['new-order-notification']['buyer-shipping-address']['country-code']['VALUE']; $shipping_address1 = $data['new-order-notification']['buyer-shipping-address']['address1']['VALUE']; $shipping_address2 = $data['new-order-notification']['buyer-shipping-address']['address2']['VALUE']; $shipping_city = $data['new-order-notification']['buyer-shipping-address']['city']['VALUE']; $shipping_region = $data['new-order-notification']['buyer-shipping-address']['region']['VALUE']; $billing_name = $data['new-order-notification']['buyer-billing-address']['contact-name']['VALUE']; $billing_name = explode(" ", $shipping_name); $billing_firstname = $shipping_name[0]; $billing_lastname = $shipping_name[count($shipping_name) - 1]; $billing_region = $data['new-order-notification']['buyer-billing-address']['region']['VALUE']; $billing_country = $data['new-order-notification']['buyer-billing-address']['country-code']['VALUE']; $total_price = $data['new-order-notification']['order-total']['VALUE']; $billing_email = $data['new-order-notification']['buyer-billing-address']['email']['VALUE']; $billing_phone = $data['new-order-notification']['buyer-billing-address']['phone']['VALUE']; $billing_address = $data['new-order-notification']['buyer-billing-address']['address1']['VALUE']; $billing_address .= " " . $data['new-order-notification']['buyer-billing-address']['address2']['VALUE']; $billing_address .= " " . $data['new-order-notification']['buyer-billing-address']['city']['VALUE']; $billing_city = $data['new-order-notification']['buyer-billing-address']['city']['VALUE']; $google_order_number = $data['new-order-notification']['google-order-number']['VALUE']; $pnp = $data['new-order-notification']['order-adjustment']['shipping']['flat-rate-shipping-adjustment']['shipping-cost']['VALUE']; $affiliate_id = $data['new-order-notification']['shopping-cart']['merchant-private-data']; $affiliate_id = explode('=', $affiliate_id); if ($affiliate_id[0] == 'affiliate_id') { if ($affiliate_id[1] == '') { $affiliate_id = null; } else { $affiliate_id = $affiliate_id[1]; } } //$tax = $data['new-order-notification']['order-adjustment'][]; $Grequest = new GoogleRequest($merchant_id, $merchant_key, $server_type, $currency); $result = $Grequest->SendProcessOrder($google_order_number); $region_number = $wpdb->get_var("SELECT id FROM " . WPSC_TABLE_REGION_TAX . "` WHERE code ='" . $billing_region . "'"); $sql = "INSERT INTO `" . WPSC_TABLE_PURCHASE_LOGS . "` ( `totalprice` , `sessionid` , `date`, `billing_country`, `shipping_country`,`base_shipping`,`shipping_region`, `user_ID`, `discount_value`,`gateway`, `google_order_number`, `google_user_marketing_preference`, `affiliate_id`) VALUES ( '" . $total_price . "', '" . $sessionid . "', '" . time() . "', '" . $billing_country . "', '" . $shipping_country . "', '" . $pnp . "','" . $region_number . "' , '" . $user_ID . "' , '" . $_SESSION['wpsc_discount'] . "','" . get_option('payment_gateway') . "','" . $google_order_number . "','" . $user_marketing_preference . "', '" . $affiliate_id . "')"; // mail('*****@*****.**',"",$sql); $wpdb->query($sql); $log_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `sessionid` IN('" . $sessionid . "') LIMIT 1"); $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET firstname='" . $shipping_firstname . "', lastname='" . $shipping_lastname . "', email='" . $billing_email . "', phone='" . $billing_phone . "' WHERE id='" . $log_id . "'"; $wpdb->query($sql); if (array_key_exists(0, $cart_items['item'])) { $cart_items = $cart_items['item']; } //logging to submited_form_data $billing_fname_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='first_name' LIMIT 1"); $sql = "INSERT INTO `" . WPSC_TABLE_SUBMITED_FORM_DATA . "` (log_id, form_id, value) VALUES ('" . $log_id . "','" . $billing_fname_id . "','" . $billing_firstname . "')"; //$wpdb->query($sql) ; $billing_lname_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='last_name' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $billing_lname_id . "','" . $billing_lastname . "')"; $billing_address_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='address' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $billing_address_id . "','" . $billing_address . "')"; $billing_city_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='city' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $billing_city_id . "','" . $billing_city . "')"; $billing_country_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='country' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $billing_country_id . "','" . $billing_country . "')"; $billing_state_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='state' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $billing_state_id . "','" . $billing_region . "')"; $shipping_fname_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='delivery_first_name' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $shipping_fname_id . "','" . $shipping_firstname . "')"; $shipping_lname_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='delivery_last_name' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $shipping_lname_id . "','" . $shipping_lastname . "')"; $shipping_address_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='delivery_address' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $shipping_address_id . "','" . $shipping_address1 . " " . $shipping_address2 . "')"; $shipping_city_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='delivery_city' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $shipping_city_id . "','" . $shipping_city . "')"; $shipping_state_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='delivery_state' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $shipping_state_id . "','" . $shipping_region . "')"; $shipping_country_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='delivery_country' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $shipping_country_id . "','" . $shipping_country . "')"; $wpdb->query($sql); //$variations = $cart_item->product_variations; foreach ($cart_items as $cart_item) { $product_id = $cart_item['merchant-item-id']['VALUE']; $item_name = $cart_item['item-name']['VALUE']; $item_desc = $cart_item['item-description']['VALUE']; $item_unit_price = $cart_item['unit-price']['VALUE']; $item_quantity = $cart_item['quantity']['VALUE']; $product_info = $wpdb->get_results("SELECT * FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE id='" . $product_id . "' LIMIT 1", ARRAY_A); $product_info = $product_info[0]; //mail("*****@*****.**","",print_r($product_info,1)); if ($product_info['notax'] != 1) { //$price = nzshpcrt_calculate_tax($item_unit_price, $billing_country, $region_number); if (get_option('base_country') == $billing_country) { $country_data = $wpdb->get_row("SELECT * FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `isocode` IN('" . get_option('base_country') . "') LIMIT 1", ARRAY_A); if ($country_data['has_regions'] == 1) { if (get_option('base_region') == $region_number) { $region_data = $wpdb->get_row("SELECT `" . WPSC_TABLE_REGION_TAX . "`.* FROM `" . WPSC_TABLE_REGION_TAX . "` WHERE `" . WPSC_TABLE_REGION_TAX . "`.`country_id` IN('" . $country_data['id'] . "') AND `" . WPSC_TABLE_REGION_TAX . "`.`id` IN('" . get_option('base_region') . "') ", ARRAY_A); } $gst = $region_data['tax']; } else { $gst = $country_data['tax']; } } else { $gst = 0; } } else { $gst = 0; } if ($product_info['no_shipping'] == '0') { if ($shipping_country == get_option('base_country')) { $pnp = $product_info['pnp']; } else { $pnp = $product_info['international_pnp']; } } else { $pnp = 0; } $cartsql = "INSERT INTO `" . WPSC_TABLE_CART_CONTENTS . "` ( `prodid` , `purchaseid`, `price`, `pnp`, `gst`, `quantity`, `donation`, `no_shipping` ) VALUES ('" . $product_id . "', '" . $log_id . "','" . $item_unit_price . "','" . $pnp . "', '" . $gst . "','" . $item_quantity . "', '" . $product_info['donation'] . "', '" . $product_info['no_shipping'] . "')"; $wpdb->query($cartsql); } } if ($root == "order-state-change-notification") { $google_order_number = $data['order-state-change-notification']['google-order-number']['VALUE']; $google_status = $wpdb->get_var("SELECT google_status FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number='" . $google_order_number . "'"); $google_status = unserialize($google_status); if ($google_status[0] != 'Partially Charged' && $google_status[0] != 'Partially Refunded') { $google_status[0] = $data['order-state-change-notification']['new-financial-order-state']['VALUE']; $google_status[1] = $data['order-state-change-notification']['new-fulfillment-order-state']['VALUE']; } $google_status = serialize($google_status); $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET google_status='" . $google_status . "' WHERE google_order_number='" . $google_order_number . "'"; $wpdb->query($sql); if ($data['order-state-change-notification']['new-financial-order-state']['VALUE'] == 'CHARGEABLE' && get_option('google_auto_charge') == '1') { $Grequest = new GoogleRequest($merchant_id, $merchant_key, $server_type, $currency); $result = $Grequest->SendChargeOrder($google_order_number); $_SESSION['nzshpcrt_cart'] = ''; unset($_SESSION['coupon_num'], $_SESSION['google_session']); $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET processed='2' WHERE google_order_number='" . $google_order_number . "'"; $wpdb->query($sql); } } if ($root == "charge-amount-notification") { $google_order_number = $data['charge-amount-notification']['google-order-number']['VALUE']; $google_status = $wpdb->get_var("SELECT google_status FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number='" . $google_order_number . "'"); $google_status = unserialize($google_status); $total_charged = $data['charge-amount-notification']['total-charge-amount']['VALUE']; $google_status['partial_charge_amount'] = $total_charged; $totalprice = $wpdb->get_var("SELECT totalprice FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number='" . $google_order_number . "'"); if ($totalprice > $total_charged) { $google_status[0] = 'Partially Charged'; } else { if ($totalprice = $total_charged) { $google_status[0] = 'CHARGED'; } } $google_status = serialize($google_status); $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET google_status='" . $google_status . "' WHERE google_order_number='" . $google_order_number . "'"; $wpdb->query($sql); } if ($root == "refund-amount-notification") { $google_order_number = $data['refund-amount-notification']['google-order-number']['VALUE']; $google_status = $wpdb->get_var("SELECT google_status FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number='" . $google_order_number . "'"); $google_status = unserialize($google_status); $total_charged = $data['refund-amount-notification']['total-refund-amount']['VALUE']; $google_status['partial_refund_amount'] = $total_charged; $totalprice = $wpdb->get_var("SELECT totalprice FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number='" . $google_order_number . "'"); if ($totalprice > $total_charged) { $google_status[0] = 'Partially refunded'; } else { if ($totalprice = $total_charged) { $google_status[0] = 'REFUNDED'; } } $google_status = serialize($google_status); $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET google_status='" . $google_status . "' WHERE google_order_number='" . $google_order_number . "'"; $wpdb->query($sql); } // <avs-response>Y</avs-response> // <cvn-response>M</cvn-response> if ($root == "risk-information-notification") { $google_order_number = $data['risk-information-notification']['google-order-number']['VALUE']; $google_status = $wpdb->get_var("SELECT google_status FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number='" . $google_order_number . "'"); $google_status = unserialize($google_status); $google_status['cvn'] = $data['risk-information-notification']['risk-information']['cvn-response']['VALUE']; $google_status['avs'] = $data['risk-information-notification']['risk-information']['avs-response']['VALUE']; $google_status['protection'] = $data['risk-information-notification']['risk-information']['eligible-for-protection']['VALUE']; $google_status = serialize($google_status); $google_status = $wpdb->query("UPDATE " . WPSC_TABLE_PURCHASE_LOGS . " SET google_status='" . $google_status . "' WHERE google_order_number='" . $google_order_number . "'"); if ($data['risk-information-notification']['risk-information']['cvn-response']['VALUE'] == 'E') { $google_risk = 'cvn'; } if (in_array($data['risk-information-notification']['risk-information']['avs-response']['VALUE'], array('N', 'U'))) { if (isset($google_risk)) { $google_risk = 'cvn+avs'; } else { $google_risk = 'avs'; } } if (isset($google_risk)) { $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET google_risk='" . $google_risk . "' WHERE google_order_number='" . $google_order_number . "'"; $wpdb->query($sql); } } if ($root == "order-state-change-notification") { $google_order_number = $data['order-state-change-notification']['google-order-number']['VALUE']; if ($data['order-state-change-notification']['new-financial-order-state']['VALUE'] == "CANCELLED_BY_GOOGLE") { $google_status = $wpdb->get_var("SELECT google_status FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number='" . $google_order_number . "'"); $google_status = unserialize($google_status); $google_status[0] = "CANCELLED_BY_GOOGLE"; $wpdb->get_var("UPDATE " . WPSC_TABLE_PURCHASE_LOGS . " SET google_status='" . serialize($google_status) . "' WHERE google_order_number='" . $google_order_number . "'"); } } // mail('*****@*****.**',"",$root . " <pre>". print_r($data,1)."</pre>"); exit; } }
public function external_hook($hook) { switch ($hook) { case 'notification': if (!isset($_REQUEST['serial-number']) || !$_REQUEST['serial-number']) { send_error('Google API not setup correctly. Please ensure API callback URL is set correctly as per instructions'); exit; } chdir(dirname(__FILE__)); //'includes/plugin_paymethod_google/'); require_once 'library/googleresponse.php'; //require_once('library/googlemerchantcalculations.php'); require_once 'library/googleresult.php'; require_once 'library/googlerequest.php'; require_once 'library/googlenotification.php'; require_once 'library/googlenotificationhistory.php'; define('RESPONSE_HANDLER_ERROR_LOG_FILE', _UCM_FOLDER . '/temp/googleerror.log'); define('RESPONSE_HANDLER_LOG_FILE', _UCM_FOLDER . '/temp/googlemessage.log'); $server_type = self::is_sandbox() ? "sandbox" : ''; //$currency = module_config::get_currency($currency_id); //$Gresponse = new GoogleResponse(self::get_merchant_id(), self::get_merchant_key()); $Grequest = new GoogleRequest(self::get_merchant_id(), self::get_merchant_key(), $server_type); $GNotificationHistory = new GoogleNotificationHistoryRequest(self::get_merchant_id(), self::get_merchant_key(), $server_type); $response = $GNotificationHistory->SendNotificationHistoryRequest($_REQUEST['serial-number']); $xml_response_status = $response[0]; $xml_response = $response[1]; $response = new SimpleXMLElement($xml_response); $root = $response->getName(); // debugging //send_error('xml2: '.$root.var_export($response,true)); //$Gresponse->SetMerchantAuthentication(self::get_merchant_id(), self::get_merchant_key()); //$Gresponse->SendAck(); $google_order_number = (string) $response->{'google-order-number'}; if ($google_order_number) { $invoice_payment_data = get_single('invoice_payment', 'other_id', $google_order_number); } else { $invoice_payment_data = false; send_error('Google payment API failure, no Google Order Number located'); } if ($invoice_payment_data && $invoice_payment_data['invoice_payment_id']) { self::add_payment_data($invoice_payment_data['invoice_payment_id'], 'log', 'Recorded an API hit: ' . $root); } switch ($root) { case "new-order-notification": // an order has been placed. // dont really need to do anything. // we grab the merchant private data from this bit // it contains the private id etc.. if ($google_order_number && $response->{'shopping-cart'}->{'merchant-private-data'}) { $private_data = (array) $response->{'shopping-cart'}->{'merchant-private-data'}; $invoice_id = (int) $private_data['invoice_id']; $invoice_payment_id = (int) $private_data['invoice_payment_id']; if ($invoice_id > 0 && $invoice_payment_id > 0) { self::add_payment_data($invoice_payment_id, 'log', 'Found Google Order Number: ' . $google_order_number); $sql = "UPDATE `" . _DB_PREFIX . "invoice_payment` SET `other_id` = '" . mysql_real_escape_string($google_order_number) . "' WHERE invoice_id = '{$invoice_id}' AND invoice_payment_id = '{$invoice_payment_id}'"; query($sql); } } break; case "order-state-change-notification": $new_financial_state = (string) $response->{'new-financial-order-state'}; if ($invoice_payment_data && $invoice_payment_data['invoice_payment_id']) { self::add_payment_data($invoice_payment_data['invoice_payment_id'], 'log', 'Recorded an order status change to: ' . $new_financial_state); } switch ($new_financial_state) { case 'REVIEWING': break; case 'CHARGEABLE': if ($invoice_payment_data && $invoice_payment_data['invoice_payment_id']) { self::add_payment_data($invoice_payment_data['invoice_payment_id'], 'log', 'Charging customers credit card. '); } // process and charge the order: $Grequest->SendProcessOrder($google_order_number); $Grequest->SendChargeOrder($google_order_number, ''); break; case 'CHARGING': break; case 'CHARGED': break; case 'PAYMENT_DECLINED': break; case 'CANCELLED': break; case 'CANCELLED_BY_GOOGLE': //$Grequest->SendBuyerMessage($data[$root]['google-order-number']['VALUE'], // "Sorry, your order is cancelled by Google", true); break; default: break; } break; case "charge-amount-notification": // payment has been made! // update the order. $Grequest->SendArchiveOrder($google_order_number); if ($invoice_payment_data && $invoice_payment_data['invoice_payment_id']) { self::add_payment_data($invoice_payment_data['invoice_payment_id'], 'log', 'Received a payment of ' . $response->{'latest-charge-amount'} . ' and assigning it to this invoice.'); update_insert("invoice_payment_id", $invoice_payment_data['invoice_payment_id'], "invoice_payment", array('date_paid' => date('Y-m-d'))); } break; case "chargeback-amount-notification": break; case "refund-amount-notification": break; case "risk-information-notification": break; default: //$Gresponse->SendBadRequestStatus("Invalid or not supported Message"); break; } break; } }
function google_checkout_state_change($check_status, $status, $oID, $cust_notify, $notify_comments) { global $db, $messageStack, $orders_statuses; define('API_CALLBACK_ERROR_LOG', DIR_FS_CATALOG . "/googlecheckout/logs/response_error.log"); define('API_CALLBACK_MESSAGE_LOG', DIR_FS_CATALOG . "/googlecheckout/logs/response_message.log"); include_once DIR_FS_CATALOG . '/includes/modules/payment/googlecheckout.php'; include_once DIR_FS_CATALOG . '/googlecheckout/library/googlerequest.php'; require_once DIR_FS_CATALOG . '/googlecheckout/library/configuration/google_configuration.php'; require_once DIR_FS_CATALOG . '/googlecheckout/library/configuration/google_configuration_keys.php'; $config = new GoogleConfigurationKeys(); $googlecheckout = new googlecheckout(); $google_request = new GoogleRequest($googlecheckout->merchantid, $googlecheckout->merchantkey, MODULE_PAYMENT_GOOGLECHECKOUT_MODE == 'https://sandbox.google.com/checkout/' ? "sandbox" : "production", DEFAULT_CURRENCY); $google_request->SetLogFiles(API_CALLBACK_ERROR_LOG, API_CALLBACK_MESSAGE_LOG); $google_answer = tep_db_fetch_array(tep_db_query("SELECT go.google_order_number, go.order_amount, o.customers_email_address, gc.buyer_id, o.customers_id\n FROM " . $googlecheckout->table_order . " go \n inner join " . TABLE_ORDERS . " o on go.orders_id = o.orders_id\n inner join " . $googlecheckout->table_name . " gc on gc.customers_id = o.customers_id\n WHERE go.orders_id = '" . (int) $oID . "'\n group by o.customers_id order by o.orders_id desc")); $google_order = $google_answer['google_order_number']; $amount = $google_answer['order_amount']; // If status update is from Google New -> Google Processing on the Admin UI // this invokes the processing-order and charge-order commands // 1->Google New, 2-> Google Processing if ($check_status['orders_status'] == GC_STATE_NEW && $status == GC_STATE_PROCESSING && $google_order != '') { list($curl_status, ) = $google_request->SendChargeOrder($google_order, $amount); if ($curl_status != 200) { $messageStack->add_session(GOOGLECHECKOUT_ERR_SEND_CHARGE_ORDER, 'error'); } else { $messageStack->add_session(GOOGLECHECKOUT_SUCCESS_SEND_CHARGE_ORDER, 'success'); } list($curl_status, ) = $google_request->SendProcessOrder($google_order); if ($curl_status != 200) { $messageStack->add_session(GOOGLECHECKOUT_ERR_SEND_PROCESS_ORDER, 'error'); } else { $messageStack->add_session(GOOGLECHECKOUT_SUCCESS_SEND_PROCESS_ORDER, 'success'); } } else { if (($check_status['orders_status'] == GC_STATE_PROCESSING || $check_status['orders_status'] == GC_STATE_REFUNDED) && ($status == GC_STATE_SHIPPED || $status == GC_STATE_SHIPPED_REFUNDED) && $google_order != '') { $carrier = $tracking_no = ""; // Add tracking Data if (isset($_POST['carrier_select']) && $_POST['carrier_select'] != 'select' && isset($_POST['tracking_number']) && !empty($_POST['tracking_number'])) { $carrier = $_POST['carrier_select']; $tracking_no = $_POST['tracking_number']; $comments = GOOGLECHECKOUT_STATE_STRING_TRACKING . "\n" . GOOGLECHECKOUT_STATE_STRING_TRACKING_CARRIER . $_POST['carrier_select'] . "\n" . GOOGLECHECKOUT_STATE_STRING_TRACKING_NUMBER . $_POST['tracking_number'] . ""; tep_db_query("insert into " . TABLE_ORDERS_STATUS_HISTORY . "\n (orders_id, orders_status_id, date_added, customer_notified, comments)\n values ('" . (int) $oID . "',\n '" . tep_db_input($check_status['orders_status'] == GC_STATE_REFUNDED ? GC_STATE_SHIPPED_REFUNDED : GC_STATE_SHIPPED) . "',\n now(),\n '" . tep_db_input($cust_notify) . "',\n '" . tep_db_input($comments) . "')"); } list($curl_status, ) = $google_request->SendDeliverOrder($google_order, $carrier, $tracking_no, $cust_notify == 1 ? "true" : "false"); if ($curl_status != 200) { $messageStack->add_session(GOOGLECHECKOUT_ERR_SEND_DELIVER_ORDER, 'error'); } else { $messageStack->add_session(GOOGLECHECKOUT_SUCCESS_SEND_DELIVER_ORDER, 'success'); } list($curl_status, ) = $google_request->SendArchiveOrder($google_order); if ($curl_status != 200) { $messageStack->add_session(GOOGLECHECKOUT_ERR_SEND_ARCHIVE_ORDER, 'error'); } else { $messageStack->add_session(GOOGLECHECKOUT_SUCCESS_SEND_ARCHIVE_ORDER, 'success'); } } else { if ($check_status['orders_status'] != GC_STATE_CANCELED && $status == GC_STATE_CANCELED && $google_order != '') { if ($check_status['orders_status'] != GC_STATE_NEW) { list($curl_status, ) = $google_request->SendRefundOrder($google_order, 0, GOOGLECHECKOUT_STATE_STRING_ORDER_CANCELED); if ($curl_status != 200) { $messageStack->add_session(GOOGLECHECKOUT_ERR_SEND_REFUND_ORDER, 'error'); } else { $messageStack->add_session(GOOGLECHECKOUT_SUCCESS_SEND_REFUND_ORDER, 'success'); } } else { // Tell google witch is the OSC's internal order Number list($curl_status, ) = $google_request->SendMerchantOrderNumber($google_order, $oID); if ($curl_status != 200) { $messageStack->add_session(GOOGLECHECKOUT_ERR_SEND_MERCHANT_ORDER_NUMBER, 'error'); } else { $messageStack->add_session(GOOGLECHECKOUT_SUCCESS_SEND_MERCHANT_ORDER_NUMBER, 'success'); } } // Is the order is not archive, I do it if ($check_status['orders_status'] != GC_STATE_SHIPPED && $check_status['orders_status'] != GC_STATE_SHIPPED_REFUNDED) { list($curl_status, ) = $google_request->SendArchiveOrder($google_order); if ($curl_status != 200) { $messageStack->add_session(GOOGLECHECKOUT_ERR_SEND_ARCHIVE_ORDER, 'error'); } else { $messageStack->add_session(GOOGLECHECKOUT_SUCCESS_SEND_ARCHIVE_ORDER, 'success'); } } // Cancel the order list($curl_status, ) = $google_request->SendCancelOrder($google_order, GOOGLECHECKOUT_STATE_STRING_ORDER_CANCELED, $notify_comments); if ($curl_status != 200) { $messageStack->add_session(GOOGLECHECKOUT_ERR_SEND_CANCEL_ORDER, 'error'); } else { $messageStack->add_session(GOOGLECHECKOUT_SUCCESS_SEND_CANCEL_ORDER, 'success'); } } else { if ($google_order != '' && $check_status['orders_status'] != $status) { $statuses = array(); foreach ($orders_statuses as $status_array) { $statuses[$status_array['id']] = $status_array['text']; } $messageStack->add_session(sprintf(GOOGLECHECKOUT_ERR_INVALID_STATE_TRANSITION, $statuses[$check_status['orders_status']], $statuses[$status], $statuses[$check_status['orders_status']]), 'error'); } } } } // Send Buyer's message if ($cust_notify == 1 && isset($notify_comments) && !empty($notify_comments)) { $cust_notify_ok = '0'; $use_cart_messaging = gc_get_configuration_value($config->useCartMessaging()) == 'True'; if (!(strlen(htmlentities(strip_tags($notify_comments))) > GOOGLE_MESSAGE_LENGTH && $use_cart_messaging)) { list($curl_status, ) = $google_request->sendBuyerMessage($google_order, $notify_comments, "true"); if ($curl_status != 200) { $messageStack->add_session(GOOGLECHECKOUT_ERR_SEND_MESSAGE_ORDER, 'error'); $cust_notify_ok = '0'; } else { $messageStack->add_session(GOOGLECHECKOUT_SUCCESS_SEND_MESSAGE_ORDER, 'success'); $cust_notify_ok = '1'; } if (strlen(htmlentities(strip_tags($notify_comments))) > GOOGLE_MESSAGE_LENGTH) { $messageStack->add_session(sprintf(GOOGLECHECKOUT_WARNING_CHUNK_MESSAGE, GOOGLE_MESSAGE_LENGTH), 'warning'); } } // Cust notified return $cust_notify_ok; } // Cust notified return '0'; }
function nzsc_googleResponse() { global $wpdb, $user_ID; $merchant_id = get_option('google_id'); $merchant_key = get_option('google_key'); $server_type = get_option('google_server_type'); $currency = get_option('google_cur'); $xml_response = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : file_get_contents("php://input"); define('RESPONSE_HANDLER_ERROR_LOG_FILE', 'library/googleerror.log'); define('RESPONSE_HANDLER_LOG_FILE', 'library/googlemessage.log'); if (stristr($_SERVER['HTTP_USER_AGENT'], "Google Checkout Notification Agent")) { $Gresponse = new GoogleResponse($merchant_id, $merchant_key); $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); $message = "<pre>" . print_r($user_marketing_preference, 1) . "</pre>"; $sessionid = mt_rand(100, 999) . time(); if ($root == "new-order-notification") { $_SESSION['nzshpcrt_cart'] = ''; $cart_items = $data['new-order-notification']['shopping-cart']['items']; $user_marketing_preference = $data['new-order-notification']['buyer-marketing-preferences']['email-allowed']['VALUE']; $shipping_name = $data['new-order-notification']['buyer-shipping-address']['contact-name']['VALUE']; $shipping_name = explode(" ", $shipping_name); $shipping_firstname = $shipping_name[0]; $shipping_lastname = $shipping_name[count($shipping_name) - 1]; $shipping_country = $data['new-order-notification']['buyer-shipping-address']['country-code']['VALUE']; $shipping_address1 = $data['new-order-notification']['buyer-shipping-address']['address1']['VALUE']; $shipping_address2 = $data['new-order-notification']['buyer-shipping-address']['address2']['VALUE']; $shipping_city = $data['new-order-notification']['buyer-shipping-address']['city']['VALUE']; $shipping_region = $data['new-order-notification']['buyer-shipping-address']['region']['VALUE']; $billing_name = $data['new-order-notification']['buyer-billing-address']['contact-name']['VALUE']; $billing_name = explode(" ", $shipping_name); $billing_firstname = $shipping_name[0]; $billing_lastname = $shipping_name[count($shipping_name) - 1]; $billing_region = $data['new-order-notification']['buyer-billing-address']['region']['VALUE']; $billing_country = $data['new-order-notification']['buyer-billing-address']['country-code']['VALUE']; $total_price = $data['new-order-notification']['order-total']['VALUE']; $billing_email = $data['new-order-notification']['buyer-billing-address']['email']['VALUE']; $billing_phone = $data['new-order-notification']['buyer-billing-address']['phone']['VALUE']; $billing_address = $data['new-order-notification']['buyer-billing-address']['address1']['VALUE']; $billing_address .= " " . $data['new-order-notification']['buyer-billing-address']['address2']['VALUE']; $billing_address .= " " . $data['new-order-notification']['buyer-billing-address']['city']['VALUE']; $billing_city = $data['new-order-notification']['buyer-billing-address']['city']['VALUE']; $google_order_number = $data['new-order-notification']['google-order-number']['VALUE']; $pnp = $data['new-order-notification']['order-adjustment']['shipping']['flat-rate-shipping-adjustment']['shipping-cost']['VALUE']; $affiliate_id = $data['new-order-notification']['shopping-cart']['merchant-private-data']; $affiliate_id = explode('=', $affiliate_id); if ($affiliate_id[0] == 'affiliate_id') { if ($affiliate_id[1] == '') { $affiliate_id = null; } else { $affiliate_id = $affiliate_id[1]; } } $Grequest = new GoogleRequest($merchant_id, $merchant_key, $server_type, $currency); $result = $Grequest->SendProcessOrder($google_order_number); $region_number = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . WPSC_TABLE_REGION_TAX . "` WHERE code = %s", $billing_region)); $wpdb->insert(WPSC_TABLE_PURCHASE_LOGS, array('totalprice' => $total_price, 'sessionid' => $sessionid, 'date' => time(), 'billing_country' => $billing_country, 'shipping_country' => $shipping_country, 'base_shipping' => $pnp, 'shipping_region' => $region_number, 'user_ID' => $user_ID, 'discount_value' => $_SESSION['wpsc_discount'], 'gateway' => get_option('payment_gateway'), 'google_order_number' => $google_order_number, 'google_user_marketing_preference' => $user_marketing_preference, 'affiliate_id' => $affiliate_id), array('%f', '%s', '%s', '%s', '%s', '%f', '%s', '%d', '%f', '%s', '%s', '%s', '%s')); $log_id = $wpdb->get_var($wpdb->prepare("SELECT `id` FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `sessionid` IN(%s) LIMIT 1", $sessionid)); $wpdb->update(WPSC_TABLE_PURCHASE_LOGS, array('firstname' => $shipping_firstname, 'lastname' => $shipping_lastname, 'email' => $billing_email, 'phone' => $billing_phone), array('id' => $log_id), '%s', '%d'); if (array_key_exists(0, $cart_items['item'])) { $cart_items = $cart_items['item']; } //logging to submited_form_data $billing_fname_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='first_name' LIMIT 1"); $sql = "INSERT INTO `" . WPSC_TABLE_SUBMITED_FORM_DATA . "` (log_id, form_id, value) VALUES ('" . $log_id . "','" . $billing_fname_id . "','" . esc_sql($billing_firstname) . "')"; $billing_lname_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='last_name' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $billing_lname_id . "','" . esc_sql($billing_lastname) . "')"; $billing_address_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='address' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $billing_address_id . "','" . esc_sql($billing_address) . "')"; $billing_city_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='city' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $billing_city_id . "','" . esc_sql($billing_city) . "')"; $billing_country_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='country' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $billing_country_id . "','" . esc_sql($billing_country) . "')"; $billing_state_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='state' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $billing_state_id . "','" . esc_sql($billing_region) . "')"; $shipping_fname_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='delivery_first_name' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $shipping_fname_id . "','" . esc_sql($shipping_firstname) . "')"; $shipping_lname_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='delivery_last_name' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $shipping_lname_id . "','" . esc_sql($shipping_lastname) . "')"; $shipping_address_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='delivery_address' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $shipping_address_id . "','" . esc_sql($shipping_address1) . " " . esc_sql($shipping_address2) . "')"; $shipping_city_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='delivery_city' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $shipping_city_id . "','" . esc_sql($shipping_city) . "')"; $shipping_state_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='delivery_state' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $shipping_state_id . "','" . esc_sql($shipping_region) . "')"; $shipping_country_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type`='delivery_country' LIMIT 1"); $sql .= ", ('" . $log_id . "','" . $shipping_country_id . "','" . esc_sql($shipping_country) . "')"; $wpdb->query($sql); foreach ($cart_items as $cart_item) { $product_id = $cart_item['merchant-item-id']['VALUE']; $item_name = $cart_item['item-name']['VALUE']; $item_desc = $cart_item['item-description']['VALUE']; $item_unit_price = $cart_item['unit-price']['VALUE']; $item_quantity = $cart_item['quantity']['VALUE']; $product_info = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . $wpdb->posts . "` WHERE id= %d LIMIT 1", $product_id), ARRAY_A); $product_info = $product_info[0]; if ($product_info['notax'] != 1) { if (get_option('base_country') == $billing_country) { $country_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `isocode` IN(%s) LIMIT 1", get_option('base_country')), ARRAY_A); if ($country_data['has_regions'] == 1) { if (get_option('base_region') == $region_number) { $region_data = $wpdb->get_row($wpdb->prepare("SELECT `" . WPSC_TABLE_REGION_TAX . "`.* FROM `" . WPSC_TABLE_REGION_TAX . "` WHERE `" . WPSC_TABLE_REGION_TAX . "`.`country_id` IN(%d) AND `" . WPSC_TABLE_REGION_TAX . "`.`id` IN(%d)", $country_data['id'], get_option('base_region')), ARRAY_A); } $gst = $region_data['tax']; } else { $gst = $country_data['tax']; } } else { $gst = 0; } } else { $gst = 0; } if ($product_info['no_shipping'] == '0') { if ($shipping_country == get_option('base_country')) { $pnp = $product_info['pnp']; } else { $pnp = $product_info['international_pnp']; } } else { $pnp = 0; } $wpdb->insert(WPSC_TABLE_CART_CONTENTS, array('prodid' => $product_id, 'purchaseid' => $log_id, 'price' => $item_unit_price, 'pnp' => $pnp, 'gst' => $gst, 'quantity' => $item_quantity, 'donation' => $product_info['donation'], 'no_shipping' => $product_info['no_shipping']), array('%d', '%d', '%s', '%f', '%f', '%d', '%s', '%s')); } } if ($root == "order-state-change-notification") { $google_order_number = $data['order-state-change-notification']['google-order-number']['VALUE']; $google_status = $wpdb->get_var($wpdb->prepare("SELECT google_status FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number = %s", $google_order_number)); $google_status = unserialize($google_status); if ($google_status[0] != 'Partially Charged' && $google_status[0] != 'Partially Refunded') { $google_status[0] = $data['order-state-change-notification']['new-financial-order-state']['VALUE']; $google_status[1] = $data['order-state-change-notification']['new-fulfillment-order-state']['VALUE']; } $google_status = serialize($google_status); $wpdb->update(WPSC_TABLE_PURCHASE_LOGS, array('google_status' => $google_status), array('google_order_number' => $google_order_number)); if ($data['order-state-change-notification']['new-financial-order-state']['VALUE'] == 'CHARGEABLE' && get_option('google_auto_charge') == '1') { $Grequest = new GoogleRequest($merchant_id, $merchant_key, $server_type, $currency); $result = $Grequest->SendChargeOrder($google_order_number); $_SESSION['nzshpcrt_cart'] = ''; unset($_SESSION['coupon_num'], $_SESSION['google_session']); $wpdb->update(WPSC_TABLE_PURCHASE_LOGS, array('processed' => '3'), array('google_order_number' => $google_order_number)); } } if ($root == "charge-amount-notification") { $google_order_number = $data['charge-amount-notification']['google-order-number']['VALUE']; $google_status = $wpdb->get_var($wpdb->prepare("SELECT google_status FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number = %s", $google_order_number)); $google_status = unserialize($google_status); $total_charged = $data['charge-amount-notification']['total-charge-amount']['VALUE']; $google_status['partial_charge_amount'] = $total_charged; $totalprice = $wpdb->get_var($wpdb->prepare("SELECT totalprice FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number = %s", $google_order_number)); if ($totalprice > $total_charged) { $google_status[0] = 'Partially Charged'; } else { if ($totalprice = $total_charged) { $google_status[0] = 'CHARGED'; } } $google_status = serialize($google_status); $wpdb->update(WPSC_TABLE_PURCHASE_LOGS, array('google_status' => $google_status), array('google_order_number' => $google_order_number)); } if ($root == "refund-amount-notification") { $google_order_number = $data['refund-amount-notification']['google-order-number']['VALUE']; $google_status = $wpdb->get_var($wpdb->prepare("SELECT google_status FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number= %s", $google_order_number)); $google_status = unserialize($google_status); $total_charged = $data['refund-amount-notification']['total-refund-amount']['VALUE']; $google_status['partial_refund_amount'] = $total_charged; $totalprice = $wpdb->get_var($wpdb->prepare("SELECT totalprice FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number = %s", $google_order_number)); if ($totalprice > $total_charged) { $google_status[0] = 'Partially refunded'; } else { if ($totalprice = $total_charged) { $google_status[0] = 'REFUNDED'; } } $google_status = serialize($google_status); $wpdb->update(WPSC_TABLE_PURCHASE_LOGS, array('google_status' => $google_status), array('google_order_number' => $google_order_number)); } if ($root == "risk-information-notification") { $google_order_number = $data['risk-information-notification']['google-order-number']['VALUE']; $google_status = $wpdb->get_var($wpdb->prepare("SELECT google_status FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number = %s", $google_order_number)); $google_status = unserialize($google_status); $google_status['cvn'] = $data['risk-information-notification']['risk-information']['cvn-response']['VALUE']; $google_status['avs'] = $data['risk-information-notification']['risk-information']['avs-response']['VALUE']; $google_status['protection'] = $data['risk-information-notification']['risk-information']['eligible-for-protection']['VALUE']; $google_status = serialize($google_status); $wpdb->update(WPSC_TABLE_PURCHASE_LOGS, array('google_status' => $google_status), array('google_order_number' => $google_order_number)); if ($data['risk-information-notification']['risk-information']['cvn-response']['VALUE'] == 'E') { $google_risk = 'cvn'; } if (in_array($data['risk-information-notification']['risk-information']['avs-response']['VALUE'], array('N', 'U'))) { if (isset($google_risk)) { $google_risk = 'cvn+avs'; } else { $google_risk = 'avs'; } } if (isset($google_risk)) { $wpdb->update(WPSC_TABLE_PURCHASE_LOGS, array('google_risk' => $google_risk), array('google_order_number' => $google_order_number)); } } if ($root == "order-state-change-notification") { $google_order_number = $data['order-state-change-notification']['google-order-number']['VALUE']; if ($data['order-state-change-notification']['new-financial-order-state']['VALUE'] == "CANCELLED_BY_GOOGLE") { $google_status = $wpdb->get_var($wpdb->prepare("SELECT google_status FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE google_order_number = %s", $google_order_number)); $google_status = unserialize($google_status); $google_status[0] = "CANCELLED_BY_GOOGLE"; $wpdb->update(WPSC_TABLE_PURCHASE_LOGS, array('google_status' => serialize($google_status)), array('google_order_number' => $google_order_number)); } } exit; } }