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