You are here

public function Checkout::buildConfigurationForm in Commerce PayPal 8

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 PaymentGatewayBase::buildConfigurationForm

File

src/Plugin/Commerce/PaymentGateway/Checkout.php, line 114

Class

Checkout
Provides the PayPal Checkout payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  $documentation_url = Url::fromUri('https://www.drupal.org/node/3042053')
    ->toString();
  $form['mode']['#weight'] = 0;
  $form['payment_solution'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('PayPal Commerce Platform features'),
    '#options' => [
      'smart_payment_buttons' => $this
        ->t('Accept PayPal with Smart Payment Buttons'),
      'custom_card_fields' => $this
        ->t('Accept credit cards'),
    ],
    '#default_value' => $this->configuration['payment_solution'],
    '#weight' => 0,
  ];

  // Some settings are visible only when the "Smart Payment Buttons" payment
  // solution is selected.
  $spb_states = [
    'visible' => [
      ':input[name="configuration[' . $this->pluginId . '][payment_solution]"]' => [
        'value' => 'smart_payment_buttons',
      ],
    ],
  ];
  $form['credentials'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('API Credentials'),
    '#weight' => 0,
  ];
  $form['credentials']['help'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#attributes' => [
      'class' => [
        'form-item',
      ],
    ],
    '#value' => $this
      ->t('Refer to the <a href=":url" target="_blank">module documentation</a> to find your API credentials.', [
      ':url' => $documentation_url,
    ]),
  ];
  $form['credentials']['client_id'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Client ID'),
    '#default_value' => $this->configuration['client_id'],
    '#maxlength' => 255,
    '#required' => TRUE,
    '#parents' => array_merge($form['#parents'], [
      'client_id',
    ]),
  ];
  $form['credentials']['secret'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Secret'),
    '#maxlength' => 255,
    '#default_value' => $this->configuration['secret'],
    '#required' => TRUE,
    '#parents' => array_merge($form['#parents'], [
      'secret',
    ]),
  ];
  $form['collect_billing_information']['#field_suffix'] = $this
    ->t('Collect billing information');
  $form['collect_billing_information']['#description'] = $this
    ->t('When disabled, PayPal will collect the billing information instead, in the opened modal.');
  $form['collect_billing_information']['#states'] = $spb_states;
  $form['collect_billing_information']['#title_display'] = 'before';
  $form['collect_billing_information']['#title'] = $this
    ->t('General');
  $form['enable_on_cart'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show Smart Payment Buttons on the cart page.'),
    '#default_value' => $this->configuration['enable_on_cart'],
    '#states' => $spb_states,
  ];
  $form['intent'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Transaction type'),
    '#options' => [
      'capture' => $this
        ->t("Capture (capture payment immediately after customer's approval)"),
      'authorize' => $this
        ->t('Authorize (requires manual or automated capture after checkout)'),
    ],
    '#description' => $this
      ->t('For more information on capturing a prior authorization, please refer to <a href=":url" target="_blank">Capture an authorization</a>.', [
      ':url' => 'https://docs.drupalcommerce.org/commerce2/user-guide/payments/capture',
    ]),
    '#default_value' => $this->configuration['intent'],
  ];
  $form['disable_funding'] = [
    '#title' => $this
      ->t('Disable funding sources'),
    '#description' => $this
      ->t('The disabled funding sources for the transaction. Any funding sources passed do not display with Smart Payment Buttons. By default, funding source eligibility is smartly decided based on a variety of factors.'),
    '#type' => 'checkboxes',
    '#options' => [
      'card' => $this
        ->t('Credit or Debit Cards'),
      'credit' => $this
        ->t('PayPal Credit'),
      'sepa' => $this
        ->t('SEPA-Lastschrift'),
      'sofort' => $this
        ->t('Sofort'),
      'mybank' => $this
        ->t('MyBank'),
    ],
    '#default_value' => $this->configuration['disable_funding'],
    '#states' => $spb_states,
  ];
  $form['disable_card'] = [
    '#title' => $this
      ->t('Disable card types'),
    '#description' => $this
      ->t('The disabled cards for the transaction. Any cards passed do not display with Smart Payment Buttons. By default, card eligibility is smartly decided based on a variety of factors.'),
    '#type' => 'checkboxes',
    '#options' => [
      'visa' => $this
        ->t('Visa'),
      'mastercard' => $this
        ->t('Mastercard'),
      'amex' => $this
        ->t('American Express'),
      'discover' => $this
        ->t('Discover'),
      'jcb' => $this
        ->t('JCB'),
      'elo' => $this
        ->t('Elo'),
      'hiper' => $this
        ->t('Hiper'),
    ],
    '#default_value' => $this->configuration['disable_card'],
  ];
  $shipping_enabled = $this->moduleHandler
    ->moduleExists('commerce_shipping');
  $form['shipping_preference'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Shipping address collection'),
    '#options' => [
      'no_shipping' => $this
        ->t('Do not ask for a shipping address at PayPal.'),
      'get_from_file' => $this
        ->t('Ask for a shipping address at PayPal even if the order already has one.'),
      'set_provided_address' => $this
        ->t('Ask for a shipping address at PayPal if the order does not have one yet.'),
    ],
    '#default_value' => $this->configuration['shipping_preference'],
    '#access' => $shipping_enabled,
    '#states' => $spb_states,
  ];
  $form['update_billing_profile'] = [
    '#type' => 'checkbox',
    '#title' => t('Update the billing customer profile with address information the customer enters at PayPal.'),
    '#default_value' => $this->configuration['update_billing_profile'],
    '#states' => $spb_states,
  ];
  $form['update_shipping_profile'] = [
    '#type' => 'checkbox',
    '#title' => t('Update shipping customer profiles with address information the customer enters at PayPal.'),
    '#default_value' => $this->configuration['update_shipping_profile'],
    '#access' => $shipping_enabled,
    '#states' => $spb_states,
  ];
  $form['customize_buttons'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Smart Payment Buttons style'),
    '#default_value' => !empty($this->configuration['style']),
    '#title_display' => 'before',
    '#field_suffix' => $this
      ->t('Customize view'),
    '#description_display' => 'before',
    '#states' => $spb_states,
  ];
  $form['style'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Settings'),
    '#description' => $this
      ->t('For more information, please visit <a href=":url" target="_blank">customize the PayPal buttons</a>.', [
      ':url' => 'https://developer.paypal.com/docs/checkout/integration-features/customize-button/#layout',
    ]),
    '#states' => array_merge_recursive($spb_states, [
      'visible' => [
        ':input[name="configuration[' . $this->pluginId . '][customize_buttons]"]' => [
          'checked' => TRUE,
        ],
      ],
    ]),
  ];

  // Define some default values for the style configuration.
  $this->configuration['style'] += [
    'layout' => 'vertical',
    'color' => 'gold',
    'shape' => 'rect',
    'label' => 'paypal',
    'tagline' => FALSE,
  ];
  $form['style']['layout'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Layout'),
    '#default_value' => $this->configuration['style']['layout'],
    '#options' => [
      'vertical' => $this
        ->t('Vertical (Recommended)'),
      'horizontal' => $this
        ->t('Horizontal'),
    ],
  ];
  $form['style']['color'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Color'),
    '#options' => [
      'gold' => $this
        ->t('Gold (Recommended)'),
      'blue' => $this
        ->t('Blue'),
      'silver' => $this
        ->t('Silver'),
    ],
    '#default_value' => $this->configuration['style']['color'],
  ];
  $form['style']['shape'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Shape'),
    '#options' => [
      'rect' => $this
        ->t('Rect (Default)'),
      'pill' => $this
        ->t('Pill'),
    ],
    '#default_value' => $this->configuration['style']['shape'],
  ];
  $form['style']['label'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Label'),
    '#options' => [
      'paypal' => $this
        ->t('Displays the PayPal logo (Default)'),
      'checkout' => $this
        ->t('Displays the PayPal Checkout button'),
      'buynow' => $this
        ->t('Displays the PayPal Buy Now button'),
      'pay' => $this
        ->t('Displays the Pay With PayPal button'),
    ],
    '#default_value' => $this->configuration['style']['label'],
  ];
  $form['style']['tagline'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display tagline'),
    '#default_value' => $this->configuration['style']['tagline'],
    '#states' => array_merge_recursive($spb_states, [
      'visible' => [
        ':input[name="configuration[' . $this->pluginId . '][style][layout]"]' => [
          'value' => 'horizontal',
        ],
      ],
    ]),
  ];
  return $form;
}