+ /**
+ * 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;
+ }
+
+ function _isAgentBlacklisted() {
+ $ip = $_SERVER['REMOTE_ADDR'];
+ $agent = $_SERVER['HTTP_USER_AGENT'];
+ $dao = CRM_Core_DAO::executeQuery('SELECT * FROM `trustcommerce_useragent_blacklist`');
+ while($dao->fetch()) {
+ if(preg_match('/'.$dao->name.'/', $agent) === 1) {
+ error_log(' [client '.$ip.'] [agent '.$agent.'] - Blacklisted by USER_AGENT rule #'.$dao->id);
+ return TRUE;
+ }
+ }
+ return FALSE;
+ }
+
+ function _isIPBlacklisted() {
+ $ip = $_SERVER['REMOTE_ADDR'];
+ $agent = $_SERVER['HTTP_USER_AGENT'];
+ $ip = ip2long($ip);
+ $blacklist = array();
+ $dao = CRM_Core_DAO::executeQuery('SELECT * FROM `trustcommerce_blacklist`');
+ while($dao->fetch()) {
+ if($ip >= $dao->start && $ip <= $dao->end) {
+ error_log('[client '.long2ip($ip).'] [agent '.$agent.'] Blacklisted by IP rule #'.$dao->id);
+ return TRUE;
+ }
+ }
+ return FALSE;
+ }
+
+ function _sendTCRequest($request) {
+ $this->_logger($request);
+ return tclink_send($request);
+ }
+
+ function _logger($params) {
+ $msg = '';
+ foreach ($params as $key => $data) {
+ /* Delete any data we should not be writing to disk. This includes:
+ * custid, password, cc, exp, and cvv
+ */
+ switch($key) {
+ case 'custid':
+ case 'password':
+ case 'cc':
+ case 'exp':
+ case 'cvv':
+ break;
+ default:
+ $msg .= ' '.$key.' => '.$data;
+ }
+ }
+ error_log('[client '.$_SERVER['REMOTE_ADDR'].'] TrustCommerce:'.$msg);
+ }
+