Commit | Line | Data |
---|---|---|
33422de3 LMM |
1 | <?php |
2 | /* | |
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 | * | |
ab8492a8 | 18 | * Copyright 2016, Lisa Marie Maginnis <lisa@fsf.org> (http://www.fsf.org) |
33422de3 LMM |
19 | * |
20 | */ | |
21 | ||
ab8492a8 LMM |
22 | /** |
23 | * CiviCRM (Instant Payment Notification) IPN processor module for | |
24 | * TrustCommerece. | |
25 | * | |
26 | * For full documentation on the | |
27 | * TrustCommerece API, please see the TCDevGuide for more information: | |
28 | * https://vault.trustcommerce.com/downloads/TCDevGuide.htm | |
29 | * | |
30 | * This module supports the following features: Single credit/debit card | |
31 | * transactions, AVS checking, recurring (create, update, and cancel | |
32 | * subscription) optional blacklist with fail2ban, | |
33 | * | |
34 | * @copyright Lisa Marie Maginnis <lisa@fsf.org> (http://www.fsf.org) | |
35 | * @version 1.0 | |
36 | * @package org.fsf.payment.trustcommerce.ipn | |
37 | */ | |
38 | ||
1a2a4b0c | 39 | define("MAX_FAILURES", 4); |
ab8492a8 | 40 | |
33422de3 | 41 | class CRM_Core_Payment_trustcommerce_IPN extends CRM_Core_Payment_BaseIPN { |
ab8492a8 LMM |
42 | |
43 | /** | |
44 | * Inheret | |
45 | * | |
46 | * @return void | |
47 | */ | |
33422de3 LMM |
48 | function __construct() { |
49 | parent::__construct(); | |
50 | } | |
51 | ||
ab8492a8 | 52 | function getLastFailures($recur_id) { |
1a2a4b0c LMM |
53 | $sql=<<<EOF |
54 | SELECT count(*) as numfails | |
55 | FROM civicrm_contribution | |
56 | WHERE contribution_recur_id = $recur_id | |
57 | AND | |
58 | id > (SELECT MAX(id) FROM civicrm_contribution WHERE contribution_recur_id = $recur_id AND contribution_status_id = 1); | |
59 | EOF; | |
33422de3 | 60 | |
ab8492a8 LMM |
61 | $result = CRM_Core_DAO::executeQuery($sql); |
62 | if($result->fetch()) { | |
63 | $failures = $result->numfails; | |
64 | } else { | |
65 | $failures = NULL; | |
66 | } | |
67 | ||
68 | return $failures; | |
69 | ||
70 | } | |
33422de3 LMM |
71 | |
72 | function main($component = 'contribute') { | |
73 | static $no = NULL; | |
74 | $billingid = CRM_Utils_Request::retrieve('billingid', 'String', $no, FALSE, 'GET'); | |
75 | $input['status'] = CRM_Utils_Request::retrieve('status', 'String', $no, FALSE, 'GET'); | |
76 | $input['amount'] = CRM_Utils_Request::retrieve('amount', 'String', $no, FALSE, 'GET'); | |
77 | $input['date'] = CRM_Utils_Request::retrieve('date', 'String', $no, FALSE, 'GET'); | |
78 | $input['trxn_id'] = CRM_Utils_Request::retrieve('trxn_id', 'String', $no, FALSE, 'GET'); | |
79 | $checksum = CRM_Utils_Request::retrieve('checksum', 'String', $no, FALSE, 'GET'); | |
80 | ||
81 | if ($billingid) { | |
82 | if( $input['status'] == '' || $input['amount'] == '' || $input['date'] == '' || $input['trxn_id'] == '' || md5($billingid.$input['trxn_id'].$input['amount'].$input['date']) != $checksum) { | |
83 | CRM_Core_Error::debug_log_message("Error: IPN called with out proper fields"); | |
84 | echo "Error: invalid paramaters<p>\n"; | |
85 | exit; | |
86 | } | |
87 | ||
88 | ||
89 | $ids = $objects = array(); | |
90 | $input['component'] = $component; | |
91 | ||
92 | // load post ids in $ids | |
93 | $ids = NULL; | |
94 | $ids = $this->getIDs($billingid, $input, $input['component']); | |
95 | ||
96 | $ids['trxn_id'] = $input['trxn_id']; | |
97 | ||
98 | if($this->checkDuplicate($input, $ids) != NULL) { | |
1a2a4b0c | 99 | $msg = 'TrustCommerceIPN: Skipping duplicate contribution: for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id']."\n"; |
de12f901 LMM |
100 | echo $msg; |
101 | CRM_Core_Error::debug_log_message($msg); | |
33422de3 LMM |
102 | exit; |
103 | } | |
33422de3 LMM |
104 | |
105 | if(array_key_exists('membership', $ids)) { | |
106 | $membership = array(); | |
107 | $params = array('id' => $ids['membership']); | |
108 | $obj = CRM_Member_BAO_Membership::retrieve($params, $membership); | |
109 | $objects['membership'] = array(&$obj); | |
110 | } | |
33422de3 LMM |
111 | |
112 | $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', | |
113 | 'TrustCommerce', 'id', 'name' | |
114 | ); | |
115 | ||
116 | if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) { | |
117 | return FALSE; | |
118 | } | |
119 | // var_dump($objects); | |
120 | ||
121 | if ($component == 'contribute' && $ids['contributionRecur']) { | |
122 | // check if first contribution is completed, else complete first contribution | |
123 | $first = TRUE; | |
124 | if ($objects['contribution']->contribution_status_id == 1) { | |
125 | $first = FALSE; | |
126 | } | |
127 | ||
128 | ||
129 | return $this->processRecur($input, $ids, $objects, $first); | |
130 | ||
1a2a4b0c | 131 | } |
33422de3 | 132 | |
33422de3 | 133 | } |
33422de3 | 134 | } |
33422de3 | 135 | |
2f23bd2f | 136 | protected function disableAutorenew($recur_id) { |
1a2a4b0c LMM |
137 | /* Load payment processor object */ |
138 | // HARD CODED | |
139 | $msg = 'TrustCommerceIPN: MAX_FAILURES hit, unstoring billing ID: '.$recur_id."\n"; | |
140 | ||
141 | CRM_Core_Error::debug_log_message($msg); | |
142 | echo $msg; | |
143 | ||
144 | $sql = "SELECT user_name, password, url_site FROM civicrm_payment_processor WHERE id = 8 LIMIT 1"; | |
145 | ||
146 | $result = CRM_Core_DAO::executeQuery($sql); | |
147 | if($result->fetch()) { | |
148 | $request = array( | |
149 | 'custid' => $result->user_name, | |
150 | 'password' => $result->password, | |
151 | 'action' => 'unstore', | |
152 | 'billingid' => $recur_id | |
153 | ); | |
154 | ||
155 | $update = 'UPDATE civicrm_contribution_recur SET contribution_status_id = 3 WHERE processor_id = "'.$recur_id.'";'; | |
156 | $result1 = CRM_Core_DAO::executeQuery($update); | |
157 | ||
158 | $tc = tclink_send($request); | |
159 | if(!$tc) { | |
160 | return -1; | |
161 | } | |
162 | ||
163 | return TRUE; | |
164 | ||
165 | } else { | |
166 | echo 'CRITICAL ERROR: Could not load payment processor object'; | |
167 | return; | |
168 | } | |
169 | ||
170 | ||
33422de3 LMM |
171 | |
172 | } | |
33422de3 LMM |
173 | |
174 | protected function checkDuplicate($input, $ids) { | |
175 | // $sql='select id from civicrm_contribution where receive_date like \''.$input['date'].'%\' and total_amount='.$input['amount'].' and contact_id='.$ids['contact'].' and contribution_status_id = 1 limit 1'; | |
1c234935 | 176 | $sql="select id from civicrm_contribution where trxn_id = '".$ids['trxn_id']."' and contribution_status_id != 2"; |
33422de3 LMM |
177 | |
178 | ||
179 | $result = CRM_Core_DAO::executeQuery($sql); | |
de12f901 LMM |
180 | if($result->fetch()) { |
181 | $id = $result->id; | |
182 | } else { | |
183 | $id = NULL; | |
184 | } | |
185 | ||
33422de3 LMM |
186 | return $id; |
187 | } | |
188 | protected function processRecur($input, $ids, $objects, $first) { | |
189 | $recur = &$objects['contributionRecur']; | |
190 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
191 | ||
192 | $transaction = new CRM_Core_Transaction(); | |
193 | ||
194 | $now = date('YmdHis'); | |
195 | ||
196 | // fix dates that already exist | |
197 | $dates = array('create_date', 'start_date', 'end_date', 'cancel_date', 'modified_date'); | |
198 | foreach ($dates as $name) { | |
199 | if ($recur->$name) { | |
200 | $recur->$name = CRM_Utils_Date::isoToMysql($recur->$name); | |
201 | } | |
202 | } | |
203 | ||
204 | if (!$first) { | |
205 | // create a contribution and then get it processed | |
206 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
207 | $contribution->contact_id = $ids['contact']; | |
208 | $contribution->financial_type_id = $objects['contributionType']->id; | |
209 | $contribution->contribution_page_id = $ids['contributionPage']; | |
210 | $contribution->contribution_recur_id = $ids['contributionRecur']; | |
211 | $contribution->receive_date = $input['date']; | |
212 | $contribution->currency = $objects['contribution']->currency; | |
213 | $contribution->payment_instrument_id = 1; | |
214 | $contribution->amount_level = $objects['contribution']->amount_level; | |
215 | $contribution->address_id = $objects['contribution']->address_id; | |
33422de3 LMM |
216 | $contribution->campaign_id = $objects['contribution']->campaign_id; |
217 | $contribution->total_amount = $input['amount']; | |
218 | ||
219 | $objects['contribution'] = &$contribution; | |
220 | } | |
de12f901 | 221 | |
33422de3 LMM |
222 | $objects['contribution']->invoice_id = md5(uniqid(rand(), TRUE)); |
223 | // $objects['contribution']->total_amount = $objects['contribution']->total_amount; | |
224 | $objects['contribution']->trxn_id = $input['trxn_id']; | |
225 | ||
3e3d3df5 LMM |
226 | // check if contribution is already completed, if so we ignore this ipn |
227 | if ($objects['contribution']->contribution_status_id == 1) { | |
228 | $transaction->commit(); | |
229 | CRM_Core_Error::debug_log_message("returning since contribution has already been handled"); | |
230 | echo 'Success: Contribution has already been handled<p>'; | |
231 | echo ''; | |
232 | return TRUE; | |
233 | } | |
33422de3 LMM |
234 | |
235 | $sendNotification = FALSE; | |
de12f901 LMM |
236 | |
237 | $recur->trxn_id = $input['trxn_id']; | |
238 | $recur->total_amount = $input['amount']; | |
239 | $recur->payment_instrument_id = 1; | |
240 | $recur->fee = 0; | |
241 | $recur->net_amount = $input['amount']; | |
242 | ||
33422de3 LMM |
243 | if ($input['status'] == 1) { |
244 | ||
245 | // Approved | |
246 | if ($first) { | |
247 | $recur->start_date = $now; | |
248 | $sendNotification = TRUE; | |
249 | $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_START; | |
250 | } | |
251 | $statusName = 'In Progress'; | |
252 | if (($recur->installments > 0) && | |
253 | ($input['subscription_paynum'] >= $recur->installments) | |
254 | ) { | |
255 | // this is the last payment | |
256 | $statusName = 'Completed'; | |
257 | $recur->end_date = $now; | |
258 | ||
259 | $sendNotification = TRUE; | |
260 | $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_END; | |
261 | } | |
33422de3 LMM |
262 | |
263 | $recur->modified_date = $now; | |
264 | $recur->contribution_status_id = array_search($statusName, $contributionStatus); | |
265 | $recur->save(); | |
de12f901 | 266 | $input['is_test'] = 0; |
1a2a4b0c | 267 | $msg = 'TrustCommerceIPN: Created contribution: for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id'].' status: Completed'."\n"; |
de12f901 LMM |
268 | echo $msg; |
269 | CRM_Core_Error::debug_log_message($msg); | |
270 | ||
271 | $this->completeTransaction($input, $ids, $objects, $transaction, $recur); | |
33422de3 | 272 | } |
de12f901 | 273 | else if( $input['status'] == 4 ) { |
33422de3 LMM |
274 | // Declined |
275 | // failed status | |
de12f901 | 276 | |
33422de3 LMM |
277 | $recur->contribution_status_id = array_search('Failed', $contributionStatus); |
278 | $recur->cancel_date = $now; | |
279 | $recur->save(); | |
de12f901 | 280 | |
1a2a4b0c | 281 | $msg = 'TrustCommerceIPN: Created contribution: for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id'].' status: Failed'."\n"; |
de12f901 LMM |
282 | echo $msg; |
283 | CRM_Core_Error::debug_log_message($msg); | |
571c1830 LMM |
284 | |
285 | /* Disable cancelling transactions */ | |
286 | $input['skipComponentSync'] = 1; | |
0ad89801 | 287 | |
eced3337 LMM |
288 | /* Action for repeated failures */ |
289 | if(MAX_FAILURES <= $this->getLastFailures($ids['contributionRecur'])) { | |
1a2a4b0c LMM |
290 | //$this->disableAutoRenew(($ids['contributionRecur'])); |
291 | $this->disableAutorenew($ids['processor_id']); | |
eced3337 | 292 | } |
de12f901 LMM |
293 | |
294 | return $this->failed($objects, $transaction); | |
33422de3 LMM |
295 | } |
296 | ||
33422de3 LMM |
297 | if ($sendNotification) { |
298 | $autoRenewMembership = FALSE; | |
de12f901 | 299 | if ($recur->id && isset($ids['membership']) && $ids['membership'] ) { |
33422de3 LMM |
300 | $autoRenewMembership = TRUE; |
301 | } | |
302 | ||
33422de3 LMM |
303 | //send recurring Notification email for user |
304 | CRM_Contribute_BAO_ContributionPage::recurringNotify($subscriptionPaymentStatus, | |
305 | $ids['contact'], | |
306 | $ids['contributionPage'], | |
307 | $recur, | |
308 | $autoRenewMembership | |
309 | ); | |
33422de3 | 310 | } |
33422de3 LMM |
311 | } |
312 | ||
313 | protected function getIDs($billingid, $input, $module) { | |
314 | $sql = "SELECT cr.id, cr.contact_id, co.id as coid | |
315 | FROM civicrm_contribution_recur cr | |
316 | INNER JOIN civicrm_contribution co ON co.contribution_recur_id = cr.id | |
317 | WHERE cr.processor_id = '$billingid' LIMIT 1"; | |
318 | ||
319 | ||
320 | $result = CRM_Core_DAO::executeQuery($sql); | |
321 | $result->fetch(); | |
322 | $ids['contribution'] = $result->coid; | |
323 | $ids['contributionRecur'] = $result->id; | |
324 | $ids['contact'] = $result->contact_id; | |
2f23bd2f | 325 | $ids['processor_id'] = $billingid; |
33422de3 LMM |
326 | |
327 | if (!$ids['contributionRecur']) { | |
328 | CRM_Core_Error::debug_log_message("Could not find billingid: ".$billingid); | |
1c234935 | 329 | echo "Failure: Could not find contributionRecur: $billingid <p>\n"; |
33422de3 LMM |
330 | exit(); |
331 | } | |
332 | ||
333 | // get page id based on contribution id | |
334 | $ids['contributionPage'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', | |
335 | $ids['contribution'], | |
336 | 'contribution_page_id' | |
337 | ); | |
338 | ||
339 | if ($module == 'event') { | |
340 | // FIXME: figure out fields for event | |
341 | } | |
342 | else { | |
343 | // get the optional ids | |
344 | ||
345 | // Get membershipId. Join with membership payment table for additional checks | |
346 | $sql = " | |
347 | SELECT m.id | |
348 | FROM civicrm_membership as m | |
349 | WHERE m.contribution_recur_id = '{$ids['contributionRecur']}' | |
350 | LIMIT 1"; | |
351 | if ($membershipId = CRM_Core_DAO::singleValueQuery($sql)) { | |
352 | ||
353 | $ids['membership'] = $membershipId; | |
354 | } | |
355 | ||
356 | } | |
357 | ||
358 | return $ids; | |
359 | } | |
360 | ||
361 | ||
362 | ||
363 | } |