Source for file gWebListener.php

Documentation is available at gWebListener.php

  1. <?php
  2. /**
  3. * ========================================================
  4. * phpGCheckout, Open Source PHP G Checkout Library
  5. * http://www.phpgcheckout.com
  6. * ========================================================
  7. *
  8. * Copyright (c) 2006 Expert Database Solutions, LLC
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a
  11. * copy of this software and associated documentation files (the "Software"),
  12. * to deal in the Software without restriction, including without limitation the
  13. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in all
  18. * copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  21. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  22. * PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
  25. * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. */
  28.  
  29. class gWebListener {
  30. var $_raw_message;
  31. var $_message_type;
  32. var $_message;
  33. var $_rev_merchant_id;
  34. var $_rev_merchant_key;
  35. var $_my_merchant_id;
  36. var $_my_merchant_key;
  37. var $_auto_authenticate;
  38. /**
  39. * Public Constructor
  40. *
  41. * @param unknown_type $auto_authenticate
  42. * @param unknown_type $merchant_id
  43. * @param unknown_type $merchant_key
  44. * @return gWebListener
  45. */
  46. function gWebListener($merchant_id = null, $merchant_key = null, $auto_authenticate = true) {
  47. $this->_auto_authenticate = $auto_authenticate;
  48. $this->_my_merchant_id = $merchant_id;
  49. $this->_my_merchant_key = $merchant_key;
  50. }
  51. ////////////////////////////////////
  52. // Public Methods
  53. ////////////////////////////////////
  54. /**
  55. * Start The Web Listener
  56. *
  57. */
  58. function Start() {
  59. /**
  60. * Authorize Message Post
  61. */
  62. $this->_HTTPGetAuthentication();
  63. /**
  64. * Auto Authentication
  65. */
  66. if($this->_auto_authenticate) {
  67. if($this->_my_merchant_id != $this->_rev_merchant_id ||
  68. $this->_my_merchant_key != $this->_rev_merchant_key) {
  69. exit();
  70. }
  71. }
  72. /**
  73. * Read Input Stream
  74. */
  75. $this->_getRawPostData();
  76. $this->_parseRawMessage();
  77. }
  78. /**
  79. * Returns the received merchant id from the
  80. * authentication header
  81. *
  82. * @return unknown
  83. */
  84. function getHTTPMerchantId() {
  85. return $this->_rev_merchant_id;
  86. }
  87. /**
  88. * Returns the received merchant key from the
  89. * authentication header
  90. *
  91. * @return unknown
  92. */
  93. function getHTTPMerchantKey() {
  94. return $this->_rev_merchant_key;
  95. }
  96. /**
  97. * Returns Message Type
  98. *
  99. * @return unknown
  100. */
  101. function getMessageType() {
  102. return $this->_message_type;
  103. }
  104. /**
  105. * Return Message
  106. *
  107. * @return unknown
  108. */
  109. function getMessage() {
  110. switch($this->_message_type) {
  111. case NEW_ORDER_NOTIFICATION:
  112. $message = new gNewOrderNotification();
  113. $message->buildMessage($this->_message);
  114. return $message;
  115. case ORDER_STATE_CHANGE_NOTIFICATION:
  116. $message = new gOrderStateChangeNotification();
  117. $message->buildMessage($this->_message);
  118. return $message;
  119. case CHARGE_AMOUNT_NOTIFICATION:
  120. $message = new gChargeAmountNotification();
  121. $message->buildMessage($this->_message);
  122. return $message;
  123. case RISK_INFORMATION_NOTIFICATION:
  124. $message = new gRiskInformationNotification();
  125. $message->buildMessage($this->_message);
  126. return $message;
  127. case REFUND_AMOUNT_NOTIFICATION:
  128. $message = new gRefundAmountNotification();
  129. $message->buildMessage($this->_message);
  130. return $message;
  131. case CHARGEBACK_AMOUNT_NOTIFICATION:
  132. $message = new gChargebackAmountNotification();
  133. $message->buildMessage($this->_message);
  134. return $message;
  135. }
  136. }
  137. ////////////////////////////////////
  138. // Private Methods
  139. ////////////////////////////////////
  140. /**
  141. * Enter description here...
  142. *
  143. */
  144. function _HTTPGetAuthentication(){
  145. /**
  146. * Perform HTTP Authentication
  147. * Send Headers
  148. */
  149. if (!isset($_SERVER['PHP_AUTH_USER'])) {
  150. header('WWW-Authenticate: Basic realm="My Realm"');
  151. header('HTTP/1.0 401 Unauthorized');
  152. echo 'Unauthorized\n';
  153. exit;
  154. } else {
  155. /**
  156. * Get Username and password
  157. */
  158. $this->_rev_merchant_id = $_SERVER['PHP_AUTH_USER'];
  159. $this->_rev_merchant_key= $_SERVER['PHP_AUTH_PW'];
  160. }
  161. }
  162. /**
  163. * Read the raw post data from the input stream
  164. *
  165. */
  166. function _getRawPostData() {
  167. /**
  168. *
  169. */
  170. $fp= fopen("php://input", "r");
  171. /**
  172. * Read from input
  173. */
  174. $input = '';
  175. while(!feof($fp)) {
  176. $input .= fread($fp, sizeof($fp));
  177. }
  178. /**
  179. * Close Stream
  180. */
  181. fclose($fp);
  182. /**
  183. * Set Received Message
  184. */
  185. $this->_raw_message = $input;
  186. }
  187. /**
  188. * Parse the Raw Message
  189. *
  190. */
  191. function _parseRawMessage() {
  192. $xml_parser = new XML_Unserializer();
  193. if($xml_parser->unserialize($this->_raw_message)) {
  194. $this->_parseMessageType($this->_raw_message);
  195. $parsed_response = $xml_parser->getUnserializedData();
  196. $this->_message = $parsed_response;
  197. }
  198. else
  199. $this->_message = false;
  200. }
  201. /**
  202. * Parse Message Type
  203. *
  204. * @param unknown_type $raw_data
  205. */
  206. function _parseMessageType($raw_data) {
  207. /**
  208. * New Order Notification
  209. */
  210. if(strstr($raw_data, NEW_ORDER_NOTIFICATION)) {
  211. $this->_message_type = NEW_ORDER_NOTIFICATION;
  212. }
  213. /**
  214. * Risk Information Notification
  215. */
  216. if(strstr($raw_data, RISK_INFORMATION_NOTIFICATION))
  217. $this->_message_type = RISK_INFORMATION_NOTIFICATION;
  218. /**
  219. * Order State Change Notification
  220. */
  221. if(strstr($raw_data, ORDER_STATE_CHANGE_NOTIFICATION))
  222. $this->_message_type = ORDER_STATE_CHANGE_NOTIFICATION;
  223. /**
  224. * Charge Amount Notification
  225. */
  226. if(strstr($raw_data, CHARGE_AMOUNT_NOTIFICATION))
  227. $this->_message_type = CHARGE_AMOUNT_NOTIFICATION;
  228. /**
  229. * Refund Amount Notification
  230. */
  231. if(strstr($raw_data, REFUND_AMOUNT_NOTIFICATION))
  232. $this->_message_type = REFUND_AMOUNT_NOTIFICATION;
  233. /**
  234. * Charge Back Amount Notification
  235. */
  236. if(strstr($raw_data, CHARGEBACK_AMOUNT_NOTIFICATION))
  237. $this->_message_type = CHARGEBACK_AMOUNT_NOTIFICATION;
  238. }
  239. }
  240. ?>

Documentation generated on Mon, 04 Dec 2006 11:09:42 -0500 by phpDocumentor 1.3.0RC3