merged changes
[tc-ipn-receiver.git] / trustcommerceIPN.php
index a2e5fc6924bd33e599609609ff32aff02dffce41..28ce87bca51a0f882918c88dce6e1e7798d66aaa 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 (C) 2012
- * Licensed to CiviCRM under the GPL v3 or higher
- *
- * Modified by Lisa Marie Maginnis <lisa@fsf.org> (http://www.fsf.org)
+ * Copyright 2016, 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=<<<EOF
+SELECT count(*) as numfails
+   FROM civicrm_contribution
+   WHERE contribution_recur_id = $recur_id
+    AND
+    id > (SELECT MAX(id) FROM civicrm_contribution WHERE contribution_recur_id = $recur_id AND contribution_status_id = 1);
+EOF;
+
+    $result = CRM_Core_DAO::executeQuery($sql);
+    if($result->fetch()) {
+      $failures = $result->numfails;
+    } else {
+      $failures = NULL;
+    }
+
+    return $failures;
+
+  }
 
   function main($component = 'contribute') {
   static $no = NULL;
@@ -55,7 +96,7 @@ class CRM_Core_Payment_trustcommerce_IPN extends CRM_Core_Payment_BaseIPN {
        $ids['trxn_id'] = $input['trxn_id'];
 
        if($this->checkDuplicate($input, $ids) != NULL) {
-        $msg = 'TrustCommerceIPN: Skipping duplicate contribution: '.$ids['contribution'].' for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id']."\n";
+        $msg = 'TrustCommerceIPN: Skipping duplicate contribution: for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id']."\n";
         echo $msg;
         CRM_Core_Error::debug_log_message($msg);
         exit;
@@ -87,10 +128,48 @@ class CRM_Core_Payment_trustcommerce_IPN extends CRM_Core_Payment_BaseIPN {
        
        return $this->processRecur($input, $ids, $objects, $first);
        
+      }
+
     }
+  }
+
+  protected function disableAutorenew($recur_id) {
+    /* Load payment processor object */
+    // HARD CODED
+    $msg = 'TrustCommerceIPN: MAX_FAILURES hit, unstoring billing ID: '.$recur_id."\n";
+
+    CRM_Core_Error::debug_log_message($msg);
+    echo $msg;
+
+    $sql = "SELECT user_name, password, url_site FROM civicrm_payment_processor WHERE id =  8 LIMIT 1";
+
+    $result = CRM_Core_DAO::executeQuery($sql);
+    if($result->fetch()) {
+      $request = array(
+                     'custid' => $result->user_name,
+                     'password' => $result->password,
+                     'action' => 'unstore',
+                     'billingid' => $recur_id
+                     );
+
+      $update = 'UPDATE civicrm_contribution_recur SET contribution_status_id = 3 WHERE processor_id = "'.$recur_id.'";';
+    $result1 = CRM_Core_DAO::executeQuery($update);
+
+      $tc = tclink_send($request);
+      if(!$tc) {
+       return -1;
+      }
+
+      return TRUE;
+      
+    } else {
+      echo 'CRITICAL ERROR: Could not load payment processor object';
+      return;
+    }
+
+    
 
   }
-}
 
   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';
@@ -185,7 +264,7 @@ class CRM_Core_Payment_trustcommerce_IPN extends CRM_Core_Payment_BaseIPN {
       $recur->contribution_status_id = array_search($statusName, $contributionStatus);
       $recur->save();
       $input['is_test'] = 0;
-      $msg = 'TrustCommerceIPN: Created contribution: '.$ids['contribution'].' for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id'].' status: Completed'."\n";
+      $msg = 'TrustCommerceIPN: Created contribution: for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id'].' status: Completed'."\n";
       echo $msg;
       CRM_Core_Error::debug_log_message($msg);
 
@@ -199,13 +278,20 @@ class CRM_Core_Payment_trustcommerce_IPN extends CRM_Core_Payment_BaseIPN {
       $recur->cancel_date = $now;
       $recur->save();
       
-      $msg = 'TrustCommerceIPN: Created contribution: '.$ids['contribution'].' for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id'].' status: Failed'."\n";
+      $msg = 'TrustCommerceIPN: Created contribution: for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id'].' status: Failed'."\n";
       echo $msg;
       CRM_Core_Error::debug_log_message($msg);
 
       /* Disable cancelling transactions */
       $input['skipComponentSync'] = 1;
-      return $this->failed($objects, $transaction, $input);
+
+      /* Action for repeated failures */
+      if(MAX_FAILURES <= $this->getLastFailures($ids['contributionRecur'])) {
+       //$this->disableAutoRenew(($ids['contributionRecur']));
+       $this->disableAutorenew($ids['processor_id']);
+      }
+      
+      return $this->failed($objects, $transaction);
     }
 
     if ($sendNotification) {
@@ -236,6 +322,7 @@ class CRM_Core_Payment_trustcommerce_IPN extends CRM_Core_Payment_BaseIPN {
     $ids['contribution'] = $result->coid;
     $ids['contributionRecur'] = $result->id;
     $ids['contact'] = $result->contact_id;
+    $ids['processor_id'] = $billingid;
 
     if (!$ids['contributionRecur']) {
       CRM_Core_Error::debug_log_message("Could not find billingid: ".$billingid);