function run()
 {
     $get = $_GET;
     $cid = $get['cid'];
     $mid = $get['mid'];
     $cr_id = $get['cr_id'];
     $payer_reference = $get['reference_number'];
     $this->assign('payer_reference', $reference_number);
     $params = array();
     if (!empty($cid)) {
         $contact = CRM_SmartdebitReconciliation_Page_MembershipRecurDetails::_get_contact_details($cid);
         $address = CRM_SmartdebitReconciliation_Page_MembershipRecurDetails::_get_address($cid);
         $this->assign('aContact', $contact);
         $this->assign('aAddress', $address);
         $params['contact_id'] = $cid;
     }
     if (!empty($mid)) {
         $membership = CRM_SmartdebitReconciliation_Page_MembershipRecurDetails::_get_membership($mid);
         $this->assign('aMembership', $membership);
         $params['membership_id'] = $mid;
     }
     if (!empty($cr_id)) {
         $cRecur = CRM_SmartdebitReconciliation_Page_MembershipRecurDetails::_get_contribution_recur($cr_id);
         $this->assign('aContributionRecur', $cRecur);
         $params['contribution_recur_id'] = $cr_id;
     }
     $params['payer_reference'] = $payer_reference;
     // Check the details are correct and everything has been passed over
     // Call the routine that will fix everything
     require_once 'CRM/SmartdebitReconciliation/Form/SmartdebitReconciliationList.php';
     CRM_SmartdebitReconciliation_Form_SmartdebitReconciliationList::repair_missing_from_civicrm_record($params);
     $params = sprintf('reset=1&cid=%d', $params['contact_id']);
     $url = CRM_Utils_System::url('civicrm/contact/view', $params);
     CRM_Utils_System::redirect($url);
 }
 function buildQuickForm()
 {
     $this->addElement('select', 'membership_record', ts('Select Membership'), array('' => ts('Loading...')));
     $this->addElement('select', 'contribution_recur_record', ts('Select Recur Record'), array('' => ts('Loading...')));
     //$this->addElement('text', 'contact_name', 'Contact', array('size' => 50, 'maxlength' => 255));
     $this->addEntityRef('contact_name', ts('Contact'), array('create' => FALSE, 'api' => array('extra' => array('email'))));
     $this->addElement('hidden', 'cid', 'cid');
     $this->addElement('text', 'reference_number', 'Smart Debit Reference', array('size' => 50, 'maxlength' => 255));
     $this->addButtons(array(array('type' => 'upload', 'name' => ts('Next'))));
     // Get the smart Debit mandate details
     require_once 'CRM/SmartdebitReconciliation/Form/SmartdebitReconciliationList.php';
     $smartDebitResponse = CRM_SmartdebitReconciliation_Form_SmartdebitReconciliationList::getSmartDebitPayments(CRM_Utils_Array::value('reference_number', $_GET));
     $smartDebitMandate = $smartDebitResponse[0];
     // Display the smart debit payments details
     $el = $this->addElement('text', 'first_name', 'First Name', array('size' => 50, 'maxlength' => 255));
     $el->freeze();
     $el = $this->addElement('text', 'last_name', 'Last Name', array('size' => 50, 'maxlength' => 255));
     $el->freeze();
     $el = $this->addElement('text', 'email_address', 'Email Address', array('size' => 50, 'maxlength' => 255));
     $el->freeze();
     $el = $this->addElement('text', 'regular_amount', 'Amount', array('size' => 50, 'maxlength' => 255));
     $el->freeze();
     $el = $this->addElement('text', 'start_date', 'Start Date', array('size' => 50, 'maxlength' => 255));
     $el->freeze();
     $this->assign('SDMandateArray', $smartDebitMandate);
     $this->assign('memStatusCurrent', self::c_current_membership_status);
     //MV, to set the current membership as default, when ajax loading
     $cid = CRM_Utils_Array::value('cid', $_GET);
     $this->assign('cid', $cid);
     $this->addFormRule(array('CRM_SmartdebitReconciliation_Form_MembershipRecurDetails', 'formRule'), $this);
     parent::buildQuickForm();
 }
 function run()
 {
     $get = $_GET;
     $cid = $get['cid'];
     $payer_reference = $get['reference_number'];
     $this->assign('payer_reference', $reference_number);
     $params = array();
     // Find a membership for the contact - if they have more than one then bomb out
     $mid = $get['mid'];
     // Find recurring for the contact
     // The recurring should have the a status of pending
     $cr_id = $get['cr_id'];
     // Find the contribution attached to the recurring record thats also set to pending (incomplete transaction)
     if (!empty($cid)) {
         $contact = CRM_SmartdebitReconciliation_Page_MembershipRecurDetails::_get_contact_details($cid);
         $address = CRM_SmartdebitReconciliation_Page_MembershipRecurDetails::_get_address($cid);
         $this->assign('aContact', $contact);
         $this->assign('aAddress', $address);
         $params['contact_id'] = $cid;
     }
     if (!empty($mid)) {
         $membership = CRM_SmartdebitReconciliation_Page_MembershipRecurDetails::_get_membership($mid);
         $this->assign('aMembership', $membership);
         $params['membership_id'] = $mid;
     }
     if (!empty($cr_id)) {
         $cRecur = CRM_SmartdebitReconciliation_Page_MembershipRecurDetails::_get_contribution_recur($cr_id);
         $this->assign('aContributionRecur', $cRecur);
         $params['contribution_recur_id'] = $cr_id;
     }
     $params['payer_reference'] = $payer_reference;
     // Check the details are correct and everything has been passed over
     // Call the routine that will fix everything
     require_once 'CRM/SmartdebitReconciliation/Form/SmartdebitReconciliationList.php';
     // Then Call the IPN code i.e. we're pretending we've just completed the smart debit call and firing the code that was in the
     CRM_SmartdebitReconciliation_Form_SmartdebitReconciliationList::repair_corrupt_in_civicrm_record($params);
     $params = sprintf('reset=1&cid=%d', $cid);
     $url = CRM_Utils_System::url('civicrm/contact/view', $params);
     CRM_Utils_System::redirect($url);
 }
