Commit 10032eee authored by bojanz's avatar bojanz

Issue #2838978 by bojanz: Allow offsite gateways to be used without checkout

parent c568ca22
......@@ -4,6 +4,7 @@ namespace Drupal\commerce_payment\Plugin\Commerce\CheckoutPane;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_payment\Exception\DeclineException;
use Drupal\commerce_payment\Exception\PaymentGatewayException;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayInterface;
......@@ -11,6 +12,7 @@ use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OnsitePaymentGatewayI
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -164,6 +166,8 @@ class PaymentProcess extends CheckoutPaneBase implements ContainerFactoryPluginI
'#type' => 'commerce_payment_gateway_form',
'#operation' => 'offsite-payment',
'#default_value' => $payment,
'#return_url' => $this->buildReturnUrl($this->order),
'#cancel_url' => $this->buildCancelUrl($this->order),
];
$complete_form['actions']['next']['#value'] = $this->t('Proceed to @gateway', [
......@@ -174,6 +178,38 @@ class PaymentProcess extends CheckoutPaneBase implements ContainerFactoryPluginI
}
}
/**
* Builds the URL to the "return" page.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order.
*
* @return string
* The "return" page url.
*/
protected function buildReturnUrl(OrderInterface $order) {
return Url::fromRoute('commerce_payment.checkout.return', [
'commerce_order' => $order->id(),
'step' => 'payment',
], ['absolute' => TRUE])->toString();
}
/**
* Builds the URL to the "cancel" page.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order.
*
* @return string
* The "cancel" page url.
*/
protected function buildCancelUrl(OrderInterface $order) {
return Url::fromRoute('commerce_payment.checkout.cancel', [
'commerce_order' => $order->id(),
'step' => 'payment',
], ['absolute' => TRUE])->toString();
}
/**
* Redirects to a previous checkout step on error.
*
......
......@@ -11,26 +11,6 @@ use Symfony\Component\HttpFoundation\Request;
*/
abstract class OffsitePaymentGatewayBase extends PaymentGatewayBase implements OffsitePaymentGatewayInterface {
/**
* {@inheritdoc}
*/
public function getReturnUrl(OrderInterface $order) {
return Url::fromRoute('commerce_payment.checkout.return', [
'commerce_order' => $order->id(),
'step' => 'payment',
], ['absolute' => TRUE]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl(OrderInterface $order) {
return Url::fromRoute('commerce_payment.checkout.cancel', [
'commerce_order' => $order->id(),
'step' => 'payment',
], ['absolute' => TRUE]);
}
/**
* {@inheritdoc}
*/
......
......@@ -26,28 +26,6 @@ use Symfony\Component\HttpFoundation\Request;
*/
interface OffsitePaymentGatewayInterface extends PaymentGatewayInterface {
/**
* Gets the URL to the "return" page.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order.
*
* @return \Drupal\Core\Url
* The "return" page url.
*/
public function getReturnUrl(OrderInterface $order);
/**
* Gets the URL to the "cancel" page.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order.
*
* @return \Drupal\Core\Url
* The "cancel" page url.
*/
public function getCancelUrl(OrderInterface $order);
/**
* Gets the URL to the "notify" page.
*
......
......@@ -19,6 +19,20 @@ abstract class PaymentOffsiteForm extends PaymentGatewayFormBase {
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.');
}
return $form;
}
/**
* Builds the redirect form.
*
......
......@@ -12,9 +12,10 @@ class PaymentOffsiteForm extends BasePaymentOffsiteForm {
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
/** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
$payment = $this->entity;
$order = $payment->getOrder();
/** @var \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayInterface $payment_gateway_plugin */
$payment_gateway_plugin = $payment->getPaymentGateway()->getPlugin();
$redirect_method = $payment_gateway_plugin->getConfiguration()['redirect_method'];
......@@ -22,6 +23,7 @@ class PaymentOffsiteForm extends BasePaymentOffsiteForm {
$redirect_url = Url::fromRoute('commerce_payment_example.dummy_redirect_post')->toString();
}
else {
$order = $payment->getOrder();
// Gateways that use the GET redirect method usually perform an API call
// that prepares the remote payment and provides the actual url to
// redirect to. Any params received from that API call that need to be
......@@ -31,8 +33,8 @@ class PaymentOffsiteForm extends BasePaymentOffsiteForm {
$redirect_url = Url::fromRoute('commerce_payment_example.dummy_redirect_302', [], ['absolute' => TRUE])->toString();
}
$data = [
'return' => $payment_gateway_plugin->getReturnUrl($order)->toString(),
'cancel' => $payment_gateway_plugin->getCancelUrl($order)->toString(),
'return' => $form['#return_url'],
'cancel' => $form['#cancel_url'],
'total' => $payment->getAmount()->getNumber(),
];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment