You are here

public function SermepaForm::buildConfigurationForm in Commerce sermepa 8.2

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PaymentOffsiteForm::buildConfigurationForm

File

src/PluginForm/OffsiteRedirect/SermepaForm.php, line 77

Class

SermepaForm
Provides the Sermepa/Redsýs class for the payment form.

Namespace

Drupal\commerce_sermepa\PluginForm\OffsiteRedirect

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
  $payment = $this->entity;
  $order = $payment
    ->getOrder();

  /** @var \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayInterface $payment_gateway_plugin */
  $payment_gateway_plugin = $payment
    ->getPaymentGateway()
    ->getPlugin();

  // Get the gateway settings.
  $gateway_settings = $payment_gateway_plugin
    ->getConfiguration();
  $merchant_consumer_language = $payment_gateway_plugin
    ->getUnknowFallbackLanguage();

  // Create a new instance of the Sermepa library and initialize it.
  try {
    $gateway = new SermepaApi($gateway_settings['merchant_name'], $gateway_settings['merchant_code'], $gateway_settings['merchant_terminal'], $gateway_settings['merchant_password'], $payment_gateway_plugin
      ->getMode());

    // Configure the gateway transaction.
    $date = DrupalDateTime::createFromTimestamp($this->time
      ->getRequestTime());
    $parameters = FALSE;

    // Check if the payment currency code and the payment method settings are
    // the same.
    $currency_code = $payment
      ->getAmount()
      ->getCurrencyCode();

    /** @var \Drupal\commerce_price\Entity\CurrencyInterface $currency */
    $currency = $this->entityTypeManager
      ->getStorage('commerce_currency')
      ->load($currency_code);
    if ($currency
      ->getNumericCode() == $gateway_settings['currency']) {

      // Prepare the amount converting 120.00 or 120 to 12000.
      $amount = $payment
        ->getAmount()
        ->multiply(100)
        ->getNumber();
      $gateway
        ->setAmount($amount)
        ->setCurrency($gateway_settings['currency'])
        ->setOrder(substr($date
        ->format('ymdHis') . 'Id' . $order
        ->id(), -12, 12))
        ->setMerchantMerchantGroup($gateway_settings['merchant_group'])
        ->setPaymentMethod($gateway_settings['merchant_paymethods'])
        ->setConsumerLanguage($merchant_consumer_language)
        ->setMerchantData($order
        ->id())
        ->setTransactionType($gateway_settings['transaction_type'])
        ->setMerchantURL($payment_gateway_plugin
        ->getNotifyUrl()
        ->toString())
        ->setUrlKO($form['#cancel_url'])
        ->setUrlOK($form['#return_url']);

      // Get the transaction fields for the sermepa form.
      $parameters = $gateway
        ->composeMerchantParameters();
    }
  } catch (\Exception $exception) {
    watchdog_exception('commerce_sermepa', $exception);
  }
  if (empty($parameters)) {
    $this->messenger
      ->addError($this
      ->t('An error has been occurred trying of process the payment data, please contact with us.'));
    return $this
      ->redirectToPaymentInformationPane($order);
  }
  $data = [
    'Ds_SignatureVersion' => $gateway
      ->getSignatureVersion(),
    'Ds_MerchantParameters' => $parameters,
    'Ds_Signature' => $gateway
      ->composeMerchantSignature(),
  ];
  return $this
    ->buildRedirectForm($form, $form_state, $gateway
    ->getEnvironment(), $data, BasePaymentOffsiteForm::REDIRECT_POST);
}