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 | * | |
38aae692 RR |
18 | * Copyright (C) 2012 |
19 | * Licensed to CiviCRM under the GPL v3 or higher | |
20 | * | |
21 | * Modified by Lisa Marie Maginnis <lisa@fsf.org> (http://www.fsf.org) | |
33422de3 LMM |
22 | * |
23 | */ | |
24 | ||
25 | class CRM_Core_Payment_trustcommerce_IPN extends CRM_Core_Payment_BaseIPN { | |
26 | function __construct() { | |
27 | parent::__construct(); | |
28 | } | |
29 | ||
30 | ||
31 | function main($component = 'contribute') { | |
32 | static $no = NULL; | |
33 | $billingid = CRM_Utils_Request::retrieve('billingid', 'String', $no, FALSE, 'GET'); | |
34 | $input['status'] = CRM_Utils_Request::retrieve('status', 'String', $no, FALSE, 'GET'); | |
35 | $input['amount'] = CRM_Utils_Request::retrieve('amount', 'String', $no, FALSE, 'GET'); | |
36 | $input['date'] = CRM_Utils_Request::retrieve('date', 'String', $no, FALSE, 'GET'); | |
37 | $input['trxn_id'] = CRM_Utils_Request::retrieve('trxn_id', 'String', $no, FALSE, 'GET'); | |
38 | $checksum = CRM_Utils_Request::retrieve('checksum', 'String', $no, FALSE, 'GET'); | |
39 | ||
40 | if ($billingid) { | |
41 | if( $input['status'] == '' || $input['amount'] == '' || $input['date'] == '' || $input['trxn_id'] == '' || md5($billingid.$input['trxn_id'].$input['amount'].$input['date']) != $checksum) { | |
42 | CRM_Core_Error::debug_log_message("Error: IPN called with out proper fields"); | |
43 | echo "Error: invalid paramaters<p>\n"; | |
44 | exit; | |
45 | } | |
46 | ||
47 | ||
48 | $ids = $objects = array(); | |
49 | $input['component'] = $component; | |
50 | ||
51 | // load post ids in $ids | |
52 | $ids = NULL; | |
53 | $ids = $this->getIDs($billingid, $input, $input['component']); | |
54 | ||
55 | $ids['trxn_id'] = $input['trxn_id']; | |
56 | ||
57 | if($this->checkDuplicate($input, $ids) != NULL) { | |
38aae692 | 58 | $msg = 'TrustCommerceIPN: Skipping duplicate contribution: '.$ids['contribution'].' for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id']."\n"; |
de12f901 LMM |
59 | echo $msg; |
60 | CRM_Core_Error::debug_log_message($msg); | |
33422de3 LMM |
61 | exit; |
62 | } | |
33422de3 LMM |
63 | |
64 | if(array_key_exists('membership', $ids)) { | |
65 | $membership = array(); | |
66 | $params = array('id' => $ids['membership']); | |
67 | $obj = CRM_Member_BAO_Membership::retrieve($params, $membership); | |
68 | $objects['membership'] = array(&$obj); | |
69 | } | |
33422de3 LMM |
70 | |
71 | $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', | |
72 | 'TrustCommerce', 'id', 'name' | |
73 | ); | |
74 | ||
75 | if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) { | |
76 | return FALSE; | |
77 | } | |
78 | // var_dump($objects); | |
79 | ||
80 | if ($component == 'contribute' && $ids['contributionRecur']) { | |
81 | // check if first contribution is completed, else complete first contribution | |
82 | $first = TRUE; | |
83 | if ($objects['contribution']->contribution_status_id == 1) { | |
84 | $first = FALSE; | |
85 | } | |
86 | ||
87 | ||
88 | return $this->processRecur($input, $ids, $objects, $first); | |
89 | ||
90 | } | |
91 | ||
92 | } | |
38aae692 | 93 | } |
33422de3 LMM |
94 | |
95 | protected function checkDuplicate($input, $ids) { | |
96 | // $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 | 97 | $sql="select id from civicrm_contribution where trxn_id = '".$ids['trxn_id']."' and contribution_status_id != 2"; |
33422de3 LMM |
98 | |
99 | ||
100 | $result = CRM_Core_DAO::executeQuery($sql); | |
de12f901 LMM |
101 | if($result->fetch()) { |
102 | $id = $result->id; | |
103 | } else { | |
104 | $id = NULL; | |
105 | } | |
106 | ||
33422de3 LMM |
107 | return $id; |
108 | } | |
109 | protected function processRecur($input, $ids, $objects, $first) { | |
110 | $recur = &$objects['contributionRecur']; | |
111 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
112 | ||
113 | $transaction = new CRM_Core_Transaction(); | |
114 | ||
115 | $now = date('YmdHis'); | |
116 | ||
117 | // fix dates that already exist | |
118 | $dates = array('create_date', 'start_date', 'end_date', 'cancel_date', 'modified_date'); | |
119 | foreach ($dates as $name) { | |
120 | if ($recur->$name) { | |
121 | $recur->$name = CRM_Utils_Date::isoToMysql($recur->$name); | |
122 | } | |
123 | } | |
124 | ||
125 | if (!$first) { | |
126 | // create a contribution and then get it processed | |
127 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
128 | $contribution->contact_id = $ids['contact']; | |
129 | $contribution->financial_type_id = $objects['contributionType']->id; | |
130 | $contribution->contribution_page_id = $ids['contributionPage']; | |
131 | $contribution->contribution_recur_id = $ids['contributionRecur']; | |
132 | $contribution->receive_date = $input['date']; | |
133 | $contribution->currency = $objects['contribution']->currency; | |
134 | $contribution->payment_instrument_id = 1; | |
135 | $contribution->amount_level = $objects['contribution']->amount_level; | |
136 | $contribution->address_id = $objects['contribution']->address_id; | |
33422de3 LMM |
137 | $contribution->campaign_id = $objects['contribution']->campaign_id; |
138 | $contribution->total_amount = $input['amount']; | |
139 | ||
140 | $objects['contribution'] = &$contribution; | |
141 | } | |
de12f901 | 142 | |
33422de3 LMM |
143 | $objects['contribution']->invoice_id = md5(uniqid(rand(), TRUE)); |
144 | // $objects['contribution']->total_amount = $objects['contribution']->total_amount; | |
145 | $objects['contribution']->trxn_id = $input['trxn_id']; | |
146 | ||
3e3d3df5 LMM |
147 | // check if contribution is already completed, if so we ignore this ipn |
148 | if ($objects['contribution']->contribution_status_id == 1) { | |
149 | $transaction->commit(); | |
150 | CRM_Core_Error::debug_log_message("returning since contribution has already been handled"); | |
151 | echo 'Success: Contribution has already been handled<p>'; | |
152 | echo ''; | |
153 | return TRUE; | |
154 | } | |
33422de3 LMM |
155 | |
156 | $sendNotification = FALSE; | |
de12f901 LMM |
157 | |
158 | $recur->trxn_id = $input['trxn_id']; | |
159 | $recur->total_amount = $input['amount']; | |
160 | $recur->payment_instrument_id = 1; | |
161 | $recur->fee = 0; | |
162 | $recur->net_amount = $input['amount']; | |
163 | ||
33422de3 LMM |
164 | if ($input['status'] == 1) { |
165 | ||
166 | // Approved | |
167 | if ($first) { | |
168 | $recur->start_date = $now; | |
169 | $sendNotification = TRUE; | |
170 | $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_START; | |
171 | } | |
172 | $statusName = 'In Progress'; | |
173 | if (($recur->installments > 0) && | |
174 | ($input['subscription_paynum'] >= $recur->installments) | |
175 | ) { | |
176 | // this is the last payment | |
177 | $statusName = 'Completed'; | |
178 | $recur->end_date = $now; | |
179 | ||
180 | $sendNotification = TRUE; | |
181 | $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_END; | |
182 | } | |
33422de3 LMM |
183 | |
184 | $recur->modified_date = $now; | |
185 | $recur->contribution_status_id = array_search($statusName, $contributionStatus); | |
186 | $recur->save(); | |
de12f901 | 187 | $input['is_test'] = 0; |
38aae692 | 188 | $msg = 'TrustCommerceIPN: Created contribution: '.$ids['contribution'].' for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id'].' status: Completed'."\n"; |
de12f901 LMM |
189 | echo $msg; |
190 | CRM_Core_Error::debug_log_message($msg); | |
191 | ||
192 | $this->completeTransaction($input, $ids, $objects, $transaction, $recur); | |
33422de3 | 193 | } |
de12f901 | 194 | else if( $input['status'] == 4 ) { |
33422de3 LMM |
195 | // Declined |
196 | // failed status | |
de12f901 | 197 | |
33422de3 LMM |
198 | $recur->contribution_status_id = array_search('Failed', $contributionStatus); |
199 | $recur->cancel_date = $now; | |
200 | $recur->save(); | |
de12f901 | 201 | |
38aae692 | 202 | $msg = 'TrustCommerceIPN: Created contribution: '.$ids['contribution'].' for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id'].' status: Failed'."\n"; |
de12f901 LMM |
203 | echo $msg; |
204 | CRM_Core_Error::debug_log_message($msg); | |
571c1830 LMM |
205 | |
206 | /* Disable cancelling transactions */ | |
207 | $input['skipComponentSync'] = 1; | |
38aae692 | 208 | return $this->failed($objects, $transaction, $input); |
33422de3 LMM |
209 | } |
210 | ||
33422de3 LMM |
211 | if ($sendNotification) { |
212 | $autoRenewMembership = FALSE; | |
de12f901 | 213 | if ($recur->id && isset($ids['membership']) && $ids['membership'] ) { |
33422de3 LMM |
214 | $autoRenewMembership = TRUE; |
215 | } | |
216 | ||
33422de3 LMM |
217 | //send recurring Notification email for user |
218 | CRM_Contribute_BAO_ContributionPage::recurringNotify($subscriptionPaymentStatus, | |
219 | $ids['contact'], | |
220 | $ids['contributionPage'], | |
221 | $recur, | |
222 | $autoRenewMembership | |
223 | ); | |
33422de3 | 224 | } |
33422de3 LMM |
225 | } |
226 | ||
227 | protected function getIDs($billingid, $input, $module) { | |
228 | $sql = "SELECT cr.id, cr.contact_id, co.id as coid | |
229 | FROM civicrm_contribution_recur cr | |
230 | INNER JOIN civicrm_contribution co ON co.contribution_recur_id = cr.id | |
231 | WHERE cr.processor_id = '$billingid' LIMIT 1"; | |
232 | ||
233 | ||
234 | $result = CRM_Core_DAO::executeQuery($sql); | |
235 | $result->fetch(); | |
236 | $ids['contribution'] = $result->coid; | |
237 | $ids['contributionRecur'] = $result->id; | |
238 | $ids['contact'] = $result->contact_id; | |
239 | ||
240 | if (!$ids['contributionRecur']) { | |
241 | CRM_Core_Error::debug_log_message("Could not find billingid: ".$billingid); | |
1c234935 | 242 | echo "Failure: Could not find contributionRecur: $billingid <p>\n"; |
33422de3 LMM |
243 | exit(); |
244 | } | |
245 | ||
246 | // get page id based on contribution id | |
247 | $ids['contributionPage'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', | |
248 | $ids['contribution'], | |
249 | 'contribution_page_id' | |
250 | ); | |
251 | ||
252 | if ($module == 'event') { | |
253 | // FIXME: figure out fields for event | |
254 | } | |
255 | else { | |
256 | // get the optional ids | |
257 | ||
258 | // Get membershipId. Join with membership payment table for additional checks | |
259 | $sql = " | |
260 | SELECT m.id | |
261 | FROM civicrm_membership as m | |
262 | WHERE m.contribution_recur_id = '{$ids['contributionRecur']}' | |
263 | LIMIT 1"; | |
264 | if ($membershipId = CRM_Core_DAO::singleValueQuery($sql)) { | |
265 | ||
266 | $ids['membership'] = $membershipId; | |
267 | } | |
268 | ||
269 | } | |
270 | ||
271 | return $ids; | |
272 | } | |
273 | ||
274 | ||
275 | ||
276 | } |