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 */ |
32bf0975 | 101 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $tc_params); |
71a6ba5c LMM |
102 | |
103 | // TrustCommerce will not refuse duplicates, so we should check if the user already submitted this transaction | |
f322addf | 104 | if ($this->_checkDupe($tc_params['ticket'])) { |
71a6ba5c LMM |
105 | 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.'); |
106 | } | |
107 | ||
f322addf LMM |
108 | /* Call the TC API, and grab the reply */ |
109 | $reply = tclink_send($tc_params); | |
71a6ba5c | 110 | |
f322addf LMM |
111 | /* Parse our reply */ |
112 | $result = $this->_getTrustCommerceReply($reply); | |
71a6ba5c | 113 | |
f322addf LMM |
114 | if($result == 0) { |
115 | /* We were successful, congrats. Lets wrap it up: | |
116 | * Convert back to dollars | |
117 | * Save the transaction ID | |
118 | */ | |
119 | $params['trxn_id'] = $reply['transid']; | |
120 | $params['gross_amount'] = $tc_params['amount'] / 100; | |
71a6ba5c | 121 | |
f322addf | 122 | return $params; |
71a6ba5c | 123 | |
f322addf LMM |
124 | } else { |
125 | /* Otherwise we return the error object */ | |
126 | return $result; | |
127 | } | |
71a6ba5c LMM |
128 | } |
129 | ||
130 | /** | |
f322addf LMM |
131 | * Gets the recurring billing fields for the TC API |
132 | * @param array $fields The fields to modify. | |
133 | * @return array The fields for tclink_send(), modified for recurring billing. | |
71a6ba5c LMM |
134 | * @public |
135 | */ | |
f322addf | 136 | function _getRecurPaymentFields($fields) { |
285eaf25 LMM |
137 | $payments = $this->_getParam('frequency_interval'); |
138 | $cycle = $this->_getParam('frequency_unit'); | |
71a6ba5c | 139 | |
f322addf | 140 | /* Translate billing cycle from CiviCRM -> TC */ |
285eaf25 LMM |
141 | switch($cycle) { |
142 | case 'day': | |
143 | $cycle = 'd'; | |
144 | break; | |
145 | case 'week': | |
146 | $cycle = 'w'; | |
147 | break; | |
148 | case 'month': | |
149 | $cycle = 'm'; | |
150 | break; | |
151 | case 'year': | |
152 | $cycle = 'y'; | |
153 | break; | |
71a6ba5c | 154 | } |
bc323ec4 LMM |
155 | |
156 | /* Translate frequency interval from CiviCRM -> TC | |
157 | * Payments are the same, HOWEVER a payment of 1 (forever) should be 0 in TC */ | |
158 | if($payments == 1) { | |
159 | $payments = 0; | |
71a6ba5c | 160 | } |
71a6ba5c | 161 | |
bc323ec4 LMM |
162 | $fields['cycle'] = '1'.$cycle; /* The billing cycle in years, months, weeks, or days. */ |
163 | $fields['payments'] = $payments; | |
164 | $fields['action'] = 'store'; /* Change our mode to `store' mode. */ | |
71a6ba5c | 165 | |
bc323ec4 | 166 | return $fields; |
a79ba043 LMM |
167 | } |
168 | ||
169 | /* Parses a response from TC via the tclink_send() command. | |
170 | * @param $reply array The result of a call to tclink_send(). | |
171 | * @return mixed self::error() if transaction failed, otherwise returns 0. | |
172 | */ | |
bc323ec4 | 173 | function _getTrustCommerceReply($reply) { |
71a6ba5c | 174 | |
285eaf25 | 175 | /* DUPLIATE CODE, please refactor. ~lisa */ |
bc323ec4 | 176 | if (!$reply) { |
285eaf25 | 177 | return self::error(9002, 'Could not initiate connection to payment gateway'); |
71a6ba5c LMM |
178 | } |
179 | ||
bc323ec4 | 180 | switch($reply['status']) { |
285eaf25 LMM |
181 | case self::AUTH_APPROVED: |
182 | // It's all good | |
183 | break; | |
184 | case self::AUTH_DECLINED: | |
185 | // TODO FIXME be more or less specific? | |
186 | // declinetype can be: decline, avs, cvv, call, expiredcard, carderror, authexpired, fraud, blacklist, velocity | |
187 | // See TC documentation for more info | |
a8c5366f | 188 | return self::error(9009, "Your transaction was declined: {$reply['declinetype']}"); |
285eaf25 LMM |
189 | break; |
190 | case self::AUTH_BADDATA: | |
a8c5366f LMM |
191 | // TODO FIXME do something with $reply['error'] and $reply['offender'] |
192 | return self::error(9011, "Invalid credit card information. The following fields were invalid: {$reply['offenders']}."); | |
285eaf25 LMM |
193 | break; |
194 | case self::AUTH_ERROR: | |
195 | return self::error(9002, 'Could not initiate connection to payment gateway'); | |
196 | break; | |
71a6ba5c | 197 | } |
a79ba043 | 198 | return 0; |
71a6ba5c LMM |
199 | } |
200 | ||
201 | function _getTrustCommerceFields() { | |
202 | // Total amount is from the form contribution field | |
203 | $amount = $this->_getParam('total_amount'); | |
204 | // CRM-9894 would this ever be the case?? | |
205 | if (empty($amount)) { | |
206 | $amount = $this->_getParam('amount'); | |
207 | } | |
208 | $fields = array(); | |
209 | $fields['custid'] = $this->_getParam('user_name'); | |
210 | $fields['password'] = $this->_getParam('password'); | |
211 | $fields['action'] = 'sale'; | |
212 | ||
213 | // Enable address verification | |
214 | $fields['avs'] = 'y'; | |
215 | ||
216 | $fields['address1'] = $this->_getParam('street_address'); | |
217 | $fields['zip'] = $this->_getParam('postal_code'); | |
218 | ||
219 | $fields['name'] = $this->_getParam('billing_first_name') . ' ' . $this->_getParam('billing_last_name'); | |
220 | ||
221 | // This assumes currencies where the . is used as the decimal point, like USD | |
222 | $amount = preg_replace("/([^0-9\\.])/i", "", $amount); | |
223 | ||
224 | // We need to pass the amount to TrustCommerce in dollar cents | |
225 | $fields['amount'] = $amount * 100; | |
226 | ||
227 | // Unique identifier | |
228 | $fields['ticket'] = substr($this->_getParam('invoiceID'), 0, 20); | |
229 | ||
230 | // cc info | |
231 | $fields['cc'] = $this->_getParam('credit_card_number'); | |
232 | $fields['cvv'] = $this->_getParam('cvv2'); | |
233 | $exp_month = str_pad($this->_getParam('month'), 2, '0', STR_PAD_LEFT); | |
234 | $exp_year = substr($this->_getParam('year'),-2); | |
235 | $fields['exp'] = "$exp_month$exp_year"; | |
236 | ||
237 | if ($this->_mode != 'live') { | |
238 | $fields['demo'] = 'y'; | |
239 | } | |
71a6ba5c LMM |
240 | return $fields; |
241 | } | |
242 | ||
243 | /** | |
244 | * Checks to see if invoice_id already exists in db | |
245 | * | |
246 | * @param int $invoiceId The ID to check | |
247 | * | |
248 | * @return bool True if ID exists, else false | |
249 | */ | |
250 | function _checkDupe($invoiceId) { | |
251 | require_once 'CRM/Contribute/DAO/Contribution.php'; | |
252 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
253 | $contribution->invoice_id = $invoiceId; | |
254 | return $contribution->find(); | |
255 | } | |
256 | ||
257 | /** | |
258 | * Get the value of a field if set | |
259 | * | |
260 | * @param string $field the field | |
261 | * | |
262 | * @return mixed value of the field, or empty string if the field is | |
263 | * not set | |
264 | */ | |
265 | function _getParam($field) { | |
266 | return CRM_Utils_Array::value($field, $this->_params, ''); | |
267 | } | |
268 | ||
269 | function &error($errorCode = NULL, $errorMessage = NULL) { | |
270 | $e = CRM_Core_Error::singleton(); | |
271 | if ($errorCode) { | |
272 | $e->push($errorCode, 0, NULL, $errorMessage); | |
273 | } | |
274 | else { | |
275 | $e->push(9001, 0, NULL, 'Unknown System Error.'); | |
276 | } | |
277 | return $e; | |
278 | } | |
279 | ||
280 | /** | |
281 | * Set a field to the specified value. Value must be a scalar (int, | |
282 | * float, string, or boolean) | |
283 | * | |
284 | * @param string $field | |
285 | * @param mixed $value | |
286 | * | |
287 | * @return bool false if value is not a scalar, true if successful | |
288 | */ | |
289 | function _setParam($field, $value) { | |
290 | if (!is_scalar($value)) { | |
291 | return FALSE; | |
292 | } | |
293 | else { | |
294 | $this->_params[$field] = $value; | |
295 | } | |
296 | } | |
297 | ||
298 | /** | |
299 | * This function checks to see if we have the right config values | |
300 | * | |
301 | * @return string the error message if any | |
302 | * @public | |
303 | */ | |
304 | function checkConfig() { | |
305 | $error = array(); | |
306 | if (empty($this->_paymentProcessor['user_name'])) { | |
307 | $error[] = ts('Customer ID is not set for this payment processor'); | |
308 | } | |
309 | ||
310 | if (empty($this->_paymentProcessor['password'])) { | |
311 | $error[] = ts('Password is not set for this payment processor'); | |
312 | } | |
313 | ||
314 | if (!empty($error)) { | |
315 | return implode('<p>', $error); | |
316 | } else { | |
317 | return NULL; | |
318 | } | |
319 | } | |
320 | ||
14f15ff3 | 321 | function cancelSubscription(&$message = '', $params = array()) { |
7964e0e9 LMM |
322 | $tc_params['custid'] = $this->_getParam('user_name'); |
323 | $tc_params['password'] = $this->_getParam('password'); | |
324 | $tc_params['action'] = 'unstore'; | |
cbf5200e | 325 | $tc_params['billingid'] = CRM_Utils_Array::value('trxn_id', $params); |
71a6ba5c | 326 | |
7964e0e9 | 327 | $result = tclink_send($tc_params); |
71a6ba5c | 328 | |
7964e0e9 LMM |
329 | /* Test if call failed */ |
330 | if(!$result) { | |
71a6ba5c LMM |
331 | return self::error(9002, 'Could not initiate connection to payment gateway'); |
332 | } | |
7964e0e9 | 333 | /* We are done, pass success */ |
71a6ba5c LMM |
334 | return TRUE; |
335 | } | |
336 | ||
337 | public function install() { | |
338 | return TRUE; | |
339 | } | |
340 | ||
341 | public function uninstall() { | |
342 | return TRUE; | |
343 | } | |
344 | ||
345 | } |