/**
  * Store a payment token.
  *
  * From 4.6 onwards the payment token table can store these tokens.
  *
  * We don't want fails here to trigger a rollback to we set is_transactional to false.
  *
  * @param array $params
  * @param array $contribution
  * @param string $tokenReference
  *
  * @throws \CiviCRM_API3_Exception
  */
 protected function storePaymentToken($params, $contribution, $tokenReference)
 {
     if (!omnipaymultiprocessor__versionAtLeast(4.6)) {
         return;
     }
     $token = civicrm_api3('payment_token', 'create', array('contact_id' => $contribution['contact_id'], 'payment_processor_id' => $params['processor_id'], 'token' => $tokenReference, 'is_transactional' => FALSE));
     civicrm_api3('contribution_recur', 'create', array('id' => $contribution['contribution_recur_id'], 'payment_token_id' => $token['id'], 'is_transactional' => FALSE));
 }
/**
 * Implement buildForm hook to remove billing fields if elsewhere on the form.
 *
 * @param string $formName
 * @param CRM_Contribute_Form_Contribution_Main|CRM_Event_Form_Registration_Register $form
 */
function omnipaymultiprocessor_civicrm_buildForm($formName, &$form)
{
    if ($formName == 'CRM_Admin_Form_PaymentProcessor') {
        foreach (array('signature', 'test_signature') as $fieldName) {
            if ($form->elementExists($fieldName)) {
                $label = $form->_elements[$form->_elementIndex[$fieldName]]->_label;
                $form->removeElement($fieldName);
                $form->add('textarea', $fieldName, $label, array('rows' => 4, 'cols' => 40));
            }
        }
    }
    if (omnipaymultiprocessor__versionAtLeast(4.6)) {
        return;
    }
    if (!omnipaymultiprocessor_is_credit_card_form($formName) || $form->_paymentProcessor['class_name'] != 'Payment_OmnipayMultiProcessor') {
        return;
    }
    $paymentType = civicrm_api3('option_value', 'getsingle', array('value' => $form->_paymentProcessor['payment_type'], 'option_group_id' => 'payment_type'));
    $form->assign('paymentTypeName', $paymentType['name']);
    $paymentFields = omnipaymultiprocessor_get_valid_form_payment_fields($formName == 'CRM_Contribute_Form_Contribution_Main' ? 'contribute' : 'event', $form->_paymentProcessor, empty($form->_paymentFields) ? array() : $form->_paymentFields);
    if (!empty($paymentFields)) {
        $form->assign('paymentFields', $paymentFields);
        $form->assign('paymentTypeLabel', ts($paymentType['label'] . ' Information'));
    } else {
        $form->assign('paymentFields', NULL);
        $form->assign('paymentTypeLabel', NULL);
    }
    $billingDetailsFields = omnipaymultiprocessor_getBillingPersonalDetailsFields($form->_paymentProcessor);
    //we trick CiviCRM into adding the credit card form so we can remove the parts we don't want (the credit card fields)
    //for a transparent redirect like Cybersource
    $billingMode = $form->_paymentProcessor['billing_mode'];
    $form->_paymentProcessor['billing_mode'] = CRM_Core_Payment::BILLING_MODE_FORM;
    CRM_Core_Payment_Form::buildCreditCard($form);
    $form->_paymentProcessor['billing_mode'] = $billingMode;
    //CiviCRM assumes that if it is Not a credit card it MUST be a direct debit & makes those required
    $suppressedFields = omnipaymultiprocessor_get_suppressed_billing_fields((array) $billingDetailsFields, (array) $paymentFields, (array) $form->_paymentFields);
    foreach ($suppressedFields as $suppressedField) {
        $form->_paymentFields[$suppressedField]['is_required'] = FALSE;
    }
    $form->assign('suppressedFields', $suppressedFields);
    $form->assign('billingDetailsFields', $billingDetailsFields);
    CRM_Core_Region::instance('billing-block')->update('default', array('disabled' => TRUE));
    CRM_Core_Region::instance('billing-block')->add(array('template' => 'SubstituteBillingBlock.tpl'));
}
 /**
  * Handle processor error.
  *
  * If we pass error handling through this function it will be easy to switch to throwing exceptions later.
  *
  * @param string $level
  * @param string $message
  * @param string $context
  *
  * @param int $errorCode
  * @param string $userMessage
  *
  * @return mixed
  */
 protected function handleError($level, $message, $context, $errorCode = 9001, $userMessage = NULL)
 {
     if (omnipaymultiprocessor__versionAtLeast(4.5)) {
         $log = new CRM_Utils_SystemLogger();
         $log->log($level, $message, (array) $context);
     } else {
         CRM_Core_Error::debug($errorCode . ': ' . $message . print_r($context, TRUE));
     }
     CRM_Core_Session::setStatus(empty($userMessage) ? $message : $userMessage);
     return new CRM_Core_Error();
 }