4 * Copyright (C) 2007 Google Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 /* This is the response handler code that will be invoked every time
20 * a notification or request is sent by the Google Server
22 * To allow this code to receive responses, the url for this file
23 * must be set on the seller page under Settings->Integration as the
25 * Order processing commands can be sent automatically by placing these
26 * commands appropriately
28 * To use this code for merchant-calculated feedback, this url must be
29 * set also as the merchant-calculations-url when the cart is posted
30 * Depending on your calculations for shipping, taxes, coupons and gift
31 * certificates update parts of the code as required
36 require_once('library/googleresponse.php');
37 require_once('library/googlemerchantcalculations.php');
38 require_once('library/googleresult.php');
39 require_once('library/googlerequest.php');
41 define('RESPONSE_HANDLER_ERROR_LOG_FILE', 'googleerror.log');
42 define('RESPONSE_HANDLER_LOG_FILE', 'googlemessage.log');
44 $merchant_id = "778068064150108"; // Your Merchant ID
45 $merchant_key = "rFQNe6TbiBeO44y9S9o8Dw"; // Your Merchant Key
46 $server_type = "sandbox"; // change this to go live
47 $currency = 'USD'; // set to GBP if in the UK
49 $Gresponse = new GoogleResponse($merchant_id, $merchant_key);
51 $Grequest = new GoogleRequest($merchant_id, $merchant_key, $server_type, $currency);
54 $Gresponse->SetLogFiles(RESPONSE_HANDLER_ERROR_LOG_FILE
,
55 RESPONSE_HANDLER_LOG_FILE
, L_ALL
);
57 // Retrieve the XML sent in the HTTP POST request to the ResponseHandler
58 $xml_response = isset($HTTP_RAW_POST_DATA)?
59 $HTTP_RAW_POST_DATA:file_get_contents("php://input");
60 if (get_magic_quotes_gpc()) {
61 $xml_response = stripslashes($xml_response);
63 list($root, $data) = $Gresponse->GetParsedXML($xml_response);
64 $Gresponse->SetMerchantAuthentication($merchant_id, $merchant_key);
66 /*$status = $Gresponse->HttpAuthentication();
68 die('authentication failed');
71 /* Commands to send the various order processing APIs
72 * Send charge order : $Grequest->SendChargeOrder($data[$root]
73 * ['google-order-number']['VALUE'], <amount>);
74 * Send process order : $Grequest->SendProcessOrder($data[$root]
75 * ['google-order-number']['VALUE']);
76 * Send deliver order: $Grequest->SendDeliverOrder($data[$root]
77 * ['google-order-number']['VALUE'], <carrier>, <tracking-number>,
79 * Send archive order: $Grequest->SendArchiveOrder($data[$root]
80 * ['google-order-number']['VALUE']);
85 case "request-received": {
94 case "checkout-redirect": {
97 case "merchant-calculation-callback": {
98 // Create the results and send it
99 $merchant_calc = new GoogleMerchantCalculations($currency);
101 // Loop through the list of address ids from the callback
102 $addresses = get_arr_result($data[$root]['calculate']['addresses']['anonymous-address']);
103 foreach($addresses as $curr_address) {
104 $curr_id = $curr_address['id'];
105 $country = $curr_address['country-code']['VALUE'];
106 $city = $curr_address['city']['VALUE'];
107 $region = $curr_address['region']['VALUE'];
108 $postal_code = $curr_address['postal-code']['VALUE'];
110 // Loop through each shipping method if merchant-calculated shipping
111 // support is to be provided
112 if(isset($data[$root]['calculate']['shipping'])) {
113 $shipping = get_arr_result($data[$root]['calculate']['shipping']['method']);
114 foreach($shipping as $curr_ship) {
115 $name = $curr_ship['name'];
116 //Compute the price for this shipping method and address id
117 $price = 12; // Modify this to get the actual price
118 $shippable = "true"; // Modify this as required
119 $merchant_result = new GoogleResult($curr_id);
120 $merchant_result->SetShippingDetails($name, $price, $shippable);
122 if($data[$root]['calculate']['tax']['VALUE'] == "true") {
123 //Compute tax for this address id and shipping type
124 $amount = 15; // Modify this to the actual tax value
125 $merchant_result->SetTaxDetails($amount);
128 if(isset($data[$root]['calculate']['merchant-code-strings']
129 ['merchant-code-string'])) {
130 $codes = get_arr_result($data[$root]['calculate']['merchant-code-strings']
131 ['merchant-code-string']);
132 foreach($codes as $curr_code) {
133 //Update this data as required to set whether the coupon is valid, the code and the amount
134 $coupons = new GoogleGiftcerts("true", $curr_code['code'], 10, "debugtest");
135 $merchant_result->AddGiftCertificates($coupons);
138 $merchant_calc->AddResult($merchant_result);
141 $merchant_result = new GoogleResult($curr_id);
142 if($data[$root]['calculate']['tax']['VALUE'] == "true") {
143 //Compute tax for this address id and shipping type
144 $amount = 15; // Modify this to the actual tax value
145 $merchant_result->SetTaxDetails($amount);
147 $codes = get_arr_result($data[$root]['calculate']['merchant-code-strings']
148 ['merchant-code-string']);
149 foreach($codes as $curr_code) {
150 //Update this data as required to set whether the coupon is valid, the code and the amount
151 $coupons = new GoogleGiftcerts("true", $curr_code['code'], 10, "debugtest");
152 $merchant_result->AddGiftCertificates($coupons);
154 $merchant_calc->AddResult($merchant_result);
157 $Gresponse->ProcessMerchantCalculations($merchant_calc);
160 case "new-order-notification": {
161 $Gresponse->SendAck();
164 case "order-state-change-notification": {
165 $Gresponse->SendAck();
166 $new_financial_state = $data[$root]['new-financial-order-state']['VALUE'];
167 $new_fulfillment_order = $data[$root]['new-fulfillment-order-state']['VALUE'];
169 switch($new_financial_state) {
174 //$Grequest->SendProcessOrder($data[$root]['google-order-number']['VALUE']);
175 //$Grequest->SendChargeOrder($data[$root]['google-order-number']['VALUE'],'');
184 case 'PAYMENT_DECLINED': {
190 case 'CANCELLED_BY_GOOGLE': {
191 //$Grequest->SendBuyerMessage($data[$root]['google-order-number']['VALUE'],
192 // "Sorry, your order is cancelled by Google", true);
199 switch($new_fulfillment_order) {
209 case 'WILL_NOT_DELIVER': {
217 case "charge-amount-notification": {
218 //$Grequest->SendDeliverOrder($data[$root]['google-order-number']['VALUE'],
219 // <carrier>, <tracking-number>, <send-email>);
220 //$Grequest->SendArchiveOrder($data[$root]['google-order-number']['VALUE'] );
221 $Gresponse->SendAck();
224 case "chargeback-amount-notification": {
225 $Gresponse->SendAck();
228 case "refund-amount-notification": {
229 $Gresponse->SendAck();
232 case "risk-information-notification": {
233 $Gresponse->SendAck();
237 $Gresponse->SendBadRequestStatus("Invalid or not supported Message");
240 /* In case the XML API contains multiple open tags
241 with the same value, then invoke this function and
242 perform a foreach on the resultant array.
243 This takes care of cases when there is only one unique tag
245 Examples of this are "anonymous-address", "merchant-code-string"
246 from the merchant-calculations-callback API
248 function get_arr_result($child_node) {
250 if(isset($child_node)) {
251 if(is_associative_array($child_node)) {
252 $result[] = $child_node;
255 foreach($child_node as $curr_node){
256 $result[] = $curr_node;
263 /* Returns true if a given variable represents an associative array */
264 function is_associative_array( $var ) {
265 return is_array( $var ) && !is_numeric( implode( '', array_keys( $var ) ) );