Commit | Line | Data |
---|---|---|
71a6ba5c LMM |
1 | <?php |
2 | /* | |
93b42747 LMM |
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 | * | |
71a6ba5c LMM |
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) | |
68f3cf09 | 22 | * Modified by Lisa Marie Maginnis <lisa@fsf.org> (http://www.fsf.org) |
71a6ba5c LMM |
23 | * |
24 | */ | |
25 | ||
26 | // Define logging level (0 = off, 4 = log everything) | |
27 | define('TRUSTCOMMERCE_LOGGING_LEVEL', 4); | |
28 | ||
29 | require_once 'CRM/Core/Payment.php'; | |
30 | class org_fsf_payment_trustcommerce extends CRM_Core_Payment { | |
31 | CONST CHARSET = 'iso-8859-1'; | |
32 | CONST AUTH_APPROVED = 'approve'; | |
33 | CONST AUTH_DECLINED = 'decline'; | |
34 | CONST AUTH_BADDATA = 'baddata'; | |
35 | CONST AUTH_ERROR = 'error'; | |
4d58ae2f | 36 | CONST AUTH_BLACKLIST = 'blacklisted'; |
71a6ba5c LMM |
37 | |
38 | static protected $_mode = NULL; | |
39 | ||
40 | static protected $_params = array(); | |
41 | ||
42 | /** | |
43 | * We only need one instance of this object. So we use the singleton | |
44 | * pattern and cache the instance in this variable | |
45 | * | |
46 | * @var object | |
47 | * @static | |
48 | */ | |
49 | static private $_singleton = NULL; | |
50 | ||
51 | /** | |
52 | * Constructor | |
53 | * | |
54 | * @param string $mode the mode of operation: live or test | |
55 | * | |
56 | * @return void | |
57 | */ function __construct($mode, &$paymentProcessor) { | |
58 | $this->_mode = $mode; | |
59 | ||
60 | $this->_paymentProcessor = $paymentProcessor; | |
61 | ||
62 | $this->_processorName = ts('TrustCommerce'); | |
63 | ||
64 | $config = CRM_Core_Config::singleton(); | |
65 | $this->_setParam('user_name', $paymentProcessor['user_name']); | |
66 | $this->_setParam('password', $paymentProcessor['password']); | |
67 | ||
68 | $this->_setParam('timestamp', time()); | |
69 | srand(time()); | |
70 | $this->_setParam('sequence', rand(1, 1000)); | |
71 | $this->logging_level = TRUSTCOMMERCE_LOGGING_LEVEL; | |
4d58ae2f | 72 | |
71a6ba5c LMM |
73 | } |
74 | ||
75 | /** | |
76 | * singleton function used to manage this object | |
77 | * | |
78 | * @param string $mode the mode of operation: live or test | |
79 | * | |
80 | * @return object | |
81 | * @static | |
82 | * | |
83 | */ | |
84 | static | |
85 | function &singleton($mode, &$paymentProcessor) { | |
86 | $processorName = $paymentProcessor['name']; | |
87 | if (self::$_singleton[$processorName] === NULL) { | |
88 | self::$_singleton[$processorName] = new org_fsf_payment_trustcommerce($mode, $paymentProcessor); | |
89 | } | |
90 | return self::$_singleton[$processorName]; | |
91 | } | |
92 | ||
93 | /** | |
e0273c0a LMM |
94 | * Submit a payment using the TC API |
95 | * @param array $params The params we will be sending to tclink_send() | |
96 | * @return mixed An array of our results, or an error object if the transaction fails. | |
71a6ba5c LMM |
97 | * @public |
98 | */ | |
99 | function doDirectPayment(&$params) { | |
100 | if (!extension_loaded("tclink")) { | |
101 | return self::error(9001, 'TrustCommerce requires that the tclink module is loaded'); | |
102 | } | |
103 | ||
f322addf LMM |
104 | /* Copy our paramaters to ourself */ |
105 | foreach ($params as $field => $value) { | |
71a6ba5c LMM |
106 | $this->_setParam($field, $value); |
107 | } | |
108 | ||
f322addf LMM |
109 | /* Get our fields to pass to tclink_send() */ |
110 | $tc_params = $this->_getTrustCommerceFields(); | |
71a6ba5c | 111 | |
f322addf LMM |
112 | /* Are we recurring? If so add the extra API fields. */ |
113 | if (CRM_Utils_Array::value('is_recur', $params) && $params['contributionRecurID']) { | |
114 | $tc_params = $this->_getRecurPaymentFields($tc_params); | |
115 | } | |
71a6ba5c | 116 | |
f322addf | 117 | /* Pass our cooked params to the alter hook, per Core/Payment/Dummy.php */ |
32bf0975 | 118 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $tc_params); |
71a6ba5c LMM |
119 | |
120 | // TrustCommerce will not refuse duplicates, so we should check if the user already submitted this transaction | |
f322addf | 121 | if ($this->_checkDupe($tc_params['ticket'])) { |
71a6ba5c LMM |
122 | 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.'); |
123 | } | |
124 | ||
4d58ae2f LMM |
125 | /* This implements a local blacklist, and passes us though as a normal failure |
126 | * if the luser is on the blacklist. */ | |
127 | if(!$this->_isBlacklisted()) { | |
128 | /* Call the TC API, and grab the reply */ | |
129 | $reply = $this->_sendTCRequest($tc_params); | |
130 | } else { | |
131 | $this->_logger($tc_params); | |
132 | $reply['status'] = self::AUTH_BLACKLIST; | |
133 | } | |
71a6ba5c | 134 | |
f322addf | 135 | /* Parse our reply */ |
5efae07f | 136 | $result = $this->_getTCReply($reply); |
71a6ba5c | 137 | |
f322addf LMM |
138 | if($result == 0) { |
139 | /* We were successful, congrats. Lets wrap it up: | |
140 | * Convert back to dollars | |
141 | * Save the transaction ID | |
142 | */ | |
143 | $params['trxn_id'] = $reply['transid']; | |
144 | $params['gross_amount'] = $tc_params['amount'] / 100; | |
71a6ba5c | 145 | |
f322addf | 146 | return $params; |
71a6ba5c | 147 | |
f322addf LMM |
148 | } else { |
149 | /* Otherwise we return the error object */ | |
150 | return $result; | |
151 | } | |
71a6ba5c LMM |
152 | } |
153 | ||
24f18244 DT |
154 | /* Return TRUE when client is either IP or agent blacklisted, or |
155 | * FALSE otherwise. | |
156 | */ | |
4d58ae2f | 157 | function _isBlacklisted() { |
24f18244 | 158 | return $this->_isIPBlacklisted() || $this->_IsAgentBlacklisted(); |
4d58ae2f LMM |
159 | } |
160 | ||
161 | function _isAgentBlacklisted() { | |
162 | $ip = $_SERVER['REMOTE_ADDR']; | |
163 | $agent = $_SERVER['HTTP_USER_AGENT']; | |
164 | $dao = CRM_Core_DAO::executeQuery('SELECT * FROM `trustcommerce_useragent_blacklist`'); | |
165 | while($dao->fetch()) { | |
166 | if(preg_match('/'.$dao->name.'/', $agent) === 1) { | |
8c39d2f7 DT |
167 | error_log(' [client '.$ip.'] [agent '.$agent.'] - Blacklisted by USER_AGENT rule #'.$dao->id); |
168 | return TRUE; | |
4d58ae2f LMM |
169 | } |
170 | } | |
171 | return FALSE; | |
172 | } | |
173 | ||
174 | function _isIPBlacklisted() { | |
175 | $ip = $_SERVER['REMOTE_ADDR']; | |
c389ccec | 176 | $agent = $_SERVER['HTTP_USER_AGENT']; |
4d58ae2f LMM |
177 | $ip = ip2long($ip); |
178 | $blacklist = array(); | |
179 | $dao = CRM_Core_DAO::executeQuery('SELECT * FROM `trustcommerce_blacklist`'); | |
180 | while($dao->fetch()) { | |
181 | if($ip >= $dao->start && $ip <= $dao->end) { | |
8c39d2f7 DT |
182 | error_log('[client '.$ip.'] [agent '.$agent.'] Blacklisted by IP rule #'.$dao->id); |
183 | return TRUE; | |
4d58ae2f LMM |
184 | } |
185 | } | |
186 | return FALSE; | |
187 | } | |
188 | ||
e055e8ef LMM |
189 | function _sendTCRequest($request) { |
190 | $this->_logger($request); | |
191 | return tclink_send($request); | |
192 | } | |
193 | ||
194 | function _logger($params) { | |
e990a9a5 | 195 | $msg = ''; |
69a1e975 | 196 | foreach ($params as $key => $data) { |
e055e8ef LMM |
197 | /* Delete any data we should not be writing to disk. This includes: |
198 | * custid, password, cc, exp, and cvv | |
199 | */ | |
e055e8ef | 200 | switch($key) { |
e990a9a5 LMM |
201 | case 'custid': |
202 | case 'password': | |
203 | case 'cc': | |
204 | case 'exp': | |
205 | case 'cvv': | |
e055e8ef LMM |
206 | break; |
207 | default: | |
e990a9a5 | 208 | $msg .= ' '.$key.' => '.$data; |
e055e8ef LMM |
209 | } |
210 | } | |
4d58ae2f | 211 | error_log('[client '.$_SERVER['REMOTE_ADDR'].'] TrustCommerce:'.$msg); |
e055e8ef LMM |
212 | } |
213 | ||
71a6ba5c | 214 | /** |
f322addf LMM |
215 | * Gets the recurring billing fields for the TC API |
216 | * @param array $fields The fields to modify. | |
217 | * @return array The fields for tclink_send(), modified for recurring billing. | |
71a6ba5c LMM |
218 | * @public |
219 | */ | |
f322addf | 220 | function _getRecurPaymentFields($fields) { |
285eaf25 LMM |
221 | $payments = $this->_getParam('frequency_interval'); |
222 | $cycle = $this->_getParam('frequency_unit'); | |
71a6ba5c | 223 | |
f322addf | 224 | /* Translate billing cycle from CiviCRM -> TC */ |
285eaf25 LMM |
225 | switch($cycle) { |
226 | case 'day': | |
227 | $cycle = 'd'; | |
228 | break; | |
229 | case 'week': | |
230 | $cycle = 'w'; | |
231 | break; | |
232 | case 'month': | |
233 | $cycle = 'm'; | |
234 | break; | |
235 | case 'year': | |
236 | $cycle = 'y'; | |
237 | break; | |
71a6ba5c | 238 | } |
bc323ec4 LMM |
239 | |
240 | /* Translate frequency interval from CiviCRM -> TC | |
241 | * Payments are the same, HOWEVER a payment of 1 (forever) should be 0 in TC */ | |
242 | if($payments == 1) { | |
243 | $payments = 0; | |
71a6ba5c | 244 | } |
71a6ba5c | 245 | |
bc323ec4 LMM |
246 | $fields['cycle'] = '1'.$cycle; /* The billing cycle in years, months, weeks, or days. */ |
247 | $fields['payments'] = $payments; | |
248 | $fields['action'] = 'store'; /* Change our mode to `store' mode. */ | |
71a6ba5c | 249 | |
bc323ec4 | 250 | return $fields; |
a79ba043 LMM |
251 | } |
252 | ||
253 | /* Parses a response from TC via the tclink_send() command. | |
254 | * @param $reply array The result of a call to tclink_send(). | |
255 | * @return mixed self::error() if transaction failed, otherwise returns 0. | |
256 | */ | |
5efae07f | 257 | function _getTCReply($reply) { |
71a6ba5c | 258 | |
285eaf25 | 259 | /* DUPLIATE CODE, please refactor. ~lisa */ |
bc323ec4 | 260 | if (!$reply) { |
285eaf25 | 261 | return self::error(9002, 'Could not initiate connection to payment gateway'); |
71a6ba5c LMM |
262 | } |
263 | ||
4d58ae2f LMM |
264 | $this->_logger($reply); |
265 | ||
bc323ec4 | 266 | switch($reply['status']) { |
4d58ae2f LMM |
267 | case self::AUTH_BLACKLIST: |
268 | return self::error(9001, "Your transaction was declined: error #90210"); | |
269 | break; | |
285eaf25 LMM |
270 | case self::AUTH_APPROVED: |
271 | // It's all good | |
272 | break; | |
273 | case self::AUTH_DECLINED: | |
274 | // TODO FIXME be more or less specific? | |
275 | // declinetype can be: decline, avs, cvv, call, expiredcard, carderror, authexpired, fraud, blacklist, velocity | |
276 | // See TC documentation for more info | |
a8c5366f | 277 | return self::error(9009, "Your transaction was declined: {$reply['declinetype']}"); |
285eaf25 LMM |
278 | break; |
279 | case self::AUTH_BADDATA: | |
a8c5366f LMM |
280 | // TODO FIXME do something with $reply['error'] and $reply['offender'] |
281 | return self::error(9011, "Invalid credit card information. The following fields were invalid: {$reply['offenders']}."); | |
285eaf25 LMM |
282 | break; |
283 | case self::AUTH_ERROR: | |
284 | return self::error(9002, 'Could not initiate connection to payment gateway'); | |
285 | break; | |
71a6ba5c | 286 | } |
a79ba043 | 287 | return 0; |
71a6ba5c LMM |
288 | } |
289 | ||
290 | function _getTrustCommerceFields() { | |
291 | // Total amount is from the form contribution field | |
292 | $amount = $this->_getParam('total_amount'); | |
293 | // CRM-9894 would this ever be the case?? | |
294 | if (empty($amount)) { | |
295 | $amount = $this->_getParam('amount'); | |
296 | } | |
297 | $fields = array(); | |
298 | $fields['custid'] = $this->_getParam('user_name'); | |
299 | $fields['password'] = $this->_getParam('password'); | |
300 | $fields['action'] = 'sale'; | |
301 | ||
302 | // Enable address verification | |
303 | $fields['avs'] = 'y'; | |
304 | ||
305 | $fields['address1'] = $this->_getParam('street_address'); | |
306 | $fields['zip'] = $this->_getParam('postal_code'); | |
307 | ||
308 | $fields['name'] = $this->_getParam('billing_first_name') . ' ' . $this->_getParam('billing_last_name'); | |
309 | ||
310 | // This assumes currencies where the . is used as the decimal point, like USD | |
311 | $amount = preg_replace("/([^0-9\\.])/i", "", $amount); | |
312 | ||
313 | // We need to pass the amount to TrustCommerce in dollar cents | |
314 | $fields['amount'] = $amount * 100; | |
315 | ||
316 | // Unique identifier | |
317 | $fields['ticket'] = substr($this->_getParam('invoiceID'), 0, 20); | |
318 | ||
319 | // cc info | |
320 | $fields['cc'] = $this->_getParam('credit_card_number'); | |
321 | $fields['cvv'] = $this->_getParam('cvv2'); | |
322 | $exp_month = str_pad($this->_getParam('month'), 2, '0', STR_PAD_LEFT); | |
323 | $exp_year = substr($this->_getParam('year'),-2); | |
324 | $fields['exp'] = "$exp_month$exp_year"; | |
325 | ||
326 | if ($this->_mode != 'live') { | |
327 | $fields['demo'] = 'y'; | |
328 | } | |
71a6ba5c LMM |
329 | return $fields; |
330 | } | |
331 | ||
332 | /** | |
333 | * Checks to see if invoice_id already exists in db | |
334 | * | |
335 | * @param int $invoiceId The ID to check | |
336 | * | |
337 | * @return bool True if ID exists, else false | |
338 | */ | |
339 | function _checkDupe($invoiceId) { | |
340 | require_once 'CRM/Contribute/DAO/Contribution.php'; | |
341 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
342 | $contribution->invoice_id = $invoiceId; | |
343 | return $contribution->find(); | |
344 | } | |
345 | ||
346 | /** | |
347 | * Get the value of a field if set | |
348 | * | |
349 | * @param string $field the field | |
350 | * | |
351 | * @return mixed value of the field, or empty string if the field is | |
352 | * not set | |
353 | */ | |
354 | function _getParam($field) { | |
355 | return CRM_Utils_Array::value($field, $this->_params, ''); | |
356 | } | |
357 | ||
358 | function &error($errorCode = NULL, $errorMessage = NULL) { | |
359 | $e = CRM_Core_Error::singleton(); | |
360 | if ($errorCode) { | |
361 | $e->push($errorCode, 0, NULL, $errorMessage); | |
362 | } | |
363 | else { | |
364 | $e->push(9001, 0, NULL, 'Unknown System Error.'); | |
365 | } | |
366 | return $e; | |
367 | } | |
368 | ||
369 | /** | |
370 | * Set a field to the specified value. Value must be a scalar (int, | |
371 | * float, string, or boolean) | |
372 | * | |
373 | * @param string $field | |
374 | * @param mixed $value | |
375 | * | |
376 | * @return bool false if value is not a scalar, true if successful | |
377 | */ | |
378 | function _setParam($field, $value) { | |
379 | if (!is_scalar($value)) { | |
380 | return FALSE; | |
381 | } | |
382 | else { | |
383 | $this->_params[$field] = $value; | |
384 | } | |
385 | } | |
386 | ||
387 | /** | |
388 | * This function checks to see if we have the right config values | |
389 | * | |
390 | * @return string the error message if any | |
391 | * @public | |
392 | */ | |
393 | function checkConfig() { | |
394 | $error = array(); | |
395 | if (empty($this->_paymentProcessor['user_name'])) { | |
396 | $error[] = ts('Customer ID is not set for this payment processor'); | |
397 | } | |
398 | ||
399 | if (empty($this->_paymentProcessor['password'])) { | |
400 | $error[] = ts('Password is not set for this payment processor'); | |
401 | } | |
402 | ||
403 | if (!empty($error)) { | |
404 | return implode('<p>', $error); | |
405 | } else { | |
406 | return NULL; | |
407 | } | |
408 | } | |
409 | ||
14f15ff3 | 410 | function cancelSubscription(&$message = '', $params = array()) { |
7964e0e9 LMM |
411 | $tc_params['custid'] = $this->_getParam('user_name'); |
412 | $tc_params['password'] = $this->_getParam('password'); | |
413 | $tc_params['action'] = 'unstore'; | |
cbf5200e | 414 | $tc_params['billingid'] = CRM_Utils_Array::value('trxn_id', $params); |
e055e8ef | 415 | |
e990a9a5 | 416 | $result = $this->_sendTCRequest($tc_params); |
71a6ba5c | 417 | |
7964e0e9 LMM |
418 | /* Test if call failed */ |
419 | if(!$result) { | |
71a6ba5c LMM |
420 | return self::error(9002, 'Could not initiate connection to payment gateway'); |
421 | } | |
7964e0e9 | 422 | /* We are done, pass success */ |
71a6ba5c LMM |
423 | return TRUE; |
424 | } | |
425 | ||
426 | public function install() { | |
427 | return TRUE; | |
428 | } | |
429 | ||
430 | public function uninstall() { | |
431 | return TRUE; | |
432 | } | |
433 | ||
434 | } |