Skip to content

Commit 755924e

Browse files
jackbravobojanz
authored andcommitted
Issue #2906360 by jackbravo, bojanz: Move OffsitePaymentGatewayInterface::onNotify to its own interface
1 parent 0b26454 commit 755924e

5 files changed

Lines changed: 86 additions & 55 deletions

File tree

modules/payment/commerce_payment.routing.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ entity.commerce_payment_method.collection:
7070
commerce_payment.checkout.return:
7171
path: '/checkout/{commerce_order}/{step}/return'
7272
defaults:
73-
_controller: '\Drupal\commerce_payment\Controller\OffsitePaymentController::returnCheckoutPage'
73+
_controller: '\Drupal\commerce_payment\Controller\PaymentCheckoutController::returnPage'
7474
requirements:
7575
_custom_access: '\Drupal\commerce_checkout\Controller\CheckoutController::checkAccess'
7676
_module_dependencies: commerce_checkout
@@ -82,7 +82,7 @@ commerce_payment.checkout.return:
8282
commerce_payment.checkout.cancel:
8383
path: '/checkout/{commerce_order}/{step}/cancel'
8484
defaults:
85-
_controller: '\Drupal\commerce_payment\Controller\OffsitePaymentController::cancelCheckoutPage'
85+
_controller: '\Drupal\commerce_payment\Controller\PaymentCheckoutController::cancelPage'
8686
requirements:
8787
_custom_access: '\Drupal\commerce_checkout\Controller\CheckoutController::checkAccess'
8888
_module_dependencies: commerce_checkout
@@ -94,7 +94,7 @@ commerce_payment.checkout.cancel:
9494
commerce_payment.notify:
9595
path: '/payment/notify/{commerce_payment_gateway}'
9696
defaults:
97-
_controller: '\Drupal\commerce_payment\Controller\OffsitePaymentController::notifyPage'
97+
_controller: '\Drupal\commerce_payment\Controller\PaymentNotificationController::notifyPage'
9898
requirements:
9999
_access: 'TRUE'
100100
options:

modules/payment/src/Controller/OffsitePaymentController.php renamed to modules/payment/src/Controller/PaymentCheckoutController.php

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@
44

55
use Drupal\commerce_checkout\CheckoutOrderManagerInterface;
66
use Drupal\commerce_order\Entity\OrderInterface;
7-
use Drupal\commerce_payment\Entity\PaymentGatewayInterface;
87
use Drupal\commerce_payment\Exception\PaymentGatewayException;
98
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayInterface;
109
use Drupal\Core\Access\AccessException;
1110
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
1211
use Symfony\Component\DependencyInjection\ContainerInterface;
1312
use Symfony\Component\HttpFoundation\Request;
14-
use Symfony\Component\HttpFoundation\Response;
1513

