PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/membership/lib/authorize.net/lib/AuthorizeNetCP.php

https://gitlab.com/najomie/fit-hippie
PHP | 227 lines | 114 code | 34 blank | 79 comment | 21 complexity | 55b9cc23901508391bc4f2d80a1151e3 MD5 | raw file
  1. <?php
  2. /**
  3. * Easily interact with the Authorize.Net Card Present API.
  4. *
  5. *
  6. * @package AuthorizeNet
  7. * @subpackage AuthorizeNetCP
  8. * @link http://www.authorize.net/support/CP_guide.pdf Card Present Guide
  9. */
  10. /**
  11. * Builds and sends an AuthorizeNet CP Request.
  12. *
  13. * @package AuthorizeNet
  14. * @subpackage AuthorizeNetCP
  15. */
  16. class M2_AuthorizeNetCP extends M2_AuthorizeNetAIM
  17. {
  18. const LIVE_URL = 'https://cardpresent.authorize.net/gateway/transact.dll';
  19. public $verify_x_fields = false;
  20. /**
  21. * Holds all the x_* name/values that will be posted in the request.
  22. * Default values are provided for best practice fields.
  23. */
  24. protected $_x_post_fields = array(
  25. "cpversion" => "1.0",
  26. "delim_char" => ",",
  27. "encap_char" => "|",
  28. "market_type" => "2",
  29. "response_format" => "1", // 0 - XML, 1 - NVP
  30. );
  31. /**
  32. * Device Types (x_device_type)
  33. * 1 = Unknown
  34. * 2 = Unattended Terminal
  35. * 3 = Self Service Terminal
  36. * 4 = Electronic Cash Register
  37. * 5 = Personal Computer- Based Terminal
  38. * 6 = AirPay
  39. * 7 = Wireless POS
  40. * 8 = Website
  41. * 9 = Dial Terminal
  42. * 10 = Virtual Terminal
  43. */
  44. /**
  45. * Only used if merchant wants to send custom fields.
  46. */
  47. private $_custom_fields = array();
  48. /**
  49. * Strip sentinels and set track1 field.
  50. *
  51. * @param string $track1data
  52. */
  53. public function setTrack1Data($track1data) {
  54. if (preg_match('/^%.*\?$/', $track1data)) {
  55. $this->track1 = substr($track1data, 1, -1);
  56. } else {
  57. $this->track1 = $track1data;
  58. }
  59. }
  60. /**
  61. * Strip sentinels and set track2 field.
  62. *
  63. * @param string $track2data
  64. */
  65. public function setTrack2Data($track2data) {
  66. if (preg_match('/^;.*\?$/', $track2data)) {
  67. $this->track2 = substr($track2data, 1, -1);
  68. } else {
  69. $this->track2 = $track2data;
  70. }
  71. }
  72. /**
  73. *
  74. *
  75. * @param string $response
  76. *
  77. * @return AuthorizeNetAIM_Response
  78. */
  79. protected function _handleResponse($response)
  80. {
  81. return new M2_AuthorizeNetCP_Response($response, $this->_x_post_fields['delim_char'], $this->_x_post_fields['encap_char'], $this->_custom_fields);
  82. }
  83. }
  84. /**
  85. * Parses an AuthorizeNet Card Present Response.
  86. *
  87. * @package AuthorizeNet
  88. * @subpackage AuthorizeNetCP
  89. */
  90. class M2_AuthorizeNetCP_Response extends M2_AuthorizeNetResponse
  91. {
  92. private $_response_array = array(); // An array with the split response.
  93. /**
  94. * Constructor. Parses the AuthorizeNet response string.
  95. *
  96. * @param string $response The response from the AuthNet server.
  97. * @param string $delimiter The delimiter used (default is ",")
  98. * @param string $encap_char The encap_char used (default is "|")
  99. * @param array $custom_fields Any custom fields set in the request.
  100. */
  101. public function __construct($response, $delimiter, $encap_char, $custom_fields)
  102. {
  103. if ($response) {
  104. // If it's an XML response
  105. if (substr($response, 0, 5) == "<?xml") {
  106. $this->xml = @simplexml_load_string($response);
  107. // Set all fields
  108. $this->version = array_pop(array_slice(explode('"', $response), 1,1));
  109. $this->response_code = (string)$this->xml->ResponseCode;
  110. if ($this->response_code == 1) {
  111. $this->response_reason_code = (string)$this->xml->Messages->Message->Code;
  112. $this->response_reason_text = (string)$this->xml->Messages->Message->Description;
  113. } else {
  114. $this->response_reason_code = (string)$this->xml->Errors->Error->ErrorCode;
  115. $this->response_reason_text = (string)$this->xml->Errors->Error->ErrorText;
  116. }
  117. $this->authorization_code = (string)$this->xml->AuthCode;
  118. $this->avs_code = (string)$this->xml->AVSResultCode;
  119. $this->card_code_response = (string)$this->xml->CVVResultCode;
  120. $this->transaction_id = (string)$this->xml->TransID;
  121. $this->md5_hash = (string)$this->xml->TransHash;
  122. $this->user_ref = (string)$this->xml->UserRef;
  123. $this->card_num = (string)$this->xml->AccountNumber;
  124. $this->card_type = (string)$this->xml->AccountType;
  125. $this->test_mode = (string)$this->xml->TestMode;
  126. $this->ref_trans_id = (string)$this->xml->RefTransID;
  127. } else { // If it's an NVP response
  128. // Split Array
  129. $this->response = $response;
  130. if ($encap_char) {
  131. $this->_response_array = explode($encap_char.$delimiter.$encap_char, substr($response, 1, -1));
  132. } else {
  133. $this->_response_array = explode($delimiter, $response);
  134. }
  135. /**
  136. * If AuthorizeNet doesn't return a delimited response.
  137. */
  138. if (count($this->_response_array) < 10) {
  139. $this->approved = false;
  140. $this->error = true;
  141. $this->error_message = "Unrecognized response from AuthorizeNet: $response";
  142. return;
  143. }
  144. // Set all fields
  145. $this->version = $this->_response_array[0];
  146. $this->response_code = $this->_response_array[1];
  147. $this->response_reason_code = $this->_response_array[2];
  148. $this->response_reason_text = $this->_response_array[3];
  149. $this->authorization_code = $this->_response_array[4];
  150. $this->avs_code = $this->_response_array[5];
  151. $this->card_code_response = $this->_response_array[6];
  152. $this->transaction_id = $this->_response_array[7];
  153. $this->md5_hash = $this->_response_array[8];
  154. $this->user_ref = $this->_response_array[9];
  155. $this->card_num = $this->_response_array[20];
  156. $this->card_type = $this->_response_array[21];
  157. $this->split_tender_id = isset($this->_response_array[22]) ? $this->_response_array[22] : NULL;
  158. $this->requested_amount = isset($this->_response_array[23]) ? $this->_response_array[23] : NULL;
  159. $this->approved_amount = isset($this->_response_array[24]) ? $this->_response_array[24] : NULL;
  160. $this->card_balance = isset($this->_response_array[25]) ? $this->_response_array[25] : NULL;
  161. }
  162. $this->approved = ($this->response_code == self::APPROVED);
  163. $this->declined = ($this->response_code == self::DECLINED);
  164. $this->error = ($this->response_code == self::ERROR);
  165. $this->held = ($this->response_code == self::HELD);
  166. if ($this->error) {
  167. $this->error_message = "AuthorizeNet Error:
  168. Response Code: ".$this->response_code."
  169. Response Reason Code: ".$this->response_reason_code."
  170. Response Reason Text: ".$this->response_reason_text."
  171. ";
  172. }
  173. } else {
  174. $this->approved = false;
  175. $this->error = true;
  176. $this->error_message = "Error connecting to AuthorizeNet";
  177. }
  178. }
  179. /**
  180. * Is the MD5 provided correct?
  181. *
  182. * @param string $api_login_id
  183. * @param string $md5_setting
  184. * @return bool
  185. */
  186. public function isAuthorizeNet($api_login_id = false, $md5_setting = false)
  187. {
  188. $amount = ($this->amount ? $this->amount : '0.00');
  189. $api_login_id = ($api_login_id ? $api_login_id : AUTHORIZENET_API_LOGIN_ID);
  190. $md5_setting = ($md5_setting ? $md5_setting : AUTHORIZENET_MD5_SETTING);
  191. return ($this->md5_hash == strtoupper(md5($md5_setting . $api_login_id . $this->transaction_id . $amount)));
  192. }
  193. }