Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN { | |
0dbefed3 | 34 | |
b5c2afd0 | 35 | /** |
fe482240 | 36 | * Constructor function. |
0dbefed3 | 37 | * |
5a4f6742 CW |
38 | * @param array $inputData |
39 | * contents of HTTP REQUEST. | |
0dbefed3 EM |
40 | * |
41 | * @throws CRM_Core_Exception | |
b5c2afd0 | 42 | */ |
00be9182 | 43 | public function __construct($inputData) { |
0dbefed3 | 44 | $this->setInputParameters($inputData); |
6a488035 TO |
45 | parent::__construct(); |
46 | } | |
47 | ||
6c786a9b EM |
48 | /** |
49 | * @param string $component | |
50 | * | |
51 | * @return bool|void | |
52 | */ | |
00be9182 | 53 | public function main($component = 'contribute') { |
6a488035 TO |
54 | |
55 | //we only get invoice num as a key player from payment gateway response. | |
56 | //for ARB we get x_subscription_id and x_subscription_paynum | |
0dbefed3 | 57 | $x_subscription_id = $this->retrieve('x_subscription_id', 'String'); |
b6b59c64 | 58 | $ids = $objects = $input = array(); |
6a488035 TO |
59 | |
60 | if ($x_subscription_id) { | |
61 | //Approved | |
62 | ||
6a488035 TO |
63 | $input['component'] = $component; |
64 | ||
65 | // load post vars in $input | |
66 | $this->getInput($input, $ids); | |
67 | ||
68 | // load post ids in $ids | |
69 | $this->getIDs($ids, $input); | |
70 | ||
11c03c08 | 71 | // This is an unreliable method as there could be more than one instance. |
72 | // Recommended approach is to use the civicrm/payment/ipn/xx url where xx is the payment | |
73 | // processor id & the handleNotification function (which should call the completetransaction api & by-pass this | |
74 | // entirely). The only thing the IPN class should really do is extract data from the request, validate it | |
75 | // & call completetransaction or call fail? (which may not exist yet). | |
76 | $paymentProcessorTypeID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', | |
6a488035 TO |
77 | 'AuthNet', 'id', 'name' |
78 | ); | |
11c03c08 | 79 | $paymentProcessorID = (int) civicrm_api3('PaymentProcessor', 'getvalue', array( |
80 | 'is_test' => 0, | |
81 | 'options' => array('limit' => 1), | |
82 | 'payment_processor_type_id' => $paymentProcessorTypeID, | |
83 | 'return' => 'id', | |
84 | )); | |
6a488035 TO |
85 | |
86 | if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) { | |
87 | return FALSE; | |
88 | } | |
89 | ||
90 | if ($component == 'contribute' && $ids['contributionRecur']) { | |
91 | // check if first contribution is completed, else complete first contribution | |
92 | $first = TRUE; | |
93 | if ($objects['contribution']->contribution_status_id == 1) { | |
94 | $first = FALSE; | |
95 | } | |
96 | return $this->recur($input, $ids, $objects, $first); | |
97 | } | |
98 | } | |
0dbefed3 | 99 | return TRUE; |
6a488035 TO |
100 | } |
101 | ||
6c786a9b | 102 | /** |
7a6073fd EM |
103 | * @param array $input |
104 | * @param array $ids | |
0dbefed3 | 105 | * @param array $objects |
6c786a9b | 106 | * @param $first |
7a6073fd EM |
107 | * |
108 | * @return bool | |
6c786a9b | 109 | */ |
00be9182 | 110 | public function recur(&$input, &$ids, &$objects, $first) { |
937cf542 | 111 | $this->_isRecurring = TRUE; |
6a488035 | 112 | $recur = &$objects['contributionRecur']; |
d6944518 | 113 | $paymentProcessorObject = $objects['contribution']->_relatedObjects['paymentProcessor']['object']; |
6a488035 TO |
114 | |
115 | // do a subscription check | |
116 | if ($recur->processor_id != $input['subscription_id']) { | |
117 | CRM_Core_Error::debug_log_message("Unrecognized subscription."); | |
118 | echo "Failure: Unrecognized subscription<p>"; | |
119 | return FALSE; | |
120 | } | |
121 | ||
122 | // At this point $object has first contribution loaded. | |
123 | // Lets do a check to make sure this payment has the amount same as that of first contribution. | |
124 | if ($objects['contribution']->total_amount != $input['amount']) { | |
125 | CRM_Core_Error::debug_log_message("Subscription amount mismatch."); | |
126 | echo "Failure: Subscription amount mismatch<p>"; | |
127 | return FALSE; | |
128 | } | |
129 | ||
130 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
131 | ||
132 | $transaction = new CRM_Core_Transaction(); | |
133 | ||
134 | $now = date('YmdHis'); | |
135 | ||
136 | // fix dates that already exist | |
137 | $dates = array('create_date', 'start_date', 'end_date', 'cancel_date', 'modified_date'); | |
138 | foreach ($dates as $name) { | |
139 | if ($recur->$name) { | |
140 | $recur->$name = CRM_Utils_Date::isoToMysql($recur->$name); | |
141 | } | |
142 | } | |
143 | ||
144 | //load new contribution object if required. | |
145 | if (!$first) { | |
146 | // create a contribution and then get it processed | |
147 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
148 | $contribution->contact_id = $ids['contact']; | |
353ffa53 | 149 | $contribution->financial_type_id = $objects['contributionType']->id; |
6a488035 TO |
150 | $contribution->contribution_page_id = $ids['contributionPage']; |
151 | $contribution->contribution_recur_id = $ids['contributionRecur']; | |
152 | $contribution->receive_date = $now; | |
153 | $contribution->currency = $objects['contribution']->currency; | |
154 | $contribution->payment_instrument_id = $objects['contribution']->payment_instrument_id; | |
155 | $contribution->amount_level = $objects['contribution']->amount_level; | |
156 | $contribution->address_id = $objects['contribution']->address_id; | |
94d1bc8d PJ |
157 | $contribution->campaign_id = $objects['contribution']->campaign_id; |
158 | ||
6a488035 TO |
159 | $objects['contribution'] = &$contribution; |
160 | } | |
161 | $objects['contribution']->invoice_id = md5(uniqid(rand(), TRUE)); | |
162 | $objects['contribution']->total_amount = $input['amount']; | |
163 | $objects['contribution']->trxn_id = $input['trxn_id']; | |
164 | ||
d6944518 | 165 | $this->checkMD5($paymentProcessorObject, $input); |
6a488035 | 166 | |
6a488035 TO |
167 | if ($input['response_code'] == 1) { |
168 | // Approved | |
169 | if ($first) { | |
170 | $recur->start_date = $now; | |
171 | $recur->trxn_id = $recur->processor_id; | |
937cf542 | 172 | $this->_isFirstOrLastRecurringPayment = CRM_Core_Payment::RECURRING_PAYMENT_START; |
6a488035 TO |
173 | } |
174 | $statusName = 'In Progress'; | |
175 | if (($recur->installments > 0) && | |
176 | ($input['subscription_paynum'] >= $recur->installments) | |
177 | ) { | |
178 | // this is the last payment | |
179 | $statusName = 'Completed'; | |
180 | $recur->end_date = $now; | |
937cf542 | 181 | $this->_isFirstOrLastRecurringPayment = CRM_Core_Payment::RECURRING_PAYMENT_END; |
6a488035 TO |
182 | } |
183 | $recur->modified_date = $now; | |
184 | $recur->contribution_status_id = array_search($statusName, $contributionStatus); | |
185 | $recur->save(); | |
186 | } | |
187 | else { | |
188 | // Declined | |
189 | // failed status | |
190 | $recur->contribution_status_id = array_search('Failed', $contributionStatus); | |
191 | $recur->cancel_date = $now; | |
192 | $recur->save(); | |
193 | ||
267353f4 CB |
194 | $message = ts("Subscription payment failed - %1", array(1 => htmlspecialchars($input['response_reason_text']))); |
195 | CRM_Core_Error::debug_log_message($message); | |
6a488035 TO |
196 | |
197 | // the recurring contribution has declined a payment or has failed | |
198 | // so we just fix the recurring contribution and not change any of | |
7a6073fd | 199 | // the existing contributions |
6a488035 TO |
200 | // CRM-9036 |
201 | return TRUE; | |
202 | } | |
203 | ||
204 | // check if contribution is already completed, if so we ignore this ipn | |
205 | if ($objects['contribution']->contribution_status_id == 1) { | |
206 | $transaction->commit(); | |
267353f4 | 207 | CRM_Core_Error::debug_log_message("Returning since contribution has already been handled."); |
6a488035 TO |
208 | echo "Success: Contribution has already been handled<p>"; |
209 | return TRUE; | |
210 | } | |
211 | ||
212 | $this->completeTransaction($input, $ids, $objects, $transaction, $recur); | |
6a488035 TO |
213 | } |
214 | ||
6c786a9b EM |
215 | /** |
216 | * @param $input | |
217 | * @param $ids | |
7a9ab499 EM |
218 | * |
219 | * @return bool | |
6c786a9b | 220 | */ |
00be9182 | 221 | public function getInput(&$input, &$ids) { |
0dbefed3 EM |
222 | $input['amount'] = $this->retrieve('x_amount', 'String'); |
223 | $input['subscription_id'] = $this->retrieve('x_subscription_id', 'Integer'); | |
224 | $input['response_code'] = $this->retrieve('x_response_code', 'Integer'); | |
225 | $input['MD5_Hash'] = $this->retrieve('x_MD5_Hash', 'String', FALSE, ''); | |
226 | $input['response_reason_code'] = $this->retrieve('x_response_reason_code', 'String', FALSE); | |
227 | $input['response_reason_text'] = $this->retrieve('x_response_reason_text', 'String', FALSE); | |
228 | $input['subscription_paynum'] = $this->retrieve('x_subscription_paynum', 'Integer', FALSE, 0); | |
229 | $input['trxn_id'] = $this->retrieve('x_trans_id', 'String', FALSE); | |
230 | ||
6a488035 TO |
231 | if ($input['trxn_id']) { |
232 | $input['is_test'] = 0; | |
233 | } | |
5c6c7e76 | 234 | // Only assume trxn_id 'should' have been returned for success. |
235 | // Per CRM-17611 it would also not be passed back for a decline. | |
236 | elseif ($input['response_code'] == 1) { | |
6a488035 TO |
237 | $input['is_test'] = 1; |
238 | $input['trxn_id'] = md5(uniqid(rand(), TRUE)); | |
239 | } | |
240 | ||
241 | if (!$this->getBillingID($ids)) { | |
242 | return FALSE; | |
243 | } | |
244 | $billingID = $ids['billing']; | |
245 | $params = array( | |
246 | 'first_name' => 'x_first_name', | |
247 | 'last_name' => 'x_last_name', | |
248 | "street_address-{$billingID}" => 'x_address', | |
249 | "city-{$billingID}" => 'x_city', | |
250 | "state-{$billingID}" => 'x_state', | |
251 | "postal_code-{$billingID}" => 'x_zip', | |
252 | "country-{$billingID}" => 'x_country', | |
253 | "email-{$billingID}" => 'x_email', | |
254 | ); | |
255 | foreach ($params as $civiName => $resName) { | |
0dbefed3 | 256 | $input[$civiName] = $this->retrieve($resName, 'String', FALSE); |
6a488035 TO |
257 | } |
258 | } | |
259 | ||
6c786a9b EM |
260 | /** |
261 | * @param $ids | |
262 | * @param $input | |
263 | */ | |
00be9182 | 264 | public function getIDs(&$ids, &$input) { |
0dbefed3 EM |
265 | $ids['contact'] = $this->retrieve('x_cust_id', 'Integer', FALSE, 0); |
266 | $ids['contribution'] = $this->retrieve('x_invoice_num', 'Integer'); | |
6a488035 TO |
267 | |
268 | // joining with contribution table for extra checks | |
269 | $sql = " | |
270 | SELECT cr.id, cr.contact_id | |
271 | FROM civicrm_contribution_recur cr | |
272 | INNER JOIN civicrm_contribution co ON co.contribution_recur_id = cr.id | |
273 | WHERE cr.processor_id = '{$input['subscription_id']}' AND | |
274 | (cr.contact_id = {$ids['contact']} OR co.id = {$ids['contribution']}) | |
275 | LIMIT 1"; | |
276 | $contRecur = CRM_Core_DAO::executeQuery($sql); | |
277 | $contRecur->fetch(); | |
278 | $ids['contributionRecur'] = $contRecur->id; | |
9b873358 | 279 | if ($ids['contact'] != $contRecur->contact_id) { |
267353f4 CB |
280 | $message = ts("Recurring contribution appears to have been re-assigned from id %1 to %2, continuing with %2.", array(1 => $ids['contact'], 2 => $contRecur->contact_id)); |
281 | CRM_Core_Error::debug_log_message($message); | |
8753de39 | 282 | $ids['contact'] = $contRecur->contact_id; |
6a488035 TO |
283 | } |
284 | if (!$ids['contributionRecur']) { | |
4346da4b | 285 | $message = ts("Could not find contributionRecur id"); |
286 | $log = new CRM_Utils_SystemLogger(); | |
287 | $log->error('payment_notification', array('message' => $message, 'ids' => $ids, 'input' => $input)); | |
288 | throw new CRM_Core_Exception($message); | |
6a488035 TO |
289 | } |
290 | ||
291 | // get page id based on contribution id | |
292 | $ids['contributionPage'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', | |
293 | $ids['contribution'], | |
294 | 'contribution_page_id' | |
295 | ); | |
296 | ||
297 | if ($input['component'] == 'event') { | |
298 | // FIXME: figure out fields for event | |
299 | } | |
300 | else { | |
301 | // get the optional ids | |
302 | ||
303 | // Get membershipId. Join with membership payment table for additional checks | |
304 | $sql = " | |
305 | SELECT m.id | |
306 | FROM civicrm_membership m | |
307 | INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contribution_id = {$ids['contribution']} | |
308 | WHERE m.contribution_recur_id = {$ids['contributionRecur']} | |
309 | LIMIT 1"; | |
310 | if ($membershipId = CRM_Core_DAO::singleValueQuery($sql)) { | |
311 | $ids['membership'] = $membershipId; | |
312 | } | |
313 | ||
314 | // FIXME: todo related_contact and onBehalfDupeAlert. Check paypalIPN. | |
315 | } | |
316 | } | |
317 | ||
6c786a9b | 318 | /** |
6a0b768e TO |
319 | * @param string $name |
320 | * Parameter name. | |
321 | * @param string $type | |
322 | * Parameter type. | |
323 | * @param bool $abort | |
324 | * Abort if not present. | |
325 | * @param null $default | |
326 | * Default value. | |
6c786a9b | 327 | * |
0dbefed3 | 328 | * @throws CRM_Core_Exception |
6c786a9b EM |
329 | * @return mixed |
330 | */ | |
00be9182 | 331 | public function retrieve($name, $type, $abort = TRUE, $default = NULL) { |
0dbefed3 EM |
332 | $value = CRM_Utils_Type::validate( |
333 | empty($this->_inputParameters[$name]) ? $default : $this->_inputParameters[$name], | |
334 | $type, | |
335 | FALSE | |
6a488035 TO |
336 | ); |
337 | if ($abort && $value === NULL) { | |
0dbefed3 | 338 | throw new CRM_Core_Exception("Could not find an entry for $name"); |
6a488035 TO |
339 | } |
340 | return $value; | |
2aa397bc | 341 | } |
6a488035 | 342 | |
6c786a9b | 343 | /** |
d6944518 EM |
344 | * Check and validate gateway MD5 response if present. |
345 | * | |
346 | * @param CRM_Core_Payment_AuthorizeNet $paymentObject | |
347 | * @param array $input | |
6c786a9b | 348 | * |
4346da4b | 349 | * @throws CRM_Core_Exception |
6c786a9b | 350 | */ |
d6944518 | 351 | public function checkMD5($paymentObject, $input) { |
5c6c7e76 | 352 | if (empty($input['trxn_id'])) { |
353 | // For decline we have nothing to check against. | |
354 | return; | |
355 | } | |
6a488035 | 356 | if (!$paymentObject->checkMD5($input['MD5_Hash'], $input['trxn_id'], $input['amount'], TRUE)) { |
4346da4b | 357 | $message = "Failure: Security verification failed"; |
358 | $log = new CRM_Utils_SystemLogger(); | |
11a1ad01 | 359 | $log->error('payment_notification', array('message' => $message, 'input' => $input)); |
4346da4b | 360 | throw new CRM_Core_Exception($message); |
6a488035 | 361 | } |
6a488035 | 362 | } |
96025800 | 363 | |
6a488035 | 364 | } |