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