M2_AuthorizeNetAIM

Builds and sends an AuthorizeNet AIM Request.

Defined (1)

The class is defined in the following location(s).

/lib/authorize.net/lib/AuthorizeNetAIM.php  
  1. class M2_AuthorizeNetAIM extends M2_AuthorizeNetRequest 
  2.  
  3. const LIVE_URL = 'https://secure2.authorize.net/gateway/transact.dll'; 
  4. const SANDBOX_URL = 'https://test.authorize.net/gateway/transact.dll'; 
  5.  
  6. /** 
  7. * Holds all the x_* name/values that will be posted in the request.  
  8. * Default values are provided for best practice fields. 
  9. */ 
  10. protected $_x_post_fields = array( 
  11. "version" => "3.1",  
  12. "delim_char" => ", ",  
  13. "delim_data" => "TRUE",  
  14. "relay_response" => "FALSE",  
  15. "encap_char" => "|",  
  16. ); 
  17.  
  18. /** 
  19. * Only used if merchant wants to send multiple line items about the charge. 
  20. */ 
  21. private $_additional_line_items = array(); 
  22.  
  23. /** 
  24. * Only used if merchant wants to send custom fields. 
  25. */ 
  26. private $_custom_fields = array(); 
  27.  
  28. /** 
  29. * Checks to make sure a field is actually in the API before setting. 
  30. * Set to false to skip this check. 
  31. */ 
  32. public $verify_x_fields = true; 
  33.  
  34. /** 
  35. * A list of all fields in the AIM API. 
  36. * Used to warn user if they try to set a field not offered in the API. 
  37. */ 
  38. private $_all_aim_fields = array("address", "allow_partial_auth", "amount",  
  39. "auth_code", "authentication_indicator", "bank_aba_code", "bank_acct_name",  
  40. "bank_acct_num", "bank_acct_type", "bank_check_number", "bank_name",  
  41. "card_code", "card_num", "cardholder_authentication_value", "city", "company",  
  42. "country", "cust_id", "customer_ip", "delim_char", "delim_data", "description",  
  43. "duplicate_window", "duty", "echeck_type", "email", "email_customer",  
  44. "encap_char", "exp_date", "fax", "first_name", "footer_email_receipt",  
  45. "freight", "header_email_receipt", "invoice_num", "last_name", "line_item",  
  46. "login", "method", "phone", "po_num", "recurring_billing", "relay_response",  
  47. "ship_to_address", "ship_to_city", "ship_to_company", "ship_to_country",  
  48. "ship_to_first_name", "ship_to_last_name", "ship_to_state", "ship_to_zip",  
  49. "split_tender_id", "state", "tax", "tax_exempt", "test_request", "tran_key",  
  50. "trans_id", "type", "version", "zip" 
  51. ); 
  52.  
  53. /** 
  54. * Do an AUTH_CAPTURE transaction.  
  55. *  
  56. * Required "x_" fields: card_num, exp_date, amount 
  57. * @param string $amount The dollar amount to charge 
  58. * @param string $card_num The credit card number 
  59. * @param string $exp_date CC expiration date 
  60. * @return AuthorizeNetAIM_Response 
  61. */ 
  62. public function authorizeAndCapture($amount = false, $card_num = false, $exp_date = false) 
  63. ($amount ? $this->amount = $amount : null); 
  64. ($card_num ? $this->card_num = $card_num : null); 
  65. ($exp_date ? $this->exp_date = $exp_date : null); 
  66. $this->type = "AUTH_CAPTURE"; 
  67. return $this->_sendRequest(); 
  68.  
  69. /** 
  70. * Do a PRIOR_AUTH_CAPTURE transaction. 
  71. * Required "x_" field: trans_id(The transaction id of the prior auth, unless split 
  72. * tender, then set x_split_tender_id manually.) 
  73. * amount (only if lesser than original auth) 
  74. * @param string $trans_id Transaction id to charge 
  75. * @param string $amount Dollar amount to charge if lesser than auth 
  76. * @return AuthorizeNetAIM_Response 
  77. */ 
  78. public function priorAuthCapture($trans_id = false, $amount = false) 
  79. ($trans_id ? $this->trans_id = $trans_id : null); 
  80. ($amount ? $this->amount = $amount : null); 
  81. $this->type = "PRIOR_AUTH_CAPTURE"; 
  82. return $this->_sendRequest(); 
  83.  
  84. /** 
  85. * Do an AUTH_ONLY transaction. 
  86. * Required "x_" fields: card_num, exp_date, amount 
  87. * @param string $amount The dollar amount to charge 
  88. * @param string $card_num The credit card number 
  89. * @param string $exp_date CC expiration date 
  90. * @return AuthorizeNetAIM_Response 
  91. */ 
  92. public function authorizeOnly($amount = false, $card_num = false, $exp_date = false) 
  93. ($amount ? $this->amount = $amount : null); 
  94. ($card_num ? $this->card_num = $card_num : null); 
  95. ($exp_date ? $this->exp_date = $exp_date : null); 
  96. $this->type = "AUTH_ONLY"; 
  97. return $this->_sendRequest(); 
  98.  
  99. /** 
  100. * Do a VOID transaction. 
  101. * Required "x_" field: trans_id(The transaction id of the prior auth, unless split 
  102. * tender, then set x_split_tender_id manually.) 
  103. * @param string $trans_id Transaction id to void 
  104. * @return AuthorizeNetAIM_Response 
  105. */ 
  106. public function void($trans_id = false) 
  107. ($trans_id ? $this->trans_id = $trans_id : null); 
  108. $this->type = "VOID"; 
  109. return $this->_sendRequest(); 
  110.  
  111. /** 
  112. * Do a CAPTURE_ONLY transaction. 
  113. * Required "x_" fields: auth_code, amount, card_num , exp_date 
  114. * @param string $auth_code The auth code 
  115. * @param string $amount The dollar amount to charge 
  116. * @param string $card_num The last 4 of credit card number 
  117. * @param string $exp_date CC expiration date 
  118. * @return AuthorizeNetAIM_Response 
  119. */ 
  120. public function captureOnly($auth_code = false, $amount = false, $card_num = false, $exp_date = false) 
  121. ($auth_code ? $this->auth_code = $auth_code : null); 
  122. ($amount ? $this->amount = $amount : null); 
  123. ($card_num ? $this->card_num = $card_num : null); 
  124. ($exp_date ? $this->exp_date = $exp_date : null); 
  125. $this->type = "CAPTURE_ONLY"; 
  126. return $this->_sendRequest(); 
  127.  
  128. /** 
  129. * Do a CREDIT transaction. 
  130. * Required "x_" fields: trans_id, amount, card_num (just the last 4) 
  131. * @param string $trans_id Transaction id to credit 
  132. * @param string $amount The dollar amount to credit 
  133. * @param string $card_num The last 4 of credit card number 
  134. * @return AuthorizeNetAIM_Response 
  135. */ 
  136. public function credit($trans_id = false, $amount = false, $card_num = false) 
  137. ($trans_id ? $this->trans_id = $trans_id : null); 
  138. ($amount ? $this->amount = $amount : null); 
  139. ($card_num ? $this->card_num = $card_num : null); 
  140. $this->type = "CREDIT"; 
  141. return $this->_sendRequest(); 
  142.  
  143. /** 
  144. * Alternative syntax for setting x_ fields. 
  145. * Usage: $sale->method = "echeck"; 
  146. * @param string $name 
  147. * @param string $value 
  148. */ 
  149. public function __set($name, $value)  
  150. $this->setField($name, $value); 
  151.  
  152. /** 
  153. * Quickly set multiple fields. 
  154. * Note: The prefix x_ will be added to all fields. If you want to set a 
  155. * custom field without the x_ prefix, use setCustomField or setCustomFields. 
  156. * @param array $fields Takes an array or object. 
  157. */ 
  158. public function setFields($fields) 
  159. $array = (array)$fields; 
  160. foreach ($array as $key => $value) { 
  161. $this->setField($key, $value); 
  162.  
  163. /** 
  164. * Quickly set multiple custom fields. 
  165. * @param array $fields 
  166. */ 
  167. public function setCustomFields($fields) 
  168. $array = (array)$fields; 
  169. foreach ($array as $key => $value) { 
  170. $this->setCustomField($key, $value); 
  171.  
  172. /** 
  173. * Add a line item. 
  174. *  
  175. * @param string $item_id 
  176. * @param string $item_name 
  177. * @param string $item_description 
  178. * @param string $item_quantity 
  179. * @param string $item_unit_price 
  180. * @param string $item_taxable 
  181. */ 
  182. public function addLineItem($item_id, $item_name, $item_description, $item_quantity, $item_unit_price, $item_taxable) 
  183. $line_item = ""; 
  184. $delimiter = ""; 
  185. foreach (func_get_args() as $key => $value) { 
  186. $line_item .= $delimiter . $value; 
  187. $delimiter = "<|>"; 
  188. $this->_additional_line_items[] = $line_item; 
  189.  
  190. /** 
  191. * Use ECHECK as payment type. 
  192. */ 
  193. public function setECheck($bank_aba_code, $bank_acct_num, $bank_acct_type, $bank_name, $bank_acct_name, $echeck_type = 'WEB') 
  194. $this->setFields( 
  195. array( 
  196. 'method' => 'echeck',  
  197. 'bank_aba_code' => $bank_aba_code,  
  198. 'bank_acct_num' => $bank_acct_num,  
  199. 'bank_acct_type' => $bank_acct_type,  
  200. 'bank_name' => $bank_name,  
  201. 'bank_acct_name' => $bank_acct_type,  
  202. 'echeck_type' => $echeck_type,  
  203. ); 
  204.  
  205. /** 
  206. * Set an individual name/value pair. This will append x_ to the name 
  207. * before posting. 
  208. * @param string $name 
  209. * @param string $value 
  210. */ 
  211. public function setField($name, $value) 
  212. if ($this->verify_x_fields) { 
  213. if (in_array($name, $this->_all_aim_fields)) { 
  214. $this->_x_post_fields[$name] = $value; 
  215. } else { 
  216. throw new M2_AuthorizeNetException("Error: no field $name exists in the AIM API. 
  217. To set a custom field use setCustomField('field', 'value') instead."); 
  218. } else { 
  219. $this->_x_post_fields[$name] = $value; 
  220.  
  221. /** 
  222. * Set a custom field. Note: the x_ prefix will not be added to 
  223. * your custom field if you use this method. 
  224. * @param string $name 
  225. * @param string $value 
  226. */ 
  227. public function setCustomField($name, $value) 
  228. $this->_custom_fields[$name] = $value; 
  229.  
  230. /** 
  231. * Unset an x_ field. 
  232. * @param string $name Field to unset. 
  233. */ 
  234. public function unsetField($name) 
  235. unset($this->_x_post_fields[$name]); 
  236.  
  237. /** 
  238. * @param string $response 
  239. *  
  240. * @return AuthorizeNetAIM_Response 
  241. */ 
  242. protected function _handleResponse($response) 
  243. return new M2_AuthorizeNetAIM_Response($response, $this->_x_post_fields['delim_char'], $this->_x_post_fields['encap_char'], $this->_custom_fields); 
  244.  
  245. /** 
  246. * @return string 
  247. */ 
  248. protected function _getPostUrl() 
  249. return ($this->_sandbox ? self::SANDBOX_URL : self::LIVE_URL); 
  250.  
  251. /** 
  252. * Converts the x_post_fields array into a string suitable for posting. 
  253. */ 
  254. protected function _setPostString() 
  255. $this->_x_post_fields['login'] = $this->_api_login; 
  256. $this->_x_post_fields['tran_key'] = $this->_transaction_key; 
  257. $this->_post_string = ""; 
  258. foreach ($this->_x_post_fields as $key => $value) { 
  259. $this->_post_string .= "x_$key=" . urlencode($value) . "&"; 
  260. // Add line items 
  261. foreach ($this->_additional_line_items as $key => $value) { 
  262. $this->_post_string .= "x_line_item=" . urlencode($value) . "&"; 
  263. // Add custom fields 
  264. foreach ($this->_custom_fields as $key => $value) { 
  265. $this->_post_string .= "$key=" . urlencode($value) . "&"; 
  266. $this->_post_string = rtrim($this->_post_string, "& ");