PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/app/code/core/Mage/Centinel/Model/Api.php

https://bitbucket.org/jit_bec/shopifine
PHP | 267 lines | 147 code | 26 blank | 94 comment | 6 complexity | d7055a0c4e4c3658b3d1908558e076e6 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Centinel
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * 3D Secure Validation Library for Payment
  28. */
  29. include_once '3Dsecure/CentinelClient.php';
  30. /**
  31. * 3D Secure Validation Api
  32. */
  33. class Mage_Centinel_Model_Api extends Varien_Object
  34. {
  35. /**
  36. * Fields that should be replaced in debug with '***'
  37. *
  38. * @var array
  39. */
  40. protected $_debugReplacePrivateDataKeys = array('TransactionPwd', 'CardNumber', 'CardExpMonth', 'CardExpYear');
  41. protected static $_iso4217Currencies = array(
  42. 'AED' => '784', 'AFN' => '971',
  43. 'ALL' => '008', 'AMD' => '051', 'ANG' => '532', 'AOA' => '973', 'ARS' => '032', 'AUD' => '036', 'AWG' => '533',
  44. 'AZN' => '944', 'BAM' => '977', 'BBD' => '052', 'BDT' => '050', 'BGN' => '975', 'BHD' => '048', 'BIF' => '108',
  45. 'BMD' => '060', 'BND' => '096', 'BOB' => '068', 'BOV' => '984', 'BRL' => '986', 'BSD' => '044', 'BTN' => '064',
  46. 'BWP' => '072', 'BYR' => '974', 'BZD' => '084', 'CAD' => '124', 'CDF' => '976', 'CHE' => '947', 'CHF' => '756',
  47. 'CHW' => '948', 'CLF' => '990', 'CLP' => '152', 'CNY' => '156', 'COP' => '170', 'COU' => '970', 'CRC' => '188',
  48. 'CUC' => '931', 'CUP' => '192', 'CVE' => '132', 'CZK' => '203', 'DJF' => '262', 'DKK' => '208', 'DOP' => '214',
  49. 'DZD' => '012', 'EEK' => '233', 'EGP' => '818', 'ERN' => '232', 'ETB' => '230', 'EUR' => '978', 'FJD' => '242',
  50. 'FKP' => '238', 'GBP' => '826', 'GEL' => '981', 'GHS' => '936', 'GIP' => '292', 'GMD' => '270', 'GNF' => '324',
  51. 'GTQ' => '320', 'GYD' => '328', 'HKD' => '344', 'HNL' => '340', 'HRK' => '191', 'HTG' => '332', 'HUF' => '348',
  52. 'IDR' => '360', 'ILS' => '376', 'INR' => '356', 'IQD' => '368', 'IRR' => '364', 'ISK' => '352', 'JMD' => '388',
  53. 'JOD' => '400', 'JPY' => '392', 'KES' => '404', 'KGS' => '417', 'KHR' => '116', 'KMF' => '174', 'KPW' => '408',
  54. 'KRW' => '410', 'KWD' => '414', 'KYD' => '136', 'KZT' => '398', 'LAK' => '418', 'LBP' => '422', 'LKR' => '144',
  55. 'LRD' => '430', 'LSL' => '426', 'LTL' => '440', 'LVL' => '428', 'LYD' => '434', 'MAD' => '504', 'MDL' => '498',
  56. 'MGA' => '969', 'MKD' => '807', 'MMK' => '104', 'MNT' => '496', 'MOP' => '446', 'MRO' => '478', 'MUR' => '480',
  57. 'MVR' => '462', 'MWK' => '454', 'MXN' => '484', 'MXV' => '979', 'MYR' => '458', 'MZN' => '943', 'NAD' => '516',
  58. 'NGN' => '566', 'NIO' => '558', 'NOK' => '578', 'NPR' => '524', 'NZD' => '554', 'OMR' => '512', 'PAB' => '590',
  59. 'PEN' => '604', 'PGK' => '598', 'PHP' => '608', 'PKR' => '586', 'PLN' => '985', 'PYG' => '600', 'QAR' => '634',
  60. 'RON' => '946', 'RSD' => '941', 'RUB' => '643', 'RWF' => '646', 'SAR' => '682', 'SBD' => '090', 'SCR' => '690',
  61. 'SDG' => '938', 'SEK' => '752', 'SGD' => '702', 'SHP' => '654', 'SLL' => '694', 'SOS' => '706', 'SRD' => '968',
  62. 'STD' => '678', 'SYP' => '760', 'SZL' => '748', 'THB' => '764', 'TJS' => '972', 'TMT' => '934', 'TND' => '788',
  63. 'TOP' => '776', 'TRY' => '949', 'TTD' => '780', 'TWD' => '901', 'TZS' => '834', 'UAH' => '980', 'UGX' => '800',
  64. 'USD' => '840', 'USN' => '997', 'USS' => '998', 'UYU' => '858', 'UZS' => '860', 'VEF' => '937', 'VND' => '704',
  65. 'VUV' => '548', 'WST' => '882', 'XAF' => '950', 'XAG' => '961', 'XAU' => '959', 'XBA' => '955', 'XBB' => '956',
  66. 'XBC' => '957', 'XBD' => '958', 'XCD' => '951', 'XDR' => '960', 'XOF' => '952', 'XPD' => '964', 'XPF' => '953',
  67. 'XPT' => '962', 'XTS' => '963', 'XXX' => '999', 'YER' => '886', 'ZAR' => '710', 'ZMK' => '894', 'ZWL' => '932',
  68. );
  69. /**
  70. * Centinel validation client
  71. *
  72. * @var CentinelClient
  73. */
  74. protected $_clientInstance = null;
  75. /**
  76. * Return Centinel thin client object
  77. *
  78. * @return CentinelClient
  79. */
  80. protected function _getClientInstance()
  81. {
  82. if (empty($this->_clientInstance)) {
  83. $this->_clientInstance = new CentinelClient();
  84. }
  85. return $this->_clientInstance;
  86. }
  87. /**
  88. * Return Centinel Api version
  89. *
  90. * @return string
  91. */
  92. protected function _getVersion()
  93. {
  94. return '1.7';
  95. }
  96. /**
  97. * Return transaction type. according centinel documetation it should be "C"
  98. *
  99. * @return "C"
  100. */
  101. protected function _getTransactionType()
  102. {
  103. return 'C';
  104. }
  105. /**
  106. * Return Timeout Connect
  107. *
  108. * @return int
  109. */
  110. protected function _getTimeoutConnect()
  111. {
  112. return 100;
  113. }
  114. /**
  115. * Return Timeout Read
  116. *
  117. * @return int
  118. */
  119. protected function _getTimeoutRead()
  120. {
  121. return 100;
  122. }
  123. /**
  124. * Call centinel api methods by given method name and data
  125. *
  126. * @param $method string
  127. * @param $data array
  128. *
  129. * @return CentinelClient
  130. */
  131. protected function _call($method, $data)
  132. {
  133. $client = $this->_getClientInstance();
  134. $request = array_merge(array(
  135. 'MsgType' => $method,
  136. 'Version' => $this->_getVersion(),
  137. 'ProcessorId' => $this->getProcessorId(),
  138. 'MerchantId' => $this->getMerchantId(),
  139. 'TransactionPwd' => $this->getTransactionPwd(),
  140. 'TransactionType' => $this->_getTransactionType(),
  141. ), $data);
  142. $debugData = array('request' => $request);
  143. try {
  144. foreach($request as $key => $val) {
  145. $client->add($key, $val);
  146. }
  147. $client->sendHttp($this->_getApiEndpointUrl(), $this->_getTimeoutConnect(), $this->_getTimeoutRead());
  148. } catch (Exception $e) {
  149. $debugData['response'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  150. $this->_debug($debugData);
  151. throw $e;
  152. }
  153. $debugData['response'] = $client->response;
  154. $this->_debug($debugData);
  155. return $client;
  156. }
  157. /**
  158. * Getter for API call URL
  159. *
  160. * @return string
  161. * @throws Exception
  162. */
  163. protected function _getApiEndpointUrl()
  164. {
  165. if ($this->getIsTestMode()) {
  166. return 'https://centineltest.cardinalcommerce.com/maps/txns.asp';
  167. }
  168. $url = $this->getApiEndpointUrl();
  169. if (!$url) {
  170. throw new Exception('Centinel API endpoint URL is not configured properly.');
  171. }
  172. return $url;
  173. }
  174. /**
  175. * Call centinel api lookup method
  176. *
  177. * @return Mage_Centinel_Model_Api
  178. */
  179. public function callLookup($data)
  180. {
  181. $result = new Varien_Object();
  182. $month = strlen($data->getCardExpMonth()) == 1 ? '0' . $data->getCardExpMonth() : $data->getCardExpMonth();
  183. $currencyCode = $data->getCurrencyCode();
  184. $currencyNumber = isset(self::$_iso4217Currencies[$currencyCode]) ? self::$_iso4217Currencies[$currencyCode] : '';
  185. if (!$currencyNumber) {
  186. return $result->setErrorNo(1)->setErrorDesc(
  187. Mage::helper('payment')->__('Unsupported currency code: %s.', $currencyCode)
  188. );
  189. }
  190. $clientResponse = $this->_call('cmpi_lookup', array(
  191. 'Amount' => round($data->getAmount() * 100),
  192. 'CurrencyCode' => $currencyNumber,
  193. 'CardNumber' => $data->getCardNumber(),
  194. 'CardExpMonth'=> $month,
  195. 'CardExpYear' => $data->getCardExpYear(),
  196. 'OrderNumber' => $data->getOrderNumber()
  197. ));
  198. $result->setErrorNo($clientResponse->getValue('ErrorNo'));
  199. $result->setErrorDesc($clientResponse->getValue('ErrorDesc'));
  200. $result->setTransactionId($clientResponse->getValue('TransactionId'));
  201. $result->setEnrolled($clientResponse->getValue('Enrolled'));
  202. $result->setAcsUrl($clientResponse->getValue('ACSUrl'));
  203. $result->setPayload($clientResponse->getValue('Payload'));
  204. $result->setEciFlag($clientResponse->getValue('EciFlag'));
  205. return $result;
  206. }
  207. /**
  208. * Call centinel api authentication method
  209. *
  210. * @return Mage_Centinel_Model_Api
  211. */
  212. public function callAuthentication($data)
  213. {
  214. $result = new Varien_Object();
  215. $clientResponse = $this->_call('cmpi_authenticate', array(
  216. 'TransactionId' => $data->getTransactionId(),
  217. 'PAResPayload' => $data->getPaResPayload(),
  218. ));
  219. $result->setErrorNo($clientResponse->getValue('ErrorNo'));
  220. $result->setErrorDesc($clientResponse->getValue('ErrorDesc'));
  221. $result->setPaResStatus($clientResponse->getValue('PAResStatus'));
  222. $result->setSignatureVerification($clientResponse->getValue('SignatureVerification'));
  223. $result->setCavv($clientResponse->getValue('Cavv'));
  224. $result->setEciFlag($clientResponse->getValue('EciFlag'));
  225. $result->setXid($clientResponse->getValue('Xid'));
  226. return $result;
  227. }
  228. /**
  229. * Log debug data to file
  230. *
  231. * @param mixed $debugData
  232. */
  233. protected function _debug($debugData)
  234. {
  235. if ($this->getDebugFlag()) {
  236. Mage::getModel('core/log_adapter', 'card_validation_3d_secure.log')
  237. ->setFilterDataKeys($this->_debugReplacePrivateDataKeys)
  238. ->log($debugData);
  239. }
  240. }
  241. }