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