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