PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/packages/Google/demo/basicapiresponsehandlerdemo.php

https://github.com/michaelmcandrew/th
PHP | 168 lines | 95 code | 15 blank | 58 comment | 11 complexity | 8490da3276e03e0f2a5bdf2f15d8a8a2 MD5 | raw file
  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. /* This is the response handler code that will be invoked every time
  18. * a notification or request is sent by the Google Server. This code is
  19. * targeted at Google Checkout API v2.5 but can also handle previous versions
  20. *
  21. * To allow this code to receive responses, the url for this file
  22. * must be set on the seller page under Settings->Integration as the
  23. * "API Callback URL"
  24. * Order processing commands can be sent automatically by placing these
  25. * commands appropriately. The charge and ship example is provided for you.
  26. *
  27. */
  28. chdir("..");
  29. require_once('library/googleresponse.php');
  30. require_once('library/googlemerchantcalculations.php');
  31. require_once('library/googlerequest.php');
  32. require_once('library/googlenotificationhistory.php');
  33. define('RESPONSE_HANDLER_ERROR_LOG_FILE', 'googleerror.log');
  34. define('RESPONSE_HANDLER_LOG_FILE', 'googlemessage.log');
  35. //Definitions
  36. $merchant_id = ""; // Your Merchant ID
  37. $merchant_key = ""; // Your Merchant Key
  38. $server_type = "sandbox"; // change this to go live
  39. $currency = 'USD'; // set to GBP if in the UK
  40. //Create the response object
  41. $Gresponse = new GoogleResponse($merchant_id, $merchant_key);
  42. //Setup the log file
  43. $Gresponse->SetLogFiles('', '', L_OFF); //Change this to L_ON to log
  44. //Retrieve the XML sent in the HTTP POST request to the ResponseHandler
  45. $xml_response = isset($HTTP_RAW_POST_DATA)?
  46. $HTTP_RAW_POST_DATA:file_get_contents("php://input");
  47. //If serial-number-notification pull serial number and request xml
  48. if(strpos($xml_response, "xml") == FALSE){
  49. //Find serial-number ack notification
  50. $serial_array = array();
  51. parse_str($xml_response, $serial_array);
  52. $serial_number = $serial_array["serial-number"];
  53. //Request XML notification
  54. $Grequest = new GoogleNotificationHistoryRequest($merchant_id, $merchant_key, $server_type);
  55. $raw_xml_array = $Grequest->SendNotificationHistoryRequest($serial_number);
  56. if ($raw_xml_array[0] != 200){
  57. //Add code here to retry with exponential backoff
  58. } else {
  59. $raw_xml = $raw_xml_array[1];
  60. }
  61. $Gresponse->SendAck($serial_number, false);
  62. }
  63. else{
  64. //Else assume pre 2.5 XML notification
  65. //Check Basic Authentication
  66. $Gresponse->SetMerchantAuthentication($merchant_id, $merchant_key);
  67. $status = $Gresponse->HttpAuthentication();
  68. if(! $status) {
  69. die('authentication failed');
  70. }
  71. $raw_xml = $xml_response;
  72. $Gresponse->SendAck(null, false);
  73. }
  74. if (get_magic_quotes_gpc()) {
  75. $raw_xml = stripslashes($raw_xml);
  76. }
  77. //Parse XML to array
  78. list($root, $data) = $Gresponse->GetParsedXML($raw_xml);
  79. /* Commands to send the various order processing APIs
  80. * Send charge order : $Grequest->SendChargeOrder($data[$root]
  81. * ['google-order-number']['VALUE'], <amount>);
  82. * Send process order : $Grequest->SendProcessOrder($data[$root]
  83. * ['google-order-number']['VALUE']);
  84. * Send deliver order: $Grequest->SendDeliverOrder($data[$root]
  85. * ['google-order-number']['VALUE'], <carrier>, <tracking-number>,
  86. * <send_mail>);
  87. * Send archive order: $Grequest->SendArchiveOrder($data[$root]
  88. * ['google-order-number']['VALUE']);
  89. *
  90. */
  91. switch($root){
  92. case "new-order-notification": {
  93. break;
  94. }
  95. case "risk-information-notification": {
  96. break;
  97. }
  98. case "charge-amount-notification": {
  99. break;
  100. }
  101. case "authorization-amount-notification": {
  102. $google_order_number = $data[$root]['google-order-number']['VALUE'];
  103. $tracking_data = array("Z12345" => "UPS", "Y12345" => "Fedex");
  104. $GChargeRequest = new GoogleRequest($merchant_id, $merchant_key, $server_type);
  105. $GChargeRequest->SendChargeAndShipOrder($google_order_number, $tracking_data);
  106. break;
  107. }
  108. case "refund-amount-notification": {
  109. break;
  110. }
  111. case "chargeback-amount-notification": {
  112. break;
  113. }
  114. case "order-numbers": {
  115. break;
  116. }
  117. case "invalid-order-numbers": {
  118. break;
  119. }
  120. case "order-state-cahnge-notification": {
  121. break;
  122. }
  123. default: {
  124. break;
  125. }
  126. }
  127. /* In case the XML API contains multiple open tags
  128. with the same value, then invoke this function and
  129. perform a foreach on the resultant array.
  130. This takes care of cases when there is only one unique tag
  131. or multiple tags.
  132. Examples of this are "anonymous-address", "merchant-code-string"
  133. from the merchant-calculations-callback API
  134. */
  135. function get_arr_result($child_node) {
  136. $result = array();
  137. if(isset($child_node)) {
  138. if(is_associative_array($child_node)) {
  139. $result[] = $child_node;
  140. }
  141. else {
  142. foreach($child_node as $curr_node){
  143. $result[] = $curr_node;
  144. }
  145. }
  146. }
  147. return $result;
  148. }
  149. /* Returns true if a given variable represents an associative array */
  150. function is_associative_array( $var ) {
  151. return is_array( $var ) && !is_numeric( implode( '', array_keys( $var ) ) );
  152. }
  153. ?>