Reverted autocancel feature, moved to a branch
[tc-ipn-receiver.git] / trustcommerceIPN.php
index 532c70aad9f84b7dad646b80d5b2ef24bb3be76d..a2e5fc6924bd33e599609609ff32aff02dffce41 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with CiviCRM.  If not, see <http://www.gnu.org/licenses/>.
  *
- * Copyright 2016, Lisa Marie Maginnis <lisa@fsf.org> (http://www.fsf.org)
+ * Copyright (C) 2012
+ * Licensed to CiviCRM under the GPL v3 or higher
+ *
+ * Modified by Lisa Marie Maginnis <lisa@fsf.org> (http://www.fsf.org)
  *
  */
 
-/**
-  * CiviCRM (Instant Payment Notification) IPN processor module for
-  * TrustCommerece.
-  *
-  * For full documentation on the 
-  * TrustCommerece API, please see the TCDevGuide for more information:
-  * https://vault.trustcommerce.com/downloads/TCDevGuide.htm
-  *
-  * This module supports the following features: Single credit/debit card
-  * transactions, AVS checking, recurring (create, update, and cancel
-  * subscription) optional blacklist with fail2ban,
-  *
-  * @copyright Lisa Marie Maginnis <lisa@fsf.org> (http://www.fsf.org)
-  * @version   1.0
-  * @package   org.fsf.payment.trustcommerce.ipn
-  */
-
-define("MAX_FAILURES", 4)
-
 class CRM_Core_Payment_trustcommerce_IPN extends CRM_Core_Payment_BaseIPN {
-
-  /**
-   * Inheret  
-   *
-   * @return void
-   */
   function __construct() {
     parent::__construct();
   }
 
-  function getLastFailures($recur_id) {
-    $sql="SELECT contribution_recur_id, trxn_id, contribution_status_id,
-             SUM(id > COALESCE(
-                ( SELECT max(id) FROM civicrm_contribution AS c2
-                   WHERE c2.id = c.id and c2.contribution_status_id = 1),
-                4 )) AS numfails
-              FROM civicrm_contribution AS c
-              WHERE c.contribution_recur_id = $recur_id GROUP BY c.contribution_recur_id;"
-
-    $result = CRM_Core_DAO::executeQuery($sql);
-    if($result->fetch()) {
-      $failures = $result->numfails;
-    } else {
-      $failures = NULL;
-    }
-
-    return $failures;
-
-  }
 
   function main($component = 'contribute') {
   static $no = NULL;
@@ -135,7 +94,7 @@ class CRM_Core_Payment_trustcommerce_IPN extends CRM_Core_Payment_BaseIPN {
 
   protected function checkDuplicate($input, $ids) {
 //    $sql='select id from civicrm_contribution where receive_date like \''.$input['date'].'%\' and total_amount='.$input['amount'].' and contact_id='.$ids['contact'].' and contribution_status_id =  1 limit 1';
-    $sql="select id from civicrm_contribution where trxn_id = '".$ids['trxn_id']."'";
+    $sql="select id from civicrm_contribution where trxn_id = '".$ids['trxn_id']."' and contribution_status_id != 2";
 
 
     $result = CRM_Core_DAO::executeQuery($sql);
@@ -244,12 +203,9 @@ class CRM_Core_Payment_trustcommerce_IPN extends CRM_Core_Payment_BaseIPN {
       echo $msg;
       CRM_Core_Error::debug_log_message($msg);
 
-      /* Action for repeated failures */
-      if(MAX_FAILURES <= $this->getLastFailures($ids['contributionRecur'])) {
-       $this->disableAutoRenew(($ids['contributionRecur']));
-      }
-      
-      return $this->failed($objects, $transaction);
+      /* Disable cancelling transactions */
+      $input['skipComponentSync'] = 1;
+      return $this->failed($objects, $transaction, $input);
     }
 
     if ($sendNotification) {
@@ -283,7 +239,7 @@ class CRM_Core_Payment_trustcommerce_IPN extends CRM_Core_Payment_BaseIPN {
 
     if (!$ids['contributionRecur']) {
       CRM_Core_Error::debug_log_message("Could not find billingid: ".$billingid);
-      echo "Failure: Could not find contributionRecur<p>\n";
+      echo "Failure: Could not find contributionRecur: $billingid <p>\n";
       exit();
     }