You are here

function mollie_payment_configuration in Mollie Payment 7.2

Same name and namespace in other branches
  1. 7 mollie_payment.module \mollie_payment_configuration()

Payment method configuration form elements callback.

Parameters

array $form: A Drupal form array.

array $form_state: The current state of the form.

Return value

array A Drupal form array.

File

./mollie_payment.module, line 567
Provides Mollie integration for the Payment platform.

Code

function mollie_payment_configuration(array $form, array &$form_state) {
  $controller_data = $form_state['payment']->method->controller_data;
  if ($controller_data['advanced']) {
    try {
      $payment = $form_state['payment'];

      /** @var \Mollie\Api\MollieApiClient $client */
      $client = mollie_payment_get_client($payment);
      if ($client) {
        $sequence_type = \Mollie\Api\Types\SequenceType::SEQUENCETYPE_ONEOFF;

        // Set recurring if needed.
        if (module_exists('payment_recurring') && isset($payment->recurring)) {
          $sequence_type = \Mollie\Api\Types\SequenceType::SEQUENCETYPE_FIRST;
        }

        // Get list of available methods, based on multiple variables.
        // Make sure we have 2 decimals in the amount.
        $amount = number_format($payment
          ->totalAmount(TRUE), 2, '.', '');
        $methods_list = $client->methods
          ->all(array(
          'sequenceType' => $sequence_type,
          'amount' => array(
            'value' => $amount,
            'currency' => $payment->currency_code,
          ),
        ));
        $methods = array();
        foreach ($methods_list as $method) {
          $methods[$method->id] = $method->description;
        }
        $form['mollie_payment_method'] = array(
          '#type' => 'select',
          '#title' => t('Method'),
          '#options' => $methods,
          '#required' => TRUE,
        );

        // Do not bother users with a form element if there is nothing to choose.
        if (count($methods) < 2) {
          $form['mollie_payment_method']['#type'] = 'hidden';
          reset($methods);
          $form['mollie_payment_method']['#value'] = key($methods);
        }

        /** @var \Mollie\Api\Resources\IssuerCollection $issuer_list */
        $issuer_list = $client->methods
          ->get(\Mollie\Api\Types\PaymentMethod::IDEAL, array(
          "include" => "issuers",
        ))
          ->issuers();
        $issuers = array();

        /** @var \Mollie\Api\Resources\Issuer $issuer */
        foreach ($issuer_list as $issuer) {
          $issuers[$issuer->id] = $issuer->name;
        }

        // Show the issuers but only if the selected method is 'ideal'.
        $form['mollie_payment_issuer'] = array(
          '#type' => 'select',
          '#title' => t('Issuer'),
          '#options' => $issuers,
          '#states' => array(
            'visible' => array(
              ':input[name$="[mollie_payment_method]"]' => array(
                'value' => \Mollie\Api\Types\PaymentMethod::IDEAL,
              ),
            ),
          ),
          '#empty_option' => t('- Select -'),
          '#empty_value' => '',
          '#required' => FALSE,
        );
      }
    } catch (Exception $e) {
      watchdog('mollie_payment', 'Failed to load Advanced payment options: <pre>@data</pre>', array(
        '@data' => $e
          ->getMessage(),
      ), WATCHDOG_ERROR);
    }
  }
  if ($controller_data['test_mode']) {
    $form['test_mode'] = array(
      '#type' => 'markup',
      '#markup' => theme('html_tag', array(
        'element' => array(
          '#tag' => 'pre',
          '#value' => t('Mollie is in test mode. No real money is being transfered. Be sure to switch off test mode on production websites.'),
        ),
      )),
    );
  }
  return $form;
}