Commit | Line | Data |
---|---|---|
71a6ba5c LMM |
1 | <?php |
2 | /* | |
3 | * Copyright (C) 2012 | |
4 | * Licensed to CiviCRM under the GPL v3 or higher | |
5 | * | |
6 | * Written and contributed by Ward Vandewege <ward@fsf.org> (http://www.fsf.org) | |
68f3cf09 | 7 | * Modified by Lisa Marie Maginnis <lisa@fsf.org> (http://www.fsf.org) |
71a6ba5c LMM |
8 | * |
9 | */ | |
10 | ||
11 | // Define logging level (0 = off, 4 = log everything) | |
12 | define('TRUSTCOMMERCE_LOGGING_LEVEL', 4); | |
13 | ||
14 | require_once 'CRM/Core/Payment.php'; | |
15 | class org_fsf_payment_trustcommerce extends CRM_Core_Payment { | |
16 | CONST CHARSET = 'iso-8859-1'; | |
17 | CONST AUTH_APPROVED = 'approve'; | |
18 | CONST AUTH_DECLINED = 'decline'; | |
19 | CONST AUTH_BADDATA = 'baddata'; | |
20 | CONST AUTH_ERROR = 'error'; | |
21 | ||
22 | static protected $_mode = NULL; | |
23 | ||
24 | static protected $_params = array(); | |
25 | ||
26 | /** | |
27 | * We only need one instance of this object. So we use the singleton | |
28 | * pattern and cache the instance in this variable | |
29 | * | |
30 | * @var object | |
31 | * @static | |
32 | */ | |
33 | static private $_singleton = NULL; | |
34 | ||
35 | /** | |
36 | * Constructor | |
37 | * | |
38 | * @param string $mode the mode of operation: live or test | |
39 | * | |
40 | * @return void | |
41 | */ function __construct($mode, &$paymentProcessor) { | |
42 | $this->_mode = $mode; | |
43 | ||
44 | $this->_paymentProcessor = $paymentProcessor; | |
45 | ||
46 | $this->_processorName = ts('TrustCommerce'); | |
47 | ||
48 | $config = CRM_Core_Config::singleton(); | |
49 | $this->_setParam('user_name', $paymentProcessor['user_name']); | |
50 | $this->_setParam('password', $paymentProcessor['password']); | |
51 | ||
52 | $this->_setParam('timestamp', time()); | |
53 | srand(time()); | |
54 | $this->_setParam('sequence', rand(1, 1000)); | |
55 | $this->logging_level = TRUSTCOMMERCE_LOGGING_LEVEL; | |
56 | } | |
57 | ||
58 | /** | |
59 | * singleton function used to manage this object | |
60 | * | |
61 | * @param string $mode the mode of operation: live or test | |
62 | * | |
63 | * @return object | |
64 | * @static | |
65 | * | |
66 | */ | |
67 | static | |
68 | function &singleton($mode, &$paymentProcessor) { | |
69 | $processorName = $paymentProcessor['name']; | |
70 | if (self::$_singleton[$processorName] === NULL) { | |
71 | self::$_singleton[$processorName] = new org_fsf_payment_trustcommerce($mode, $paymentProcessor); | |
72 | } | |
73 | return self::$_singleton[$processorName]; | |
74 | } | |
75 | ||
76 | /** | |
77 | * Submit a payment using Advanced Integration Method | |
78 | * | |
79 | * @param array $params assoc array of input parameters for this transaction | |
80 | * | |
81 | * @return array the result in a nice formatted array (or an error object) | |
82 | * @public | |
83 | */ | |
84 | function doDirectPayment(&$params) { | |
85 | if (!extension_loaded("tclink")) { | |
86 | return self::error(9001, 'TrustCommerce requires that the tclink module is loaded'); | |
87 | } | |
88 | ||
71a6ba5c LMM |
89 | $newParams = $params; |
90 | if (CRM_Utils_Array::value('is_recur', $params) && | |
91 | $params['contributionRecurID'] | |
92 | ) { | |
93 | CRM_Utils_Hook::alterPaymentProcessorParams($this, | |
94 | $params, | |
95 | $newParams | |
96 | ); | |
97 | } | |
98 | foreach ($newParams as $field => $value) { | |
99 | $this->_setParam($field, $value); | |
100 | } | |
101 | ||
102 | if (CRM_Utils_Array::value('is_recur', $params) && | |
103 | $params['contributionRecurID'] | |
104 | ) { | |
105 | return $this->doRecurPayment($params); | |
106 | } | |
107 | ||
108 | $postFields = array(); | |
109 | $tclink = $this->_getTrustCommerceFields(); | |
110 | ||
111 | // Set up our call for hook_civicrm_paymentProcessor, | |
112 | // since we now have our parameters as assigned for the AIM back end. | |
113 | CRM_Utils_Hook::alterPaymentProcessorParams($this, | |
114 | $params, | |
115 | $tclink | |
116 | ); | |
117 | ||
118 | // TrustCommerce will not refuse duplicates, so we should check if the user already submitted this transaction | |
119 | if ($this->_checkDupe($tclink['ticket'])) { | |
120 | 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.'); | |
121 | } | |
122 | ||
123 | $result = tclink_send($tclink); | |
124 | ||
125 | if (!$result) { | |
126 | return self::error(9002, 'Could not initiate connection to payment gateway'); | |
127 | } | |
128 | ||
129 | foreach ($result as $field => $value) { | |
130 | error_log("result: $field => $value"); | |
131 | } | |
132 | ||
133 | switch($result['status']) { | |
134 | case self::AUTH_APPROVED: | |
135 | // It's all good | |
136 | break; | |
137 | case self::AUTH_DECLINED: | |
138 | // TODO FIXME be more or less specific? | |
139 | // declinetype can be: decline, avs, cvv, call, expiredcard, carderror, authexpired, fraud, blacklist, velocity | |
140 | // See TC documentation for more info | |
141 | return self::error(9009, "Your transaction was declined: {$result['declinetype']}"); | |
142 | break; | |
143 | case self::AUTH_BADDATA: | |
144 | // TODO FIXME do something with $result['error'] and $result['offender'] | |
145 | return self::error(9011, "Invalid credit card information. Please re-enter."); | |
146 | break; | |
147 | case self::AUTH_ERROR: | |
148 | return self::error(9002, 'Could not initiate connection to payment gateway'); | |
149 | break; | |
150 | } | |
151 | ||
152 | // Success | |
153 | ||
154 | $params['trxn_id'] = $result['transid']; | |
155 | $params['gross_amount'] = $tclink['amount'] / 100; | |
156 | ||
157 | return $params; | |
158 | } | |
159 | ||
160 | /** | |
161 | * Submit an Automated Recurring Billing subscription | |
162 | * | |
163 | * @param array $params assoc array of input parameters for this transaction | |
164 | * | |
165 | * @return array the result in a nice formatted array (or an error object) | |
166 | * @public | |
167 | */ | |
168 | function doRecurPayment(&$params) { | |
285eaf25 LMM |
169 | $payments = $this->_getParam('frequency_interval'); |
170 | $cycle = $this->_getParam('frequency_unit'); | |
71a6ba5c | 171 | |
285eaf25 LMM |
172 | /* Sort out our billing scheme */ |
173 | switch($cycle) { | |
174 | case 'day': | |
175 | $cycle = 'd'; | |
176 | break; | |
177 | case 'week': | |
178 | $cycle = 'w'; | |
179 | break; | |
180 | case 'month': | |
181 | $cycle = 'm'; | |
182 | break; | |
183 | case 'year': | |
184 | $cycle = 'y'; | |
185 | break; | |
186 | default: | |
187 | return self::error(9001, 'Payment interval not set! Unable to process payment.'); | |
188 | break; | |
71a6ba5c LMM |
189 | } |
190 | ||
71a6ba5c | 191 | |
285eaf25 LMM |
192 | $params['authnow'] = 'y'; /* Process this payment `now' */ |
193 | $params['cycle'] = $cycle; /* The billing cycle in years, months, weeks, or days. */ | |
194 | $params['payments'] = $payments; | |
71a6ba5c | 195 | |
71a6ba5c | 196 | |
285eaf25 | 197 | $tclink = $this->_getTrustCommerceFields(); |
71a6ba5c | 198 | |
285eaf25 LMM |
199 | // Set up our call for hook_civicrm_paymentProcessor, |
200 | // since we now have our parameters as assigned for the AIM back end. | |
201 | CRM_Utils_Hook::alterPaymentProcessorParams($this, | |
202 | $params, | |
203 | $tclink | |
204 | ); | |
71a6ba5c | 205 | |
285eaf25 LMM |
206 | // TrustCommerce will not refuse duplicates, so we should check if the user already submitted this transaction |
207 | if ($this->_checkDupe($tclink['ticket'])) { | |
208 | 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.'); | |
71a6ba5c | 209 | } |
71a6ba5c | 210 | |
285eaf25 | 211 | $result = tclink_send($tclink); |
71a6ba5c | 212 | |
a79ba043 LMM |
213 | $result = _getTrustCommereceResponse($result); |
214 | ||
215 | if($result == 0) { | |
216 | /* Transaction was sucessful */ | |
217 | $params['trxn_id'] = $result['transid']; /* Get our transaction ID */ | |
218 | $params['gross_amount'] = $tclink['amount']/100; /* Convert from cents to dollars */ | |
219 | return $params; | |
220 | } else { | |
221 | /* Transaction was *not* successful */ | |
222 | return $result; | |
223 | } | |
224 | } | |
225 | ||
226 | /* Parses a response from TC via the tclink_send() command. | |
227 | * @param $reply array The result of a call to tclink_send(). | |
228 | * @return mixed self::error() if transaction failed, otherwise returns 0. | |
229 | */ | |
230 | function _getTrustCommerceResponse($reply) { | |
71a6ba5c | 231 | |
285eaf25 LMM |
232 | /* DUPLIATE CODE, please refactor. ~lisa */ |
233 | if (!$result) { | |
234 | return self::error(9002, 'Could not initiate connection to payment gateway'); | |
71a6ba5c LMM |
235 | } |
236 | ||
285eaf25 LMM |
237 | switch($result['status']) { |
238 | case self::AUTH_APPROVED: | |
239 | // It's all good | |
240 | break; | |
241 | case self::AUTH_DECLINED: | |
242 | // TODO FIXME be more or less specific? | |
243 | // declinetype can be: decline, avs, cvv, call, expiredcard, carderror, authexpired, fraud, blacklist, velocity | |
244 | // See TC documentation for more info | |
245 | return self::error(9009, "Your transaction was declined: {$result['declinetype']}"); | |
246 | break; | |
247 | case self::AUTH_BADDATA: | |
248 | // TODO FIXME do something with $result['error'] and $result['offender'] | |
249 | return self::error(9011, "Invalid credit card information. Please re-enter."); | |
250 | break; | |
251 | case self::AUTH_ERROR: | |
252 | return self::error(9002, 'Could not initiate connection to payment gateway'); | |
253 | break; | |
71a6ba5c | 254 | } |
a79ba043 | 255 | return 0; |
71a6ba5c LMM |
256 | } |
257 | ||
258 | function _getTrustCommerceFields() { | |
259 | // Total amount is from the form contribution field | |
260 | $amount = $this->_getParam('total_amount'); | |
261 | // CRM-9894 would this ever be the case?? | |
262 | if (empty($amount)) { | |
263 | $amount = $this->_getParam('amount'); | |
264 | } | |
265 | $fields = array(); | |
266 | $fields['custid'] = $this->_getParam('user_name'); | |
267 | $fields['password'] = $this->_getParam('password'); | |
268 | $fields['action'] = 'sale'; | |
269 | ||
270 | // Enable address verification | |
271 | $fields['avs'] = 'y'; | |
272 | ||
273 | $fields['address1'] = $this->_getParam('street_address'); | |
274 | $fields['zip'] = $this->_getParam('postal_code'); | |
275 | ||
276 | $fields['name'] = $this->_getParam('billing_first_name') . ' ' . $this->_getParam('billing_last_name'); | |
277 | ||
278 | // This assumes currencies where the . is used as the decimal point, like USD | |
279 | $amount = preg_replace("/([^0-9\\.])/i", "", $amount); | |
280 | ||
281 | // We need to pass the amount to TrustCommerce in dollar cents | |
282 | $fields['amount'] = $amount * 100; | |
283 | ||
284 | // Unique identifier | |
285 | $fields['ticket'] = substr($this->_getParam('invoiceID'), 0, 20); | |
286 | ||
287 | // cc info | |
288 | $fields['cc'] = $this->_getParam('credit_card_number'); | |
289 | $fields['cvv'] = $this->_getParam('cvv2'); | |
290 | $exp_month = str_pad($this->_getParam('month'), 2, '0', STR_PAD_LEFT); | |
291 | $exp_year = substr($this->_getParam('year'),-2); | |
292 | $fields['exp'] = "$exp_month$exp_year"; | |
293 | ||
294 | if ($this->_mode != 'live') { | |
295 | $fields['demo'] = 'y'; | |
296 | } | |
71a6ba5c LMM |
297 | return $fields; |
298 | } | |
299 | ||
300 | /** | |
301 | * Checks to see if invoice_id already exists in db | |
302 | * | |
303 | * @param int $invoiceId The ID to check | |
304 | * | |
305 | * @return bool True if ID exists, else false | |
306 | */ | |
307 | function _checkDupe($invoiceId) { | |
308 | require_once 'CRM/Contribute/DAO/Contribution.php'; | |
309 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
310 | $contribution->invoice_id = $invoiceId; | |
311 | return $contribution->find(); | |
312 | } | |
313 | ||
314 | /** | |
315 | * Get the value of a field if set | |
316 | * | |
317 | * @param string $field the field | |
318 | * | |
319 | * @return mixed value of the field, or empty string if the field is | |
320 | * not set | |
321 | */ | |
322 | function _getParam($field) { | |
323 | return CRM_Utils_Array::value($field, $this->_params, ''); | |
324 | } | |
325 | ||
326 | function &error($errorCode = NULL, $errorMessage = NULL) { | |
327 | $e = CRM_Core_Error::singleton(); | |
328 | if ($errorCode) { | |
329 | $e->push($errorCode, 0, NULL, $errorMessage); | |
330 | } | |
331 | else { | |
332 | $e->push(9001, 0, NULL, 'Unknown System Error.'); | |
333 | } | |
334 | return $e; | |
335 | } | |
336 | ||
337 | /** | |
338 | * Set a field to the specified value. Value must be a scalar (int, | |
339 | * float, string, or boolean) | |
340 | * | |
341 | * @param string $field | |
342 | * @param mixed $value | |
343 | * | |
344 | * @return bool false if value is not a scalar, true if successful | |
345 | */ | |
346 | function _setParam($field, $value) { | |
347 | if (!is_scalar($value)) { | |
348 | return FALSE; | |
349 | } | |
350 | else { | |
351 | $this->_params[$field] = $value; | |
352 | } | |
353 | } | |
354 | ||
355 | /** | |
356 | * This function checks to see if we have the right config values | |
357 | * | |
358 | * @return string the error message if any | |
359 | * @public | |
360 | */ | |
361 | function checkConfig() { | |
362 | $error = array(); | |
363 | if (empty($this->_paymentProcessor['user_name'])) { | |
364 | $error[] = ts('Customer ID is not set for this payment processor'); | |
365 | } | |
366 | ||
367 | if (empty($this->_paymentProcessor['password'])) { | |
368 | $error[] = ts('Password is not set for this payment processor'); | |
369 | } | |
370 | ||
371 | if (!empty($error)) { | |
372 | return implode('<p>', $error); | |
373 | } else { | |
374 | return NULL; | |
375 | } | |
376 | } | |
377 | ||
378 | function cancelSubscriptionURL($entityID = NULL, $entity = NULL) { | |
379 | if ($entityID && $entity == 'membership') { | |
380 | require_once 'CRM/Contact/BAO/Contact/Utils.php'; | |
381 | $contactID = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $entityID, "contact_id"); | |
382 | $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf'); | |
383 | ||
384 | return CRM_Utils_System::url('civicrm/contribute/unsubscribe', | |
385 | "reset=1&mid={$entityID}&cs={$checksumValue}", TRUE, NULL, FALSE, FALSE | |
386 | ); | |
387 | } | |
388 | ||
389 | return ($this->_mode == 'test') ? 'https://test.authorize.net' : 'https://authorize.net'; | |
390 | } | |
391 | ||
392 | function cancelSubscription() { | |
393 | $template = CRM_Core_Smarty::singleton(); | |
394 | ||
395 | $template->assign('subscriptionType', 'cancel'); | |
396 | ||
397 | $template->assign('apiLogin', $this->_getParam('apiLogin')); | |
398 | $template->assign('paymentKey', $this->_getParam('paymentKey')); | |
399 | $template->assign('subscriptionId', $this->_getParam('subscriptionId')); | |
400 | ||
401 | $arbXML = $template->fetch('CRM/Contribute/Form/Contribution/AuthorizeNetARB.tpl'); | |
402 | ||
403 | // submit to authorize.net | |
404 | $submit = curl_init($this->_paymentProcessor['url_recur']); | |
405 | if (!$submit) { | |
406 | return self::error(9002, 'Could not initiate connection to payment gateway'); | |
407 | } | |
408 | ||
409 | curl_setopt($submit, CURLOPT_RETURNTRANSFER, 1); | |
410 | curl_setopt($submit, CURLOPT_HTTPHEADER, array("Content-Type: text/xml")); | |
411 | curl_setopt($submit, CURLOPT_HEADER, 1); | |
412 | curl_setopt($submit, CURLOPT_POSTFIELDS, $arbXML); | |
413 | curl_setopt($submit, CURLOPT_POST, 1); | |
414 | curl_setopt($submit, CURLOPT_SSL_VERIFYPEER, 0); | |
415 | ||
416 | $response = curl_exec($submit); | |
417 | ||
418 | if (!$response) { | |
419 | return self::error(curl_errno($submit), curl_error($submit)); | |
420 | } | |
421 | ||
422 | curl_close($submit); | |
423 | ||
424 | $responseFields = $this->_ParseArbReturn($response); | |
425 | ||
426 | if ($responseFields['resultCode'] == 'Error') { | |
427 | return self::error($responseFields['code'], $responseFields['text']); | |
428 | } | |
429 | ||
430 | // carry on cancelation procedure | |
431 | return TRUE; | |
432 | } | |
433 | ||
434 | public function install() { | |
435 | return TRUE; | |
436 | } | |
437 | ||
438 | public function uninstall() { | |
439 | return TRUE; | |
440 | } | |
441 | ||
442 | } |