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