class PaymentMethodAddForm in Commerce Braintree 8
Provides the HostedFields payment method add form.
Hierarchy
- class \Drupal\Core\Plugin\PluginFormBase implements PluginAwareInterface, PluginFormInterface
- class \Drupal\commerce_payment\PluginForm\PaymentGatewayFormBase implements PaymentGatewayFormInterface uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\commerce_payment\PluginForm\PaymentMethodFormBase implements ContainerInjectionInterface
- class \Drupal\commerce_payment\PluginForm\PaymentMethodAddForm
- class \Drupal\commerce_braintree\PluginForm\HostedFields\PaymentMethodAddForm
- class \Drupal\commerce_payment\PluginForm\PaymentMethodAddForm
- class \Drupal\commerce_payment\PluginForm\PaymentMethodFormBase implements ContainerInjectionInterface
- class \Drupal\commerce_payment\PluginForm\PaymentGatewayFormBase implements PaymentGatewayFormInterface uses DependencySerializationTrait, StringTranslationTrait
Expanded class hierarchy of PaymentMethodAddForm
File
- src/
PluginForm/ HostedFields/ PaymentMethodAddForm.php, line 11
Namespace
Drupal\commerce_braintree\PluginForm\HostedFieldsView source
class PaymentMethodAddForm extends BasePaymentMethodAddForm {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$payment_method = $this->entity;
if ($payment_method
->bundle() === 'paypal_credit') {
$form['payment_details'] = $this
->buildPayPalForm($form['payment_details'], $form_state);
}
return $form;
}
/**
* {@inheritdoc}
*/
public function buildPayPalForm(array $element, FormStateInterface $form_state) {
/** @var \Drupal\commerce_braintree\Plugin\Commerce\PaymentGateway\HostedFieldsInterface $plugin */
$plugin = $this->plugin;
$element['#attached']['library'][] = 'commerce_braintree/paypal';
$element['#attached']['drupalSettings']['commerceBraintree'] = [
'clientToken' => $plugin
->generateClientToken(),
'integration' => 'paypal',
'paypalButton' => 'paypal-button',
'environment' => $plugin
->getMode() == 'test' ? 'sandbox' : 'production',
'paymentMethodType' => $this->entity
->bundle(),
];
$element['#attributes']['class'][] = 'braintree-form';
$element['paypal_button'] = [
'#type' => 'container',
'#id' => 'paypal-button',
];
// Populated by the JS library.
$element['payment_method_nonce'] = [
'#type' => 'hidden',
'#attributes' => [
'class' => [
'braintree-nonce',
],
],
];
// Put the PayPal button below the billing address.
$element['#weight'] = 50;
return $element;
}
/**
* {@inheritdoc}
*/
public function buildCreditCardForm(array $element, FormStateInterface $form_state) {
/** @var \Drupal\commerce_braintree\Plugin\Commerce\PaymentGateway\HostedFieldsInterface $plugin */
$plugin = $this->plugin;
$element['#attached']['library'][] = 'commerce_braintree/hosted-fields';
$element['#attached']['drupalSettings']['commerceBraintree'] = [
'clientToken' => $plugin
->generateClientToken(),
'integration' => 'custom',
'hostedFields' => [
'number' => [
'selector' => '#card-number',
],
'cvv' => [
'selector' => '#cvv',
],
'expirationMonth' => [
'selector' => '#expiration-month',
],
'expirationYear' => [
'selector' => '#expiration-year',
],
],
];
$element['#attributes']['class'][] = 'braintree-form';
// Populated by the JS library.
$element['payment_method_nonce'] = [
'#type' => 'hidden',
'#attributes' => [
'class' => [
'braintree-nonce',
],
],
];
$element['card_type'] = [
'#type' => 'hidden',
'#attributes' => [
'class' => [
'braintree-card-type',
],
],
];
$element['last2'] = [
'#type' => 'hidden',
'#attributes' => [
'class' => [
'braintree-last2',
],
],
];
$element['number'] = [
'#type' => 'item',
'#title' => t('Card number'),
'#markup' => '<div id="card-number" class="braintree-hosted-field"></div>',
];
$element['expiration'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'credit-card-form__expiration',
],
],
];
$element['expiration']['month'] = [
'#type' => 'item',
'#title' => t('Month'),
'#markup' => '<div id="expiration-month" class="braintree-hosted-field"></div>',
];
$element['expiration']['divider'] = [
'#type' => 'item',
'#title' => '',
'#markup' => '<span class="credit-card-form__divider">/</span>',
];
$element['expiration']['year'] = [
'#type' => 'item',
'#title' => t('Year'),
'#markup' => '<div id="expiration-year" class="braintree-hosted-field"></div>',
];
$element['cvv'] = [
'#type' => 'item',
'#title' => t('CVV'),
'#markup' => '<div id="cvv" class="braintree-hosted-field"></div>',
];
return $element;
}
/**
* {@inheritdoc}
*/
protected function validatePayPalForm(array &$element, FormStateInterface $form_state) {
// The JS library performs its own validation.
}
/**
* {@inheritdoc}
*/
protected function validateCreditCardForm(array &$element, FormStateInterface $form_state) {
// The JS library performs its own validation.
}
/**
* {@inheritdoc}
*/
public function submitPayPalForm(array $element, FormStateInterface $form_state) {
// The payment gateway plugin will process the submitted payment details.
}
/**
* {@inheritdoc}
*/
public function submitCreditCardForm(array $element, FormStateInterface $form_state) {
// The payment gateway plugin will process the submitted payment details.
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
PaymentGatewayFormBase:: |
protected | property | The form entity. | |
PaymentGatewayFormBase:: |
public | function |
Gets the form entity. Overrides PaymentGatewayFormInterface:: |
|
PaymentGatewayFormBase:: |
public | function |
Sets the form entity. Overrides PaymentGatewayFormInterface:: |
|
PaymentMethodAddForm:: |
public | function |
Form constructor. Overrides PaymentMethodAddForm:: |
|
PaymentMethodAddForm:: |
public | function |
Builds the credit card form. Overrides PaymentMethodAddForm:: |
|
PaymentMethodAddForm:: |
public | function |
Builds the PayPal form. Overrides PaymentMethodAddForm:: |
|
PaymentMethodAddForm:: |
public | function |
Gets the form element to which errors should be assigned. Overrides PaymentGatewayFormBase:: |
|
PaymentMethodAddForm:: |
public | function |
Form submission handler. Overrides PaymentMethodFormBase:: |
|
PaymentMethodAddForm:: |
public | function |
Handles the submission of the credit card form. Overrides PaymentMethodAddForm:: |
|
PaymentMethodAddForm:: |
public | function |
Handles the submission of the PayPal form. Overrides PaymentMethodAddForm:: |
|
PaymentMethodAddForm:: |
public | function |
Form validation handler. Overrides PaymentMethodFormBase:: |
|
PaymentMethodAddForm:: |
protected | function |
Validates the credit card form. Overrides PaymentMethodAddForm:: |
|
PaymentMethodAddForm:: |
protected | function |
Validates the PayPal form. Overrides PaymentMethodAddForm:: |
|
PaymentMethodFormBase:: |
protected | property | The current store. | |
PaymentMethodFormBase:: |
protected | property | The entity type manager. | |
PaymentMethodFormBase:: |
protected | property | The inline form manager. | |
PaymentMethodFormBase:: |
protected | property | The logger. | |
PaymentMethodFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
PaymentMethodFormBase:: |
public | function | Constructs a new PaymentMethodFormBase. | |
PluginFormBase:: |
protected | property | The plugin this form is for. | 3 |
PluginFormBase:: |
public | function |
Sets the plugin for this object. Overrides PluginAwareInterface:: |
1 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |