commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / tools / extensions / org.civicrm.payment.googlecheckout / packages / Google / library / googleresponse.php
1 <?php
2
3 /**
4 * Copyright (C) 2006 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 class is instantiated everytime any notification or
20 * order processing commands are received.
21 *
22 * It has a SendReq function to post different requests to the Google Server
23 * Send functions are provided for most of the commands that are supported
24 * by the server
25 * Refer demo/responsehandlerdemo.php for different use case scenarios
26 * for this code
27 */
28 class GoogleResponse {
29 var $merchant_id;
30 var $merchant_key;
31 var $server_url;
32 var $schema_url;
33 var $base_url;
34 var $checkout_url;
35 var $checkout_diagnose_url;
36 var $request_url;
37 var $request_diagnose_url;
38
39 var $response;
40 var $root;
41 var $data;
42 var $xml_parser;
43
44 /**
45 * @param $id
46 * @param $key
47 * @param $response
48 * @param string $server_type
49 */
50 function GoogleResponse($id, $key, $response, $server_type = "checkout") {
51 $this->merchant_id = $id;
52 $this->merchant_key = $key;
53
54 if ($server_type == "sandbox") {
55
56 $this->server_url = "https://sandbox.google.com/checkout/";
57 }
58 else $this->server_url = "https://checkout.google.com/";
59
60 $this->schema_url = "http://checkout.google.com/schema/2";
61 $this->base_url = $this->server_url . "cws/v2/Merchant/" . $this->merchant_id;
62 $this->checkout_url = $this->base_url . "/checkout";
63 $this->checkout_diagnose_url = $this->base_url . "/checkout/diagnose";
64 $this->request_url = $this->base_url . "/request";
65 $this->request_diagnose_url = $this->base_url . "/request/diagnose";
66
67 $this->response = $response;
68
69 if (strpos(__FILE__, ':') !== FALSE) {
70
71 $path_delimiter = ';';
72 }
73 else $path_delimiter = ':';
74
75 ini_set('include_path', ini_get('include_path') . $path_delimiter . '.');
76 require_once ('xml-processing/xmlparser.php');
77 $this->xml_parser = new XmlParser($response);
78 $this->root = $this->xml_parser->GetRoot();
79 $this->data = $this->xml_parser->GetData();
80 }
81
82 /**
83 * @param $headers
84 *
85 * @return bool
86 */
87 function HttpAuthentication($headers) {
88 if (isset($headers['Authorization'])) {
89 $auth_encode = $headers['Authorization'];
90 $auth = base64_decode(substr($auth_encode,
91 strpos($auth_encode, " ") + 1
92 ));
93 $compare_mer_id = substr($auth, 0, strpos($auth, ":"));
94 $compare_mer_key = substr($auth, strpos($auth, ":") + 1);
95 }
96 else {
97 return FALSE;
98 }
99 if ($compare_mer_id != $this->merchant_id ||
100 $compare_mer_key != $this->merchant_key
101 ) return FALSE;
102 return TRUE;
103 }
104
105 /**
106 * @param $google_order
107 * @param string $amount
108 * @param $message_log
109 */
110 function SendChargeOrder($google_order, $amount = '', $message_log) {
111 $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
112 <charge-order xmlns=\"" . $this->schema_url . "\" google-order-number=\"" . $google_order . "\">";
113 if ($amount != '') {
114 $postargs .= "<amount currency=\"USD\">" . $amount . "</amount>";
115 }
116 $postargs .= "</charge-order>";
117 return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
118 $postargs, $message_log
119 );
120 }
121
122 /**
123 * @param $google_order
124 * @param $amount
125 * @param $reason
126 * @param $comment
127 * @param $message_log
128 */
129 function SendRefundOrder($google_order, $amount, $reason, $comment, $message_log) {
130 $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
131 <refund-order xmlns=\"" . $this->schema_url . "\" google-order-number=\"" . $google_order . "\">
132 <reason>" . $reason . "</reason>
133 <amount currency=\"USD\">" . htmlentities($amount) . "</amount>
134 <comment>" . htmlentities($comment) . "</comment>
135 </refund-order>";
136 return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
137 $postargs, $message_log
138 );
139 }
140
141 /**
142 * @param $google_order
143 * @param $reason
144 * @param $comment
145 * @param $message_log
146 */
147 function SendCancelOrder($google_order, $reason, $comment, $message_log) {
148 $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
149 <cancel-order xmlns=\"" . $this->schema_url . "\" google-order-number=\"" . $google_order . "\">
150 <reason>" . htmlentities($reason) . "</reason>
151 <comment>" . htmlentities($comment) . "</comment>
152 </cancel-order>";
153 return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
154 $postargs, $message_log
155 );
156 }
157
158 /**
159 * @param $google_order
160 * @param $carrier
161 * @param $tracking_no
162 * @param $message_log
163 */
164 function SendTrackingData($google_order, $carrier, $tracking_no, $message_log) {
165 $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
166 <add-tracking-data xmlns=\"" . $this->schema_url . "\" google-order-number=\"" . $google_order . "\">
167 <tracking-data>
168 <carrier>" . htmlentities($carrier) . "</carrier>
169 <tracking-number>" . $tracking_no . "</tracking-number>
170 </tracking-data>
171 </add-tracking-data>";
172 return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
173 $postargs, $message_log
174 );
175 }
176
177 /**
178 * @param $google_order
179 * @param $merchant_order
180 * @param $message_log
181 */
182 function SendMerchantOrderNumber($google_order, $merchant_order, $message_log) {
183 $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
184 <add-merchant-order-number xmlns=\"" . $this->schema_url . "\" google-order-number=\"" . $google_order . "\">
185 <merchant-order-number>" . $merchant_order . "</merchant-order-number>
186 </add-merchant-order-number>";
187 return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
188 $postargs, $message_log
189 );
190 }
191
192 /**
193 * @param $google_order
194 * @param $message
195 * @param string $send_mail
196 * @param $message_log
197 */
198 function SendBuyerMessage($google_order, $message, $send_mail = "true", $message_log) {
199 $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
200 <send-buyer-message xmlns=\"" . $this->schema_url . "\" google-order-number=\"" . $google_order . "\">
201 <message>" . $message . "</message>
202 <send-mail>" . $send_mail . "</send-mail>
203 </send-buyer-message>";
204 return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
205 $postargs, $message_log
206 );
207 }
208
209 /**
210 * @param $google_order
211 * @param $message_log
212 */
213 function SendProcessOrder($google_order, $message_log) {
214 $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
215 <process-order xmlns=\"" . $this->schema_url . "\" google-order-number=\"" . $google_order . "\"/> ";
216 return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
217 $postargs, $message_log
218 );
219 }
220
221 /**
222 * @param $google_order
223 * @param $carrier
224 * @param $tracking_no
225 * @param string $send_mail
226 * @param $message_log
227 */
228 function SendDeliverOrder($google_order, $carrier, $tracking_no, $send_mail = "true", $message_log) {
229 $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
230 <deliver-order xmlns=\"" . $this->schema_url . "\" google-order-number=\"" . $google_order . "\">
231 <tracking-data>
232 <carrier>" . htmlentities($carrier) . "</carrier>
233 <tracking-number>" . $tracking_no . "</tracking-number>
234 </tracking-data>
235 <send-email>" . $send_mail . "</send-email>
236 </deliver-order>";
237 return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
238 $postargs, $message_log
239 );
240 }
241
242 /**
243 * @param $google_order
244 * @param $message_log
245 */
246 function SendArchiveOrder($google_order, $message_log) {
247 $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
248 <archive-order xmlns=\"" . $this->schema_url . "\" google-order-number=\"" . $google_order . "\"/>";
249 return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
250 $postargs, $message_log
251 );
252 }
253
254 /**
255 * @param $google_order
256 * @param $message_log
257 */
258 function SendUnarchiveOrder($google_order, $message_log) {
259 $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
260 <unarchive-order xmlns=\"" . $this->schema_url . "\" google-order-number=\"" . $google_order . "\"/>";
261 return $this->SendReq($this->request_url, $this->GetAuthenticationHeaders(),
262 $postargs, $message_log
263 );
264 }
265
266 /**
267 * @param $merchant_calc
268 */
269 function ProcessMerchantCalculations($merchant_calc) {
270 $result = $merchant_calc->GetXML();
271 echo $result;
272 }
273
274 /**
275 * @return array
276 */
277 function GetAuthenticationHeaders() {
278 $headers = array();
279 $headers[] = "Authorization: Basic " . base64_encode(
280 $this->merchant_id . ':' . $this->merchant_key
281 );
282 $headers[] = "Content-Type: application/xml";
283 $headers[] = "Accept: application/xml";
284 return $headers;
285 }
286
287 /**
288 * @param $url
289 * @param $header_arr
290 * @param $postargs
291 * @param $message_log
292 */
293 function SendReq($url, $header_arr, $postargs, $message_log) {
294 // Get the curl session object
295 $session = curl_init($url);
296
297 // Set the POST options.
298 curl_setopt($session, CURLOPT_POST, TRUE);
299 curl_setopt($session, CURLOPT_HTTPHEADER, $header_arr);
300 curl_setopt($session, CURLOPT_POSTFIELDS, $postargs);
301 curl_setopt($session, CURLOPT_HEADER, TRUE);
302 curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
303
304 // Do the POST and then close the session
305 $response = curl_exec($session);
306 if (curl_errno($session)) {
307 die(curl_error($session));
308 }
309 else {
310 curl_close($session);
311 }
312
313 // Get HTTP Status code from the response
314 $status_code = array();
315 preg_match('/\d\d\d/', $response, $status_code);
316
317 // Check for errors
318 switch ($status_code[0]) {
319 case 200:
320 // Success
321 break;
322
323 case 503:
324 die('Error 503: Service unavailable.');
325 break;
326
327 case 403:
328 die('Error 403: Forbidden.');
329 break;
330
331 case 400:
332 die('Error 400: Bad request.');
333 break;
334
335 default:
336 echo $response;
337 die('Error :' . $status_code[0]);
338 }
339 fwrite($message_log, sprintf("\n\r%s:- %s\n", date("D M j G:i:s T Y"),
340 $response
341 ));
342 }
343
344 function SendAck() {
345 $acknowledgment = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "<notification-acknowledgment xmlns=\"" . $this->schema_url . "\"/>";
346 echo $acknowledgment;
347 }
348 }
349