4 * Copyright (C) 2006 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 class is instantiated everytime any notification or
20 * order processing commands are received.
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
25 * Refer demo/responsehandlerdemo.php for different use case scenarios
28 class GoogleResponse
{
35 var $checkout_diagnose_url;
37 var $request_diagnose_url;
48 * @param string $server_type
50 function GoogleResponse($id, $key, $response, $server_type = "checkout") {
51 $this->merchant_id
= $id;
52 $this->merchant_key
= $key;
54 if ($server_type == "sandbox") {
56 $this->server_url
= "https://sandbox.google.com/checkout/";
58 else $this->server_url
= "https://checkout.google.com/";
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";
67 $this->response
= $response;
69 if (strpos(__FILE__
, ':') !== FALSE) {
71 $path_delimiter = ';';
73 else $path_delimiter = ':';
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();
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
93 $compare_mer_id = substr($auth, 0, strpos($auth, ":"));
94 $compare_mer_key = substr($auth, strpos($auth, ":") +
1);
99 if ($compare_mer_id != $this->merchant_id ||
100 $compare_mer_key != $this->merchant_key
106 * @param $google_order
107 * @param string $amount
108 * @param $message_log
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 . "\">";
114 $postargs .= "<amount currency=\"USD\">" . $amount . "</amount>";
116 $postargs .= "</charge-order>";
117 return $this->SendReq($this->request_url
, $this->GetAuthenticationHeaders(),
118 $postargs, $message_log
123 * @param $google_order
127 * @param $message_log
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>
136 return $this->SendReq($this->request_url
, $this->GetAuthenticationHeaders(),
137 $postargs, $message_log
142 * @param $google_order
145 * @param $message_log
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>
153 return $this->SendReq($this->request_url
, $this->GetAuthenticationHeaders(),
154 $postargs, $message_log
159 * @param $google_order
161 * @param $tracking_no
162 * @param $message_log
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 . "\">
168 <carrier>" . htmlentities($carrier) . "</carrier>
169 <tracking-number>" . $tracking_no . "</tracking-number>
171 </add-tracking-data>";
172 return $this->SendReq($this->request_url
, $this->GetAuthenticationHeaders(),
173 $postargs, $message_log
178 * @param $google_order
179 * @param $merchant_order
180 * @param $message_log
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
193 * @param $google_order
195 * @param string $send_mail
196 * @param $message_log
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
210 * @param $google_order
211 * @param $message_log
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
222 * @param $google_order
224 * @param $tracking_no
225 * @param string $send_mail
226 * @param $message_log
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 . "\">
232 <carrier>" . htmlentities($carrier) . "</carrier>
233 <tracking-number>" . $tracking_no . "</tracking-number>
235 <send-email>" . $send_mail . "</send-email>
237 return $this->SendReq($this->request_url
, $this->GetAuthenticationHeaders(),
238 $postargs, $message_log
243 * @param $google_order
244 * @param $message_log
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
255 * @param $google_order
256 * @param $message_log
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
267 * @param $merchant_calc
269 function ProcessMerchantCalculations($merchant_calc) {
270 $result = $merchant_calc->GetXML();
277 function GetAuthenticationHeaders() {
279 $headers[] = "Authorization: Basic " . base64_encode(
280 $this->merchant_id
. ':' . $this->merchant_key
282 $headers[] = "Content-Type: application/xml";
283 $headers[] = "Accept: application/xml";
291 * @param $message_log
293 function SendReq($url, $header_arr, $postargs, $message_log) {
294 // Get the curl session object
295 $session = curl_init($url);
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);
304 // Do the POST and then close the session
305 $response = curl_exec($session);
306 if (curl_errno($session)) {
307 die(curl_error($session));
310 curl_close($session);
313 // Get HTTP Status code from the response
314 $status_code = array();
315 preg_match('/\d\d\d/', $response, $status_code);
318 switch ($status_code[0]) {
324 die('Error 503: Service unavailable.');
328 die('Error 403: Forbidden.');
332 die('Error 400: Bad request.');
337 die('Error :' . $status_code[0]);
339 fwrite($message_log, sprintf("\n\r%s:- %s\n", date("D M j G:i:s T Y"),
345 $acknowledgment = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "<notification-acknowledgment xmlns=\"" . $this->schema_url
. "\"/>";
346 echo $acknowledgment;