static function repair_missing_from_civicrm_record($params)
 {
     foreach (array('contact_id', 'payer_reference') as $required) {
         if (!isset($params[$required]) || empty($params[$required])) {
             throw new InvalidArgumentException("Missing params[{$required}]");
         }
     }
     require_once 'UK_Direct_Debit/Form/Main.php';
     // Get the Smart Debit details for the payer
     $smartDebitResponse = self::getSmartDebitPayments($params['payer_reference']);
     foreach ($smartDebitResponse as $key => $smartDebitRecord) {
         // Setup params for the relevant rec
         $params['recur_frequency_interval'] = self::translateSmartDebitFrequency($smartDebitRecord['frequency_type']);
         $params['amount'] = self::getCleanSmartDebitAmount($smartDebitRecord['regular_amount']);
         $params['recur_start_date'] = $smartDebitRecord['start_date'] . ' 00:00:00';
         $params['recur_next_payment_date'] = $smartDebitRecord['start_date'] . ' 00:00:00';
         $params['recur_frequency_unit'] = self::translateSmartDebitFrequencyUnit($smartDebitRecord['frequency_type']);
         $params['payment_processor_id'] = self::getSmartDebitPaymentProcessorID();
         $params['payment_instrument_id'] = UK_Direct_Debit_Form_Main::getDDPaymentInstrumentID();
         $params['trxn_id'] = $params['payer_reference'];
         $params['current_state'] = $smartDebitRecord['current_state'];
         list($y, $m, $d) = explode('-', $smartDebitRecord['start_date']);
         $params['cycle_day'] = $d;
         // First Check if a recurring record has beeen selected
         if (!isset($params['contribution_recur_id']) || empty($params['contribution_recur_id'])) {
             // Create the Recurring
             self::create_recur($params);
         } else {
             // Repair the Recurring
             self::repair_recur($params);
         }
         /* First Check if the membership has beeen selected */
         if (isset($params['membership_id']) && !empty($params['membership_id'])) {
             // Link it to the Recurring Record
             self::link_membership_to_recur($params);
         }
     }
 }
 function doTransferCheckout(&$params, $component)
 {
     $paymentProcessorType = CRM_Core_PseudoConstant::paymentProcessorType(false, null, 'name');
     $paymentProcessorTypeId = CRM_Utils_Array::key('Gocardless', $paymentProcessorType);
     $domainID = CRM_Core_Config::domainID();
     $sql = " SELECT user_name ";
     $sql .= " ,      password ";
     $sql .= " ,      signature ";
     $sql .= " ,      subject ";
     $sql .= " FROM civicrm_payment_processor ";
     $sql .= " WHERE payment_processor_type_id = %1 ";
     $sql .= " AND is_test= %2 ";
     $sql .= " AND domain_id = %3 ";
     $isTest = 0;
     if ($this->_mode == 'test') {
         $isTest = 1;
     }
     $sql_params = array(1 => array($paymentProcessorTypeId, 'Integer'), 2 => array($isTest, 'Int'), 3 => array($domainID, 'Int'));
     $dao = CRM_Core_DAO::executeQuery($sql, $sql_params);
     if ($dao->fetch()) {
         $app_id = $dao->user_name;
         $app_secret = $dao->password;
         $merchant_id = $dao->signature;
         $access_token = $dao->subject;
     }
     $account_details = array('app_id' => $app_id, 'app_secret' => $app_secret, 'merchant_id' => $merchant_id, 'access_token' => $access_token);
     // Fail nicely if no account details set
     if (!$account_details['app_id'] && !$account_details['app_secret']) {
         echo '<p>First sign up to <a href="http://gocardless.com">GoCardless</a> and
     copy your sandbox API credentials from the \'Developer\' tab into the top of
     this script.</p>';
         exit;
     }
     // Set $environment to 'production' if live. Default is 'sandbox'
     if ($this->_mode == 'live') {
         GoCardless::$environment = 'production';
     }
     // Initialize GoCardless
     GoCardless::set_account_details($account_details);
     $goCardLessParams = array();
     $goCardLessParams['amount'] = $params['amount'];
     $goCardLessParams['interval_length'] = $params['frequency_interval'];
     $goCardLessParams['interval_unit'] = $params['frequency_unit'];
     if (!empty($params['preferred_collection_day'])) {
         $preferredCollectionDay = $params['preferred_collection_day'];
         $collectionDate = UK_Direct_Debit_Form_Main::firstCollectionDate($preferredCollectionDay, null);
         // ISO8601 format.
         $goCardLessParams['start_at'] = $collectionDate->format('c');
     }
     $url = $component == 'event' ? 'civicrm/event/register' : 'civicrm/contribute/transact';
     $cancel = $component == 'event' ? '_qf_Register_display' : '_qf_Main_display';
     $returnURL = CRM_Utils_System::url($url, "_qf_ThankYou_display=1&qfKey={$params['qfKey']}" . "&cid={$params['contactID']}", true, null, false);
     $goCardLessParams['redirect_uri'] = $returnURL;
     $goCardLessParams['user'] = array('email' => isset($params['email-5']) ? $params['email-5'] : NULL, 'first_name' => isset($params['first_name']) ? $params['first_name'] : NULL, 'last_name' => isset($params['last_name']) ? $params['last_name'] : NULL, 'billing_address1' => isset($params['street_address']) ? $params['street_address'] : NULL, 'billing_town' => isset($params['city']) ? $params['city'] : NULL, 'billing_postcode' => isset($params['postal_code']) ? $params['postal_code'] : NULL, 'country_code' => 'GB');
     // Allow further manipulation of the arguments via custom hooks ..
     CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $goCardLessParams);
     $subscription_url = GoCardless::new_subscription_url($goCardLessParams);
     CRM_Utils_System::redirect($subscription_url);
 }