3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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) {
55 $this->setInputParameters($inputData);
56 parent
::__construct();
66 public function retrieve($name, $type, $abort = TRUE) {
68 $value = CRM_Utils_Type
::validate(
69 CRM_Utils_Array
::value($name, $this->_inputParameters
),
73 if ($abort && $value === NULL) {
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');
89 public function recur(&$input, &$ids, &$objects, $first) {
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>";
96 if ($input['txnType'] == 'subscr_payment' &&
97 $input['paymentStatus'] != 'Completed'
99 CRM_Core_Error
::debug_log_message("Ignore all IPN payments that are not completed");
100 echo "Failure: Invalid parameters<p>";
104 $recur = &$objects['contributionRecur'];
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>";
114 $now = date('YmdHis');
116 // fix dates that already exist
117 $dates = array('create', 'start', 'end', 'cancel', 'modified');
118 foreach ($dates as $date) {
119 $name = "{$date}_date";
121 $recur->$name = CRM_Utils_Date
::isoToMysql($recur->$name);
124 $sendNotification = FALSE;
125 $subscriptionPaymentStatus = NULL;
126 //set transaction type
127 $txnType = $this->retrieve('txn_type', 'String');
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'
136 if ($statusID != 5) {
137 $recur->contribution_status_id
= 2;
139 $recur->processor_id
= $this->retrieve('subscr_id', 'String');
140 $recur->trxn_id
= $recur->processor_id
;
141 $sendNotification = TRUE;
142 $subscriptionPaymentStatus = CRM_Core_Payment
::RECURRING_PAYMENT_START
;
146 if ($recur->contribution_status_id
!= 3) {
147 $recur->contribution_status_id
= 1;
149 $recur->end_date
= $now;
150 $sendNotification = TRUE;
151 $subscriptionPaymentStatus = CRM_Core_Payment
::RECURRING_PAYMENT_END
;
154 case 'subscr_cancel':
155 $recur->contribution_status_id
= 3;
156 $recur->cancel_date
= $now;
159 case 'subscr_failed':
160 $recur->contribution_status_id
= 4;
161 $recur->modified_date
= $now;
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>";
169 case 'subscr_payment':
171 $recur->start_date
= $now;
174 $recur->modified_date
= $now;
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;
187 if ($sendNotification) {
189 $autoRenewMembership = FALSE;
191 isset($ids['membership']) && $ids['membership']
193 $autoRenewMembership = TRUE;
196 //send recurring Notification email for user
197 CRM_Contribute_BAO_ContributionPage
::recurringNotify($subscriptionPaymentStatus,
199 $ids['contributionPage'],
205 if ($txnType != 'subscr_payment') {
210 //check if this contribution transaction is already processed
211 //if not create a contribution and then get it processed
212 $contribution = new CRM_Contribute_BAO_Contribution();
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>";
220 $contribution->contact_id
= $ids['contact'];
221 $contribution->financial_type_id
= $objects['contributionType']->id
;
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
;
228 $contribution->campaign_id
= $objects['contribution']->campaign_id
;
230 $objects['contribution'] = &$contribution;
233 $this->single($input, $ids, $objects,
247 public function single(
248 &$input, &$ids, &$objects,
252 $contribution = &$objects['contribution'];
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>";
263 $contribution->invoice_id
= md5(uniqid(rand(), TRUE));
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>";
274 $contribution->total_amount
= $input['amount'];
277 $transaction = new CRM_Core_Transaction();
279 $participant = &$objects['participant'];
280 $membership = &$objects['membership'];
282 $status = $input['paymentStatus'];
283 if ($status == 'Denied' ||
$status == 'Failed' ||
$status == 'Voided') {
284 return $this->failed($objects, $transaction);
286 elseif ($status == 'Pending') {
287 return $this->pending($objects, $transaction);
289 elseif ($status == 'Refunded' ||
$status == 'Reversed') {
290 return $this->cancelled($objects, $transaction);
292 elseif ($status != 'Completed') {
293 return $this->unhandled($objects, $transaction);
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>";
304 $this->completeTransaction($input, $ids, $objects, $transaction, $recur);
312 public function main() {
314 $objects = $ids = $input = array();
315 $component = $this->retrieve('module', 'String');
316 $input['component'] = $component;
318 $ids['contact'] = $this->retrieve('contactID', 'Integer', TRUE);
319 $ids['contribution'] = $this->retrieve('contributionID', 'Integer', TRUE);
321 $this->getInput($input, $ids);
323 if ($component == 'event') {
324 $ids['event'] = $this->retrieve('eventID', 'Integer', TRUE);
325 $ids['participant'] = $this->retrieve('participantID', 'Integer', TRUE);
328 // get the optional ids
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);
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,
344 $processorInfo = array();
345 if (!CRM_Financial_BAO_PaymentProcessor
::retrieve($processorParams, $processorInfo)) {
348 $paymentProcessorID = $processorInfo['id'];
351 if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) {
355 self
::$_paymentProcessor = &$objects['paymentProcessor'];
356 if ($component == 'contribute') {
357 if ($ids['contributionRecur']) {
358 // check if first contribution is completed, else complete first contribution
360 if ($objects['contribution']->contribution_status_id
== 1) {
363 return $this->recur($input, $ids, $objects, $first);
366 return $this->single($input, $ids, $objects, FALSE, FALSE);
370 return $this->single($input, $ids, $objects, FALSE, FALSE);
380 public function getInput(&$input, &$ids) {
381 if (!$this->getBillingID($ids)) {
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);
391 $billingID = $ids['billing'];
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',
401 foreach ($lookup as $name => $paypalName) {
402 $value = $this->retrieve($paypalName, 'String', FALSE);
403 $input[$name] = $value ?
$value : NULL;
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);