abstract class PaymentOffsiteForm in Commerce Core 8.2
Same name in this branch
- 8.2 modules/payment/src/PluginForm/PaymentOffsiteForm.php \Drupal\commerce_payment\PluginForm\PaymentOffsiteForm
- 8.2 modules/payment_example/src/PluginForm/OffsiteRedirect/PaymentOffsiteForm.php \Drupal\commerce_payment_example\PluginForm\OffsiteRedirect\PaymentOffsiteForm
Provides the base class for payment off-site forms.
Payment gateways are expected to inherit from this class and provide a buildConfigurationForm() method which calls buildRedirectForm() with the right parameters.
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\PaymentOffsiteForm
- class \Drupal\commerce_payment\PluginForm\PaymentGatewayFormBase implements PaymentGatewayFormInterface uses DependencySerializationTrait, StringTranslationTrait
Expanded class hierarchy of PaymentOffsiteForm
1 file declares its use of PaymentOffsiteForm
- PaymentOffsiteForm.php in modules/
payment_example/ src/ PluginForm/ OffsiteRedirect/ PaymentOffsiteForm.php
File
- modules/
payment/ src/ PluginForm/ PaymentOffsiteForm.php, line 17
Namespace
Drupal\commerce_payment\PluginFormView source
abstract class PaymentOffsiteForm extends PaymentGatewayFormBase {
// The redirect methods.
const REDIRECT_GET = 'get';
const REDIRECT_POST = 'post';
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
if (empty($form['#return_url'])) {
throw new \InvalidArgumentException('The offsite-payment form requires the #return_url property.');
}
if (empty($form['#cancel_url'])) {
throw new \InvalidArgumentException('The offsite-payment form requires the #cancel_url property.');
}
if (!isset($form['#capture'])) {
$form['#capture'] = TRUE;
}
return $form;
}
/**
* Builds the redirect form.
*
* @param array $form
* The plugin form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param string $redirect_url
* The redirect url.
* @param array $data
* Data that should be sent along.
* @param string $redirect_method
* The redirect method (REDIRECT_GET or REDIRECT_POST constant).
*
* @return array
* The redirect form, if $redirect_method is REDIRECT_POST.
*
* @throws \Drupal\commerce\Response\NeedsRedirectException
* The redirect exception, if $redirect_method is REDIRECT_GET.
*/
protected function buildRedirectForm(array $form, FormStateInterface $form_state, $redirect_url, array $data, $redirect_method = self::REDIRECT_GET) {
if ($redirect_method == self::REDIRECT_POST) {
$form['#attached']['library'][] = 'commerce_payment/offsite_redirect';
$form['#process'][] = [
get_class($this),
'processRedirectForm',
];
$form['#redirect_url'] = $redirect_url;
foreach ($data as $key => $value) {
$form[$key] = [
'#type' => 'hidden',
'#value' => $value,
// Ensure the correct keys by sending values from the form root.
'#parents' => [
$key,
],
];
}
// The key is prefixed with 'commerce_' to prevent conflicts with $data.
$form['commerce_message'] = [
'#markup' => '<div class="checkout-help">' . t('Please wait while you are redirected to the payment server. If nothing happens within 10 seconds, please click on the button below.') . '</div>',
'#weight' => -10,
];
}
else {
$redirect_url = Url::fromUri($redirect_url, [
'absolute' => TRUE,
'query' => $data,
])
->toString();
throw new NeedsRedirectException($redirect_url);
}
return $form;
}
/**
* Prepares the complete form for a POST redirect.
*
* Sets the form #action, adds a class for the JS to target.
* Workaround for buildConfigurationForm() not receiving $complete_form.
*
* @param array $form
* The plugin form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*
* @return array
* The processed form element.
*/
public static function processRedirectForm(array $form, FormStateInterface $form_state, array &$complete_form) {
$complete_form['#action'] = $form['#redirect_url'];
$complete_form['#attributes']['class'][] = 'payment-redirect-form';
// The form actions are hidden by default, but needed in this case.
$complete_form['actions']['#access'] = TRUE;
foreach (Element::children($complete_form['actions']) as $element_name) {
$complete_form['actions'][$element_name]['#access'] = TRUE;
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
// Nothing. Off-site payment gateways do not submit forms to Drupal.
}
}
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 |
Gets the form element to which errors should be assigned. Overrides PaymentGatewayFormInterface:: |
1 |
PaymentGatewayFormBase:: |
public | function |
Sets the form entity. Overrides PaymentGatewayFormInterface:: |
|
PaymentOffsiteForm:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
1 |
PaymentOffsiteForm:: |
protected | function | Builds the redirect form. | |
PaymentOffsiteForm:: |
public static | function | Prepares the complete form for a POST redirect. | |
PaymentOffsiteForm:: |
constant | |||
PaymentOffsiteForm:: |
constant | |||
PaymentOffsiteForm:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
PluginFormBase:: |
protected | property | The plugin this form is for. | 3 |
PluginFormBase:: |
public | function |
Sets the plugin for this object. Overrides PluginAwareInterface:: |
1 |
PluginFormBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
2 |
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. |