3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 class CRM_Core_Payment_PayPalIPN
extends CRM_Core_Payment_BaseIPN
{
37 static $_paymentProcessor = NULL;
40 * Input parameters from payment processor. Store these so that
41 * the code does not need to keep retrieving from the http request
44 protected $_inputParameters = array();
47 * Constructor function.
49 * @param array $inputData
50 * Contents of HTTP REQUEST.
52 * @throws CRM_Core_Exception
54 public function __construct($inputData) {
56 $params = (!empty($inputData['custom'])) ?
57 array_merge($inputData, json_decode($inputData['custom'], TRUE)) :
59 $this->setInputParameters($params);
60 parent
::__construct();
70 public function retrieve($name, $type, $abort = TRUE) {
72 $value = CRM_Utils_Type
::validate(
73 CRM_Utils_Array
::value($name, $this->_inputParameters
),
77 if ($abort && $value === NULL) {
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');
93 public function recur(&$input, &$ids, &$objects, $first) {
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>";
100 if ($input['txnType'] == 'subscr_payment' &&
101 $input['paymentStatus'] != 'Completed'
103 CRM_Core_Error
::debug_log_message("Ignore all IPN payments that are not completed");
104 echo "Failure: Invalid parameters<p>";
108 $recur = &$objects['contributionRecur'];
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>";
118 $now = date('YmdHis');
120 // fix dates that already exist
121 $dates = array('create', 'start', 'end', 'cancel', 'modified');
122 foreach ($dates as $date) {
123 $name = "{$date}_date";
125 $recur->$name = CRM_Utils_Date
::isoToMysql($recur->$name);
128 $sendNotification = FALSE;
129 $subscriptionPaymentStatus = NULL;
130 //set transaction type
131 $txnType = $this->retrieve('txn_type', 'String');
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'
140 if ($statusID != 5) {
141 $recur->contribution_status_id
= 2;
143 $recur->processor_id
= $this->retrieve('subscr_id', 'String');
144 $recur->trxn_id
= $recur->processor_id
;
145 $sendNotification = TRUE;
146 $subscriptionPaymentStatus = CRM_Core_Payment
::RECURRING_PAYMENT_START
;
150 if ($recur->contribution_status_id
!= 3) {
151 $recur->contribution_status_id
= 1;
153 $recur->end_date
= $now;
154 $sendNotification = TRUE;
155 $subscriptionPaymentStatus = CRM_Core_Payment
::RECURRING_PAYMENT_END
;
158 case 'subscr_cancel':
159 $recur->contribution_status_id
= 3;
160 $recur->cancel_date
= $now;
163 case 'subscr_failed':
164 $recur->contribution_status_id
= 4;
165 $recur->modified_date
= $now;
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>";
173 case 'subscr_payment':
175 $recur->start_date
= $now;
178 $recur->modified_date
= $now;
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;
191 if ($sendNotification) {
193 $autoRenewMembership = FALSE;
195 isset($ids['membership']) && $ids['membership']
197 $autoRenewMembership = TRUE;
200 //send recurring Notification email for user
201 CRM_Contribute_BAO_ContributionPage
::recurringNotify($subscriptionPaymentStatus,
203 $ids['contributionPage'],
209 if ($txnType != 'subscr_payment') {
214 //check if this contribution transaction is already processed
215 //if not create a contribution and then get it processed
216 $contribution = new CRM_Contribute_BAO_Contribution();
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>";
224 $contribution->contact_id
= $ids['contact'];
225 $contribution->financial_type_id
= $objects['contributionType']->id
;
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
;
232 $contribution->campaign_id
= $objects['contribution']->campaign_id
;
234 $objects['contribution'] = &$contribution;
237 $this->single($input, $ids, $objects,
251 public function single(
252 &$input, &$ids, &$objects,
256 $contribution = &$objects['contribution'];
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>";
267 $contribution->invoice_id
= md5(uniqid(rand(), TRUE));
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>";
278 $contribution->total_amount
= $input['amount'];
281 $transaction = new CRM_Core_Transaction();
283 $participant = &$objects['participant'];
284 $membership = &$objects['membership'];
286 $status = $input['paymentStatus'];
287 if ($status == 'Denied' ||
$status == 'Failed' ||
$status == 'Voided') {
288 return $this->failed($objects, $transaction);
290 elseif ($status == 'Pending') {
291 return $this->pending($objects, $transaction);
293 elseif ($status == 'Refunded' ||
$status == 'Reversed') {
294 return $this->cancelled($objects, $transaction);
296 elseif ($status != 'Completed') {
297 return $this->unhandled($objects, $transaction);
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>";
308 $this->completeTransaction($input, $ids, $objects, $transaction, $recur);
316 public function main() {
318 $objects = $ids = $input = array();
319 $component = $this->retrieve('module', 'String');
320 $input['component'] = $component;
322 $ids['contact'] = $this->retrieve('contactID', 'Integer', TRUE);
323 $ids['contribution'] = $this->retrieve('contributionID', 'Integer', TRUE);
325 $this->getInput($input, $ids);
327 if ($component == 'event') {
328 $ids['event'] = $this->retrieve('eventID', 'Integer', TRUE);
329 $ids['participant'] = $this->retrieve('participantID', 'Integer', TRUE);
332 // get the optional ids
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);
340 $paymentProcessorID = $this->retrieve('processor_id', 'Integer', FALSE);
341 if (empty($paymentProcessorID)) {
342 $processorParams = array(
343 'user_name' => $this->retrieve('business', '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,
348 $processorInfo = array();
349 if (!CRM_Financial_BAO_PaymentProcessor
::retrieve($processorParams, $processorInfo)) {
352 $paymentProcessorID = $processorInfo['id'];
355 if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) {
359 self
::$_paymentProcessor = &$objects['paymentProcessor'];
360 if ($component == 'contribute') {
361 if ($ids['contributionRecur']) {
362 // check if first contribution is completed, else complete first contribution
364 if ($objects['contribution']->contribution_status_id
== 1) {
367 return $this->recur($input, $ids, $objects, $first);
370 return $this->single($input, $ids, $objects, FALSE, FALSE);
374 return $this->single($input, $ids, $objects, FALSE, FALSE);
384 public function getInput(&$input, &$ids) {
385 if (!$this->getBillingID($ids)) {
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);
395 $billingID = $ids['billing'];
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',
405 foreach ($lookup as $name => $paypalName) {
406 $value = $this->retrieve($paypalName, 'String', FALSE);
407 $input[$name] = $value ?
$value : NULL;
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);