Commit | Line | Data |
---|---|---|
e751ae55 AE |
1 | <?php |
2 | /* | |
3 | * This file is part of CiviCRM. | |
4 | * | |
5 | * CiviCRM is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * CiviCRM is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with CiviCRM. If not, see <http://www.gnu.org/licenses/>. | |
17 | * | |
18 | * Copyright (C) 2012 | |
19 | * Licensed to CiviCRM under the GPL v3 or higher | |
20 | * | |
21 | * Written and contributed by Ward Vandewege <ward@fsf.org> (http://www.fsf.org) | |
22 | * Modified by Lisa Marie Maginnis <lisa@fsf.org> (http://www.fsf.org) | |
23 | * Copyright © 2015 David Thompson <davet@gnu.org> | |
24 | * | |
25 | */ | |
26 | ||
27 | /** | |
28 | * CiviCRM payment processor module for TrustCommerece. | |
29 | * | |
30 | * This module uses the TrustCommerece API via the tc_link module (GPLv3) | |
31 | * distributed by TrustCommerece.com. For full documentation on the | |
32 | * TrustCommerece API, please see the TCDevGuide for more information: | |
33 | * https://vault.trustcommerce.com/downloads/TCDevGuide.htm | |
34 | * | |
35 | * This module supports the following features: Single credit/debit card | |
36 | * transactions, AVS checking, recurring (create, update, and cancel | |
37 | * subscription) optional blacklist with fail2ban, | |
38 | * | |
39 | * @copyright Ward Vandewege <ward@fsf.org> (http://www.fsf.org) | |
40 | * @copyright Lisa Marie Maginnis <lisa@fsf.org> (http://www.fsf.org) | |
41 | * @copyright David Thompson <davet@gnu.org> | |
42 | * @version 0.4 | |
43 | * @package org.fsf.payment.trustcommerce | |
44 | */ | |
45 | ||
46 | /** | |
47 | * Define logging level (0 = off, 4 = log everything) | |
48 | */ | |
49 | define('TRUSTCOMMERCE_LOGGING_LEVEL', 4); | |
50 | ||
51 | /** | |
52 | * Load the CiviCRM core payment class so we can extend it. | |
53 | */ | |
54 | require_once 'CRM/Core/Payment.php'; | |
55 | ||
56 | /** | |
57 | * The payment processor object, it extends CRM_Core_Payment. | |
58 | */ | |
59 | //class org_fsf_payment_trustcommerce extends CRM_Core_Payment { | |
60 | class CRM_Core_Payment_TrustCommerce extends CRM_Core_Payment { | |
61 | ||
62 | /**#@+ | |
63 | * Constants | |
64 | */ | |
65 | /** | |
66 | * This is our default charset, currently unused. | |
67 | */ | |
68 | CONST CHARSET = 'iso-8859-1'; | |
69 | /** | |
70 | * The API response value for transaction approved. | |
71 | */ | |
72 | CONST AUTH_APPROVED = 'approve'; | |
73 | /** | |
74 | * The API response value for transaction declined. | |
75 | */ | |
76 | CONST AUTH_DECLINED = 'decline'; | |
77 | /** | |
78 | * The API response value for baddata passed to the TC API. | |
79 | */ | |
80 | CONST AUTH_BADDATA = 'baddata'; | |
81 | /** | |
82 | * The API response value for an error in the TC API call. | |
83 | */ | |
84 | CONST AUTH_ERROR = 'error'; | |
85 | /** | |
86 | * The API response value for blacklisted in our local blacklist | |
87 | */ | |
88 | CONST AUTH_BLACKLIST = 'blacklisted'; | |
89 | /** | |
90 | * The API response value for approved status per the TCDevGuide. | |
91 | */ | |
92 | CONST AUTH_ACCEPTED = 'accepted'; | |
93 | ||
94 | /** | |
95 | * The current mode of the payment processor, valid values are: live, demo. | |
96 | * @static | |
97 | * @var string | |
98 | */ | |
66407e07 | 99 | protected $_mode = NULL; |
e751ae55 AE |
100 | /** |
101 | * The array of params cooked and passed to the TC API via tc_link(). | |
102 | * @static | |
103 | * @var array | |
104 | */ | |
66407e07 | 105 | protected $_params = array(); |
e751ae55 AE |
106 | |
107 | /** | |
108 | * We only need one instance of this object. So we use the singleton | |
109 | * pattern and cache the instance in this variable | |
110 | * @static | |
111 | * @var object | |
112 | */ | |
113 | static private $_singleton = NULL; | |
114 | ||
115 | /** | |
116 | * Sets our basic TC API paramaters (username, password). Also sets up: | |
117 | * logging level, processor name, the mode (live/demo), and creates/copies | |
118 | * our singleton. | |
119 | * | |
120 | * @param string $mode the mode of operation: live or test | |
121 | * @param CRM_Core_Payment The payment processor object. | |
122 | * | |
123 | * @return void | |
124 | */ | |
125 | function __construct($mode, &$paymentProcessor) { | |
66407e07 | 126 | $this->_mode = $mode; |
e751ae55 AE |
127 | |
128 | $this->_paymentProcessor = $paymentProcessor; | |
129 | ||
130 | $this->_processorName = ts('TrustCommerce'); | |
131 | ||
132 | $config = CRM_Core_Config::singleton(); | |
133 | $this->_setParam('user_name', $paymentProcessor['user_name']); | |
134 | $this->_setParam('password', $paymentProcessor['password']); | |
135 | ||
136 | $this->_setParam('timestamp', time()); | |
137 | srand(time()); | |
138 | $this->_setParam('sequence', rand(1, 1000)); | |
139 | $this->logging_level = TRUSTCOMMERCE_LOGGING_LEVEL; | |
140 | ||
141 | } | |
142 | ||
143 | /** | |
144 | * The singleton function used to manage this object | |
145 | * | |
146 | * @param string $mode the mode of operation: live or test | |
147 | * @param CRM_Core_Payment The payment processor object. | |
148 | * | |
149 | * @return object | |
150 | * @static | |
151 | */ | |
152 | static function &singleton($mode, &$paymentProcessor) { | |
153 | $processorName = $paymentProcessor['name']; | |
154 | if (self::$_singleton[$processorName] === NULL) { | |
155 | self::$_singleton[$processorName] = new CRM_Core_Payment_TrustCommerce($mode, $paymentProcessor); | |
156 | } | |
157 | return self::$_singleton[$processorName]; | |
158 | } | |
159 | ||
160 | /** | |
161 | * Submit a payment using the TC API | |
162 | * | |
163 | * @param array $params The params we will be sending to tclink_send() | |
164 | * @return mixed An array of our results, or an error object if the transaction fails. | |
165 | * @public | |
166 | */ | |
167 | function doDirectPayment(&$params) { | |
168 | if (!extension_loaded("tclink")) { | |
169 | return self::error(9001, 'TrustCommerce requires that the tclink module is loaded'); | |
170 | } | |
171 | ||
172 | /* Copy our paramaters to ourself */ | |
173 | foreach ($params as $field => $value) { | |
174 | $this->_setParam($field, $value); | |
175 | } | |
176 | ||
177 | /* Get our fields to pass to tclink_send() */ | |
178 | $tc_params = $this->_getTrustCommerceFields(); | |
179 | ||
180 | /* Are we recurring? If so add the extra API fields. */ | |
181 | if (CRM_Utils_Array::value('is_recur', $params) == 1) { | |
182 | $tc_params = $this->_getRecurPaymentFields($tc_params); | |
183 | $recur=1; | |
184 | } | |
185 | ||
186 | /* Pass our cooked params to the alter hook, per Core/Payment/Dummy.php */ | |
187 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $tc_params); | |
188 | ||
189 | // TrustCommerce will not refuse duplicates, so we should check if the user already submitted this transaction | |
190 | if ($this->_checkDupe($tc_params['ticket'])) { | |
191 | return self::error(9004, 'It appears that this transaction is a duplicate. Have you already submitted the form once? If so there may have been a connection problem. You can try your transaction again. If you continue to have problems please contact the site administrator.'); | |
192 | } | |
193 | ||
194 | /* This implements a local blacklist, and passes us though as a normal failure | |
195 | * if the luser is on the blacklist. */ | |
196 | if(!$this->_isBlacklisted($tc_params)) { | |
197 | /* Call the TC API, and grab the reply */ | |
198 | $reply = $this->_sendTCRequest($tc_params); | |
199 | } else { | |
200 | $this->_logger($tc_params); | |
201 | $reply['status'] = self::AUTH_BLACKLIST; | |
202 | usleep(rand(1000000,10000000)); | |
203 | } | |
204 | ||
205 | /* Parse our reply */ | |
206 | $result = $this->_getTCReply($reply); | |
207 | ||
208 | if(!is_object($result)) { | |
209 | if($result == 0) { | |
210 | /* We were successful, congrats. Lets wrap it up: | |
211 | * Convert back to dollars | |
212 | * Save the transaction ID | |
213 | */ | |
214 | ||
215 | if (array_key_exists('billingid', $reply)) { | |
216 | $params['recurr_profile_id'] = $reply['billingid']; | |
217 | CRM_Core_DAO::setFieldValue( | |
218 | 'CRM_Contribute_DAO_ContributionRecur', | |
219 | $this->_getParam('contributionRecurID'), | |
220 | 'processor_id', $reply['billingid'] | |
221 | ); | |
222 | } | |
223 | $params['trxn_id'] = $reply['transid']; | |
224 | ||
225 | $params['gross_amount'] = $tc_params['amount'] / 100; | |
226 | ||
227 | return $params; | |
228 | } | |
229 | } else { | |
230 | /* Otherwise we return the error object */ | |
231 | return $result; | |
232 | } | |
233 | } | |
234 | ||
235 | /** | |
236 | * Hook to update CC info for a recurring contribution | |
237 | * | |
238 | * @param string $message The message to dispaly on update success/failure | |
239 | * @param array $params The paramters to pass to the payment processor | |
240 | * | |
241 | * @return bool True if successful, false on failure | |
242 | */ | |
243 | function updateSubscriptionBillingInfo(&$message = '', $params = array()) { | |
244 | $expYear = $params['credit_card_exp_date']['Y']; | |
245 | $expMonth = $params['credit_card_exp_date']['M']; | |
246 | ||
247 | // TODO: This should be our build in params set function, not by hand! | |
248 | $tc_params = array( | |
249 | 'custid' => $this->_paymentProcessor['user_name'], | |
250 | 'password' => $this->_paymentProcessor['password'], | |
251 | 'action' => 'store', | |
252 | 'billingid' => $params['subscriptionId'], | |
253 | 'avs' => 'y', // Enable address verification | |
254 | 'address1' => $params['street_address'], | |
255 | 'zip' => $params['postal_code'], | |
256 | 'name' => $this->_formatBillingName($params['first_name'], | |
257 | $params['last_name']), | |
258 | 'cc' => $params['credit_card_number'], | |
259 | 'cvv' => $params['cvv2'], | |
260 | 'exp' => $this->_formatExpirationDate($expYear, $expMonth), | |
261 | 'amount' => $this->_formatAmount($params['amount']), | |
262 | ); | |
263 | ||
264 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $tc_params); | |
265 | ||
266 | $reply = $this->_sendTCRequest($tc_params); | |
267 | $result = $this->_getTCReply($reply); | |
268 | ||
269 | if($result === 0) { | |
270 | // TODO: Respect vaules for $messages passed in from our caller | |
271 | $message = 'Successfully updated TC billing id ' . $tc_params['billingid']; | |
272 | ||
273 | return TRUE; | |
274 | } else { | |
275 | return FALSE; | |
276 | } | |
277 | } | |
278 | ||
279 | // TODO: Use the formatting functions throughout the entire class to | |
280 | // dedupe the conversions done elsewhere in a less reusable way. | |
281 | ||
282 | /** | |
283 | * Internal routine to convert from CiviCRM amounts to TC amounts. | |
284 | * | |
285 | * Multiplies the amount by 100. | |
286 | * | |
287 | * @param float $amount The currency value to convert. | |
288 | * | |
289 | * @return int The TC amount | |
290 | */ | |
291 | private function _formatAmount($amount) { | |
292 | return $amount * 100; | |
293 | } | |
294 | ||
295 | /** | |
296 | * Internal routine to format the billing name for TC | |
297 | * | |
298 | * @param string $firstName The first name to submit to TC | |
299 | * @param string $lastName The last name to submit to TC | |
300 | * | |
301 | * @return string The TC name format, "$firstName $lastName" | |
302 | */ | |
303 | private function _formatBillingName($firstName, $lastName) { | |
304 | return "$firstName $lastName"; | |
305 | } | |
306 | ||
307 | /** | |
308 | * Formats the expiration date for TC | |
309 | * | |
310 | * @param int $year The credit card expiration year | |
311 | * @param int $month The credit card expiration year | |
312 | * | |
313 | * @return The TC CC expiration date format, "$month$year" | |
314 | */ | |
315 | private function _formatExpirationDate($year, $month) { | |
316 | $exp_month = str_pad($month, 2, '0', STR_PAD_LEFT); | |
317 | $exp_year = substr($year, -2); | |
318 | ||
319 | return "$exp_month$exp_year"; | |
320 | } | |
321 | ||
322 | private function _isParamsBlacklisted($tc_params) { | |
7de98bbd AE |
323 | |
324 | ///// debug help | |
325 | // | |
326 | //if ($_SERVER['REMOTE_ADDR'] === '74.94.156.211') { | |
327 | // $test-tc-log = fopen("/tmp/test-tc-log", "a"); | |
328 | // | |
329 | // fwrite($test-tc-log, $_SERVER['REMOTE_ADDR']."\n"); | |
330 | // fwrite($test-tc-log, print_r($this->_params[contributionPageID], true)."\n"); | |
331 | // //fwrite($test-tc-log, print_r($tc_params, true)."\n"); | |
332 | // //fwrite($test-tc-log, print_r($this, true)."\n"); | |
333 | // | |
334 | // fclose("$test-tc-log"); | |
335 | //} | |
336 | ||
736afaff RR |
337 | if(file_exists('ParamsBlacklist.php')) |
338 | include 'ParamsBlacklist.php'; | |
339 | ||
cafbec2a | 340 | return FALSE; |
e751ae55 AE |
341 | } |
342 | ||
343 | /** | |
344 | * Checks to see if the source IP/USERAGENT are blacklisted. | |
345 | * | |
346 | * @return bool TRUE if on the blacklist, FALSE if not. | |
347 | */ | |
348 | private function _isBlacklisted($tc_params) { | |
349 | if($this->_isIPBlacklisted()) { | |
350 | return TRUE; | |
351 | } else if($this->_isAgentBlacklisted()) { | |
352 | return TRUE; | |
353 | } else if($this->_isParamsBlacklisted($tc_params)) { | |
354 | return TRUE; | |
355 | } | |
356 | return FALSE; | |
357 | ||
358 | } | |
359 | ||
360 | /** | |
361 | * Checks to see if the source USERAGENT is blacklisted | |
362 | * | |
363 | * @return bool TRUE if on the blacklist, FALSE if not. | |
364 | */ | |
365 | private function _isAgentBlacklisted() { | |
366 | // TODO: fix DB calls to be more the CiviCRM way | |
367 | $ip = $_SERVER['REMOTE_ADDR']; | |
368 | $agent = $_SERVER['HTTP_USER_AGENT']; | |
369 | $dao = CRM_Core_DAO::executeQuery('SELECT * FROM `trustcommerce_useragent_blacklist`'); | |
370 | while($dao->fetch()) { | |
371 | if(preg_match('/'.$dao->name.'/', $agent) === 1) { | |
372 | error_log(' [client '.$ip.'] [agent '.$agent.'] - Blacklisted by USER_AGENT rule #'.$dao->id); | |
373 | return TRUE; | |
374 | } | |
375 | } | |
376 | return FALSE; | |
377 | } | |
378 | ||
379 | /** | |
380 | * Checks to see if the source IP is blacklisted | |
381 | * | |
382 | * @return bool TRUE if on the blacklist, FALSE if not. | |
383 | */ | |
384 | private function _isIPBlacklisted() { | |
385 | // TODO: fix DB calls to be more the CiviCRM way | |
386 | $ip = $_SERVER['REMOTE_ADDR']; | |
387 | $agent = $_SERVER['HTTP_USER_AGENT']; | |
e751ae55 AE |
388 | $blacklist = array(); |
389 | $dao = CRM_Core_DAO::executeQuery('SELECT * FROM `trustcommerce_blacklist`'); | |
390 | while($dao->fetch()) { | |
7e9cce83 RR |
391 | if($ip == $dao->ip) { |
392 | error_log('[client '.$ip.'] [agent '.$agent.'] Blacklisted by IP rule #'.$dao->id); | |
e751ae55 AE |
393 | return TRUE; |
394 | } | |
395 | } | |
396 | return FALSE; | |
397 | } | |
398 | ||
399 | /** | |
400 | * Sends the API call to TC for processing | |
401 | * | |
402 | * @param array $request The array of paramaters to pass the TC API | |
403 | * | |
404 | * @return array The response from the TC API | |
405 | */ | |
406 | function _sendTCRequest($request) { | |
407 | $this->_logger($request); | |
408 | return tclink_send($request); | |
409 | } | |
410 | ||
411 | /** | |
412 | * Logs paramaters from TC along with the remote address of the client | |
413 | * | |
414 | * Will log paramaters via the error_log() routine. For security reasons | |
415 | * the following values are not logged (skipped): custid, password, cc | |
416 | * exp, and cvv. | |
417 | * | |
418 | * @param array $params The paramaters to log | |
419 | */ | |
420 | function _logger($params) { | |
421 | $msg = ''; | |
422 | foreach ($params as $key => $data) { | |
423 | /* Delete any data we should not be writing to disk. This includes: | |
424 | * custid, password, cc, exp, and cvv | |
425 | */ | |
426 | switch($key) { | |
427 | case 'custid': | |
428 | case 'password': | |
429 | case 'cc': | |
430 | case 'exp': | |
431 | case 'cvv': | |
432 | break; | |
433 | default: | |
434 | $msg .= ' '.$key.' => '.$data; | |
435 | } | |
436 | } | |
437 | error_log('[client '.$_SERVER['REMOTE_ADDR'].'] TrustCommerce:'.$msg); | |
438 | } | |
439 | ||
440 | /** | |
441 | * Gets the recurring billing fields for the TC API | |
442 | * | |
443 | * @param array $fields The fields to modify. | |
444 | * @return array The fields for tclink_send(), modified for recurring billing. | |
445 | * @public | |
446 | */ | |
447 | function _getRecurPaymentFields($fields) { | |
448 | $payments = $this->_getParam('frequency_interval'); | |
449 | $cycle = $this->_getParam('frequency_unit'); | |
450 | ||
451 | /* Translate billing cycle from CiviCRM -> TC */ | |
452 | switch($cycle) { | |
453 | case 'day': | |
454 | $cycle = 'd'; | |
455 | break; | |
456 | case 'week': | |
457 | $cycle = 'w'; | |
458 | break; | |
459 | case 'month': | |
460 | $cycle = 'm'; | |
461 | break; | |
462 | case 'year': | |
463 | $cycle = 'y'; | |
464 | break; | |
465 | } | |
466 | ||
467 | /* Translate frequency interval from CiviCRM -> TC | |
468 | * Payments are the same, HOWEVER a payment of 1 (forever) should be 0 in TC */ | |
469 | if($payments == 1) { | |
470 | $payments = 0; | |
471 | } | |
472 | ||
473 | $fields['cycle'] = '1'.$cycle; /* The billing cycle in years, months, weeks, or days. */ | |
474 | $fields['payments'] = $payments; | |
475 | $fields['action'] = 'store'; /* Change our mode to `store' mode. */ | |
476 | ||
477 | return $fields; | |
478 | } | |
479 | ||
480 | /** Parses a response from TC via the tclink_send() command. | |
481 | * | |
482 | * @param array $reply The result of a call to tclink_send(). | |
483 | * | |
484 | * @return mixed|CRM_Core_Error CRM_Core_Error object if transaction failed, otherwise | |
485 | * returns 0. | |
486 | */ | |
487 | function _getTCReply($reply) { | |
488 | ||
489 | /* DUPLIATE CODE, please refactor. ~lisa */ | |
490 | if (!$reply) { | |
491 | return self::error(9002, 'Could not initiate connection to payment gateway.'); | |
492 | } | |
493 | ||
494 | $this->_logger($reply); | |
495 | ||
496 | switch($reply['status']) { | |
497 | case self::AUTH_BLACKLIST: | |
7e9cce83 | 498 | return self::error(9009, "Your transaction was declined. Please check the correctness of your credit card information, including CC number, expiration date and CVV code. "); |
e751ae55 AE |
499 | break; |
500 | case self::AUTH_APPROVED: | |
501 | break; | |
502 | case self::AUTH_ACCEPTED: | |
503 | // It's all good | |
504 | break; | |
505 | case self::AUTH_DECLINED: | |
506 | // TODO FIXME be more or less specific? | |
507 | // declinetype can be: decline, avs, cvv, call, expiredcard, carderror, authexpired, fraud, blacklist, velocity | |
508 | // See TC documentation for more info | |
509 | switch($reply['declinetype']) { | |
510 | case 'avs': | |
511 | return self::error(9009, "Your transaction was declined for address verification reasons. If your address was correct please contact us at donate@fsf.org before attempting to retry your transaction."); | |
512 | break; | |
513 | } | |
514 | return self::error(9009, "Your transaction was declined. Please check the correctness of your credit card information, including CC number, expiration date and CVV code."); | |
515 | break; | |
516 | case self::AUTH_BADDATA: | |
517 | // TODO FIXME do something with $reply['error'] and $reply['offender'] | |
518 | return self::error(9011, "Invalid credit card information. The following fields were invalid: {$reply['offenders']}."); | |
519 | break; | |
520 | case self::AUTH_ERROR: | |
521 | return self::error(9002, 'Could not initiate connection to payment gateway'); | |
522 | break; | |
523 | } | |
524 | return 0; | |
525 | } | |
526 | ||
527 | /** | |
528 | * Generate the basic paramaters to send the TC API | |
529 | * | |
530 | * @return array The array of paramaters to pass _sendTCRequest() | |
531 | */ | |
532 | function _getTrustCommerceFields() { | |
533 | // Total amount is from the form contribution field | |
534 | $amount = $this->_getParam('total_amount'); | |
535 | // CRM-9894 would this ever be the case?? | |
536 | if (empty($amount)) { | |
537 | $amount = $this->_getParam('amount'); | |
538 | } | |
539 | $fields = array(); | |
66407e07 RR |
540 | |
541 | $fields['custid'] = $this->_paymentProcessor['user_name']; | |
542 | $fields['password'] = $this->_paymentProcessor['password']; | |
543 | ||
e751ae55 AE |
544 | $fields['action'] = 'sale'; |
545 | ||
546 | // Enable address verification | |
547 | $fields['avs'] = 'y'; | |
548 | ||
549 | $fields['address1'] = $this->_getParam('street_address'); | |
550 | $fields['zip'] = $this->_getParam('postal_code'); | |
76223b76 | 551 | $fields['country'] = $this->_getParam('country'); |
e751ae55 | 552 | |
d30429aa RR |
553 | /** |
554 | * Fix AVS problem when a non-US country, has address1 and zip both starting | |
555 | * with a letter. | |
556 | */ | |
557 | if( ($fields['country'] !== '840' && $fields['country'] !== 840) | |
558 | && (preg_match("/^\D/", $fields['zip']) === 1) | |
559 | && (preg_match("/^\D/", $fields['address1']) === 1) ) { | |
560 | // Add a number to the beginning of the address. | |
561 | $fields['address1'] = preg_replace("/^/", "1 ", $fields['address1']); | |
562 | } | |
563 | ||
e751ae55 AE |
564 | $fields['name'] = $this->_getParam('billing_first_name') . ' ' . $this->_getParam('billing_last_name'); |
565 | ||
566 | // This assumes currencies where the . is used as the decimal point, like USD | |
567 | $amount = preg_replace("/([^0-9\\.])/i", "", $amount); | |
568 | ||
569 | // We need to pass the amount to TrustCommerce in dollar cents | |
570 | $fields['amount'] = $amount * 100; | |
571 | ||
572 | // Unique identifier | |
573 | $fields['ticket'] = substr($this->_getParam('invoiceID'), 0, 20); | |
574 | ||
575 | // cc info | |
576 | $fields['cc'] = $this->_getParam('credit_card_number'); | |
577 | $fields['cvv'] = $this->_getParam('cvv2'); | |
578 | $exp_month = str_pad($this->_getParam('month'), 2, '0', STR_PAD_LEFT); | |
579 | $exp_year = substr($this->_getParam('year'),-2); | |
580 | $fields['exp'] = "$exp_month$exp_year"; | |
581 | ||
66407e07 | 582 | if ($this->_mode != 'live') { |
e751ae55 AE |
583 | $fields['demo'] = 'y'; |
584 | } | |
585 | return $fields; | |
586 | } | |
587 | ||
588 | /** | |
589 | * Checks to see if invoice_id already exists in db | |
590 | * | |
591 | * @param int $invoiceId The ID to check | |
592 | * | |
593 | * @return bool True if ID exists, else false | |
594 | */ | |
595 | function _checkDupe($invoiceId) { | |
596 | require_once 'CRM/Contribute/DAO/Contribution.php'; | |
597 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
598 | $contribution->invoice_id = $invoiceId; | |
599 | return $contribution->find(); | |
600 | } | |
601 | ||
602 | /** | |
603 | * Get the value of a field if set | |
604 | * | |
605 | * @param string $field the field | |
606 | * | |
607 | * @return mixed value of the field, or empty string if the field is | |
608 | * not set | |
609 | */ | |
610 | function _getParam($field) { | |
66407e07 RR |
611 | $value = CRM_Utils_Array::value($field, $this->_params, ''); |
612 | if ($xmlSafe) { | |
613 | $value = str_replace(array('&', '"', "'", '<', '>'), '', $value); | |
614 | } | |
615 | return $value; | |
e751ae55 AE |
616 | } |
617 | ||
618 | /** | |
619 | * Sets our error message/logging information for CiviCRM | |
620 | * | |
621 | * @param int $errorCode The numerical code of the error, defaults to 9001 | |
622 | * @param string $errorMessage The error message to display/log | |
623 | * | |
624 | * @return CRM_Core_Error The error object with message and code. | |
625 | */ | |
626 | function &error($errorCode = NULL, $errorMessage = NULL) { | |
627 | $e = CRM_Core_Error::singleton(); | |
628 | if ($errorCode) { | |
629 | $e->push($errorCode, 0, NULL, $errorMessage); | |
630 | } | |
631 | else { | |
632 | $e->push(9001, 0, NULL, 'Unknown System Error.'); | |
633 | } | |
634 | return $e; | |
635 | } | |
636 | ||
637 | /** | |
638 | * Set a field to the specified value. Value must be a scalar (int, | |
639 | * float, string, or boolean) | |
640 | * | |
641 | * @param string $field | |
642 | * @param mixed $value | |
643 | * | |
644 | * @return bool false if value is not a scalar, true if successful | |
645 | */ | |
646 | function _setParam($field, $value) { | |
647 | if (!is_scalar($value)) { | |
648 | return FALSE; | |
649 | } | |
650 | else { | |
66407e07 | 651 | $this->_params[$field] = $value; |
e751ae55 AE |
652 | } |
653 | } | |
654 | ||
655 | /** | |
656 | * Checks to see if we have the manditory config values set. | |
657 | * | |
658 | * @return string the error message if any | |
659 | * @public | |
660 | */ | |
661 | function checkConfig() { | |
662 | $error = array(); | |
663 | if (empty($this->_paymentProcessor['user_name'])) { | |
664 | $error[] = ts('Customer ID is not set for this payment processor'); | |
665 | } | |
666 | ||
667 | if (empty($this->_paymentProcessor['password'])) { | |
668 | $error[] = ts('Password is not set for this payment processor'); | |
669 | } | |
670 | ||
671 | if (!empty($error)) { | |
672 | return implode('<p>', $error); | |
673 | } else { | |
674 | return NULL; | |
675 | } | |
676 | } | |
677 | ||
678 | /** | |
679 | * Hook to cancel a recurring contribution | |
680 | * | |
681 | * @param string $message The message to dispaly on update success/failure | |
682 | * @param array $params The paramters to pass to the payment processor | |
683 | * | |
684 | * @return bool True if successful, false on failure | |
685 | */ | |
686 | function cancelSubscription(&$message = '', $params = array()) { | |
687 | $tc_params['custid'] = $this->_getParam('user_name'); | |
688 | $tc_params['password'] = $this->_getParam('password'); | |
689 | $tc_params['action'] = 'unstore'; | |
690 | $tc_params['billingid'] = CRM_Utils_Array::value('subscriptionId', $params); | |
691 | ||
692 | $result = $this->_sendTCRequest($tc_params); | |
693 | ||
694 | /* Test if call failed */ | |
695 | if(!$result) { | |
696 | return self::error(9002, 'Could not initiate connection to payment gateway'); | |
697 | } | |
698 | /* We are done, pass success */ | |
699 | return TRUE; | |
700 | } | |
701 | ||
702 | /** | |
703 | * Hook to update amount billed for a recurring contribution | |
704 | * | |
705 | * @param string $message The message to dispaly on update success/failure | |
706 | * @param array $params The paramters to pass to the payment processor | |
707 | * | |
708 | * @return bool True if successful, false on failure | |
709 | */ | |
710 | function changeSubscriptionAmount(&$message = '', $params = array()) { | |
711 | $tc_params['custid'] = $this->_paymentProcessor['user_name']; | |
712 | $tc_params['password'] = $this->_paymentProcessor['password']; | |
713 | $tc_params['action'] = 'store'; | |
714 | ||
715 | $tc_params['billingid'] = CRM_Utils_Array::value('subscriptionId', $params); | |
716 | $tc_params['payments'] = CRM_Utils_Array::value('installments', $params); | |
717 | $tc_params['amount'] = CRM_Utils_Array::value('amount', $params) * 100; | |
718 | ||
719 | if($tc_params['payments'] == 1) { | |
720 | $tc_params['payments'] = 0; | |
721 | } | |
722 | $reply = $this->_sendTCRequest($tc_params); | |
723 | $result = $this->_getTCReply($reply); | |
724 | ||
725 | /* We are done, pass success */ | |
726 | return TRUE; | |
727 | ||
728 | } | |
729 | ||
730 | /** | |
731 | * Installs the trustcommerce module (currently a dummy) | |
732 | */ | |
733 | public function install() { | |
734 | return TRUE; | |
735 | } | |
736 | ||
737 | /** | |
738 | * Uninstalls the trustcommerce module (currently a dummy) | |
739 | */ | |
740 | public function uninstall() { | |
741 | return TRUE; | |
742 | } | |
743 | ||
744 | } | |
745 |