Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN { | |
b5c2afd0 EM |
36 | /** |
37 | * Constructor | |
38 | */ | |
6a488035 TO |
39 | function __construct() { |
40 | parent::__construct(); | |
41 | } | |
42 | ||
6c786a9b EM |
43 | /** |
44 | * @param string $component | |
45 | * | |
46 | * @return bool|void | |
47 | */ | |
6a488035 TO |
48 | function main($component = 'contribute') { |
49 | ||
50 | //we only get invoice num as a key player from payment gateway response. | |
51 | //for ARB we get x_subscription_id and x_subscription_paynum | |
52 | $x_subscription_id = self::retrieve('x_subscription_id', 'String'); | |
53 | ||
54 | if ($x_subscription_id) { | |
55 | //Approved | |
56 | ||
57 | $ids = $objects = array(); | |
58 | $input['component'] = $component; | |
59 | ||
60 | // load post vars in $input | |
61 | $this->getInput($input, $ids); | |
62 | ||
63 | // load post ids in $ids | |
64 | $this->getIDs($ids, $input); | |
65 | ||
66 | $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', | |
67 | 'AuthNet', 'id', 'name' | |
68 | ); | |
69 | ||
70 | if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) { | |
71 | return FALSE; | |
72 | } | |
73 | ||
74 | if ($component == 'contribute' && $ids['contributionRecur']) { | |
75 | // check if first contribution is completed, else complete first contribution | |
76 | $first = TRUE; | |
77 | if ($objects['contribution']->contribution_status_id == 1) { | |
78 | $first = FALSE; | |
79 | } | |
80 | return $this->recur($input, $ids, $objects, $first); | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
6c786a9b EM |
85 | /** |
86 | * @param $input | |
87 | * @param $ids | |
88 | * @param $objects | |
89 | * @param $first | |
90 | */ | |
6a488035 TO |
91 | function recur(&$input, &$ids, &$objects, $first) { |
92 | $recur = &$objects['contributionRecur']; | |
93 | ||
94 | // do a subscription check | |
95 | if ($recur->processor_id != $input['subscription_id']) { | |
96 | CRM_Core_Error::debug_log_message("Unrecognized subscription."); | |
97 | echo "Failure: Unrecognized subscription<p>"; | |
98 | return FALSE; | |
99 | } | |
100 | ||
101 | // At this point $object has first contribution loaded. | |
102 | // Lets do a check to make sure this payment has the amount same as that of first contribution. | |
103 | if ($objects['contribution']->total_amount != $input['amount']) { | |
104 | CRM_Core_Error::debug_log_message("Subscription amount mismatch."); | |
105 | echo "Failure: Subscription amount mismatch<p>"; | |
106 | return FALSE; | |
107 | } | |
108 | ||
109 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
110 | ||
111 | $transaction = new CRM_Core_Transaction(); | |
112 | ||
113 | $now = date('YmdHis'); | |
114 | ||
115 | // fix dates that already exist | |
116 | $dates = array('create_date', 'start_date', 'end_date', 'cancel_date', 'modified_date'); | |
117 | foreach ($dates as $name) { | |
118 | if ($recur->$name) { | |
119 | $recur->$name = CRM_Utils_Date::isoToMysql($recur->$name); | |
120 | } | |
121 | } | |
122 | ||
123 | //load new contribution object if required. | |
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']; | |
94d1bc8d | 128 | $contribution->financial_type_id = $objects['contributionType']->id; |
6a488035 TO |
129 | $contribution->contribution_page_id = $ids['contributionPage']; |
130 | $contribution->contribution_recur_id = $ids['contributionRecur']; | |
131 | $contribution->receive_date = $now; | |
132 | $contribution->currency = $objects['contribution']->currency; | |
133 | $contribution->payment_instrument_id = $objects['contribution']->payment_instrument_id; | |
134 | $contribution->amount_level = $objects['contribution']->amount_level; | |
135 | $contribution->address_id = $objects['contribution']->address_id; | |
94d1bc8d PJ |
136 | $contribution->campaign_id = $objects['contribution']->campaign_id; |
137 | ||
6a488035 TO |
138 | $objects['contribution'] = &$contribution; |
139 | } | |
140 | $objects['contribution']->invoice_id = md5(uniqid(rand(), TRUE)); | |
141 | $objects['contribution']->total_amount = $input['amount']; | |
142 | $objects['contribution']->trxn_id = $input['trxn_id']; | |
143 | ||
144 | // since we have processor loaded for sure at this point, | |
145 | // check and validate gateway MD5 response if present | |
146 | $this->checkMD5($ids, $input); | |
147 | ||
148 | $sendNotification = FALSE; | |
149 | if ($input['response_code'] == 1) { | |
150 | // Approved | |
151 | if ($first) { | |
152 | $recur->start_date = $now; | |
153 | $recur->trxn_id = $recur->processor_id; | |
154 | $sendNotification = TRUE; | |
155 | $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_START; | |
156 | } | |
157 | $statusName = 'In Progress'; | |
158 | if (($recur->installments > 0) && | |
159 | ($input['subscription_paynum'] >= $recur->installments) | |
160 | ) { | |
161 | // this is the last payment | |
162 | $statusName = 'Completed'; | |
163 | $recur->end_date = $now; | |
164 | $sendNotification = TRUE; | |
165 | $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_END; | |
166 | } | |
167 | $recur->modified_date = $now; | |
168 | $recur->contribution_status_id = array_search($statusName, $contributionStatus); | |
169 | $recur->save(); | |
170 | } | |
171 | else { | |
172 | // Declined | |
173 | // failed status | |
174 | $recur->contribution_status_id = array_search('Failed', $contributionStatus); | |
175 | $recur->cancel_date = $now; | |
176 | $recur->save(); | |
177 | ||
178 | CRM_Core_Error::debug_log_message("Subscription payment failed - '{$input['response_reason_text']}'"); | |
179 | ||
180 | // the recurring contribution has declined a payment or has failed | |
181 | // so we just fix the recurring contribution and not change any of | |
182 | // the existing contribiutions | |
183 | // CRM-9036 | |
184 | return TRUE; | |
185 | } | |
186 | ||
187 | // check if contribution is already completed, if so we ignore this ipn | |
188 | if ($objects['contribution']->contribution_status_id == 1) { | |
189 | $transaction->commit(); | |
190 | CRM_Core_Error::debug_log_message("returning since contribution has already been handled"); | |
191 | echo "Success: Contribution has already been handled<p>"; | |
192 | return TRUE; | |
193 | } | |
194 | ||
195 | $this->completeTransaction($input, $ids, $objects, $transaction, $recur); | |
196 | ||
197 | if ($sendNotification) { | |
198 | $autoRenewMembership = FALSE; | |
199 | if ($recur->id && | |
200 | isset($ids['membership']) && $ids['membership'] | |
201 | ) { | |
202 | $autoRenewMembership = TRUE; | |
203 | } | |
204 | ||
205 | //send recurring Notification email for user | |
206 | CRM_Contribute_BAO_ContributionPage::recurringNotify($subscriptionPaymentStatus, | |
207 | $ids['contact'], | |
208 | $ids['contributionPage'], | |
209 | $recur, | |
210 | $autoRenewMembership | |
211 | ); | |
212 | } | |
213 | } | |
214 | ||
6c786a9b EM |
215 | /** |
216 | * @param $input | |
217 | * @param $ids | |
218 | */ | |
6a488035 TO |
219 | function getInput(&$input, &$ids) { |
220 | $input['amount'] = self::retrieve('x_amount', 'String'); | |
221 | $input['subscription_id'] = self::retrieve('x_subscription_id', 'Integer'); | |
222 | $input['response_code'] = self::retrieve('x_response_code', 'Integer'); | |
223 | $input['MD5_Hash'] = self::retrieve('x_MD5_Hash', 'String', FALSE, ''); | |
224 | $input['fee_amount'] = self::retrieve('x_fee_amount', 'Money', FALSE, '0.00'); | |
225 | $input['net_amount'] = self::retrieve('x_net_amount', 'Money', FALSE, '0.00'); | |
226 | $input['response_reason_code'] = self::retrieve('x_response_reason_code', 'String', FALSE); | |
227 | $input['response_reason_text'] = self::retrieve('x_response_reason_text', 'String', FALSE); | |
228 | $input['subscription_paynum'] = self::retrieve('x_subscription_paynum', 'Integer', FALSE, 0); | |
229 | $input['trxn_id'] = self::retrieve('x_trans_id', 'String', FALSE); | |
230 | if ($input['trxn_id']) { | |
231 | $input['is_test'] = 0; | |
232 | } | |
233 | else { | |
234 | $input['is_test'] = 1; | |
235 | $input['trxn_id'] = md5(uniqid(rand(), TRUE)); | |
236 | } | |
237 | ||
238 | if (!$this->getBillingID($ids)) { | |
239 | return FALSE; | |
240 | } | |
241 | $billingID = $ids['billing']; | |
242 | $params = array( | |
243 | 'first_name' => 'x_first_name', | |
244 | 'last_name' => 'x_last_name', | |
245 | "street_address-{$billingID}" => 'x_address', | |
246 | "city-{$billingID}" => 'x_city', | |
247 | "state-{$billingID}" => 'x_state', | |
248 | "postal_code-{$billingID}" => 'x_zip', | |
249 | "country-{$billingID}" => 'x_country', | |
250 | "email-{$billingID}" => 'x_email', | |
251 | ); | |
252 | foreach ($params as $civiName => $resName) { | |
253 | $input[$civiName] = self::retrieve($resName, 'String', FALSE); | |
254 | } | |
255 | } | |
256 | ||
6c786a9b EM |
257 | /** |
258 | * @param $ids | |
259 | * @param $input | |
260 | */ | |
6a488035 TO |
261 | function getIDs(&$ids, &$input) { |
262 | $ids['contact'] = self::retrieve('x_cust_id', 'Integer'); | |
263 | $ids['contribution'] = self::retrieve('x_invoice_num', 'Integer'); | |
264 | ||
265 | // joining with contribution table for extra checks | |
266 | $sql = " | |
267 | SELECT cr.id, cr.contact_id | |
268 | FROM civicrm_contribution_recur cr | |
269 | INNER JOIN civicrm_contribution co ON co.contribution_recur_id = cr.id | |
270 | WHERE cr.processor_id = '{$input['subscription_id']}' AND | |
271 | (cr.contact_id = {$ids['contact']} OR co.id = {$ids['contribution']}) | |
272 | LIMIT 1"; | |
273 | $contRecur = CRM_Core_DAO::executeQuery($sql); | |
274 | $contRecur->fetch(); | |
275 | $ids['contributionRecur'] = $contRecur->id; | |
8753de39 JM |
276 | if($ids['contact'] != $contRecur->contact_id){ |
277 | CRM_Core_Error::debug_log_message("Recurring contribution appears to have been re-assigned from id {$ids['contact']} to {$contRecur->contact_id} | |
6a488035 TO |
278 | Continuing with {$contRecur->contact_id} |
279 | "); | |
8753de39 | 280 | $ids['contact'] = $contRecur->contact_id; |
6a488035 TO |
281 | } |
282 | if (!$ids['contributionRecur']) { | |
283 | CRM_Core_Error::debug_log_message("Could not find contributionRecur id: ".print_r($input, TRUE)); | |
284 | echo "Failure: Could not find contributionRecur<p>"; | |
285 | exit(); | |
286 | } | |
287 | ||
288 | // get page id based on contribution id | |
289 | $ids['contributionPage'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', | |
290 | $ids['contribution'], | |
291 | 'contribution_page_id' | |
292 | ); | |
293 | ||
294 | if ($input['component'] == 'event') { | |
295 | // FIXME: figure out fields for event | |
296 | } | |
297 | else { | |
298 | // get the optional ids | |
299 | ||
300 | // Get membershipId. Join with membership payment table for additional checks | |
301 | $sql = " | |
302 | SELECT m.id | |
303 | FROM civicrm_membership m | |
304 | INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contribution_id = {$ids['contribution']} | |
305 | WHERE m.contribution_recur_id = {$ids['contributionRecur']} | |
306 | LIMIT 1"; | |
307 | if ($membershipId = CRM_Core_DAO::singleValueQuery($sql)) { | |
308 | $ids['membership'] = $membershipId; | |
309 | } | |
310 | ||
311 | // FIXME: todo related_contact and onBehalfDupeAlert. Check paypalIPN. | |
312 | } | |
313 | } | |
314 | ||
6c786a9b EM |
315 | /** |
316 | * @param $name | |
317 | * @param $type | |
318 | * @param bool $abort | |
319 | * @param null $default | |
320 | * @param string $location | |
321 | * | |
322 | * @return mixed | |
323 | */ | |
6a488035 TO |
324 | static function retrieve($name, $type, $abort = TRUE, $default = NULL, $location = 'POST') { |
325 | static $store = NULL; | |
326 | $value = CRM_Utils_Request::retrieve($name, $type, $store, | |
327 | FALSE, $default, $location | |
328 | ); | |
329 | if ($abort && $value === NULL) { | |
330 | CRM_Core_Error::debug_log_message("Could not find an entry for $name in $location"); | |
4b7d50ce | 331 | CRM_Core_Error::debug_var('POST', $_POST); |
332 | CRM_Core_Error::debug_var('REQUEST', $_REQUEST); | |
6a488035 TO |
333 | echo "Failure: Missing Parameter<p>"; |
334 | exit(); | |
335 | } | |
336 | return $value; | |
337 | } | |
338 | ||
6c786a9b EM |
339 | /** |
340 | * @param $ids | |
341 | * @param $input | |
342 | * | |
343 | * @return bool | |
344 | */ | |
6a488035 TO |
345 | function checkMD5($ids, $input) { |
346 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($ids['paymentProcessor'], | |
347 | $input['is_test'] ? 'test' : 'live' | |
348 | ); | |
349 | $paymentObject = CRM_Core_Payment::singleton($input['is_test'] ? 'test' : 'live', $paymentProcessor); | |
350 | ||
351 | if (!$paymentObject->checkMD5($input['MD5_Hash'], $input['trxn_id'], $input['amount'], TRUE)) { | |
352 | CRM_Core_Error::debug_log_message("MD5 Verification failed."); | |
353 | echo "Failure: Security verification failed<p>"; | |
354 | exit(); | |
355 | } | |
356 | return TRUE; | |
357 | } | |
358 | } | |
359 |