Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN { | |
36 | ||
37 | static $_paymentProcessor = NULL; | |
b5c2afd0 EM |
38 | |
39 | /** | |
f78c9258 | 40 | * Input parameters from payment processor. Store these so that |
41 | * the code does not need to keep retrieving from the http request | |
42 | * @var array | |
b5c2afd0 | 43 | */ |
f78c9258 | 44 | protected $_inputParameters = array(); |
45 | ||
46 | /** | |
47 | * Constructor function. | |
48 | * | |
49 | * @param array $inputData | |
50 | * Contents of HTTP REQUEST. | |
51 | * | |
52 | * @throws CRM_Core_Exception | |
53 | */ | |
54 | public function __construct($inputData) { | |
0aeed2bc BS |
55 | //CRM-19676 |
56 | $params = (!empty($inputData['custom'])) ? | |
57 | array_merge($inputData, json_decode($inputData['custom'], TRUE)) : | |
58 | $inputData; | |
59 | $this->setInputParameters($params); | |
6a488035 TO |
60 | parent::__construct(); |
61 | } | |
62 | ||
6c786a9b | 63 | /** |
100fef9d | 64 | * @param string $name |
6c786a9b | 65 | * @param $type |
6c786a9b EM |
66 | * @param bool $abort |
67 | * | |
68 | * @return mixed | |
69 | */ | |
f78c9258 | 70 | public function retrieve($name, $type, $abort = TRUE) { |
6a488035 | 71 | static $store = NULL; |
f78c9258 | 72 | $value = CRM_Utils_Type::validate( |
73 | CRM_Utils_Array::value($name, $this->_inputParameters), | |
74 | $type, | |
75 | FALSE | |
6a488035 TO |
76 | ); |
77 | if ($abort && $value === NULL) { | |
6f9ece22 | 78 | CRM_Core_Error::debug_log_message("Could not find an entry for $name"); |
79 | echo "Failure: Missing Parameter<p>" . CRM_Utils_Type::escape($name, 'String'); | |
6a488035 TO |
80 | exit(); |
81 | } | |
82 | return $value; | |
83 | } | |
84 | ||
6c786a9b EM |
85 | /** |
86 | * @param $input | |
87 | * @param $ids | |
88 | * @param $objects | |
89 | * @param $first | |
7a9ab499 EM |
90 | * |
91 | * @return bool | |
6c786a9b | 92 | */ |
00be9182 | 93 | public function recur(&$input, &$ids, &$objects, $first) { |
6a488035 TO |
94 | if (!isset($input['txnType'])) { |
95 | CRM_Core_Error::debug_log_message("Could not find txn_type in input request"); | |
96 | echo "Failure: Invalid parameters<p>"; | |
97 | return FALSE; | |
98 | } | |
99 | ||
100 | if ($input['txnType'] == 'subscr_payment' && | |
101 | $input['paymentStatus'] != 'Completed' | |
102 | ) { | |
103 | CRM_Core_Error::debug_log_message("Ignore all IPN payments that are not completed"); | |
104 | echo "Failure: Invalid parameters<p>"; | |
105 | return FALSE; | |
106 | } | |
107 | ||
108 | $recur = &$objects['contributionRecur']; | |
109 | ||
110 | // make sure the invoice ids match | |
111 | // make sure the invoice is valid and matches what we have in the contribution record | |
112 | if ($recur->invoice_id != $input['invoice']) { | |
113 | CRM_Core_Error::debug_log_message("Invoice values dont match between database and IPN request"); | |
114 | echo "Failure: Invoice values dont match between database and IPN request<p>"; | |
115 | return FALSE; | |
116 | } | |
117 | ||
118 | $now = date('YmdHis'); | |
119 | ||
120 | // fix dates that already exist | |
121 | $dates = array('create', 'start', 'end', 'cancel', 'modified'); | |
122 | foreach ($dates as $date) { | |
123 | $name = "{$date}_date"; | |
124 | if ($recur->$name) { | |
125 | $recur->$name = CRM_Utils_Date::isoToMysql($recur->$name); | |
126 | } | |
127 | } | |
128 | $sendNotification = FALSE; | |
129 | $subscriptionPaymentStatus = NULL; | |
130 | //set transaction type | |
f78c9258 | 131 | $txnType = $this->retrieve('txn_type', 'String'); |
6a488035 TO |
132 | switch ($txnType) { |
133 | case 'subscr_signup': | |
134 | $recur->create_date = $now; | |
135 | //some times subscr_signup response come after the | |
136 | //subscr_payment and set to pending mode. | |
137 | $statusID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionRecur', | |
138 | $recur->id, 'contribution_status_id' | |
139 | ); | |
140 | if ($statusID != 5) { | |
141 | $recur->contribution_status_id = 2; | |
142 | } | |
f78c9258 | 143 | $recur->processor_id = $this->retrieve('subscr_id', 'String'); |
6a488035 TO |
144 | $recur->trxn_id = $recur->processor_id; |
145 | $sendNotification = TRUE; | |
146 | $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_START; | |
147 | break; | |
148 | ||
149 | case 'subscr_eot': | |
150 | if ($recur->contribution_status_id != 3) { | |
151 | $recur->contribution_status_id = 1; | |
152 | } | |
153 | $recur->end_date = $now; | |
154 | $sendNotification = TRUE; | |
155 | $subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_END; | |
156 | break; | |
157 | ||
158 | case 'subscr_cancel': | |
159 | $recur->contribution_status_id = 3; | |
160 | $recur->cancel_date = $now; | |
161 | break; | |
162 | ||
163 | case 'subscr_failed': | |
164 | $recur->contribution_status_id = 4; | |
165 | $recur->modified_date = $now; | |
166 | break; | |
167 | ||
168 | case 'subscr_modify': | |
169 | CRM_Core_Error::debug_log_message("We do not handle modifications to subscriptions right now"); | |
170 | echo "Failure: We do not handle modifications to subscriptions right now<p>"; | |
171 | return FALSE; | |
172 | ||
173 | case 'subscr_payment': | |
174 | if ($first) { | |
175 | $recur->start_date = $now; | |
176 | } | |
177 | else { | |
178 | $recur->modified_date = $now; | |
179 | } | |
180 | ||
181 | // make sure the contribution status is not done | |
182 | // since order of ipn's is unknown | |
183 | if ($recur->contribution_status_id != 1) { | |
184 | $recur->contribution_status_id = 5; | |
185 | } | |
186 | break; | |
187 | } | |
188 | ||
189 | $recur->save(); | |
190 | ||
191 | if ($sendNotification) { | |
192 | ||
193 | $autoRenewMembership = FALSE; | |
194 | if ($recur->id && | |
195 | isset($ids['membership']) && $ids['membership'] | |
196 | ) { | |
197 | $autoRenewMembership = TRUE; | |
198 | } | |
199 | ||
200 | //send recurring Notification email for user | |
201 | CRM_Contribute_BAO_ContributionPage::recurringNotify($subscriptionPaymentStatus, | |
202 | $ids['contact'], | |
203 | $ids['contributionPage'], | |
204 | $recur, | |
205 | $autoRenewMembership | |
206 | ); | |
207 | } | |
208 | ||
209 | if ($txnType != 'subscr_payment') { | |
210 | return; | |
211 | } | |
212 | ||
213 | if (!$first) { | |
bc66bc9e PJ |
214 | //check if this contribution transaction is already processed |
215 | //if not create a contribution and then get it processed | |
6a488035 | 216 | $contribution = new CRM_Contribute_BAO_Contribution(); |
bc66bc9e PJ |
217 | $contribution->trxn_id = $input['trxn_id']; |
218 | if ($contribution->trxn_id && $contribution->find()) { | |
219 | CRM_Core_Error::debug_log_message("returning since contribution has already been handled"); | |
220 | echo "Success: Contribution has already been handled<p>"; | |
221 | return TRUE; | |
222 | } | |
223 | ||
6a488035 | 224 | $contribution->contact_id = $ids['contact']; |
353ffa53 | 225 | $contribution->financial_type_id = $objects['contributionType']->id; |
6a488035 TO |
226 | $contribution->contribution_page_id = $ids['contributionPage']; |
227 | $contribution->contribution_recur_id = $ids['contributionRecur']; | |
228 | $contribution->receive_date = $now; | |
229 | $contribution->currency = $objects['contribution']->currency; | |
230 | $contribution->payment_instrument_id = $objects['contribution']->payment_instrument_id; | |
231 | $contribution->amount_level = $objects['contribution']->amount_level; | |
94d1bc8d | 232 | $contribution->campaign_id = $objects['contribution']->campaign_id; |
6a488035 TO |
233 | |
234 | $objects['contribution'] = &$contribution; | |
235 | } | |
236 | ||
237 | $this->single($input, $ids, $objects, | |
238 | TRUE, $first | |
239 | ); | |
240 | } | |
241 | ||
6c786a9b EM |
242 | /** |
243 | * @param $input | |
244 | * @param $ids | |
245 | * @param $objects | |
246 | * @param bool $recur | |
247 | * @param bool $first | |
7a9ab499 EM |
248 | * |
249 | * @return bool | |
6c786a9b | 250 | */ |
8d7a9d07 | 251 | public function single( |
f9f40af3 | 252 | &$input, &$ids, &$objects, |
6a488035 TO |
253 | $recur = FALSE, |
254 | $first = FALSE | |
255 | ) { | |
256 | $contribution = &$objects['contribution']; | |
257 | ||
258 | // make sure the invoice is valid and matches what we have in the contribution record | |
259 | if ((!$recur) || ($recur && $first)) { | |
260 | if ($contribution->invoice_id != $input['invoice']) { | |
261 | CRM_Core_Error::debug_log_message("Invoice values dont match between database and IPN request"); | |
262 | echo "Failure: Invoice values dont match between database and IPN request<p>"; | |
263 | return FALSE; | |
264 | } | |
265 | } | |
266 | else { | |
267 | $contribution->invoice_id = md5(uniqid(rand(), TRUE)); | |
268 | } | |
269 | ||
270 | if (!$recur) { | |
271 | if ($contribution->total_amount != $input['amount']) { | |
272 | CRM_Core_Error::debug_log_message("Amount values dont match between database and IPN request"); | |
273 | echo "Failure: Amount values dont match between database and IPN request<p>"; | |
274 | return FALSE; | |
275 | } | |
276 | } | |
277 | else { | |
278 | $contribution->total_amount = $input['amount']; | |
279 | } | |
280 | ||
281 | $transaction = new CRM_Core_Transaction(); | |
282 | ||
6a488035 TO |
283 | $participant = &$objects['participant']; |
284 | $membership = &$objects['membership']; | |
285 | ||
286 | $status = $input['paymentStatus']; | |
287 | if ($status == 'Denied' || $status == 'Failed' || $status == 'Voided') { | |
288 | return $this->failed($objects, $transaction); | |
289 | } | |
290 | elseif ($status == 'Pending') { | |
291 | return $this->pending($objects, $transaction); | |
292 | } | |
293 | elseif ($status == 'Refunded' || $status == 'Reversed') { | |
294 | return $this->cancelled($objects, $transaction); | |
295 | } | |
296 | elseif ($status != 'Completed') { | |
297 | return $this->unhandled($objects, $transaction); | |
298 | } | |
299 | ||
300 | // check if contribution is already completed, if so we ignore this ipn | |
301 | if ($contribution->contribution_status_id == 1) { | |
302 | $transaction->commit(); | |
303 | CRM_Core_Error::debug_log_message("returning since contribution has already been handled"); | |
304 | echo "Success: Contribution has already been handled<p>"; | |
305 | return TRUE; | |
306 | } | |
307 | ||
308 | $this->completeTransaction($input, $ids, $objects, $transaction, $recur); | |
309 | } | |
310 | ||
2e2605fe EM |
311 | /** |
312 | * Main function. | |
313 | * | |
314 | * @return bool | |
315 | */ | |
00be9182 | 316 | public function main() { |
6a488035 TO |
317 | |
318 | $objects = $ids = $input = array(); | |
f78c9258 | 319 | $component = $this->retrieve('module', 'String'); |
6a488035 TO |
320 | $input['component'] = $component; |
321 | ||
f78c9258 | 322 | $ids['contact'] = $this->retrieve('contactID', 'Integer', TRUE); |
323 | $ids['contribution'] = $this->retrieve('contributionID', 'Integer', TRUE); | |
6a488035 TO |
324 | |
325 | $this->getInput($input, $ids); | |
326 | ||
327 | if ($component == 'event') { | |
f78c9258 | 328 | $ids['event'] = $this->retrieve('eventID', 'Integer', TRUE); |
329 | $ids['participant'] = $this->retrieve('participantID', 'Integer', TRUE); | |
6a488035 TO |
330 | } |
331 | else { | |
332 | // get the optional ids | |
f78c9258 | 333 | $ids['membership'] = $this->retrieve('membershipID', 'Integer', FALSE); |
334 | $ids['contributionRecur'] = $this->retrieve('contributionRecurID', 'Integer', FALSE); | |
335 | $ids['contributionPage'] = $this->retrieve('contributionPageID', 'Integer', FALSE); | |
336 | $ids['related_contact'] = $this->retrieve('relatedContactID', 'Integer', FALSE); | |
337 | $ids['onbehalf_dupe_alert'] = $this->retrieve('onBehalfDupeAlert', 'Integer', FALSE); | |
6a488035 TO |
338 | } |
339 | ||
b1dc9447 | 340 | $paymentProcessorID = $this->retrieve('processor_id', 'Integer', FALSE); |
d8e1e2fb | 341 | if (empty($paymentProcessorID)) { |
b1dc9447 | 342 | $processorParams = array( |
343 | 'user_name' => $this->retrieve('receiver_email', 'String', FALSE), | |
344 | 'payment_processor_type_id' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', 'PayPal_Standard', 'id', 'name'), | |
345 | 'is_test' => empty($input['is_test']) ? 0 : 1, | |
346 | ); | |
f78c9258 | 347 | |
b1dc9447 | 348 | $processorInfo = array(); |
349 | if (!CRM_Financial_BAO_PaymentProcessor::retrieve($processorParams, $processorInfo)) { | |
350 | return FALSE; | |
351 | } | |
352 | $paymentProcessorID = $processorInfo['id']; | |
a595a8f9 | 353 | } |
6a488035 | 354 | |
b1dc9447 | 355 | if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) { |
6a488035 TO |
356 | return FALSE; |
357 | } | |
358 | ||
359 | self::$_paymentProcessor = &$objects['paymentProcessor']; | |
360 | if ($component == 'contribute') { | |
361 | if ($ids['contributionRecur']) { | |
362 | // check if first contribution is completed, else complete first contribution | |
363 | $first = TRUE; | |
364 | if ($objects['contribution']->contribution_status_id == 1) { | |
365 | $first = FALSE; | |
366 | } | |
367 | return $this->recur($input, $ids, $objects, $first); | |
368 | } | |
369 | else { | |
370 | return $this->single($input, $ids, $objects, FALSE, FALSE); | |
371 | } | |
372 | } | |
373 | else { | |
374 | return $this->single($input, $ids, $objects, FALSE, FALSE); | |
375 | } | |
376 | } | |
377 | ||
6c786a9b EM |
378 | /** |
379 | * @param $input | |
380 | * @param $ids | |
7a9ab499 EM |
381 | * |
382 | * @return bool | |
6c786a9b | 383 | */ |
00be9182 | 384 | public function getInput(&$input, &$ids) { |
6a488035 TO |
385 | if (!$this->getBillingID($ids)) { |
386 | return FALSE; | |
387 | } | |
388 | ||
f78c9258 | 389 | $input['txnType'] = $this->retrieve('txn_type', 'String', FALSE); |
390 | $input['paymentStatus'] = $this->retrieve('payment_status', 'String', FALSE); | |
391 | $input['invoice'] = $this->retrieve('invoice', 'String', TRUE); | |
392 | $input['amount'] = $this->retrieve('mc_gross', 'Money', FALSE); | |
393 | $input['reasonCode'] = $this->retrieve('ReasonCode', 'String', FALSE); | |
6a488035 TO |
394 | |
395 | $billingID = $ids['billing']; | |
396 | $lookup = array( | |
397 | "first_name" => 'first_name', | |
398 | "last_name" => 'last_name', | |
399 | "street_address-{$billingID}" => 'address_street', | |
400 | "city-{$billingID}" => 'address_city', | |
401 | "state-{$billingID}" => 'address_state', | |
402 | "postal_code-{$billingID}" => 'address_zip', | |
403 | "country-{$billingID}" => 'address_country_code', | |
404 | ); | |
405 | foreach ($lookup as $name => $paypalName) { | |
f78c9258 | 406 | $value = $this->retrieve($paypalName, 'String', FALSE); |
6a488035 TO |
407 | $input[$name] = $value ? $value : NULL; |
408 | } | |
409 | ||
f78c9258 | 410 | $input['is_test'] = $this->retrieve('test_ipn', 'Integer', FALSE); |
411 | $input['fee_amount'] = $this->retrieve('mc_fee', 'Money', FALSE); | |
412 | $input['net_amount'] = $this->retrieve('settle_amount', 'Money', FALSE); | |
413 | $input['trxn_id'] = $this->retrieve('txn_id', 'String', FALSE); | |
6a488035 | 414 | } |
96025800 | 415 | |
6a488035 | 416 | } |