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 | /** | |
e0273c0a LMM |
77 | * Submit a payment using the TC API |
78 | * @param array $params The params we will be sending to tclink_send() | |
79 | * @return mixed An array of our results, or an error object if the transaction fails. | |
71a6ba5c LMM |
80 | * @public |
81 | */ | |
82 | function doDirectPayment(&$params) { | |
83 | if (!extension_loaded("tclink")) { | |
84 | return self::error(9001, 'TrustCommerce requires that the tclink module is loaded'); | |
85 | } | |
86 | ||
f322addf LMM |
87 | /* Copy our paramaters to ourself */ |
88 | foreach ($params as $field => $value) { | |
71a6ba5c LMM |
89 | $this->_setParam($field, $value); |
90 | } | |
91 | ||
f322addf LMM |
92 | /* Get our fields to pass to tclink_send() */ |
93 | $tc_params = $this->_getTrustCommerceFields(); | |
71a6ba5c | 94 | |
f322addf LMM |
95 | /* Are we recurring? If so add the extra API fields. */ |
96 | if (CRM_Utils_Array::value('is_recur', $params) && $params['contributionRecurID']) { | |
97 | $tc_params = $this->_getRecurPaymentFields($tc_params); | |
98 | } | |
71a6ba5c | 99 | |
f322addf | 100 | /* Pass our cooked params to the alter hook, per Core/Payment/Dummy.php */ |
71a6ba5c LMM |
101 | CRM_Utils_Hook::alterPaymentProcessorParams($this, |
102 | $params, | |
f322addf | 103 | $tc_params |
71a6ba5c LMM |
104 | ); |
105 | ||
106 | // TrustCommerce will not refuse duplicates, so we should check if the user already submitted this transaction | |
f322addf | 107 | if ($this->_checkDupe($tc_params['ticket'])) { |
71a6ba5c LMM |
108 | 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.'); |
109 | } | |
110 | ||
f322addf LMM |
111 | /* Call the TC API, and grab the reply */ |
112 | $reply = tclink_send($tc_params); | |
71a6ba5c | 113 | |
f322addf LMM |
114 | /* Parse our reply */ |
115 | $result = $this->_getTrustCommerceReply($reply); | |
71a6ba5c | 116 | |
f322addf LMM |
117 | if($result == 0) { |
118 | /* We were successful, congrats. Lets wrap it up: | |
119 | * Convert back to dollars | |
120 | * Save the transaction ID | |
121 | */ | |
122 | $params['trxn_id'] = $reply['transid']; | |
123 | $params['gross_amount'] = $tc_params['amount'] / 100; | |
71a6ba5c | 124 | |
f322addf | 125 | return $params; |
71a6ba5c | 126 | |
f322addf LMM |
127 | } else { |
128 | /* Otherwise we return the error object */ | |
129 | return $result; | |
130 | } | |
71a6ba5c LMM |
131 | } |
132 | ||
133 | /** | |
f322addf LMM |
134 | * Gets the recurring billing fields for the TC API |
135 | * @param array $fields The fields to modify. | |
136 | * @return array The fields for tclink_send(), modified for recurring billing. | |
71a6ba5c LMM |
137 | * @public |
138 | */ | |
f322addf | 139 | function _getRecurPaymentFields($fields) { |
285eaf25 LMM |
140 | $payments = $this->_getParam('frequency_interval'); |
141 | $cycle = $this->_getParam('frequency_unit'); | |
71a6ba5c | 142 | |
f322addf | 143 | /* Translate billing cycle from CiviCRM -> TC */ |
285eaf25 LMM |
144 | switch($cycle) { |
145 | case 'day': | |
146 | $cycle = 'd'; | |
147 | break; | |
148 | case 'week': | |
149 | $cycle = 'w'; | |
150 | break; | |
151 | case 'month': | |
152 | $cycle = 'm'; | |
153 | break; | |
154 | case 'year': | |
155 | $cycle = 'y'; | |
156 | break; | |
71a6ba5c | 157 | } |
bc323ec4 LMM |
158 | |
159 | /* Translate frequency interval from CiviCRM -> TC | |
160 | * Payments are the same, HOWEVER a payment of 1 (forever) should be 0 in TC */ | |
161 | if($payments == 1) { | |
162 | $payments = 0; | |
71a6ba5c | 163 | } |
71a6ba5c | 164 | |
bc323ec4 LMM |
165 | $fields['cycle'] = '1'.$cycle; /* The billing cycle in years, months, weeks, or days. */ |
166 | $fields['payments'] = $payments; | |
167 | $fields['action'] = 'store'; /* Change our mode to `store' mode. */ | |
71a6ba5c | 168 | |
bc323ec4 | 169 | return $fields; |
a79ba043 LMM |
170 | } |
171 | ||
172 | /* Parses a response from TC via the tclink_send() command. | |
173 | * @param $reply array The result of a call to tclink_send(). | |
174 | * @return mixed self::error() if transaction failed, otherwise returns 0. | |
175 | */ | |
bc323ec4 | 176 | function _getTrustCommerceReply($reply) { |
71a6ba5c | 177 | |
285eaf25 | 178 | /* DUPLIATE CODE, please refactor. ~lisa */ |
bc323ec4 | 179 | if (!$reply) { |
285eaf25 | 180 | return self::error(9002, 'Could not initiate connection to payment gateway'); |
71a6ba5c LMM |
181 | } |
182 | ||
bc323ec4 | 183 | switch($reply['status']) { |
285eaf25 LMM |
184 | case self::AUTH_APPROVED: |
185 | // It's all good | |
186 | break; | |
187 | case self::AUTH_DECLINED: | |
188 | // TODO FIXME be more or less specific? | |
189 | // declinetype can be: decline, avs, cvv, call, expiredcard, carderror, authexpired, fraud, blacklist, velocity | |
190 | // See TC documentation for more info | |
191 | return self::error(9009, "Your transaction was declined: {$result['declinetype']}"); | |
192 | break; | |
193 | case self::AUTH_BADDATA: | |
194 | // TODO FIXME do something with $result['error'] and $result['offender'] | |
195 | return self::error(9011, "Invalid credit card information. Please re-enter."); | |
196 | break; | |
197 | case self::AUTH_ERROR: | |
198 | return self::error(9002, 'Could not initiate connection to payment gateway'); | |
199 | break; | |
71a6ba5c | 200 | } |
a79ba043 | 201 | return 0; |
71a6ba5c LMM |
202 | } |
203 | ||
204 | function _getTrustCommerceFields() { | |
205 | // Total amount is from the form contribution field | |
206 | $amount = $this->_getParam('total_amount'); | |
207 | // CRM-9894 would this ever be the case?? | |
208 | if (empty($amount)) { | |
209 | $amount = $this->_getParam('amount'); | |
210 | } | |
211 | $fields = array(); | |
212 | $fields['custid'] = $this->_getParam('user_name'); | |
213 | $fields['password'] = $this->_getParam('password'); | |
214 | $fields['action'] = 'sale'; | |
215 | ||
216 | // Enable address verification | |
217 | $fields['avs'] = 'y'; | |
218 | ||
219 | $fields['address1'] = $this->_getParam('street_address'); | |
220 | $fields['zip'] = $this->_getParam('postal_code'); | |
221 | ||
222 | $fields['name'] = $this->_getParam('billing_first_name') . ' ' . $this->_getParam('billing_last_name'); | |
223 | ||
224 | // This assumes currencies where the . is used as the decimal point, like USD | |
225 | $amount = preg_replace("/([^0-9\\.])/i", "", $amount); | |
226 | ||
227 | // We need to pass the amount to TrustCommerce in dollar cents | |
228 | $fields['amount'] = $amount * 100; | |
229 | ||
230 | // Unique identifier | |
231 | $fields['ticket'] = substr($this->_getParam('invoiceID'), 0, 20); | |
232 | ||
233 | // cc info | |
234 | $fields['cc'] = $this->_getParam('credit_card_number'); | |
235 | $fields['cvv'] = $this->_getParam('cvv2'); | |
236 | $exp_month = str_pad($this->_getParam('month'), 2, '0', STR_PAD_LEFT); | |
237 | $exp_year = substr($this->_getParam('year'),-2); | |
238 | $fields['exp'] = "$exp_month$exp_year"; | |
239 | ||
240 | if ($this->_mode != 'live') { | |
241 | $fields['demo'] = 'y'; | |
242 | } | |
71a6ba5c LMM |
243 | return $fields; |
244 | } | |
245 | ||
246 | /** | |
247 | * Checks to see if invoice_id already exists in db | |
248 | * | |
249 | * @param int $invoiceId The ID to check | |
250 | * | |
251 | * @return bool True if ID exists, else false | |
252 | */ | |
253 | function _checkDupe($invoiceId) { | |
254 | require_once 'CRM/Contribute/DAO/Contribution.php'; | |
255 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
256 | $contribution->invoice_id = $invoiceId; | |
257 | return $contribution->find(); | |
258 | } | |
259 | ||
260 | /** | |
261 | * Get the value of a field if set | |
262 | * | |
263 | * @param string $field the field | |
264 | * | |
265 | * @return mixed value of the field, or empty string if the field is | |
266 | * not set | |
267 | */ | |
268 | function _getParam($field) { | |
269 | return CRM_Utils_Array::value($field, $this->_params, ''); | |
270 | } | |
271 | ||
272 | function &error($errorCode = NULL, $errorMessage = NULL) { | |
273 | $e = CRM_Core_Error::singleton(); | |
274 | if ($errorCode) { | |
275 | $e->push($errorCode, 0, NULL, $errorMessage); | |
276 | } | |
277 | else { | |
278 | $e->push(9001, 0, NULL, 'Unknown System Error.'); | |
279 | } | |
280 | return $e; | |
281 | } | |
282 | ||
283 | /** | |
284 | * Set a field to the specified value. Value must be a scalar (int, | |
285 | * float, string, or boolean) | |
286 | * | |
287 | * @param string $field | |
288 | * @param mixed $value | |
289 | * | |
290 | * @return bool false if value is not a scalar, true if successful | |
291 | */ | |
292 | function _setParam($field, $value) { | |
293 | if (!is_scalar($value)) { | |
294 | return FALSE; | |
295 | } | |
296 | else { | |
297 | $this->_params[$field] = $value; | |
298 | } | |
299 | } | |
300 | ||
301 | /** | |
302 | * This function checks to see if we have the right config values | |
303 | * | |
304 | * @return string the error message if any | |
305 | * @public | |
306 | */ | |
307 | function checkConfig() { | |
308 | $error = array(); | |
309 | if (empty($this->_paymentProcessor['user_name'])) { | |
310 | $error[] = ts('Customer ID is not set for this payment processor'); | |
311 | } | |
312 | ||
313 | if (empty($this->_paymentProcessor['password'])) { | |
314 | $error[] = ts('Password is not set for this payment processor'); | |
315 | } | |
316 | ||
317 | if (!empty($error)) { | |
318 | return implode('<p>', $error); | |
319 | } else { | |
320 | return NULL; | |
321 | } | |
322 | } | |
323 | ||
324 | function cancelSubscriptionURL($entityID = NULL, $entity = NULL) { | |
325 | if ($entityID && $entity == 'membership') { | |
326 | require_once 'CRM/Contact/BAO/Contact/Utils.php'; | |
327 | $contactID = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $entityID, "contact_id"); | |
328 | $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf'); | |
329 | ||
330 | return CRM_Utils_System::url('civicrm/contribute/unsubscribe', | |
331 | "reset=1&mid={$entityID}&cs={$checksumValue}", TRUE, NULL, FALSE, FALSE | |
332 | ); | |
333 | } | |
334 | ||
335 | return ($this->_mode == 'test') ? 'https://test.authorize.net' : 'https://authorize.net'; | |
336 | } | |
337 | ||
338 | function cancelSubscription() { | |
339 | $template = CRM_Core_Smarty::singleton(); | |
340 | ||
341 | $template->assign('subscriptionType', 'cancel'); | |
342 | ||
343 | $template->assign('apiLogin', $this->_getParam('apiLogin')); | |
344 | $template->assign('paymentKey', $this->_getParam('paymentKey')); | |
345 | $template->assign('subscriptionId', $this->_getParam('subscriptionId')); | |
346 | ||
347 | $arbXML = $template->fetch('CRM/Contribute/Form/Contribution/AuthorizeNetARB.tpl'); | |
348 | ||
349 | // submit to authorize.net | |
350 | $submit = curl_init($this->_paymentProcessor['url_recur']); | |
351 | if (!$submit) { | |
352 | return self::error(9002, 'Could not initiate connection to payment gateway'); | |
353 | } | |
354 | ||
355 | curl_setopt($submit, CURLOPT_RETURNTRANSFER, 1); | |
356 | curl_setopt($submit, CURLOPT_HTTPHEADER, array("Content-Type: text/xml")); | |
357 | curl_setopt($submit, CURLOPT_HEADER, 1); | |
358 | curl_setopt($submit, CURLOPT_POSTFIELDS, $arbXML); | |
359 | curl_setopt($submit, CURLOPT_POST, 1); | |
360 | curl_setopt($submit, CURLOPT_SSL_VERIFYPEER, 0); | |
361 | ||
362 | $response = curl_exec($submit); | |
363 | ||
364 | if (!$response) { | |
365 | return self::error(curl_errno($submit), curl_error($submit)); | |
366 | } | |
367 | ||
368 | curl_close($submit); | |
369 | ||
370 | $responseFields = $this->_ParseArbReturn($response); | |
371 | ||
372 | if ($responseFields['resultCode'] == 'Error') { | |
373 | return self::error($responseFields['code'], $responseFields['text']); | |
374 | } | |
375 | ||
376 | // carry on cancelation procedure | |
377 | return TRUE; | |
378 | } | |
379 | ||
380 | public function install() { | |
381 | return TRUE; | |
382 | } | |
383 | ||
384 | public function uninstall() { | |
385 | return TRUE; | |
386 | } | |
387 | ||
388 | } |