function smartdebit_civicrm_pageRun(&$page)
{
    $pageName = $page->getVar('_name');
    // To avoid standalone new contribution fail
    if ($pageName == 'CRM_Contribute_Page_Tab' && $page->getVar('_contactId')) {
        $paymentProcessorType = CRM_Core_PseudoConstant::paymentProcessorType(false, null, 'name');
        if (!CRM_Utils_Array::key('Smart Debit', $paymentProcessorType)) {
            return;
        }
        $query = "\n      SELECT cr.id, cr.trxn_id FROM civicrm_contribution_recur cr\n      INNER JOIN civicrm_payment_processor cpp ON cpp.id = cr.payment_processor_id\n      INNER JOIN civicrm_payment_processor_type cppt ON cppt.id = cpp.payment_processor_type_id\n      LEFT JOIN civicrm_option_value opva ON (cr.payment_instrument_id = opva.value)\n      LEFT JOIN civicrm_option_group opgr ON (opgr.id = opva.option_group_id) \n      WHERE cppt.name = %1 AND cr.contact_id = %2 AND opgr.name = %3 AND opva.label = %4";
        $queryParams = array(1 => array('Smart Debit', 'String'), 2 => array($page->getVar('_contactId'), 'Int'), 3 => array('payment_instrument', 'String'), 4 => array('Direct Debit', 'String'));
        $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
        $contributionRecurDetails = array();
        while ($dao->fetch()) {
            $smartDebitResponse = CRM_SmartdebitReconciliation_Form_SmartdebitReconciliationList::getSmartDebitPayments($dao->trxn_id);
            foreach ($smartDebitResponse[0] as $key => $value) {
                $contributionRecurDetails[$dao->id][$key] = $value;
            }
        }
        $contributionRecurDetails = json_encode($contributionRecurDetails);
        $page->assign('contributionRecurDetails', $contributionRecurDetails);
        $page->assign('smartdebit', TRUE);
    }
}