Cleaned up formatting.
[trustcommerce.git] / trustcommerce.php
index 586a8050f07738c65267dfc7f5bfcd5996edbaac..8731fc78409b3a952c2018c08fb31f37a11dde36 100644 (file)
@@ -4,6 +4,7 @@
  * Licensed to CiviCRM under the GPL v3 or higher
  *
  * 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)
  *
  */
 
@@ -73,11 +74,9 @@ class org_fsf_payment_trustcommerce extends CRM_Core_Payment {
   }
 
   /**
-   * Submit a payment using Advanced Integration Method
-   *
-   * @param  array $params assoc array of input parameters for this transaction
-   *
-   * @return array the result in a nice formatted array (or an error object)
+   * Submit a payment using the TC API
+   * @param  array $params The params we will be sending to tclink_send()
+   * @return mixed An array of our results, or an error object if the transaction fails.
    * @public
    */
   function doDirectPayment(&$params) {
@@ -85,90 +84,60 @@ class org_fsf_payment_trustcommerce extends CRM_Core_Payment {
       return self::error(9001, 'TrustCommerce requires that the tclink module is loaded');
     }
 
-    $newParams = $params;
-    if (CRM_Utils_Array::value('is_recur', $params) &&
-      $params['contributionRecurID']
-    ) {
-      CRM_Utils_Hook::alterPaymentProcessorParams($this,
-        $params,
-        $newParams
-      );
-    }
-    foreach ($newParams as $field => $value) {
+    /* Copy our paramaters to ourself */
+    foreach ($params as $field => $value) {
       $this->_setParam($field, $value);
     }
 
-    if (CRM_Utils_Array::value('is_recur', $params) &&
-      $params['contributionRecurID']
-    ) {
-      return $this->doRecurPayment($params);
-    }
+    /* Get our fields to pass to tclink_send() */
+    $tc_params = $this->_getTrustCommerceFields();
 
-    $postFields = array();
-    $tclink = $this->_getTrustCommerceFields();
+    /* Are we recurring? If so add the extra API fields. */
+    if (CRM_Utils_Array::value('is_recur', $params) && $params['contributionRecurID']) {
+      $tc_params = $this->_getRecurPaymentFields($tc_params);
+    }
 
-    // Set up our call for hook_civicrm_paymentProcessor,
-    // since we now have our parameters as assigned for the AIM back end.
-    CRM_Utils_Hook::alterPaymentProcessorParams($this,
-      $params,
-      $tclink
-    );
+    /* Pass our cooked params to the alter hook, per Core/Payment/Dummy.php */
+    CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $tc_params);
 
     // TrustCommerce will not refuse duplicates, so we should check if the user already submitted this transaction
-    if ($this->_checkDupe($tclink['ticket'])) {
+    if ($this->_checkDupe($tc_params['ticket'])) {
       return self::error(9004, 'It appears that this transaction is a duplicate.  Have you already submitted the form once?  If so there may have been a connection problem. You can try your transaction again.  If you continue to have problems please contact the site administrator.');
     }
 
-    $result = tclink_send($tclink);
+    /* Call the TC API, and grab the reply */
+    $reply = tclink_send($tc_params);
 
-    if (!$result) {
-      return self::error(9002, 'Could not initiate connection to payment gateway');
-    }
-
-    foreach ($result as $field => $value) {
-      error_log("result: $field => $value");
-    }
+    /* Parse our reply */
+    $result = $this->_getTrustCommerceReply($reply);
 
-    switch($result['status']) {
-    case self::AUTH_APPROVED:
-      // It's all good
-      break;
-    case self::AUTH_DECLINED:
-      // 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
-      return self::error(9009, "Your transaction was declined: {$result['declinetype']}");
-      break;
-    case self::AUTH_BADDATA:
-      // TODO FIXME do something with $result['error'] and $result['offender']
-      return self::error(9011, "Invalid credit card information. Please re-enter.");
-      break;
-    case self::AUTH_ERROR:
-      return self::error(9002, 'Could not initiate connection to payment gateway');
-      break;
-    }
-    
-    // Success
+    if($result == 0) {
+      /* We were successful, congrats. Lets wrap it up:
+       * Convert back to dollars
+       * Save the transaction ID
+       */
+      $params['trxn_id'] = $reply['transid'];
+      $params['gross_amount'] = $tc_params['amount'] / 100;
 
-    $params['trxn_id'] = $result['transid'];
-    $params['gross_amount'] = $tclink['amount'] / 100;
+      return $params;
 
-    return $params;
+    } else {
+      /* Otherwise we return the error object */
+      return $result;
+    }
   }
 
   /**
-   * Submit an Automated Recurring Billing subscription
-   *
-   * @param  array $params assoc array of input parameters for this transaction
-   *
-   * @return array the result in a nice formatted array (or an error object)
+   * Gets the recurring billing fields for the TC API
+   * @param  array $fields The fields to modify.
+   * @return array The fields for tclink_send(), modified for recurring billing.
    * @public
    */
-  function doRecurPayment(&$params) {
+  function _getRecurPaymentFields($fields) {
     $payments = $this->_getParam('frequency_interval');
     $cycle = $this->_getParam('frequency_unit');
 
-    /* Sort out our billing scheme */
+    /* Translate billing cycle from CiviCRM -> TC */
     switch($cycle) {
     case 'day':
       $cycle = 'd';
@@ -182,58 +151,33 @@ class org_fsf_payment_trustcommerce extends CRM_Core_Payment {
     case 'year':
       $cycle = 'y';
       break;
-    default:
-      return self::error(9001, 'Payment interval not set! Unable to process payment.');
-      break;
     }
-
-
-    $params['authnow'] = 'y';    /* Process this payment `now' */    
-    $params['cycle'] = $cycle;   /* The billing cycle in years, months, weeks, or days. */
-    $params['payments'] = $payments;
-
-
-    $tclink = $this->_getTrustCommerceFields();
-
-    // Set up our call for hook_civicrm_paymentProcessor,
-    // since we now have our parameters as assigned for the AIM back end.
-    CRM_Utils_Hook::alterPaymentProcessorParams($this,
-      $params,
-      $tclink
-    );
-
-    // TrustCommerce will not refuse duplicates, so we should check if the user already submitted this transaction
-    if ($this->_checkDupe($tclink['ticket'])) {
-      return self::error(9004, 'It appears that this transaction is a duplicate.  Have you already submitted the form once?  If so there may have been a connection problem. You can try your transaction again.  If you continue to have problems please contact the site administrator.');
+    
+    /* Translate frequency interval from CiviCRM -> TC
+     * Payments are the same, HOWEVER a payment of 1 (forever) should be 0 in TC */
+    if($payments == 1) {
+      $payments = 0;
     }
 
-    $result = tclink_send($tclink);
-
-    $result = _getTrustCommereceResponse($result);
+    $fields['cycle'] = '1'.$cycle;   /* The billing cycle in years, months, weeks, or days. */
+    $fields['payments'] = $payments;
+    $fields['action'] = 'store';      /* Change our mode to `store' mode. */
 
-    if($result == 0) {
-      /* Transaction was sucessful */
-      $params['trxn_id'] = $result['transid'];         /* Get our transaction ID */
-      $params['gross_amount'] = $tclink['amount']/100; /* Convert from cents to dollars */
-      return $params;
-    } else {
-      /* Transaction was *not* successful */
-      return $result;
-    }
+    return $fields;
   }
 
   /* Parses a response from TC via the tclink_send() command.
    * @param  $reply array The result of a call to tclink_send().
    * @return mixed self::error() if transaction failed, otherwise returns 0.
    */
-  function _getTrustCommerceResponse($reply) {
+  function _getTrustCommerceReply($reply) {
 
     /* DUPLIATE CODE, please refactor. ~lisa */
-    if (!$result) {
+    if (!$reply) {
       return self::error(9002, 'Could not initiate connection to payment gateway');
     }
 
-    switch($result['status']) {
+    switch($reply['status']) {
     case self::AUTH_APPROVED:
       // It's all good
       break;
@@ -241,11 +185,11 @@ class org_fsf_payment_trustcommerce extends CRM_Core_Payment {
       // 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
-      return self::error(9009, "Your transaction was declined: {$result['declinetype']}");
+      return self::error(9009, "Your transaction was declined: {$reply['declinetype']}");
       break;
     case self::AUTH_BADDATA:
-      // TODO FIXME do something with $result['error'] and $result['offender']
-      return self::error(9011, "Invalid credit card information. Please re-enter.");
+      // TODO FIXME do something with $reply['error'] and $reply['offender']
+      return self::error(9011, "Invalid credit card information. The following fields were invalid: {$reply['offenders']}.");
       break;
     case self::AUTH_ERROR:
       return self::error(9002, 'Could not initiate connection to payment gateway');