Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | This file is a part of CiviCRM. | | |
7 | | | | |
8 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
9 | | under the terms of the GNU Affero General Public License | | |
10 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
11 | | | | |
12 | | CiviCRM is distributed in the hope that it will be useful, but | | |
13 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
14 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
15 | | See the GNU Affero General Public License for more details. | | |
16 | | | | |
17 | | You should have received a copy of the GNU Affero General Public | | |
18 | | License and the CiviCRM Licensing Exception along | | |
19 | | with this program; if not, contact CiviCRM LLC | | |
20 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
21 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
22 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
23 | +--------------------------------------------------------------------+ | |
24 | */ | |
25 | ||
26 | ||
27 | /* | |
28 | * Copyright (C) 2009 | |
29 | * Licensed to CiviCRM under the Academic Free License version 3.0. | |
30 | * | |
31 | * Written and contributed by Kirkdesigns (http://www.kirkdesigns.co.uk) | |
32 | * | |
33 | */ | |
34 | ||
35 | /** | |
36 | * | |
37 | * @package CRM | |
38 | * @author Tom Kirkpatrick <tkp@kirkdesigns.co.uk> | |
39 | * $Id$ | |
40 | */ | |
41 | class CRM_Core_Payment_Realex extends CRM_Core_Payment { | |
42 | CONST AUTH_APPROVED = '00'; | |
43 | ||
44 | protected $_mode = NULL; | |
45 | ||
46 | protected $_params = array(); | |
47 | ||
48 | /** | |
49 | * We only need one instance of this object. So we use the singleton | |
50 | * pattern and cache the instance in this variable | |
51 | * | |
52 | * @var object | |
53 | * @static | |
54 | */ | |
55 | static private $_singleton = NULL; | |
56 | ||
57 | /** | |
58 | * Constructor | |
59 | * | |
60 | * @param string $mode the mode of operation: live or test | |
61 | * | |
77b97be7 EM |
62 | * @param $paymentProcessor |
63 | * | |
64 | * @return \CRM_Core_Payment_Realex | |
6a488035 TO |
65 | */ |
66 | function __construct($mode, &$paymentProcessor) { | |
67 | $this->_mode = $mode; | |
68 | $this->_paymentProcessor = $paymentProcessor; | |
69 | $this->_processorName = ts('Realex'); | |
70 | ||
71 | $this->_setParam('merchant_ref', $paymentProcessor['user_name']); | |
72 | $this->_setParam('secret', $paymentProcessor['password']); | |
73 | $this->_setParam('account', $paymentProcessor['subject']); | |
74 | ||
75 | $this->_setParam('emailCustomer', 'TRUE'); | |
76 | srand(time()); | |
77 | $this->_setParam('sequence', rand(1, 1000)); | |
78 | } | |
79 | ||
80 | /** | |
81 | * singleton function used to manage this object | |
82 | * | |
83 | * @param string $mode the mode of operation: live or test | |
84 | * | |
77b97be7 EM |
85 | * @param object $paymentProcessor |
86 | * | |
6a488035 TO |
87 | * @return object |
88 | * @static | |
6a488035 TO |
89 | */ |
90 | static function &singleton($mode, &$paymentProcessor) { | |
91 | $processorName = $paymentProcessor['name']; | |
92 | if (self::$_singleton[$processorName] === NULL) { | |
93 | self::$_singleton[$processorName] = new CRM_Core_Payment_Realex($mode, $paymentProcessor); | |
94 | } | |
95 | return self::$_singleton[$processorName]; | |
96 | } | |
97 | ||
6c786a9b EM |
98 | /** |
99 | * @param $params | |
100 | * | |
101 | * @throws Exception | |
102 | */ | |
6a488035 TO |
103 | function setExpressCheckOut(&$params) { |
104 | CRM_Core_Error::fatal(ts('This function is not implemented')); | |
105 | } | |
106 | ||
6c786a9b EM |
107 | /** |
108 | * @param $token | |
109 | * | |
110 | * @throws Exception | |
111 | */ | |
6a488035 TO |
112 | function getExpressCheckoutDetails($token) { |
113 | CRM_Core_Error::fatal(ts('This function is not implemented')); | |
114 | } | |
115 | ||
6c786a9b EM |
116 | /** |
117 | * @param $params | |
118 | * | |
119 | * @throws Exception | |
120 | */ | |
6a488035 TO |
121 | function doExpressCheckout(&$params) { |
122 | CRM_Core_Error::fatal(ts('This function is not implemented')); | |
123 | } | |
124 | ||
6c786a9b EM |
125 | /** |
126 | * @param $params | |
127 | * | |
128 | * @throws Exception | |
129 | */ | |
6a488035 TO |
130 | function doTransferCheckout(&$params) { |
131 | CRM_Core_Error::fatal(ts('This function is not implemented')); | |
132 | } | |
133 | ||
134 | /** | |
135 | * Submit a payment using Advanced Integration Method | |
136 | * | |
137 | * @param array $params assoc array of input parameters for this transaction | |
138 | * | |
139 | * @return array the result in a nice formatted array (or an error object) | |
140 | * @public | |
141 | */ | |
142 | function doDirectPayment(&$params) { | |
143 | ||
144 | if (!defined('CURLOPT_SSLCERT')) { | |
145 | return self::error(9001, ts('RealAuth requires curl with SSL support')); | |
146 | } | |
147 | ||
148 | $result = $this->setRealexFields($params); | |
149 | ||
150 | if ($result !== TRUE) { | |
151 | return $result; | |
152 | } | |
153 | ||
154 | /********************************************************** | |
155 | * Check to see if we have a duplicate before we send | |
156 | **********************************************************/ | |
157 | if ($this->_checkDupe($this->_getParam('order_id'))) { | |
158 | return self::error(9004, ts('It appears that this transaction is a duplicate. Have you already submitted the form once? If so there may have been a connection problem. Check your email for a receipt from Authorize.net. If you do not receive a receipt within 2 hours you can try your transaction again. If you continue to have problems please contact the site administrator.')); | |
159 | } | |
160 | ||
161 | // Create sha1 hash for request | |
162 | $hashme = "{$this->_getParam('timestamp')}.{$this->_getParam('merchant_ref')}.{$this->_getParam('order_id')}.{$this->_getParam('amount')}.{$this->_getParam('currency')}.{$this->_getParam('card_number')}"; | |
163 | $sha1hash = sha1($hashme); | |
164 | $hashme = "$sha1hash.{$this->_getParam('secret')}"; | |
165 | $sha1hash = sha1($hashme); | |
166 | ||
167 | ||
168 | // Generate the request xml that is send to Realex Payments. | |
169 | $request_xml = "<request type='auth' timestamp='{$this->_getParam('timestamp')}'> | |
170 | <merchantid>{$this->_getParam('merchant_ref')}</merchantid> | |
171 | <account>{$this->_getParam('account')}</account> | |
172 | <orderid>{$this->_getParam('order_id')}</orderid> | |
173 | <amount currency='{$this->_getParam('currency')}'>{$this->_getParam('amount')}</amount> | |
174 | <card> | |
175 | <number>{$this->_getParam('card_number')}</number> | |
176 | <expdate>{$this->_getParam('exp_date')}</expdate> | |
177 | <type>{$this->_getParam('card_type')}</type> | |
178 | <chname>{$this->_getParam('card_name')}</chname> | |
179 | <issueno>{$this->_getParam('issue_number')}</issueno> | |
180 | <cvn> | |
181 | <number>{$this->_getParam('cvn')}</number> | |
182 | <presind>1</presind> | |
183 | </cvn> | |
184 | </card> | |
185 | <autosettle flag='1'/> | |
186 | <sha1hash>$sha1hash</sha1hash> | |
187 | <comments> | |
188 | <comment id='1'>{$this->_getParam('comments')}</comment> | |
189 | </comments> | |
190 | <tssinfo> | |
191 | <varref>{$this->_getParam('varref')}</varref> | |
192 | </tssinfo> | |
193 | </request>"; | |
194 | ||
195 | /********************************************************** | |
196 | * Send to the payment processor using cURL | |
197 | **********************************************************/ | |
198 | ||
199 | $submit = curl_init($this->_paymentProcessor['url_site']); | |
200 | ||
201 | if (!$submit) { | |
202 | return self::error(9002, ts('Could not initiate connection to payment gateway')); | |
203 | } | |
204 | ||
205 | curl_setopt($submit, CURLOPT_HTTPHEADER, array('SOAPAction: ""')); | |
206 | curl_setopt($submit, CURLOPT_RETURNTRANSFER, 1); | |
207 | curl_setopt($submit, CURLOPT_TIMEOUT, 60); | |
208 | curl_setopt($submit, CURLOPT_SSL_VERIFYPEER, CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL')); | |
209 | curl_setopt($submit, CURLOPT_HEADER, 0); | |
210 | ||
211 | // take caching out of the picture | |
212 | curl_setopt($submit, CURLOPT_FORBID_REUSE, 1); | |
213 | curl_setopt($submit, CURLOPT_FRESH_CONNECT, 1); | |
214 | ||
215 | // Apply the XML to our curl call | |
216 | curl_setopt($submit, CURLOPT_POST, 1); | |
217 | curl_setopt($submit, CURLOPT_POSTFIELDS, $request_xml); | |
218 | ||
219 | $response_xml = curl_exec($submit); | |
220 | ||
221 | if (!$response_xml) { | |
222 | return self::error(curl_errno($submit), curl_error($submit)); | |
223 | } | |
224 | ||
225 | curl_close($submit); | |
226 | ||
227 | // Tidy up the responce xml | |
228 | $response_xml = preg_replace("/[\s\t]/", " ", $response_xml); | |
229 | $response_xml = preg_replace("/[\n\r]/", "", $response_xml); | |
230 | ||
231 | // Parse the response xml | |
232 | $xml_parser = xml_parser_create(); | |
233 | if (!xml_parse($xml_parser, $response_xml)) { | |
234 | return self::error(9003, 'XML Error'); | |
235 | } | |
236 | ||
237 | $response = $this->xml_parse_into_assoc($response_xml); | |
238 | $response = $response['#return']['RESPONSE']; | |
239 | ||
240 | // Log the Realex response for debugging | |
241 | // CRM_Core_Error::debug_var('REALEX --------- Response from Realex: ', $response, TRUE); | |
242 | ||
243 | // Return an error if authentication was not successful | |
244 | if ($response['RESULT'] !== self::AUTH_APPROVED) { | |
245 | return self::error($response['RESULT'], ' ' . $response['MESSAGE']); | |
246 | } | |
247 | ||
248 | // Check the response hash | |
249 | $hashme = "{$this->_getParam('timestamp')}.{$this->_getParam('merchant_ref')}.{$this->_getParam('order_id')}.{$response['RESULT']}.{$response['MESSAGE']}.{$response['PASREF']}.{$response['AUTHCODE']}"; | |
250 | $sha1hash = sha1($hashme); | |
251 | $hashme = "$sha1hash.{$this->_getParam('secret')}"; | |
252 | $sha1hash = sha1($hashme); | |
253 | ||
254 | if ($response['SHA1HASH'] != $sha1hash) { | |
255 | // FIXME: Need to actually check this - I couldn't get the | |
256 | // hashes to match so I'm commenting out for now' | |
257 | // return self::error( 9001, "Hash error, please report this to the webmaster" ); | |
258 | } | |
259 | ||
260 | // FIXME: We are using the trxn_result_code column to store all these extra details since there | |
261 | // seems to be nowhere else to put them. This is THE WRONG THING TO DO! | |
262 | $extras = array( | |
263 | 'authcode' => $response['AUTHCODE'], | |
264 | 'batch_id' => $response['BATCHID'], | |
265 | 'message' => $response['MESSAGE'], | |
266 | 'trxn_result_code' => $response['RESULT'], | |
267 | ); | |
268 | ||
269 | $params['trxn_id'] = $response['PASREF']; | |
270 | $params['trxn_result_code'] = serialize($extras); | |
271 | $params['currencyID'] = $this->_getParam('currency'); | |
272 | $params['gross_amount'] = $this->_getParam('amount'); | |
273 | $params['fee_amount'] = 0; | |
274 | ||
275 | return $params; | |
276 | } | |
277 | ||
278 | /** | |
279 | * Helper function to convert XML string to multi-dimension array. | |
280 | * | |
281 | * @param $xml | |
282 | * an XML string. | |
283 | * | |
fd31fa4c | 284 | * @return array An array of the result with following keys: |
6a488035 TO |
285 | */ |
286 | function xml_parse_into_assoc($xml) { | |
287 | $input = array(); | |
288 | $result = array(); | |
289 | ||
290 | $result['#error'] = FALSE; | |
291 | $result['#return'] = NULL; | |
292 | ||
293 | $xmlparser = xml_parser_create(); | |
294 | $ret = xml_parse_into_struct($xmlparser, $xml, $input); | |
295 | ||
296 | xml_parser_free($xmlparser); | |
297 | ||
298 | if (empty($input)) { | |
299 | $result['#return'] = $xml; | |
300 | } | |
301 | else { | |
302 | if ($ret > 0) { | |
303 | $result['#return'] = $this->_xml_parse($input); | |
304 | } | |
305 | else { | |
306 | $result['#error'] = ts('Error parsing XML result - error code = %1 at line %2 char %3', | |
307 | array( | |
308 | 1 => xml_get_error_code($xmlparser), | |
309 | 2 => xml_get_current_line_number($xmlparser), | |
310 | 3 => xml_get_current_column_number($xmlparser) | |
311 | ) | |
312 | ); | |
313 | } | |
314 | } | |
315 | return $result; | |
316 | } | |
317 | ||
318 | // private helper for xml_parse_into_assoc, to recusively parsing the result | |
6c786a9b EM |
319 | /** |
320 | * @param $input | |
321 | * @param int $depth | |
322 | * | |
323 | * @return array | |
324 | */ | |
6a488035 TO |
325 | function _xml_parse($input, $depth = 1) { |
326 | $output = array(); | |
327 | $children = array(); | |
328 | ||
329 | foreach ($input as $data) { | |
330 | if ($data['level'] == $depth) { | |
331 | switch ($data['type']) { | |
332 | case 'complete': | |
333 | $output[$data['tag']] = isset($data['value']) ? $data['value'] : ''; | |
334 | break; | |
335 | ||
336 | case 'open': | |
337 | $children = array(); | |
338 | break; | |
339 | ||
340 | case 'close': | |
341 | $output[$data['tag']] = $this->_xml_parse($children, $depth + 1); | |
342 | break; | |
343 | } | |
344 | } | |
345 | else { | |
346 | $children[] = $data; | |
347 | } | |
348 | } | |
349 | return $output; | |
350 | } | |
351 | ||
352 | /** | |
353 | * Format the params from the form ready for sending to Realex. Also perform some validation | |
354 | */ | |
355 | function setRealexFields(&$params) { | |
356 | if ((int)$params['amount'] <= 0) { | |
357 | return self::error(9001, ts('Amount must be positive')); | |
358 | } | |
359 | ||
360 | // format amount to be in smallest possible units | |
361 | //list($bills, $pennies) = explode('.', $params['amount']); | |
362 | $this->_setParam('amount', 100 * $params['amount']); | |
363 | ||
364 | switch (strtolower($params['credit_card_type'])) { | |
365 | case 'mastercard': | |
366 | $this->_setParam('card_type', 'MC'); | |
367 | $this->_setParam('requiresIssueNumber', FALSE); | |
368 | break; | |
369 | ||
370 | case 'visa': | |
371 | $this->_setParam('card_type', 'VISA'); | |
372 | $this->_setParam('requiresIssueNumber', FALSE); | |
373 | break; | |
374 | ||
375 | case 'amex': | |
376 | $this->_setParam('card_type', 'AMEX'); | |
377 | $this->_setParam('requiresIssueNumber', FALSE); | |
378 | break; | |
379 | ||
380 | case 'laser': | |
381 | $this->_setParam('card_type', 'LASER'); | |
382 | $this->_setParam('requiresIssueNumber', FALSE); | |
383 | break; | |
384 | ||
385 | case 'maestro': | |
386 | case 'switch': | |
387 | case 'maestro/switch': | |
388 | case 'solo': | |
389 | $this->_setParam('card_type', 'SWITCH'); | |
390 | $this->_setParam('requiresIssueNumber', TRUE); | |
391 | break; | |
392 | ||
393 | default: | |
394 | return self::error(9001, ts('Credit card type not supported by Realex:') . ' ' . $params['credit_card_type']); | |
395 | } | |
396 | ||
397 | // get the card holder name - cater cor customized billing forms | |
398 | if (isset($params['cardholder_name'])) { | |
399 | $credit_card_name = $params['cardholder_name']; | |
400 | } | |
401 | else { | |
402 | $credit_card_name = $params['first_name'] . " "; | |
403 | if (!empty($params['middle_name'])) { | |
404 | $credit_card_name .= $params['middle_name'] . " "; | |
405 | } | |
406 | $credit_card_name .= $params['last_name']; | |
407 | } | |
408 | ||
409 | $this->_setParam('card_name', $credit_card_name); | |
410 | $this->_setParam('card_number', str_replace(' ', '', $params['credit_card_number'])); | |
411 | $this->_setParam('cvn', $params['cvv2']); | |
412 | $this->_setParam('country', $params['country']); | |
413 | $this->_setParam('post_code', $params['postal_code']); | |
414 | $this->_setParam('order_id', $params['invoiceID']); | |
415 | $params['issue_number'] = (isset($params['issue_number']) ? $params['issue_number'] : ''); | |
416 | $this->_setParam('issue_number', $params['issue_number']); | |
417 | $this->_setParam('varref', $params['contributionType_name']); | |
418 | $comment = $params['description'] . ' (page id:' . $params['contributionPageID'] . ')'; | |
419 | $this->_setParam('comments', $comment); | |
420 | //$this->_setParam('currency', $params['currencyID']); | |
421 | ||
422 | // set the currency to the default which can be overrided. | |
423 | $config = CRM_Core_Config::singleton(); | |
424 | $this->_setParam('currency', $config->defaultCurrency); | |
425 | ||
426 | // Format the expiry date to MMYY | |
427 | $expmonth = (string)$params['month']; | |
428 | $expmonth = (strlen($expmonth) === 1) ? '0' . $expmonth : $expmonth; | |
429 | $expyear = substr((string)$params['year'], 2, 2); | |
430 | $this->_setParam('exp_date', $expmonth . $expyear); | |
431 | ||
432 | if (isset($params['credit_card_start_date']) && (strlen($params['credit_card_start_date']['M']) !== 0) && | |
433 | (strlen($params['credit_card_start_date']['Y']) !== 0) | |
434 | ) { | |
435 | $startmonth = (string)$params['credit_card_start_date']['M']; | |
436 | $startmonth = (strlen($startmonth) === 1) ? '0' . $startmonth : $startmonth; | |
437 | $startyear = substr((string)$params['credit_card_start_date']['Y'], 2, 2); | |
438 | $this->_setParam('start_date', $startmonth . $startyear); | |
439 | } | |
440 | ||
441 | // Create timestamp | |
442 | $timestamp = strftime("%Y%m%d%H%M%S"); | |
443 | $this->_setParam('timestamp', $timestamp); | |
444 | ||
445 | return TRUE; | |
446 | } | |
447 | ||
448 | /** | |
449 | * Checks to see if invoice_id already exists in db | |
450 | * | |
451 | * @param int $invoiceId The ID to check | |
452 | * | |
453 | * @return bool True if ID exists, else false | |
454 | */ | |
455 | function _checkDupe($invoiceId) { | |
456 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
457 | $contribution->invoice_id = $invoiceId; | |
458 | return $contribution->find(); | |
459 | } | |
460 | ||
461 | /** | |
462 | * Get the value of a field if set | |
463 | * | |
464 | * @param string $field the field | |
465 | * | |
466 | * @return mixed value of the field, or empty string if the field is | |
467 | * not set | |
468 | */ | |
469 | function _getParam($field) { | |
470 | if (isset($this->_params[$field])) { | |
471 | return $this->_params[$field]; | |
472 | } | |
473 | else { | |
474 | return ''; | |
475 | } | |
476 | } | |
477 | ||
478 | /** | |
479 | * Set a field to the specified value. Value must be a scalar (int, | |
480 | * float, string, or boolean) | |
481 | * | |
482 | * @param string $field | |
483 | * @param mixed $value | |
484 | * | |
485 | * @return bool false if value is not a scalar, true if successful | |
486 | */ | |
487 | function _setParam($field, $value) { | |
488 | if (!is_scalar($value)) { | |
489 | return FALSE; | |
490 | } | |
491 | else { | |
492 | $this->_params[$field] = $value; | |
493 | } | |
494 | } | |
495 | ||
6c786a9b EM |
496 | /** |
497 | * @param null $errorCode | |
498 | * @param null $errorMessage | |
499 | * | |
500 | * @return object | |
501 | */ | |
6a488035 TO |
502 | function &error($errorCode = NULL, $errorMessage = NULL) { |
503 | $e = CRM_Core_Error::singleton(); | |
504 | ||
505 | if ($errorCode) { | |
506 | if ($errorCode == '101' || $errorCode == '102') { | |
507 | $display_error = ts('Card declined by bank. Please try with a different card.'); | |
508 | } | |
509 | elseif ($errorCode == '103') { | |
510 | $display_error = ts('Card reported lost or stolen. This incident will be reported.'); | |
511 | } | |
512 | elseif ($errorCode == '501') { | |
513 | $display_error = ts("It appears that this transaction is a duplicate. Have you already submitted the form once? If so there may have been a connection problem. Check your email for a receipt for this transaction. If you do not receive a receipt within 2 hours you can try your transaction again. If you continue to have problems please contact the site administrator."); | |
514 | } | |
515 | elseif ($errorCode == '509') { | |
516 | $display_error = $errorMessage; | |
517 | } | |
518 | else { | |
519 | $display_error = ts('We were unable to process your payment at this time. Please try again later.'); | |
520 | } | |
521 | $e->push($errorCode, 0, NULL, $display_error); | |
522 | } | |
523 | else { | |
524 | $e->push(9001, 0, NULL, ts('We were unable to process your payment at this time. Please try again later.')); | |
525 | } | |
526 | return $e; | |
527 | } | |
528 | ||
529 | /** | |
530 | * This function checks to see if we have the right config values | |
531 | * | |
532 | * @return string the error message if any | |
533 | * @public | |
534 | */ | |
535 | function checkConfig() { | |
536 | $error = array(); | |
537 | if (empty($this->_paymentProcessor['user_name'])) { | |
538 | $error[] = ts('Merchant ID is not set for this payment processor'); | |
539 | } | |
540 | ||
541 | if (empty($this->_paymentProcessor['password'])) { | |
542 | $error[] = ts('Secret is not set for this payment processor'); | |
543 | } | |
544 | ||
545 | if (!empty($error)) { | |
546 | return implode('<p>', $error); | |
547 | } | |
548 | else { | |
549 | return NULL; | |
550 | } | |
551 | } | |
552 | } | |
553 |