commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / extern
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 *
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)
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) {
58 CRM_Core_Error::debug_log_message("Success: This payment has already been processed.");
59 echo "Success: This payment has already been processed<p>\n";
60 exit;
61 }
62 var_dump($ids);
63 var_dump($input);
64
65 if(array_key_exists('membership', $ids)) {
66 $membership = array();
67 $params = array('id' => $ids['membership']);
68 $obj = CRM_Member_BAO_Membership::retrieve($params, $membership);
69 $objects['membership'] = array(&$obj);
70 }
71 var_dump($ids);
72 var_dump($input);
73
74 $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType',
75 'TrustCommerce', 'id', 'name'
76 );
77
78 if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) {
79 return FALSE;
80 }
81 // var_dump($objects);
82
83 if ($component == 'contribute' && $ids['contributionRecur']) {
84 // check if first contribution is completed, else complete first contribution
85 $first = TRUE;
86 if ($objects['contribution']->contribution_status_id == 1) {
87 $first = FALSE;
88 }
89
90
91 return $this->processRecur($input, $ids, $objects, $first);
92
93 }
94
95 }
96 }
97
98 protected function checkDuplicate($input, $ids) {
99 // $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';
100 $sql="select id from civicrm_contribution where trxn_id = '".$ids['trxn_id']."'";
101
102
103 $result = CRM_Core_DAO::executeQuery($sql);
104 $result->fetch();
105 $id = $result->id;
106 return $id;
107 }
108 protected function processRecur($input, $ids, $objects, $first) {
109 $recur = &$objects['contributionRecur'];
110 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
111
112 $transaction = new CRM_Core_Transaction();
113
114 $now = date('YmdHis');
115
116 // fix dates that already exist
117 $dates = array('create_date', 'start_date', 'end_date', 'cancel_date', 'modified_date');
118 foreach ($dates as $name) {
119 if ($recur->$name) {
120 $recur->$name = CRM_Utils_Date::isoToMysql($recur->$name);
121 }
122 }
123
124 if (!$first) {
125 // create a contribution and then get it processed
126 $contribution = new CRM_Contribute_BAO_Contribution();
127 $contribution->contact_id = $ids['contact'];
128 $contribution->financial_type_id = $objects['contributionType']->id;
129 $contribution->contribution_page_id = $ids['contributionPage'];
130 $contribution->contribution_recur_id = $ids['contributionRecur'];
131 $contribution->receive_date = $input['date'];
132 $contribution->currency = $objects['contribution']->currency;
133 $contribution->payment_instrument_id = 1;
134 $contribution->amount_level = $objects['contribution']->amount_level;
135 $contribution->address_id = $objects['contribution']->address_id;
136 $contribution->honor_contact_id = $objects['contribution']->honor_contact_id;
137 $contribution->honor_type_id = $objects['contribution']->honor_type_id;
138 $contribution->campaign_id = $objects['contribution']->campaign_id;
139 $contribution->total_amount = $input['amount'];
140
141 $objects['contribution'] = &$contribution;
142 }
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
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 }
155
156 $sendNotification = FALSE;
157 if ($input['status'] == 1) {
158
159 // Approved
160 if ($first) {
161 $recur->start_date = $now;
162 $sendNotification = TRUE;
163 $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_START;
164 }
165 $statusName = 'In Progress';
166 if (($recur->installments > 0) &&
167 ($input['subscription_paynum'] >= $recur->installments)
168 ) {
169 // this is the last payment
170 $statusName = 'Completed';
171 $recur->end_date = $now;
172
173 $sendNotification = TRUE;
174 $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_END;
175 }
176 $recur->trxn_id = $input['trxn_id'];
177 $recur->total_amount = $input['amount'];
178 $recur->payment_instrument_id = 1;
179 $recur->fee = 0;
180 $recur->net_amount = $input['amount'];
181
182 $recur->modified_date = $now;
183 $recur->contribution_status_id = array_search($statusName, $contributionStatus);
184 $recur->save();
185 }
186 else {
187 // Declined
188 // failed status
189
190 $recur->trxn_id = $input['trxn_id'];
191 $recur->total_amount = $input['amount'];
192 $recur->payment_instrument_id = 1;
193 $recur->fee = 0;
194 $recur->net_amount = $input['amount'];
195
196 $recur->contribution_status_id = array_search('Failed', $contributionStatus);
197 $recur->cancel_date = $now;
198 $recur->save();
199
200 CRM_Core_Error::debug_log_message("Subscription payment failed");
201
202 $input['skipComponentSync'] = TRUE;
203 $this->failed($objects, $transaction, $input);
204 }
205
206 $input['is_test'] = 0;
207
208 $this->completeTransaction($input, $ids, $objects, $transaction, $recur);
209
210 echo 'Success: Created new contribution: '.$ids['contribution'].' for cid: '.$ids['contact'].'\n';
211 CRM_Core_Error::debug_log_message('Success: Created new contribution: '.$ids['contribution'].' for cid: '.$ids['contact']);
212
213 if ($sendNotification) {
214 $autoRenewMembership = FALSE;
215 if ($recur->id &&
216 isset($ids['membership']) && $ids['membership']
217 ) {
218 $autoRenewMembership = TRUE;
219 }
220
221
222
223
224 //send recurring Notification email for user
225 CRM_Contribute_BAO_ContributionPage::recurringNotify($subscriptionPaymentStatus,
226 $ids['contact'],
227 $ids['contributionPage'],
228 $recur,
229 $autoRenewMembership
230 );
231
232
233
234
235 }
236
237
238
239 }
240
241 protected function getIDs($billingid, $input, $module) {
242 $sql = "SELECT cr.id, cr.contact_id, co.id as coid
243 FROM civicrm_contribution_recur cr
244 INNER JOIN civicrm_contribution co ON co.contribution_recur_id = cr.id
245 WHERE cr.processor_id = '$billingid' LIMIT 1";
246
247
248 $result = CRM_Core_DAO::executeQuery($sql);
249 $result->fetch();
250 $ids['contribution'] = $result->coid;
251 $ids['contributionRecur'] = $result->id;
252 $ids['contact'] = $result->contact_id;
253
254 if (!$ids['contributionRecur']) {
255 CRM_Core_Error::debug_log_message("Could not find billingid: ".$billingid);
256 echo "Failure: Could not find contributionRecur<p>\n";
257 exit();
258 }
259
260 // get page id based on contribution id
261 $ids['contributionPage'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution',
262 $ids['contribution'],
263 'contribution_page_id'
264 );
265
266 if ($module == 'event') {
267 // FIXME: figure out fields for event
268 }
269 else {
270 // get the optional ids
271
272 // Get membershipId. Join with membership payment table for additional checks
273 $sql = "
274 SELECT m.id
275 FROM civicrm_membership as m
276 WHERE m.contribution_recur_id = '{$ids['contributionRecur']}'
277 LIMIT 1";
278 if ($membershipId = CRM_Core_DAO::singleValueQuery($sql)) {
279
280 $ids['membership'] = $membershipId;
281 }
282
283 }
284
285 return $ids;
286 }
287
288
289
290 }