PaymentOffsiteForm.php in Commerce Core 8.2
File
modules/payment/src/PluginForm/PaymentOffsiteForm.php
View source
<?php
namespace Drupal\commerce_payment\PluginForm;
use Drupal\commerce\Response\NeedsRedirectException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
abstract class PaymentOffsiteForm extends PaymentGatewayFormBase {
const REDIRECT_GET = 'get';
const REDIRECT_POST = 'post';
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;
}
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,
'#parents' => [
$key,
],
];
}
$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;
}
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';
$complete_form['actions']['#access'] = TRUE;
foreach (Element::children($complete_form['actions']) as $element_name) {
$complete_form['actions'][$element_name]['#access'] = TRUE;
}
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
}