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