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