class PaymentMethodPane in Ubercart 8.4
Allows the user to select a payment method and preview the line items.
Plugin annotation
@CheckoutPane(
id = "payment",
title = @Translation("Payment method"),
weight = 6
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\uc_cart\CheckoutPanePluginBase implements CheckoutPanePluginInterface
- class \Drupal\uc_payment\Plugin\Ubercart\CheckoutPane\PaymentMethodPane implements ContainerFactoryPluginInterface
- class \Drupal\uc_cart\CheckoutPanePluginBase implements CheckoutPanePluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of PaymentMethodPane
File
- payment/
uc_payment/ src/ Plugin/ Ubercart/ CheckoutPane/ PaymentMethodPane.php, line 24
Namespace
Drupal\uc_payment\Plugin\Ubercart\CheckoutPaneView source
class PaymentMethodPane extends CheckoutPanePluginBase implements ContainerFactoryPluginInterface {
/**
* The payment method manager.
*
* @var \Drupal\uc_payment\Plugin\PaymentMethodManager
*/
protected $paymentMethodManager;
/**
* Constructs a PaymentMethodPane object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\uc_payment\Plugin\PaymentMethodManager $payment_method_manager
* The payment method manager.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, PaymentMethodManager $payment_method_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->paymentMethodManager = $payment_method_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.uc_payment.method'));
}
/**
* {@inheritdoc}
*/
public function view(OrderInterface $order, array $form, FormStateInterface $form_state) {
$contents['#attached']['library'][] = 'uc_payment/uc_payment.styles';
if ($this->configuration['show_preview']) {
$contents['line_items'] = [
'#theme' => 'uc_payment_totals',
'#order' => $order,
'#weight' => -20,
];
}
// Ensure that the form builder uses #default_value to determine which
// button should be selected after an ajax submission. This is
// necessary because the previously selected value may have become
// unavailable, which would result in an invalid selection.
$input = $form_state
->getUserInput();
unset($input['panes']['payment']['payment_method']);
$form_state
->setUserInput($input);
$options = [];
$methods = PaymentMethod::loadMultiple();
uasort($methods, 'Drupal\\uc_payment\\Entity\\PaymentMethod::sort');
foreach ($methods as $method) {
// $set = rules_config_load('uc_payment_method_' . $method['id']);
// if ($set && !$set->execute($order)) {
// continue;
// }
if ($method
->status()) {
$options[$method
->id()] = $method
->getDisplayLabel();
}
}
\Drupal::moduleHandler()
->alter('uc_payment_method_checkout', $options, $order);
if (!$options) {
$contents['#description'] = $this
->t('Checkout cannot be completed without any payment methods enabled. Please contact an administrator to resolve the issue.');
$options[''] = $this
->t('No payment methods available');
}
elseif (count($options) > 1) {
$contents['#description'] = $this
->t('Select a payment method from the following options.');
}
if (!$order
->getPaymentMethodId() || !isset($options[$order
->getPaymentMethodId()])) {
$order
->setPaymentMethodId(key($options));
}
$contents['payment_method'] = [
'#type' => 'radios',
'#title' => $this
->t('Payment method'),
'#title_display' => 'invisible',
'#options' => $options,
'#default_value' => $order
->getPaymentMethodId(),
'#disabled' => count($options) == 1,
'#required' => TRUE,
'#ajax' => [
'callback' => [
$this,
'ajaxRender',
],
'wrapper' => 'payment-details',
'progress' => [
'type' => 'throbber',
],
],
];
// If there are no payment methods available, this will be ''.
if ($order
->getPaymentMethodId()) {
$plugin = $this->paymentMethodManager
->createFromOrder($order);
$definition = $plugin
->getPluginDefinition();
$contents['details'] = [
'#prefix' => '<div id="payment-details" class="clearfix ' . Html::cleanCssIdentifier('payment-details-' . $definition['id']) . '">',
'#markup' => $this
->t('Continue with checkout to complete payment.'),
'#suffix' => '</div>',
];
try {
$details = $plugin
->cartDetails($order, $form, $form_state);
if ($details) {
unset($contents['details']['#markup']);
$contents['details'] += $details;
}
} catch (PluginException $e) {
}
}
return $contents;
}
/**
* {@inheritdoc}
*/
public function process(OrderInterface $order, array $form, FormStateInterface $form_state) {
if (!$form_state
->getValue([
'panes',
'payment',
'payment_method',
])) {
$form_state
->setErrorByName('panes][payment][payment_method', $this
->t('You cannot check out without selecting a payment method.'));
return FALSE;
}
$order
->setPaymentMethodId($form_state
->getValue([
'panes',
'payment',
'payment_method',
]));
$result = $this->paymentMethodManager
->createFromOrder($order)
->cartProcess($order, $form, $form_state);
return $result !== FALSE;
}
/**
* {@inheritdoc}
*/
public function review(OrderInterface $order) {
$line_items = $order
->getDisplayLineItems();
foreach ($line_items as $line_item) {
$review[] = [
'title' => $line_item['title'],
'data' => uc_currency_format($line_item['amount']),
];
}
$method = $this->paymentMethodManager
->createFromOrder($order);
$review[] = [
'border' => 'top',
'title' => $this
->t('Paying by'),
'data' => $method
->cartReviewTitle(),
];
$result = $method
->cartReview($order);
if (is_array($result)) {
$review = array_merge($review, $result);
}
return $review;
}
/**
* {@inheritdoc}
*/
public function settingsForm() {
$form['show_preview'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show the order total preview on the payment pane.'),
'#default_value' => $this->configuration['show_preview'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'show_preview' => TRUE,
];
}
/**
* Ajax callback to re-render the payment method pane.
*/
public function ajaxRender($form, &$form_state) {
return $form['panes']['payment']['details'];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CheckoutPanePluginBase:: |
protected | property | Whether the pane is enabled or not. | |
CheckoutPanePluginBase:: |
protected | property | The weight of the checkout pane. | |
CheckoutPanePluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns the title of the pane, to be displayed on the checkout form. Overrides CheckoutPanePluginInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns the weight of the checkout pane. Overrides CheckoutPanePluginInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns whether the checkout pane is enabled. Overrides CheckoutPanePluginInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Prepares a pane for display. Overrides CheckoutPanePluginInterface:: |
1 |
CheckoutPanePluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
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 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PaymentMethodPane:: |
protected | property | The payment method manager. | |
PaymentMethodPane:: |
public | function | Ajax callback to re-render the payment method pane. | |
PaymentMethodPane:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
PaymentMethodPane:: |
public | function |
Gets default configuration for this plugin. Overrides CheckoutPanePluginBase:: |
|
PaymentMethodPane:: |
public | function |
Processes a checkout pane. Overrides CheckoutPanePluginBase:: |
|
PaymentMethodPane:: |
public | function |
Returns the review contents of a checkout pane. Overrides CheckoutPanePluginInterface:: |
|
PaymentMethodPane:: |
public | function |
Returns the settings form for a checkout pane. Overrides CheckoutPanePluginBase:: |
|
PaymentMethodPane:: |
public | function |
Returns the contents of a checkout pane. Overrides CheckoutPanePluginInterface:: |
|
PaymentMethodPane:: |
public | function |
Constructs a PaymentMethodPane object. Overrides CheckoutPanePluginBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
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. |