M2_AuthorizeNetCIM

A class to send a request to the CIM XML API.

Defined (1)

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

/lib/authorize.net/lib/AuthorizeNetCIM.php  
  1. class M2_AuthorizeNetCIM extends M2_AuthorizeNetRequest 
  2.  
  3. const LIVE_URL = "https://api2.authorize.net/xml/v1/request.api"; 
  4. const SANDBOX_URL = "https://apitest.authorize.net/xml/v1/request.api"; 
  5.  
  6.  
  7. private $_xml; 
  8. private $_refId = false; 
  9. private $_validationMode = "none"; // "none", "testMode", "liveMode" 
  10. private $_extraOptions; 
  11. private $_transactionTypes = array( 
  12. 'AuthOnly',  
  13. 'AuthCapture',  
  14. 'CaptureOnly',  
  15. 'PriorAuthCapture',  
  16. 'Refund',  
  17. 'Void',  
  18. ); 
  19.  
  20. /** 
  21. * Optional. Used if the merchant wants to set a reference ID. 
  22. * @param string $refId 
  23. */ 
  24. public function setRefId($refId) 
  25. $this->_refId = $refId; 
  26.  
  27. /** 
  28. * Create a customer profile. 
  29. * @param AuthorizeNetCustomer $customerProfile 
  30. * @param string $validationMode 
  31. * @return AuthorizeNetCIM_Response 
  32. */ 
  33. public function createCustomerProfile($customerProfile, $validationMode = "none") 
  34. $this->_validationMode = $validationMode; 
  35. $this->_constructXml("createCustomerProfileRequest"); 
  36. $profile = $this->_xml->addChild("profile"); 
  37. $this->_addObject($profile, $customerProfile); 
  38. return $this->_sendRequest(); 
  39.  
  40. /** 
  41. * Create a customer payment profile. 
  42. * @param int $customerProfileId 
  43. * @param AuthorizeNetPaymentProfile $paymentProfile 
  44. * @param string $validationMode 
  45. * @return AuthorizeNetCIM_Response 
  46. */ 
  47. public function createCustomerPaymentProfile($customerProfileId, $paymentProfile, $validationMode = "none") 
  48. $this->_validationMode = $validationMode; 
  49. $this->_constructXml("createCustomerPaymentProfileRequest"); 
  50. $this->_xml->addChild("customerProfileId", $customerProfileId); 
  51. $profile = $this->_xml->addChild("paymentProfile"); 
  52. $this->_addObject($profile, $paymentProfile); 
  53. return $this->_sendRequest(); 
  54.  
  55. /** 
  56. * Create a shipping address. 
  57. * @param int $customerProfileId 
  58. * @param AuthorizeNetAddress $shippingAddress 
  59. * @return AuthorizeNetCIM_Response 
  60. */ 
  61. public function createCustomerShippingAddress($customerProfileId, $shippingAddress) 
  62. $this->_constructXml("createCustomerShippingAddressRequest"); 
  63. $this->_xml->addChild("customerProfileId", $customerProfileId); 
  64. $address = $this->_xml->addChild("address"); 
  65. $this->_addObject($address, $shippingAddress); 
  66. return $this->_sendRequest(); 
  67.  
  68. /** 
  69. * Create a transaction. 
  70. * @param string $transactionType 
  71. * @param AuthorizeNetTransaction $transaction 
  72. * @param string $extraOptionsString 
  73. * @return AuthorizeNetCIM_Response 
  74. */ 
  75. public function createCustomerProfileTransaction($transactionType, $transaction, $extraOptionsString = "") 
  76. $this->_constructXml("createCustomerProfileTransactionRequest"); 
  77. $transactionParent = $this->_xml->addChild("transaction"); 
  78. $transactionChild = $transactionParent->addChild("profileTrans" . $transactionType); 
  79. $this->_addObject($transactionChild, $transaction); 
  80. $this->_extraOptions = $extraOptionsString . "x_encap_char=|"; 
  81. return $this->_sendRequest(); 
  82.  
  83. /** 
  84. * Delete a customer profile. 
  85. * @param int $customerProfileId 
  86. * @return AuthorizeNetCIM_Response 
  87. */ 
  88. public function deleteCustomerProfile($customerProfileId) 
  89. $this->_constructXml("deleteCustomerProfileRequest"); 
  90. $this->_xml->addChild("customerProfileId", $customerProfileId); 
  91. return $this->_sendRequest(); 
  92.  
  93. /** 
  94. * Delete a payment profile. 
  95. * @param int $customerProfileId 
  96. * @param int $customerPaymentProfileId 
  97. * @return AuthorizeNetCIM_Response 
  98. */ 
  99. public function deleteCustomerPaymentProfile($customerProfileId, $customerPaymentProfileId) 
  100. $this->_constructXml("deleteCustomerPaymentProfileRequest"); 
  101. $this->_xml->addChild("customerProfileId", $customerProfileId); 
  102. $this->_xml->addChild("customerPaymentProfileId", $customerPaymentProfileId); 
  103. return $this->_sendRequest(); 
  104.  
  105. /** 
  106. * Delete a shipping address. 
  107. * @param int $customerProfileId 
  108. * @param int $customerAddressId 
  109. * @return AuthorizeNetCIM_Response 
  110. */ 
  111. public function deleteCustomerShippingAddress($customerProfileId, $customerAddressId) 
  112. $this->_constructXml("deleteCustomerShippingAddressRequest"); 
  113. $this->_xml->addChild("customerProfileId", $customerProfileId); 
  114. $this->_xml->addChild("customerAddressId", $customerAddressId); 
  115. return $this->_sendRequest(); 
  116.  
  117. /** 
  118. * Get all customer profile ids. 
  119. * @return AuthorizeNetCIM_Response 
  120. */ 
  121. public function getCustomerProfileIds() 
  122. $this->_constructXml("getCustomerProfileIdsRequest"); 
  123. return $this->_sendRequest(); 
  124.  
  125. /** 
  126. * Get a customer profile. 
  127. * @param int $customerProfileId 
  128. * @return AuthorizeNetCIM_Response 
  129. */ 
  130. public function getCustomerProfile($customerProfileId) 
  131. $this->_constructXml("getCustomerProfileRequest"); 
  132. $this->_xml->addChild("customerProfileId", $customerProfileId); 
  133. return $this->_sendRequest(); 
  134.  
  135. /** 
  136. * Get a payment profile. 
  137. * @param int $customerProfileId 
  138. * @param int $customerPaymentProfileId 
  139. * @return AuthorizeNetCIM_Response 
  140. */ 
  141. public function getCustomerPaymentProfile($customerProfileId, $customerPaymentProfileId) 
  142. $this->_constructXml("getCustomerPaymentProfileRequest"); 
  143. $this->_xml->addChild("customerProfileId", $customerProfileId); 
  144. $this->_xml->addChild("customerPaymentProfileId", $customerPaymentProfileId); 
  145. return $this->_sendRequest(); 
  146.  
  147. /** 
  148. * Get a shipping address. 
  149. * @param int $customerProfileId 
  150. * @param int $customerAddressId 
  151. * @return AuthorizeNetCIM_Response 
  152. */ 
  153. public function getCustomerShippingAddress($customerProfileId, $customerAddressId) 
  154. $this->_constructXml("getCustomerShippingAddressRequest"); 
  155. $this->_xml->addChild("customerProfileId", $customerProfileId); 
  156. $this->_xml->addChild("customerAddressId", $customerAddressId); 
  157. return $this->_sendRequest(); 
  158.  
  159. /** 
  160. * Update a profile. 
  161. * @param int $customerProfileId 
  162. * @param AuthorizeNetCustomer $customerProfile 
  163. * @return AuthorizeNetCIM_Response 
  164. */ 
  165. public function updateCustomerProfile($customerProfileId, $customerProfile) 
  166. $this->_constructXml("updateCustomerProfileRequest"); 
  167. $customerProfile->customerProfileId = $customerProfileId; 
  168. $profile = $this->_xml->addChild("profile"); 
  169. $this->_addObject($profile, $customerProfile); 
  170. return $this->_sendRequest(); 
  171.  
  172. /** 
  173. * Update a payment profile. 
  174. * @param int $customerProfileId 
  175. * @param int $customerPaymentProfileId 
  176. * @param AuthorizeNetPaymentProfile $paymentProfile 
  177. * @param string $validationMode 
  178. * @return AuthorizeNetCIM_Response 
  179. */ 
  180. public function updateCustomerPaymentProfile($customerProfileId, $customerPaymentProfileId, $paymentProfile, $validationMode = "none") 
  181. $this->_validationMode = $validationMode; 
  182. $this->_constructXml("updateCustomerPaymentProfileRequest"); 
  183. $this->_xml->addChild("customerProfileId", $customerProfileId); 
  184. $paymentProfile->customerPaymentProfileId = $customerPaymentProfileId; 
  185. $profile = $this->_xml->addChild("paymentProfile"); 
  186. $this->_addObject($profile, $paymentProfile); 
  187. return $this->_sendRequest(); 
  188.  
  189. /** 
  190. * Update a shipping address. 
  191. * @param int $customerProfileId 
  192. * @param int $customerShippingAddressId 
  193. * @param AuthorizeNetAddress $shippingAddress 
  194. * @return AuthorizeNetCIM_Response 
  195. */ 
  196. public function updateCustomerShippingAddress($customerProfileId, $customerShippingAddressId, $shippingAddress) 
  197.  
  198. $this->_constructXml("updateCustomerShippingAddressRequest"); 
  199. $this->_xml->addChild("customerProfileId", $customerProfileId); 
  200. $shippingAddress->customerAddressId = $customerShippingAddressId; 
  201. $sa = $this->_xml->addChild("address"); 
  202. $this->_addObject($sa, $shippingAddress); 
  203. return $this->_sendRequest(); 
  204.  
  205. /** 
  206. * Update the status of an existing order that contains multiple transactions with the same splitTenderId. 
  207. * @param int $splitTenderId 
  208. * @param string $splitTenderStatus 
  209. * @return AuthorizeNetCIM_Response 
  210. */ 
  211. public function updateSplitTenderGroup($splitTenderId, $splitTenderStatus) 
  212. $this->_constructXml("updateSplitTenderGroupRequest"); 
  213. $this->_xml->addChild("splitTenderId", $splitTenderId); 
  214. $this->_xml->addChild("splitTenderStatus", $splitTenderStatus); 
  215. return $this->_sendRequest(); 
  216.  
  217. /** 
  218. * Validate a customer payment profile. 
  219. * @param int $customerProfileId 
  220. * @param int $customerPaymentProfileId 
  221. * @param int $customerShippingAddressId 
  222. * @param int $cardCode 
  223. * @param string $validationMode 
  224. * @return AuthorizeNetCIM_Response 
  225. */ 
  226. public function validateCustomerPaymentProfile($customerProfileId, $customerPaymentProfileId, $customerShippingAddressId, $cardCode, $validationMode = "testMode") 
  227. $this->_validationMode = $validationMode; 
  228. $this->_constructXml("validateCustomerPaymentProfileRequest"); 
  229. $this->_xml->addChild("customerProfileId", $customerProfileId); 
  230. $this->_xml->addChild("customerPaymentProfileId", $customerPaymentProfileId); 
  231. $this->_xml->addChild("customerShippingAddressId", $customerShippingAddressId); 
  232. $this->_xml->addChild("cardCode", $cardCode); 
  233. return $this->_sendRequest(); 
  234.  
  235. /** 
  236. * Get hosted profile page request token 
  237. * @param string $customerProfileId 
  238. * @param mixed $settings 
  239. * @return AuthorizeNetCIM_Response 
  240. */ 
  241. public function getHostedProfilePageRequest($customerProfileId, $settings=0) 
  242. $this->_constructXml("getHostedProfilePageRequest"); 
  243. $this->_xml->addChild("customerProfileId", $customerProfileId); 
  244.  
  245. if (!empty($settings)) { 
  246. $hostedSettings = $this->_xml->addChild("hostedProfileSettings"); 
  247. foreach ($settings as $key => $val) { 
  248. $setting = $hostedSettings->addChild("setting"); 
  249. $setting->addChild("settingName", $key); 
  250. $setting->addChild("settingValue", $val); 
  251.  
  252. return $this->_sendRequest(); 
  253.  
  254. /** 
  255. * @return string 
  256. */ 
  257. protected function _getPostUrl() 
  258. return ($this->_sandbox ? self::SANDBOX_URL : self::LIVE_URL); 
  259.  
  260. /** 
  261. * @param string $response 
  262. *  
  263. * @return AuthorizeNetCIM_Response 
  264. */ 
  265. protected function _handleResponse($response) 
  266. return new M2_AuthorizeNetCIM_Response($response); 
  267.  
  268. /** 
  269. * Prepare the XML post string. 
  270. */ 
  271. protected function _setPostString() 
  272. ($this->_validationMode != "none" ? $this->_xml->addChild('validationMode', $this->_validationMode) : ""); 
  273. $this->_post_string = $this->_xml->asXML(); 
  274.  
  275. // Add extraOptions CDATA 
  276. if ($this->_extraOptions) { 
  277. $this->_xml->addChild("extraOptions"); 
  278. $this->_post_string = str_replace(array("<extraOptions></extraOptions>", "<extraOptions/>"), '<extraOptions><![CDATA[' . $this->_extraOptions . ']]></extraOptions>', $this->_xml->asXML()); 
  279. $this->_extraOptions = false; 
  280. // Blank out our validation mode, so that we don't include it in calls that 
  281. // don't use it. 
  282. $this->_validationMode = "none"; 
  283.  
  284. /** 
  285. * Start the SimpleXMLElement that will be posted. 
  286. * @param string $request_type The action to be performed. 
  287. */ 
  288. private function _constructXml($request_type) 
  289. $string = '<?xml version="1.0" encoding="utf-8"?><'.$request_type.' xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"></'.$request_type.'>'; 
  290. $this->_xml = @new SimpleXMLElement($string); 
  291. $merchant = $this->_xml->addChild('merchantAuthentication'); 
  292. $merchant->addChild('name', $this->_api_login); 
  293. $merchant->addChild('transactionKey', $this->_transaction_key); 
  294. ($this->_refId ? $this->_xml->addChild('refId', $this->_refId) : ""); 
  295.  
  296. /** 
  297. * Add an object to an SimpleXMLElement parent element. 
  298. * @param SimpleXMLElement $destination The parent element. 
  299. * @param Object $object An object, array or value.  
  300. */ 
  301. private function _addObject($destination, $object) 
  302. $array = (array)$object; 
  303. foreach ($array as $key => $value) { 
  304. if ($value && !is_object($value)) { 
  305. if (is_array($value) && count($value)) { 
  306. foreach ($value as $index => $item) { 
  307. $items = $destination->addChild($key); 
  308. $this->_addObject($items, $item); 
  309. } else { 
  310. $destination->addChild($key, $value); 
  311. } elseif (is_object($value) && self::_notEmpty($value)) { 
  312. $dest = $destination->addChild($key); 
  313. $this->_addObject($dest, $value); 
  314.  
  315. /** 
  316. * Checks whether an array or object contains any values. 
  317. * @param Object $object 
  318. * @return bool 
  319. */ 
  320. private static function _notEmpty($object) 
  321. $array = (array)$object; 
  322. foreach ($array as $key => $value) { 
  323. if ($value && !is_object($value)) { 
  324. return true; 
  325. } elseif (is_object($value)) { 
  326. if (self::_notEmpty($value)) { 
  327. return true; 
  328. return false; 
  329.