1614
/**
17-
* Provides endpoints for off-site payments.
15+
* Provides checkout endpoints for off-site payments.
1816
*/
19-
class OffsitePaymentController implements ContainerInjectionInterface {
17+
class PaymentCheckoutController implements ContainerInjectionInterface {
2018

2119
/**
2220
* The checkout order manager.
@@ -26,7 +24,7 @@ class OffsitePaymentController implements ContainerInjectionInterface {
2624
protected $checkoutOrderManager;
2725

2826
/**
29-
* Constructs a new CheckoutController object.
27+
* Constructs a new PaymentCheckoutController object.
3028
*
3129
* @param \Drupal\commerce_checkout\CheckoutOrderManagerInterface $checkout_order_manager
3230
* The checkout order manager.
@@ -54,7 +52,7 @@ public static function create(ContainerInterface $container) {
5452
* @param \Symfony\Component\HttpFoundation\Request $request
5553
* The request.
5654
*/
57-
public function returnCheckoutPage(OrderInterface $commerce_order, Request $request) {
55+
public function returnPage(OrderInterface $commerce_order, Request $request) {
5856
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
5957
$payment_gateway = $commerce_order->get('payment_gateway')->entity;
6058
$payment_gateway_plugin = $payment_gateway->getPlugin();
@@ -88,7 +86,7 @@ public function returnCheckoutPage(OrderInterface $commerce_order, Request $requ
8886
* @param \Symfony\Component\HttpFoundation\Request $request
8987
* The request.
9088
*/
91-
public function cancelCheckoutPage(OrderInterface $commerce_order, Request $request) {
89+
public function cancelPage(OrderInterface $commerce_order, Request $request) {
9290
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
9391
$payment_gateway = $commerce_order->get('payment_gateway')->entity;
9492
$payment_gateway_plugin = $payment_gateway->getPlugin();
@@ -105,31 +103,4 @@ public function cancelCheckoutPage(OrderInterface $commerce_order, Request $requ
105103
$checkout_flow_plugin->redirectToStep($previous_step_id);
106104
}
107105

108-
/**
109-
* Provides the "notify" page.
110-
*
111-
* Called the "IPN" or "status" page by some payment providers.
112-
*
113-
* @param \Drupal\commerce_payment\Entity\PaymentGatewayInterface $commerce_payment_gateway
114-
* The payment gateway.
115-
* @param \Symfony\Component\HttpFoundation\Request $request
116-
* The request.
117-
*
118-
* @return \Symfony\Component\HttpFoundation\Response
119-
* The response.
120-
*/
121-
public function notifyPage(PaymentGatewayInterface $commerce_payment_gateway, Request $request) {
122-
$payment_gateway_plugin = $commerce_payment_gateway->getPlugin();
123-
if (!$payment_gateway_plugin instanceof OffsitePaymentGatewayInterface) {
124-
throw new AccessException('Invalid payment gateway provided.');
125-
}
126-
127-
$response = $payment_gateway_plugin->onNotify($request);
128-
if (!$response) {
129-
$response = new Response('', 200);
130-
}
131-
132-
return $response;
133-
}
134-
135106
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Drupal\commerce_payment\Controller;
4+
5+
use Drupal\commerce_payment\Entity\PaymentGatewayInterface;
6+
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\SupportsNotificationsInterface;
7+
use Drupal\Core\Access\AccessException;
8+
use Symfony\Component\HttpFoundation\Request;
9+
use Symfony\Component\HttpFoundation\Response;
10+
11+
/**
12+
* Provides the endpoint for payment notifications.
13+
*/
14+
class PaymentNotificationController {
15+
16+
/**
17+
* Provides the "notify" page.
18+
*
19+
* Also called the "IPN", "status", "webhook" page by payment providers.
20+
*
21+
* @param \Drupal\commerce_payment\Entity\PaymentGatewayInterface $commerce_payment_gateway
22+
* The payment gateway.
23+
* @param \Symfony\Component\HttpFoundation\Request $request
24+
* The request.
25+
*
26+
* @return \Symfony\Component\HttpFoundation\Response
27+
* The response.
28+
*/
29+
public function notifyPage(PaymentGatewayInterface $commerce_payment_gateway, Request $request) {
30+
$payment_gateway_plugin = $commerce_payment_gateway->getPlugin();
31+
if (!$payment_gateway_plugin instanceof SupportsNotificationsInterface) {
32+
throw new AccessException('Invalid payment gateway provided.');
33+
}
34+
35+
$response = $payment_gateway_plugin->onNotify($request);
36+
if (!$response) {
37+
$response = new Response('', 200);
38+
}
39+
40+
return $response;
41+
}
42+
43+
}

modules/payment/src/Plugin/Commerce/PaymentGateway/OffsitePaymentGatewayInterface.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* the payment at the payment provider, they will be redirected back to the
2525
* cancel url.
2626
*/
27-
interface OffsitePaymentGatewayInterface extends PaymentGatewayInterface {
27+
interface OffsitePaymentGatewayInterface extends PaymentGatewayInterface, SupportsNotificationsInterface {
2828

2929
/**
3030
* Gets the URL to the "notify" page.
@@ -63,21 +63,4 @@ public function onReturn(OrderInterface $order, Request $request);
6363
*/
6464
public function onCancel(OrderInterface $order, Request $request);
6565

66-
/**
67-
* Processes the "notify" request.
68-
*
69-
* Note:
70-
* This method can't throw exceptions on failure because some payment
71-
* providers expect an error response to be returned in that case.
72-
* Therefore, the method can log the error itself and then choose which
73-
* response to return.
74-
*
75-
* @param \Symfony\Component\HttpFoundation\Request $request
76-
* The request.
77-
*
78-
* @return \Symfony\Component\HttpFoundation\Response|null
79-
* The response, or NULL to return an empty HTTP 200 response.
80-
*/
81-
public function onNotify(Request $request);
82-
8366
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Drupal\commerce_payment\Plugin\Commerce\PaymentGateway;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
7+
/**
8+
* Defines the interface for gateways which support notifications.
9+
*
10+
* Payment providers can use the notification URL (commerce_payment.notify)
11+
* to inform the site that a new pending/complete payment should be created
12+
* (if the payment happened off-site), or to provide information about an
13+
* existing payment (refunds, disputes, etc).
14+
*/
15+
interface SupportsNotificationsInterface {
16+
17+
/**
18+
* Processes the notification request.
19+
*
20+
* Note:
21+
* This method can't throw exceptions on failure because some payment
22+
* providers expect an error response to be returned in that case.
23+
* Therefore, the method can log the error itself and then choose which
24+
* response to return.
25+
*
26+
* @param \Symfony\Component\HttpFoundation\Request $request
27+
* The request.
28+
*
29+
* @return \Symfony\Component\HttpFoundation\Response|null
30+
* The response, or NULL to return an empty HTTP 200 response.
31+
*/
32+
public function onNotify(Request $request);
33+
34+
}

0 commit comments

Comments
 (0)