PageRenderTime 62ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/googlecheckout/googlerequest.php

https://bitbucket.org/MXWest/magento-ce-1.5.1.0
PHP | 769 lines | 426 code | 52 blank | 291 comment | 45 complexity | 2d9f2ffdb96c38f57772c151f517fe4c MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /*
  3. * Copyright (C) 2007 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. *
  18. */
  19. /** This class is instantiated everytime any notification or
  20. * order processing commands are received.
  21. *
  22. * @version $Id: googlerequest.php 1234 2007-09-25 14:58:57Z ropu $
  23. * It has a SendReq function to post different requests to the Google Server
  24. * Send functions are provided for most of the commands that are supported
  25. * by the server for this code
  26. */
  27. define('PHP_SAMPLE_CODE_VERSION', 'v1.2.5');
  28. define('ENTER', "\r\n");
  29. define('DOUBLE_ENTER', ENTER.ENTER);
  30. // Max size of the Google Messsage string
  31. define('GOOGLE_MESSAGE_LENGTH', 254);
  32. define('GOOGLE_REASON_LENGTH', 140);
  33. /**
  34. * Send requests to the Google Checkout server to perform different actions
  35. */
  36. // refer demo/responsehandlerdemo.php for different use case scenarios
  37. class GoogleRequest {
  38. var $merchant_id;
  39. var $merchant_key;
  40. var $currency;
  41. var $server_url;
  42. var $schema_url;
  43. var $base_url;
  44. var $checkout_url;
  45. var $checkout_diagnose_url;
  46. var $request_url;
  47. var $request_diagnose_url;
  48. var $merchant_checkout;
  49. var $proxy = array();
  50. var $certPath='';
  51. var $log;
  52. /**
  53. * @param string $id the merchant id
  54. * @param string $key the merchant key
  55. * @param string $server_type the server type of the server to be used, one
  56. * of 'sandbox' or 'production'.
  57. * defaults to 'sandbox'
  58. * @param string $currency the currency of the items to be added to the cart
  59. * , as of now values can be 'USD' or 'GBP'.
  60. * defaults to 'USD'
  61. */
  62. function GoogleRequest($id, $key, $server_type="sandbox", $currency="USD") {
  63. $this->merchant_id = $id;
  64. $this->merchant_key = $key;
  65. $this->currency = $currency;
  66. if(strtolower($server_type) == "sandbox") {
  67. $this->server_url = "https://sandbox.google.com/checkout/";
  68. } else {
  69. $this->server_url = "https://checkout.google.com/";
  70. }
  71. $this->schema_url = "http://checkout.google.com/schema/2";
  72. $this->base_url = $this->server_url . "api/checkout/v2/";
  73. $this->request_url = $this->base_url . "request/Merchant/" . $this->merchant_id;
  74. $this->merchant_checkout = $this->base_url . "merchantCheckout/Merchant/" . $this->merchant_id;
  75. #ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'.');
  76. require_once('googlelog.php');
  77. $this->log = new GoogleLog('', '', L_OFF);
  78. }
  79. function SetLogFiles($errorLogFile, $messageLogFile, $logLevel=L_ERR_RQST) {
  80. $this->log = new GoogleLog($errorLogFile, $messageLogFile, $logLevel);
  81. }
  82. /**
  83. * Submit a SetCertificatePath request.
  84. *
  85. * GC Accepted SSL Certs:
  86. * {@link http://googlecheckoutapi.blogspot.com/2007/07/accepting-50-additional-ssl-root-cas.html}
  87. *
  88. * @param string $certPath The name of a file holding one or more certificates
  89. * to verify the peer with
  90. */
  91. function SetCertificatePath($certPath) {
  92. $this->certPath = $certPath;
  93. }
  94. /**
  95. * Submit a server-to-server request.
  96. *
  97. * more info:
  98. * {@link http://code.google.com/apis/checkout/developer/index.html#alternate_technique}
  99. *
  100. * @param string $xml_cart the cart's xml
  101. * @param bool $die whether to die() or not after performing the request,
  102. * defaults to true
  103. *
  104. * @return array with the returned http status code (200 if OK) in index 0
  105. * and the redirect url returned by the server in index 1
  106. */
  107. function SendServer2ServerCart($xml_cart, $die=true) {
  108. list($status, $body) = $this->SendReq($this->merchant_checkout,
  109. $this->GetAuthenticationHeaders(), $xml_cart);
  110. if($status != 200 ){
  111. return array($status, $body);
  112. } else {
  113. #ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'.');
  114. require_once('xml-processing/gc_xmlparser.php');
  115. $xml_parser = new gc_xmlparser($body);
  116. $root = $xml_parser->GetRoot();
  117. $data = $xml_parser->GetData();
  118. $this->log->logRequest("Redirecting to: ".
  119. $data[$root]['redirect-url']['VALUE']);
  120. header('Location: ' . $data[$root]['redirect-url']['VALUE']);
  121. if($die) {
  122. die($data[$root]['redirect-url']['VALUE']);
  123. }
  124. else {
  125. return array(200, $data[$root]['redirect-url']['VALUE']);
  126. }
  127. }
  128. }
  129. /**
  130. * Send a <charge-order> command to the Google Checkout server
  131. *
  132. * info: {@link http://code.google.com/apis/checkout/developer/index.html#charge_order_example}
  133. *
  134. * @param string $google_order the google id of the order
  135. * @param double $amount the amount to be charged, if empty the whole
  136. * amount of the order will be charged
  137. *
  138. * @return array the status code and body of the response
  139. */
  140. function SendChargeOrder($google_order, $amount='') {
  141. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  142. <charge-order xmlns=\"".$this->schema_url.
  143. "\" google-order-number=\"". $google_order. "\">";
  144. if ($amount != '') {
  145. $postargs .= "<amount currency=\"" . $this->currency . "\">" .
  146. $amount . "</amount>";
  147. }
  148. $postargs .= "</charge-order>";
  149. return $this->SendReq($this->request_url,
  150. $this->GetAuthenticationHeaders(), $postargs);
  151. }
  152. /**
  153. * Send a <refund-order> command to the Google Checkout server
  154. *
  155. * info: {@link http://code.google.com/apis/checkout/developer/index.html#refund_order_example}
  156. *
  157. * @param string $google_order the google id of the order
  158. * @param double $amount the amount to be refunded, if empty the whole
  159. * amount of the order will be refunded
  160. * @param string $reason the reason why the refund is taking place
  161. * @param string $comment a comment about the refund
  162. *
  163. * @return array the status code and body of the response
  164. */
  165. function SendRefundOrder($google_order, $amount, $reason,
  166. $comment='') {
  167. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  168. <refund-order xmlns=\"".$this->schema_url.
  169. "\" google-order-number=\"". $google_order. "\">" .
  170. "<reason>". $reason . "</reason>";
  171. if($amount!=0) {
  172. $postargs .= "<amount currency=\"" . $this->currency . "\">".
  173. htmlentities($amount)."</amount>";
  174. }
  175. $postargs .= "<comment>". htmlentities($comment) . "</comment>
  176. </refund-order>";
  177. return $this->SendReq($this->request_url,
  178. $this->GetAuthenticationHeaders(), $postargs);
  179. }
  180. /**
  181. * Send a <cancel-order> command to the Google Checkout server
  182. *
  183. * info: {@link http://code.google.com/apis/checkout/developer/index.html#refund_order_example}
  184. *
  185. * @param string $google_order the google id of the order
  186. * @param string $reason the reason why the order is being cancelled
  187. * @param string $comment a comment about the cancellation
  188. *
  189. * @return array the status code and body of the response
  190. */
  191. function SendCancelOrder($google_order, $reason, $comment) {
  192. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  193. <cancel-order xmlns=\"".$this->schema_url.
  194. "\" google-order-number=\"". $google_order. "\">
  195. <reason>".
  196. (substr(htmlentities(strip_tags($reason)),0,GOOGLE_REASON_LENGTH)) .
  197. "</reason>
  198. <comment>".
  199. (substr(htmlentities(strip_tags($comment)),0,GOOGLE_REASON_LENGTH)) .
  200. "</comment>
  201. </cancel-order>";
  202. return $this->SendReq($this->request_url,
  203. $this->GetAuthenticationHeaders(), $postargs);
  204. }
  205. /**
  206. * Send an <add-tracking-data> command to the Google Checkout server, which
  207. * will associate a shipper's tracking number with an order.
  208. *
  209. * info: {@link http://code.google.com/apis/checkout/developer/index.html#add_tracking_data_example}
  210. *
  211. * @param string $google_order the google id of the order
  212. * @param string $carrier the carrier, valid values are "DHL", "FedEx",
  213. * "UPS", "USPS" and "Other"
  214. * @param string $tracking_no the shipper's tracking number
  215. *
  216. * @return array the status code and body of the response
  217. */
  218. function SendTrackingData($google_order, $carrier,
  219. $tracking_no) {
  220. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  221. <add-tracking-data xmlns=\"". $this->schema_url .
  222. "\" google-order-number=\"". $google_order . "\">
  223. <tracking-data>
  224. <carrier>". htmlentities($carrier) . "</carrier>
  225. <tracking-number>". $tracking_no . "</tracking-number>
  226. </tracking-data>
  227. </add-tracking-data>";
  228. return $this->SendReq($this->request_url,
  229. $this->GetAuthenticationHeaders(), $postargs);
  230. }
  231. /**
  232. * Send an <add-merchant-order-number> command to the Google Checkout
  233. * server, which will associate a merchant order number with an order
  234. *
  235. * info: {@link http://code.google.com/apis/checkout/developer/index.html#add_merchant_order_number_example}
  236. *
  237. * @param string $google_order the google id of the order
  238. * @param string $merchant_order the merchant id of the order
  239. *
  240. * @return array the status code and body of the response
  241. */
  242. function SendMerchantOrderNumber($google_order,
  243. $merchant_order, $timeout=false) {
  244. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  245. <add-merchant-order-number xmlns=\"". $this->schema_url .
  246. "\" google-order-number=\"". $google_order . "\">
  247. <merchant-order-number>" . $merchant_order .
  248. "</merchant-order-number>
  249. </add-merchant-order-number>";
  250. return $this->SendReq($this->request_url,
  251. $this->GetAuthenticationHeaders(), $postargs, $timeout);
  252. }
  253. /**
  254. * Send a <send-buyer-message> command to the Google Checkout
  255. * server, which will place a message in the customer's Google Checkout
  256. * account
  257. *
  258. * info: {@link http://code.google.com/apis/checkout/developer/index.html#send_buyer_message_example}
  259. *
  260. * @param string $google_order the google id of the order
  261. * @param string $message the message to be sent to the customer
  262. * @param string $send_email whether Google should send an email to
  263. * the buyer, use "true" or"false",
  264. * defaults to "true"
  265. *
  266. * @return array the status code and body of the response
  267. */
  268. function SendBuyerMessage($google_order, $message,
  269. $send_mail="true", $timeout=false) {
  270. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  271. <send-buyer-message xmlns=\"". $this->schema_url .
  272. "\" google-order-number=\"". $google_order . "\">
  273. <message>" .
  274. (substr(htmlentities(strip_tags($message)),0,GOOGLE_MESSAGE_LENGTH))
  275. . "</message>
  276. <send-email>" . strtolower($send_mail) . "</send-email>
  277. </send-buyer-message>";
  278. return $this->SendReq($this->request_url,
  279. $this->GetAuthenticationHeaders(), $postargs, $timeout);
  280. }
  281. /**
  282. * Send a <process-order> command to the Google Checkout
  283. * server, which will update an order's fulfillment state from NEW to
  284. * PROCESSING
  285. *
  286. * info: {@link http://code.google.com/apis/checkout/developer/index.html#process_order_example}
  287. *
  288. * @param string $google_order the google id of the order
  289. *
  290. * @return array the status code and body of the response
  291. */
  292. function SendProcessOrder($google_order) {
  293. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  294. <process-order xmlns=\"".$this->schema_url .
  295. "\" google-order-number=\"". $google_order. "\"/> ";
  296. return $this->SendReq($this->request_url,
  297. $this->GetAuthenticationHeaders(), $postargs);
  298. }
  299. /**
  300. * Send a <deliver-order> command to the Google Checkout server, which
  301. * will update an order's fulfillment state from either NEW or PROCESSING
  302. * to DELIVERED
  303. *
  304. * info: {@link http://code.google.com/apis/checkout/developer/index.html#deliver_order_example}
  305. *
  306. * @param string $google_order the google id of the order
  307. * @param string $carrier the carrier, valid values are "DHL", "FedEx",
  308. * "UPS", "USPS" and "Other"
  309. * @param string $tracking_no the shipper's tracking number
  310. * @param string $send_email whether Google should send an email to
  311. * the buyer, use "true" or"false",
  312. * defaults to "true"
  313. *
  314. * @return array the status code and body of the response
  315. */
  316. function SendDeliverOrder($google_order, $carrier="",
  317. $tracking_no="", $send_mail = "true") {
  318. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  319. <deliver-order xmlns=\"". $this->schema_url .
  320. "\" google-order-number=\"". $google_order . "\">";
  321. if($carrier != "" && $tracking_no != "") {
  322. $postargs .= "<tracking-data>
  323. <carrier>". htmlentities($carrier) . "</carrier>
  324. <tracking-number>". htmlentities($tracking_no) . "</tracking-number>
  325. </tracking-data>";
  326. }
  327. $postargs .= "<send-email>". strtolower($send_mail) . "</send-email>
  328. </deliver-order>";
  329. return $this->SendReq($this->request_url,
  330. $this->GetAuthenticationHeaders(), $postargs);
  331. }
  332. /**
  333. * Send a <archive-order> command to the Google Checkout
  334. * server, which removes an order from the merchant's Merchant Center Inbox
  335. *
  336. * info: {@link http://code.google.com/apis/checkout/developer/index.html#archive_order_example}
  337. *
  338. * @param string $google_order the google id of the order
  339. *
  340. * @return array the status code and body of the response
  341. */
  342. function SendArchiveOrder($google_order) {
  343. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  344. <archive-order xmlns=\"".$this->schema_url.
  345. "\" google-order-number=\"". $google_order. "\"/>";
  346. return $this->SendReq($this->request_url,
  347. $this->GetAuthenticationHeaders(), $postargs);
  348. }
  349. /**
  350. * Send a <archive-order> command to the Google Checkout
  351. * server, which returns a previously archived order to the merchant's
  352. * Merchant Center Inbox
  353. *
  354. * info: {@link http://code.google.com/apis/checkout/developer/index.html#unarchive_order_example}
  355. *
  356. * @param string $google_order the google id of the order
  357. *
  358. * @return array the status code and body of the response
  359. */
  360. function SendUnarchiveOrder($google_order) {
  361. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  362. <unarchive-order xmlns=\"".
  363. $this->schema_url."\" google-order-number=\"".
  364. $google_order. "\"/>";
  365. return $this->SendReq($this->request_url,
  366. $this->GetAuthenticationHeaders(), $postargs);
  367. }
  368. /**
  369. * Ship items API Commands
  370. * info: {@link http://code.google.com/apis/checkout/developer/Google_Checkout_XML_API_Line_Item_Shipping.html}
  371. *
  372. *
  373. */
  374. /**
  375. * Send a <ship-items> command to the Google Checkout
  376. * server,
  377. *
  378. * info: {@link http://code.google.com/apis/checkout/developer/Google_Checkout_XML_API_Line_Item_Shipping.html#tag_ship-items}
  379. *
  380. * @param string $google_order the google id of the order
  381. * @param array $items_list a list of GoogleShipItem classes.
  382. * @param string $send_email whether Google should send an email to
  383. * the buyer, use "true" or"false",
  384. * defaults to "true"
  385. *
  386. * @return array the status code and body of the response
  387. */
  388. function SendShipItems($google_order, $items_list=array(), $send_mail="true") {
  389. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  390. <ship-items xmlns=\"". $this->schema_url .
  391. "\" google-order-number=\"". $google_order . "\">" .
  392. "<item-shipping-information-list>\n";
  393. foreach($items_list as $item) {
  394. $postargs .= "<item-shipping-information>
  395. <item-id>
  396. <merchant-item-id>" . $item->merchant_item_id . "</merchant-item-id>
  397. </item-id>\n";
  398. if(count($item->tracking_data_list)) {
  399. $postargs .= "<tracking-data-list>\n";
  400. foreach($item->tracking_data_list as $tracking_data) {
  401. $postargs .= "<tracking-data>
  402. <carrier>". htmlentities($tracking_data['carrier']) . "</carrier>
  403. <tracking-number>". $tracking_data['tracking-number'] . "</tracking-number>
  404. </tracking-data>\n";
  405. }
  406. $postargs .= "</tracking-data-list>\n";
  407. }
  408. $postargs .= "</item-shipping-information>\n";
  409. }
  410. $postargs .= "</item-shipping-information-list>\n" .
  411. "<send-email>". strtolower($send_mail) . "</send-email>
  412. </ship-items>";
  413. return $this->SendReq($this->request_url,
  414. $this->GetAuthenticationHeaders(), $postargs);
  415. }
  416. /**
  417. * Send a <backorder-items> command to the Google Checkout
  418. *
  419. * info: {@link http://code.google.com/apis/checkout/developer/Google_Checkout_XML_API_Line_Item_Shipping.html#tag_backorder-items}
  420. *
  421. * @param string $google_order the google id of the order
  422. * @param array $items_list a list of GoogleShipItem classes.
  423. * @param string $send_email whether Google should send an email to
  424. * the buyer, use "true" or"false",
  425. * defaults to "true"
  426. *
  427. * @return array the status code and body of the response
  428. */
  429. function SendBackorderItems($google_order, $items_list=array(), $send_mail="true") {
  430. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  431. <backorder-items xmlns=\"".
  432. $this->schema_url."\" google-order-number=\"".
  433. $google_order. "\">";
  434. $postargs .= "<item-ids>";
  435. foreach($items_list as $item) {
  436. $postargs .= "<item-id>
  437. <merchant-item-id>" . $item->merchant_item_id . "</merchant-item-id>
  438. </item-id>";
  439. }
  440. $postargs .= "</item-ids>";
  441. $postargs .= "<send-email>". strtolower($send_mail) . "</send-email>
  442. </backorder-items>";
  443. return $this->SendReq($this->request_url,
  444. $this->GetAuthenticationHeaders(), $postargs);
  445. }
  446. /**
  447. * Send a <cancel-items> command to the Google Checkout
  448. *
  449. * info: {@link http://code.google.com/apis/checkout/developer/Google_Checkout_XML_API_Line_Item_Shipping.html#tag_cancel-items}
  450. *
  451. * @param string $google_order the google id of the order
  452. * @param array $items_list a list of GoogleShipItem classes.
  453. * @param string $reason the reason why the refund is taking place
  454. * @param string $comment a comment about the refund
  455. * @param string $send_email whether Google should send an email to
  456. * the buyer, use "true" or"false",
  457. * defaults to "true"
  458. *
  459. * @return array the status code and body of the response
  460. */
  461. function SendCancelItems($google_order, $items_list=array(), $reason,
  462. $comment='', $send_mail="true") {
  463. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  464. <cancel-items xmlns=\"".
  465. $this->schema_url."\" google-order-number=\"".
  466. $google_order. "\">";
  467. $postargs .= "<item-ids>";
  468. foreach($items_list as $item) {
  469. $postargs .= "<item-id>
  470. <merchant-item-id>" . $item->merchant_item_id . "</merchant-item-id>
  471. </item-id>";
  472. }
  473. $postargs .= "</item-ids>";
  474. $postargs .= "<send-email>". strtolower($send_mail) . "</send-email>
  475. <reason>".
  476. (substr(htmlentities(strip_tags($reason)),0,GOOGLE_REASON_LENGTH)) .
  477. "</reason>
  478. <comment>".
  479. (substr(htmlentities(strip_tags($comment)),0,GOOGLE_REASON_LENGTH)) .
  480. "</comment>
  481. </cancel-items>";
  482. return $this->SendReq($this->request_url,
  483. $this->GetAuthenticationHeaders(), $postargs);
  484. }
  485. /**
  486. * Send a <return-items> command to the Google Checkout
  487. *
  488. * info: {@link http://code.google.com/apis/checkout/developer/Google_Checkout_XML_API_Line_Item_Shipping.html#tag_return-items}
  489. *
  490. * @param string $google_order the google id of the order
  491. * @param array $items_list a list of GoogleShipItem classes.
  492. * @param string $send_email whether Google should send an email to
  493. * the buyer, use "true" or"false",
  494. * defaults to "true"
  495. *
  496. * @return array the status code and body of the response
  497. */
  498. function SendReturnItems($google_order, $items_list=array(), $send_mail="true") {
  499. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  500. <return-items xmlns=\"".
  501. $this->schema_url."\" google-order-number=\"".
  502. $google_order. "\">";
  503. $postargs .= "<item-ids>";
  504. foreach($items_list as $item) {
  505. $postargs .= "<item-id>
  506. <merchant-item-id>" . $item->merchant_item_id . "</merchant-item-id>
  507. </item-id>";
  508. }
  509. $postargs .= "</item-ids>";
  510. $postargs .= "<send-email>". strtolower($send_mail) . "</send-email>
  511. </return-items>";
  512. return $this->SendReq($this->request_url,
  513. $this->GetAuthenticationHeaders(), $postargs);
  514. }
  515. /**
  516. * Send a <reset-items-shipping-information> command to the Google Checkout
  517. *
  518. * info: {@link http://code.google.com/apis/checkout/developer/Google_Checkout_XML_API_Line_Item_Shipping.html#tag_reset-items-shipping-information}
  519. *
  520. * @param string $google_order the google id of the order
  521. * @param array $items_list a list of GoogleShipItem classes.
  522. * @param string $send_email whether Google should send an email to
  523. * the buyer, use "true" or"false",
  524. * defaults to "true"
  525. *
  526. * @return array the status code and body of the response
  527. */
  528. function SendResetItemsShippingInformation($google_order, $items_list=array(), $send_mail="true") {
  529. $postargs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  530. <reset-items-shipping-information xmlns=\"".
  531. $this->schema_url."\" google-order-number=\"".
  532. $google_order. "\">";
  533. $postargs .= "<item-ids>";
  534. foreach($items_list as $item) {
  535. $postargs .= "<item-id>
  536. <merchant-item-id>" . $item->merchant_item_id . "</merchant-item-id>
  537. </item-id>";
  538. }
  539. $postargs .= "</item-ids>";
  540. $postargs .= "<send-email>". strtolower($send_mail) . "</send-email>
  541. </reset-items-shipping-information>";
  542. return $this->SendReq($this->request_url,
  543. $this->GetAuthenticationHeaders(), $postargs);
  544. }
  545. /**
  546. * @access private
  547. */
  548. function GetAuthenticationHeaders() {
  549. $headers = array();
  550. $headers[] = "Authorization: Basic ".base64_encode(
  551. $this->merchant_id.':'.$this->merchant_key);
  552. $headers[] = "Content-Type: application/xml; charset=UTF-8";
  553. $headers[] = "Accept: application/xml; charset=UTF-8";
  554. $headers[] = "User-Agent: GC-PHP-Sample_code (" . PHP_SAMPLE_CODE_VERSION . "/ropu)";
  555. return $headers;
  556. }
  557. /**
  558. * Set the proxy to be used by the connections to the outside
  559. *
  560. * @param array $proxy Array('host' => 'proxy-host', 'port' => 'proxy-port')
  561. *
  562. */
  563. function SetProxy($proxy=array()) {
  564. if(is_array($proxy) && count($proxy)) {
  565. $this->proxy['host'] = $proxy['host'];
  566. $this->proxy['port'] = $proxy['port'];
  567. }
  568. }
  569. /**
  570. * @access private
  571. */
  572. function SendReq($url, $header_arr, $postargs, $timeout=false) {
  573. // Get the curl session object
  574. $session = curl_init($url);
  575. $this->log->LogRequest($postargs);
  576. if (Mage::getStoreConfig('google/checkout/debug')) {
  577. $debug = Mage::getModel('googlecheckout/api_debug');
  578. $debug->setDir('out')->setUrl($url)->setRequestBody($postargs)->save();
  579. }
  580. // Set the POST options.
  581. curl_setopt($session, CURLOPT_POST, true);
  582. curl_setopt($session, CURLOPT_HTTPHEADER, $header_arr);
  583. curl_setopt($session, CURLOPT_POSTFIELDS, $postargs);
  584. curl_setopt($session, CURLOPT_HEADER, true);
  585. curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
  586. if(!empty($this->certPath) && file_exists($this->certPath)) {
  587. curl_setopt($session, CURLOPT_SSL_VERIFYPEER, true);
  588. curl_setopt($session, CURLOPT_CAINFO, $this->certPath);
  589. }
  590. else {
  591. curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
  592. }
  593. if(is_array($this->proxy) && count($this->proxy)) {
  594. curl_setopt($session, CURLOPT_PROXY,
  595. $this->proxy['host'] . ":" . $this->proxy['port']);
  596. }
  597. if($timeout != false){
  598. curl_setopt($session, CURLOPT_TIMEOUT, $timeout);
  599. }
  600. // Do the POST and then close the session
  601. $response = curl_exec($session);
  602. if (!empty($debug)) {
  603. $debug->setResponseBody($response)->save();
  604. }
  605. if (curl_errno($session)) {
  606. $this->log->LogError(curl_error($session));
  607. return array("CURL_ERR", curl_error($session));
  608. } else {
  609. curl_close($session);
  610. }
  611. $heads = $this->parse_headers($response);
  612. $body = $this->get_body_x($response);
  613. // // Get HTTP Status code from the response
  614. $status_code = array();
  615. preg_match('/\d\d\d/', $heads[0], $status_code);
  616. // Check for errors
  617. switch( $status_code[0] ) {
  618. case 200:
  619. // Success
  620. $this->log->LogResponse($response);
  621. return array(200, $body);
  622. break;
  623. case 503:
  624. $this->log->LogError($response);
  625. return array(503, htmlentities($body));
  626. break;
  627. case 403:
  628. $this->log->LogError($response);
  629. return array(403, htmlentities($body));
  630. break;
  631. case 400:
  632. $this->log->LogError($response);
  633. return array(400, htmlentities($body));
  634. break;
  635. default:
  636. $this->log->LogError($response);
  637. return array("ERR", htmlentities($body));
  638. break;
  639. }
  640. }
  641. // Private functions
  642. // Function to get HTTP headers,
  643. // will also work with HTTP 200 status added by some proxy servers
  644. /**
  645. * @access private
  646. */
  647. function parse_headers($message) {
  648. $head_end = strpos($message, DOUBLE_ENTER);
  649. $headers = $this->get_headers_x(substr($message,0,
  650. $head_end + strlen(DOUBLE_ENTER)));
  651. if(!is_array($headers) || empty($headers)){
  652. return null;
  653. }
  654. if(!preg_match('%[HTTP/\d\.\d] (\d\d\d)%', $headers[0], $status_code)) {
  655. return null;
  656. }
  657. switch( $status_code[1] ) {
  658. case '200':
  659. $parsed = $this->parse_headers(substr($message,
  660. $head_end + strlen(DOUBLE_ENTER)));
  661. return is_null($parsed)?$headers:$parsed;
  662. break;
  663. default:
  664. return $headers;
  665. break;
  666. }
  667. }
  668. /**
  669. * @access private
  670. */
  671. function get_headers_x($heads, $format=0) {
  672. $fp = explode(ENTER, $heads);
  673. foreach($fp as $header){
  674. if($header == "") {
  675. $eoheader = true;
  676. break;
  677. } else {
  678. $header = trim($header);
  679. }
  680. if($format == 1) {
  681. $key = array_shift(explode(':',$header));
  682. if($key == $header) {
  683. $headers[] = $header;
  684. } else {
  685. $headers[$key]=substr($header,strlen($key)+2);
  686. }
  687. unset($key);
  688. } else {
  689. $headers[] = $header;
  690. }
  691. }
  692. return $headers;
  693. }
  694. /**
  695. * @access private
  696. */
  697. function get_body_x($heads){
  698. $fp = explode(DOUBLE_ENTER, $heads, 2);
  699. return $fp[1];
  700. }
  701. }
  702. class GoogleShipItem {
  703. var $merchant_item_id;
  704. var $tracking_data_list;
  705. var $tracking_no;
  706. function GoogleShipItem($merchant_item_id, $tracking_data_list=array()) {
  707. $this->merchant_item_id = $merchant_item_id;
  708. $this->tracking_data_list = $tracking_data_list;
  709. }
  710. function AddTrackingData($carrier, $tracking_no) {
  711. if($carrier != "" && $tracking_no != "") {
  712. $this->tracking_data_list[] = array('carrier' => $carrier,
  713. 'tracking-number' => $tracking_no);
  714. }
  715. }
  716. }
  717. ?>