class StripeReview in Commerce Stripe 8
Adds payment intent confirmation for Stripe.
This checkout pane is required. It ensures that the last step in the checkout performs authentication and confirmation of the payment intent. If the customer's card is not enrolled in 3DS then the form will submit as normal. Otherwise a modal will appear for the customer to authenticate and approve of the charge.
Plugin annotation
@CommerceCheckoutPane(
id = "stripe_review",
label = @Translation("Stripe review"),
default_step = "review",
wrapper_element = "container",
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase implements CheckoutPaneInterface, ContainerFactoryPluginInterface
- class \Drupal\commerce_stripe\Plugin\Commerce\CheckoutPane\StripeReview
- class \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase implements CheckoutPaneInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of StripeReview
File
- src/
Plugin/ Commerce/ CheckoutPane/ StripeReview.php, line 31
Namespace
Drupal\commerce_stripe\Plugin\Commerce\CheckoutPaneView source
class StripeReview extends CheckoutPaneBase {
/**
* The logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'button_id' => 'edit-actions-next',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationSummary() {
return $this
->t('Button id is @id', [
'@id' => $this->configuration['button_id'],
]);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['button_id'] = [
'#type' => 'textfield',
'#title' => $this
->t('Button HTML ID'),
'#description' => $this
->t('The HTML ID of the button that submits the checkout form. Change this if you have customized the default submit button.'),
'#default_value' => $this->configuration['button_id'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if (!$form_state
->getErrors()) {
$values = $form_state
->getValue($form['#parents']);
$this->configuration['button_id'] = $values['button_id'];
}
}
/**
* {@inheritdoc}
*/
public function isVisible() {
$gateway = $this->order
->get('payment_gateway');
if ($gateway
->isEmpty() || empty($gateway->entity)) {
return FALSE;
}
return $gateway->entity
->getPlugin() instanceof StripeInterface;
}
/**
* {@inheritdoc}
*/
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
// The only point of this pane is passing the stripe payment intent ID (and
// some other data) to js when first loading the page (and not when
// submitting the form).
if (!empty($form_state
->getValues()) || !empty($form_state
->getUserInput())) {
return $pane_form;
}
$intent_id = $this->order
->getData('stripe_intent');
/** @var \Drupal\commerce_stripe\Plugin\Commerce\PaymentGateway\StripeInterface $stripe_plugin */
$stripe_plugin = $this->order
->get('payment_gateway')->entity
->getPlugin();
if ($intent_id !== NULL) {
try {
$intent = PaymentIntent::retrieve($intent_id);
} catch (ApiErrorException $e) {
ErrorHelper::handleException($e);
}
}
else {
$payment_process_pane = $this->checkoutFlow
->getPane('payment_process');
assert($payment_process_pane instanceof CheckoutPaneInterface);
$capture = $payment_process_pane
->getConfiguration()['capture'];
$intent = $stripe_plugin
->createPaymentIntent($this->order, $capture);
}
if ($intent->status === PaymentIntent::STATUS_REQUIRES_PAYMENT_METHOD) {
$payment_method = $this->order
->get('payment_method')->entity;
assert($payment_method instanceof PaymentMethodInterface);
$payment_method_remote_id = $payment_method
->getRemoteId();
$intent = PaymentIntent::update($intent->id, [
'payment_method' => $payment_method_remote_id,
]);
}
// To display validation errors.
$pane_form['payment_errors'] = [
'#type' => 'markup',
'#markup' => '<div id="payment-errors"></div>',
'#weight' => -200,
];
$pane_form['#attached']['library'][] = 'commerce_stripe/stripe';
$pane_form['#attached']['library'][] = 'commerce_stripe/checkout_review';
$pane_form['#attached']['drupalSettings']['commerceStripe'] = [
'publishableKey' => $stripe_plugin
->getPublishableKey(),
'clientSecret' => $intent->client_secret,
'buttonId' => $this->configuration['button_id'],
'orderId' => $this->order
->id(),
'paymentMethod' => $intent->payment_method,
];
$profiles = $this->order
->collectProfiles();
if (isset($profiles['shipping']) && !$profiles['shipping']
->get('address')
->isEmpty()) {
$pane_form['#attached']['drupalSettings']['commerceStripe']['shipping'] = $profiles['shipping']
->get('address')
->first()
->toArray();
}
$cacheability = new CacheableMetadata();
$cacheability
->addCacheableDependency($this->order);
$cacheability
->setCacheMaxAge(0);
$cacheability
->applyTo($pane_form);
return $pane_form;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CheckoutPaneBase:: |
protected | property | The parent checkout flow. | |
CheckoutPaneBase:: |
protected | property | The entity type manager. | |
CheckoutPaneBase:: |
protected | property | The current order. | |
CheckoutPaneBase:: |
public | function |
Builds a summary of the pane values. Overrides CheckoutPaneInterface:: |
3 |
CheckoutPaneBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
CheckoutPaneBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
7 |
CheckoutPaneBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
CheckoutPaneBase:: |
public | function |
Gets the pane display label. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Gets the pane ID. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Gets the pane label. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Gets the pane step ID. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Gets the pane weight. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Gets the pane wrapper element. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
CheckoutPaneBase:: |
public | function |
Sets the current order. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Sets the pane step ID. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Sets the pane weight. Overrides CheckoutPaneInterface:: |
|
CheckoutPaneBase:: |
public | function |
Handles the submission of an pane form. Overrides CheckoutPaneInterface:: |
7 |
CheckoutPaneBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
CheckoutPaneBase:: |
public | function |
Validates the pane form. Overrides CheckoutPaneInterface:: |
4 |
CheckoutPaneBase:: |
public | function |
Constructs a new CheckoutPaneBase object. Overrides PluginBase:: |
6 |
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. | |
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. | |
StripeReview:: |
protected | property | The logger. | |
StripeReview:: |
public | function |
Form constructor. Overrides CheckoutPaneBase:: |
|
StripeReview:: |
public | function |
Builds a summary of the pane configuration. Overrides CheckoutPaneBase:: |
|
StripeReview:: |
public | function |
Builds the pane form. Overrides CheckoutPaneInterface:: |
|
StripeReview:: |
public | function |
Gets default configuration for this plugin. Overrides CheckoutPaneBase:: |
|
StripeReview:: |
public | function |
Determines whether the pane is visible. Overrides CheckoutPaneBase:: |
|
StripeReview:: |
public | function |
Form submission handler. Overrides CheckoutPaneBase:: |