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 | * | |
c7a8fffd RR |
18 | * Copyright 2014-2017, Free Software Foundation |
19 | * Copyright 2014-2017, Lisa Marie Maginnis <lisa@fsf.org> | |
33422de3 LMM |
20 | * |
21 | */ | |
22 | ||
c7a8fffd RR |
23 | /** |
24 | * CiviCRM (Instant Payment Notification) IPN processor module for | |
25 | * TrustCommerece. | |
26 | * | |
27 | * For full documentation on the | |
28 | * TrustCommerece API, please see the TCDevGuide for more information: | |
29 | * https://vault.trustcommerce.com/downloads/TCDevGuide.htm | |
30 | * | |
31 | * This module supports the following features: Single credit/debit card | |
32 | * transactions, AVS checking, recurring (create, update, and cancel | |
33 | * subscription) optional blacklist with fail2ban, | |
34 | * | |
35 | * @copyright Free Software Foundation 2014-2017 | |
36 | * @version 1.0 | |
37 | * @package org.fsf.payment.trustcommerce.ipn | |
38 | */ | |
39 | ||
40 | define("MAX_FAILURES", 4); | |
41 | ||
33422de3 | 42 | class CRM_Core_Payment_trustcommerce_IPN extends CRM_Core_Payment_BaseIPN { |
c7a8fffd RR |
43 | |
44 | /** | |
45 | * Inherit | |
46 | * | |
47 | * @return void | |
48 | */ | |
33422de3 LMM |
49 | function __construct() { |
50 | parent::__construct(); | |
51 | } | |
52 | ||
c7a8fffd RR |
53 | function getLastFailures($recur_id) { |
54 | $sql="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 | |
59 | AND contribution_status_id = 1);"; | |
60 | ||
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) { | |
38aae692 | 99 | $msg = 'TrustCommerceIPN: Skipping duplicate contribution: '.$ids['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 | ||
c7a8fffd RR |
131 | } |
132 | ||
133 | } | |
134 | } | |
135 | ||
136 | protected function disableAutorenew($recur_id) { | |
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; | |
33422de3 LMM |
168 | } |
169 | ||
170 | } | |
33422de3 LMM |
171 | |
172 | protected function checkDuplicate($input, $ids) { | |
c7a8fffd | 173 | // $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 | 174 | $sql="select id from civicrm_contribution where trxn_id = '".$ids['trxn_id']."' and contribution_status_id != 2"; |
33422de3 | 175 | |
33422de3 | 176 | $result = CRM_Core_DAO::executeQuery($sql); |
de12f901 LMM |
177 | if($result->fetch()) { |
178 | $id = $result->id; | |
179 | } else { | |
180 | $id = NULL; | |
181 | } | |
182 | ||
33422de3 LMM |
183 | return $id; |
184 | } | |
185 | protected function processRecur($input, $ids, $objects, $first) { | |
dcb7146e | 186 | $lastfailures = $this->getLastFailures($ids['contributionRecur']); |
33422de3 LMM |
187 | $recur = &$objects['contributionRecur']; |
188 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
189 | ||
190 | $transaction = new CRM_Core_Transaction(); | |
191 | ||
192 | $now = date('YmdHis'); | |
193 | ||
194 | // fix dates that already exist | |
195 | $dates = array('create_date', 'start_date', 'end_date', 'cancel_date', 'modified_date'); | |
196 | foreach ($dates as $name) { | |
197 | if ($recur->$name) { | |
198 | $recur->$name = CRM_Utils_Date::isoToMysql($recur->$name); | |
199 | } | |
200 | } | |
201 | ||
202 | if (!$first) { | |
203 | // create a contribution and then get it processed | |
204 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
205 | $contribution->contact_id = $ids['contact']; | |
206 | $contribution->financial_type_id = $objects['contributionType']->id; | |
207 | $contribution->contribution_page_id = $ids['contributionPage']; | |
208 | $contribution->contribution_recur_id = $ids['contributionRecur']; | |
209 | $contribution->receive_date = $input['date']; | |
210 | $contribution->currency = $objects['contribution']->currency; | |
211 | $contribution->payment_instrument_id = 1; | |
212 | $contribution->amount_level = $objects['contribution']->amount_level; | |
213 | $contribution->address_id = $objects['contribution']->address_id; | |
33422de3 LMM |
214 | $contribution->campaign_id = $objects['contribution']->campaign_id; |
215 | $contribution->total_amount = $input['amount']; | |
216 | ||
217 | $objects['contribution'] = &$contribution; | |
218 | } | |
de12f901 | 219 | |
33422de3 LMM |
220 | $objects['contribution']->invoice_id = md5(uniqid(rand(), TRUE)); |
221 | // $objects['contribution']->total_amount = $objects['contribution']->total_amount; | |
222 | $objects['contribution']->trxn_id = $input['trxn_id']; | |
223 | ||
3e3d3df5 LMM |
224 | // check if contribution is already completed, if so we ignore this ipn |
225 | if ($objects['contribution']->contribution_status_id == 1) { | |
226 | $transaction->commit(); | |
227 | CRM_Core_Error::debug_log_message("returning since contribution has already been handled"); | |
228 | echo 'Success: Contribution has already been handled<p>'; | |
229 | echo ''; | |
230 | return TRUE; | |
231 | } | |
33422de3 LMM |
232 | |
233 | $sendNotification = FALSE; | |
de12f901 LMM |
234 | |
235 | $recur->trxn_id = $input['trxn_id']; | |
236 | $recur->total_amount = $input['amount']; | |
237 | $recur->payment_instrument_id = 1; | |
238 | $recur->fee = 0; | |
239 | $recur->net_amount = $input['amount']; | |
240 | ||
7bd867b8 | 241 | $objects['contribution']->save(); |
802589c9 | 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; |
38aae692 | 267 | $msg = 'TrustCommerceIPN: Created contribution: '.$ids['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 | |
38aae692 | 281 | $msg = 'TrustCommerceIPN: Created contribution: '.$ids['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; | |
c7a8fffd RR |
287 | |
288 | /* Action for repeated failures */ | |
dcb7146e | 289 | if(MAX_FAILURES <= $lastfailures) { |
c7a8fffd RR |
290 | //$this->disableAutoRenew(($ids['contributionRecur'])); |
291 | $this->disableAutorenew($ids['processor_id']); | |
292 | } | |
293 | ||
38aae692 | 294 | return $this->failed($objects, $transaction, $input); |
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 | ||
c7a8fffd | 313 | protected function getIDs($billingid, $input, $module) { |
33422de3 | 314 | $sql = "SELECT cr.id, cr.contact_id, co.id as coid |
c7a8fffd RR |
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"; | |
33422de3 LMM |
318 | |
319 | $result = CRM_Core_DAO::executeQuery($sql); | |
320 | $result->fetch(); | |
321 | $ids['contribution'] = $result->coid; | |
322 | $ids['contributionRecur'] = $result->id; | |
323 | $ids['contact'] = $result->contact_id; | |
c7a8fffd | 324 | $ids['processor_id'] = $billingid; |
33422de3 LMM |
325 | |
326 | if (!$ids['contributionRecur']) { | |
327 | CRM_Core_Error::debug_log_message("Could not find billingid: ".$billingid); | |
1c234935 | 328 | echo "Failure: Could not find contributionRecur: $billingid <p>\n"; |
33422de3 LMM |
329 | exit(); |
330 | } | |
331 | ||
332 | // get page id based on contribution id | |
333 | $ids['contributionPage'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', | |
334 | $ids['contribution'], | |
335 | 'contribution_page_id' | |
336 | ); | |
337 | ||
338 | if ($module == 'event') { | |
339 | // FIXME: figure out fields for event | |
340 | } | |
341 | else { | |
342 | // get the optional ids | |
343 | ||
344 | // Get membershipId. Join with membership payment table for additional checks | |
c7a8fffd RR |
345 | $sql = "SELECT m.id |
346 | FROM civicrm_membership as m | |
347 | WHERE m.contribution_recur_id = '{$ids['contributionRecur']}' | |
348 | LIMIT 1"; | |
33422de3 LMM |
349 | if ($membershipId = CRM_Core_DAO::singleValueQuery($sql)) { |
350 | ||
351 | $ids['membership'] = $membershipId; | |
352 | } | |
353 | ||
354 | } | |
355 | ||
c7a8fffd RR |
356 | return $ids; |
357 | } | |
33422de3 LMM |
358 | |
359 | } |