Use stored credentials because they may not be present in params.
[trustcommerce.git] / trustcommerce.php
index 560cae34ff344d1741ee017907c0ea6b06d1031f..d36e42b22cc33fdae944a15eada67c7d2953df9a 100644 (file)
@@ -20,6 +20,7 @@
  *
  * Written and contributed by Ward Vandewege <ward@fsf.org> (http://www.fsf.org)
  * Modified by Lisa Marie Maginnis <lisa@fsf.org> (http://www.fsf.org)
+ * Copyright © 2015 David Thompson <davet@gnu.org>
  *
  */
 
@@ -110,8 +111,9 @@ class org_fsf_payment_trustcommerce extends CRM_Core_Payment {
     $tc_params = $this->_getTrustCommerceFields();
 
     /* Are we recurring? If so add the extra API fields. */
-    if (CRM_Utils_Array::value('is_recur', $params) && $params['contributionRecurID']) {
+    if (CRM_Utils_Array::value('is_recur', $params) == 1) {
       $tc_params = $this->_getRecurPaymentFields($tc_params);
+      $recur=1;
     }
 
     /* Pass our cooked params to the alter hook, per Core/Payment/Dummy.php */
@@ -141,9 +143,14 @@ class org_fsf_payment_trustcommerce extends CRM_Core_Payment {
        * Save the transaction ID
        */
 
-      if (CRM_Utils_Array::value('is_recur', $params) && $params['contributionRecurID']) {
-       $params['contributionRecurID'] = $reply['billingid'];
-      } 
+      if (array_key_exists('billingid', $reply)) {
+        $params['recurr_profile_id'] = $reply['billingid'];
+        CRM_Core_DAO::setFieldValue(
+          'CRM_Contribute_DAO_ContributionRecur',
+          $this->_getParam('contributionRecurID'),
+          'processor_id', $reply['billingid']
+        );
+      }
       $params['trxn_id'] = $reply['transid'];
 
       $params['gross_amount'] = $tc_params['amount'] / 100;
@@ -156,12 +163,66 @@ class org_fsf_payment_trustcommerce extends CRM_Core_Payment {
     }
   }
 
+  /**
+   * Update CC info for a recurring contribution
+   */
+  function updateSubscriptionBillingInfo(&$message = '', $params = array()) {
+    $expYear = $params['credit_card_exp_date']['Y'];
+    $expMonth = $params['credit_card_exp_date']['M'];
+
+    $tc_params = array(
+      'custid' => $this->_paymentProcessor['user_name'],
+      'password' => $this->_paymentProcessor['password'],
+      'action' => 'store',
+      'billingid' => $params['subscriptionId'],
+      'avs' => 'y', // Enable address verification
+      'address1' => $params['street_address'],
+      'zip' => $params['postal_code'],
+      'name' => $this->_formatBillingName($params['first_name'],
+                                          $params['last_name']),
+      'cc' => $params['credit_card_number'],
+      'cvv' => $params['cvv2'],
+      'exp' => $this->_formatExpirationDate($expYear, $expMonth),
+      'amount' => $this->_formatAmount($params['amount']),
+    );
+
+    CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $tc_params);
+
+    $reply = $this->_sendTCRequest($tc_params);
+    $result = $this->_getTCReply($reply);
+
+    if($result === 0) {
+      $message = 'Successfully updated TC billing id ' . $tc_params['billingid'];
+
+      return TRUE;
+    } else {
+      return FALSE;
+    }
+  }
+
+  // TODO: Use the formatting functions throughout the entire class to
+  // dedupe the conversions done elsewhere in a less reusable way.
+  function _formatAmount($amount) {
+    return $amount * 100;
+  }
+
+  function _formatBillingName($firstName, $lastName) {
+    return "$firstName $lastName";
+  }
+
+  function _formatExpirationDate($year, $month) {
+    $exp_month = str_pad($month, 2, '0', STR_PAD_LEFT);
+    $exp_year = substr($year, -2);
+
+    return "$exp_month$exp_year";
+  }
+
   function _isBlacklisted() {
     if($this->_isIPBlacklisted()) {
       return TRUE;
     } else if($this->_IsAgentBlacklisted()) {
       return TRUE;
-    } 
+    }
     return FALSE;
   }
 
@@ -243,7 +304,7 @@ class org_fsf_payment_trustcommerce extends CRM_Core_Payment {
       $cycle = 'y';
       break;
     }
-    
+
     /* Translate frequency interval from CiviCRM -> TC
      * Payments are the same, HOWEVER a payment of 1 (forever) should be 0 in TC */
     if($payments == 1) {
@@ -253,6 +314,7 @@ class org_fsf_payment_trustcommerce extends CRM_Core_Payment {
     $fields['cycle'] = '1'.$cycle;   /* The billing cycle in years, months, weeks, or days. */
     $fields['payments'] = $payments;
     $fields['authnow'] = 'y';
+    $fields['start'] = date("Y-m-d"); /* Start date is required when 'authnow' is used. */
     $fields['action'] = 'store';      /* Change our mode to `store' mode. */
 
     return $fields;
@@ -279,7 +341,7 @@ class org_fsf_payment_trustcommerce extends CRM_Core_Payment {
       // It's all good
       break;
     case self::AUTH_DECLINED:
-      // TODO FIXME be more or less specific? 
+      // TODO FIXME be more or less specific?
       // declinetype can be: decline, avs, cvv, call, expiredcard, carderror, authexpired, fraud, blacklist, velocity
       // See TC documentation for more info
       switch($reply['declinetype']) {
@@ -425,7 +487,7 @@ class org_fsf_payment_trustcommerce extends CRM_Core_Payment {
     $tc_params['password'] = $this->_getParam('password');
     $tc_params['action'] = 'unstore';
     $tc_params['billingid'] = CRM_Utils_Array::value('trxn_id', $params);
-    
+
     $result = $this->_sendTCRequest($tc_params);
 
     /* Test if call failed */
@@ -437,8 +499,8 @@ class org_fsf_payment_trustcommerce extends CRM_Core_Payment {
   }
 
   function changeSubscriptionAmount(&$message = '', $params = array()) {
-    $tc_params['custid'] = $this->_getParam('user_name');
-    $tc_params['password'] = $this->_getParam('password');
+    $tc_params['custid'] = $this->_paymentProcessor['user_name'];
+    $tc_params['password'] = $this->_paymentProcessor['password'];
     $tc_params['action'] = 'store';
 
     $tc_params['billingid'] = CRM_Utils_Array::value('subscriptionId', $params);