commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / packages / Google / demo / responsehandlerdemo.php
1 <?php
2
3 /**
4 * Copyright (C) 2007 Google Inc.
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 */
18
19 /* This is the response handler code that will be invoked every time
20 * a notification or request is sent by the Google Server
21 *
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
24 * "API Callback URL'
25 * Order processing commands can be sent automatically by placing these
26 * commands appropriately
27 *
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
32 *
33 */
34
35 chdir("..");
36 require_once('library/googleresponse.php');
37 require_once('library/googlemerchantcalculations.php');
38 require_once('library/googleresult.php');
39 require_once('library/googlerequest.php');
40
41 define('RESPONSE_HANDLER_ERROR_LOG_FILE', 'googleerror.log');
42 define('RESPONSE_HANDLER_LOG_FILE', 'googlemessage.log');
43
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
48
49 $Gresponse = new GoogleResponse($merchant_id, $merchant_key);
50
51 $Grequest = new GoogleRequest($merchant_id, $merchant_key, $server_type, $currency);
52
53 //Setup the log file
54 $Gresponse->SetLogFiles(RESPONSE_HANDLER_ERROR_LOG_FILE,
55 RESPONSE_HANDLER_LOG_FILE, L_ALL);
56
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);
62 }
63 list($root, $data) = $Gresponse->GetParsedXML($xml_response);
64 $Gresponse->SetMerchantAuthentication($merchant_id, $merchant_key);
65
66 /*$status = $Gresponse->HttpAuthentication();
67 if(! $status) {
68 die('authentication failed');
69 }*/
70
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>,
78 * <send_mail>);
79 * Send archive order: $Grequest->SendArchiveOrder($data[$root]
80 * ['google-order-number']['VALUE']);
81 *
82 */
83
84 switch ($root) {
85 case "request-received": {
86 break;
87 }
88 case "error": {
89 break;
90 }
91 case "diagnosis": {
92 break;
93 }
94 case "checkout-redirect": {
95 break;
96 }
97 case "merchant-calculation-callback": {
98 // Create the results and send it
99 $merchant_calc = new GoogleMerchantCalculations($currency);
100
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'];
109
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);
121
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);
126 }
127
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);
136 }
137 }
138 $merchant_calc->AddResult($merchant_result);
139 }
140 } else {
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);
146 }
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);
153 }
154 $merchant_calc->AddResult($merchant_result);
155 }
156 }
157 $Gresponse->ProcessMerchantCalculations($merchant_calc);
158 break;
159 }
160 case "new-order-notification": {
161 $Gresponse->SendAck();
162 break;
163 }
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'];
168
169 switch($new_financial_state) {
170 case 'REVIEWING': {
171 break;
172 }
173 case 'CHARGEABLE': {
174 //$Grequest->SendProcessOrder($data[$root]['google-order-number']['VALUE']);
175 //$Grequest->SendChargeOrder($data[$root]['google-order-number']['VALUE'],'');
176 break;
177 }
178 case 'CHARGING': {
179 break;
180 }
181 case 'CHARGED': {
182 break;
183 }
184 case 'PAYMENT_DECLINED': {
185 break;
186 }
187 case 'CANCELLED': {
188 break;
189 }
190 case 'CANCELLED_BY_GOOGLE': {
191 //$Grequest->SendBuyerMessage($data[$root]['google-order-number']['VALUE'],
192 // "Sorry, your order is cancelled by Google", true);
193 break;
194 }
195 default:
196 break;
197 }
198
199 switch($new_fulfillment_order) {
200 case 'NEW': {
201 break;
202 }
203 case 'PROCESSING': {
204 break;
205 }
206 case 'DELIVERED': {
207 break;
208 }
209 case 'WILL_NOT_DELIVER': {
210 break;
211 }
212 default:
213 break;
214 }
215 break;
216 }
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();
222 break;
223 }
224 case "chargeback-amount-notification": {
225 $Gresponse->SendAck();
226 break;
227 }
228 case "refund-amount-notification": {
229 $Gresponse->SendAck();
230 break;
231 }
232 case "risk-information-notification": {
233 $Gresponse->SendAck();
234 break;
235 }
236 default:
237 $Gresponse->SendBadRequestStatus("Invalid or not supported Message");
238 break;
239 }
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
244 or multiple tags.
245 Examples of this are "anonymous-address", "merchant-code-string"
246 from the merchant-calculations-callback API
247 */
248 function get_arr_result($child_node) {
249 $result = array();
250 if(isset($child_node)) {
251 if(is_associative_array($child_node)) {
252 $result[] = $child_node;
253 }
254 else {
255 foreach($child_node as $curr_node){
256 $result[] = $curr_node;
257 }
258 }
259 }
260 return $result;
261 }
262
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 ) ) );
266 }
267 ?>