allow possibility for payment module to return a response

This commit is contained in:
Manuel Raynaud
2014-02-06 12:24:52 +01:00
parent c52ec3dbaf
commit a6f0a38a7b
7 changed files with 87 additions and 33 deletions

View File

@@ -176,7 +176,11 @@ class Module extends BaseAction implements EventSubscriberInterface
$paymentModuleInstance = $this->container->get(sprintf('module.%s', $paymentModule->getCode()));
$paymentModuleInstance->pay($order);
$response = $paymentModuleInstance->pay($order);
if (null !== $response && $response instanceof \Thelia\Core\HttpFoundation\Response) {
$event->setResponse($response);
}
}
/**

View File

@@ -340,12 +340,6 @@ class Order extends BaseAction implements EventSubscriberInterface
$event->getDispatcher()->dispatch(TheliaEvents::ORDER_BEFORE_PAYMENT, new OrderEvent($placedOrder));
/* clear session */
$session
->setProcessedOrder($placedOrder)
->setOrder(new \Thelia\Model\Order())
;
/* but memorize placed order */
$event->setOrder(new \Thelia\Model\Order());
$event->setPlacedOrder($placedOrder);
@@ -353,14 +347,14 @@ class Order extends BaseAction implements EventSubscriberInterface
/* empty cart */
$dispatcher = $event->getDispatcher();
$dispatcher->dispatch(
TheliaEvents::CART_CLEAR, new CartEvent($this->getCart($dispatcher, $this->request)));
/* call pay method */
$payEvent = new OrderPaymentEvent($placedOrder);
$dispatcher->dispatch(TheliaEvents::MODULE_PAY, $payEvent);
if ($payEvent->hasResponse()) {
$event->setResponse($payEvent->getResponse());
}
}
/**

View File

@@ -135,7 +135,12 @@ trait CartTrait
$id = null;
if (ConfigQuery::read("cart.session_only", 0) == 0) {
$id = uniqid('', true);
setcookie("thelia_cart", $id, time()+ConfigQuery::read("cart.cookie_lifetime", 60*60*24*365));
setcookie(
"thelia_cart",
$id,
time()+ConfigQuery::read("cart.cookie_lifetime", 60*60*24*365),
'/'
);
}

View File

@@ -24,6 +24,7 @@
namespace Thelia\Core\Event\Order;
use Thelia\Core\Event\ActionEvent;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Model\Order;
class OrderEvent extends ActionEvent
@@ -39,6 +40,11 @@ class OrderEvent extends ActionEvent
protected $status = null;
protected $deliveryRef = null;
/**
* @var Response
*/
protected $response;
/**
* @param Order $order
*/
@@ -206,4 +212,28 @@ class OrderEvent extends ActionEvent
{
return $this->deliveryRef;
}
/**
* @param Response $response
* @return $this
*/
public function setResponse(Response $response)
{
$this->response = $response;
return $this;
}
/**
* @return Response
*/
public function getResponse()
{
return $this->response;
}
public function hasResponse()
{
return null !== $this->response;
}
}

View File

@@ -24,6 +24,7 @@
namespace Thelia\Core\Event\Order;
use Thelia\Core\Event\ActionEvent;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Model\Order;
/**
@@ -38,6 +39,11 @@ class OrderPaymentEvent extends ActionEvent
*/
protected $order;
/**
* @var \Thelia\Core\HttpFoundation\Response
*/
protected $response;
public function __construct(Order $order) {
$this->order = $order;
}
@@ -49,4 +55,29 @@ class OrderPaymentEvent extends ActionEvent
{
return $this->order;
}
/**
* @param \Thelia\Core\HttpFoundation\Response $response
*/
public function setResponse(Response $response)
{
$this->response = $response;
return $this;
}
/**
* @return \Thelia\Core\HttpFoundation\Response
*/
public function getResponse()
{
return $this->response;
}
public function hasResponse()
{
return null !== $this->response;
}
}

View File

@@ -253,26 +253,6 @@ class Session extends BaseSession
return $this->get("thelia.order");
}
/**
* @param Order $order
* @return $this
*/
public function setProcessedOrder(Order $order)
{
$this->set('thelia.order.processed', $order);
return $this;
}
/**
* Return an order already processed, usefull for payment modules
* @return Order
*/
public function getProcessedOrder()
{
return $this->get('thelia.order.processed');
}
/**
* Set consumed coupons by the Customer
*

View File

@@ -23,6 +23,7 @@
namespace Front\Controller;
use Propel\Runtime\Exception\PropelException;
use Thelia\Cart\CartTrait;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\Event\PdfEvent;
use Thelia\Core\HttpFoundation\Response;
@@ -51,6 +52,7 @@ use Thelia\Tools\URL;
*/
class OrderController extends BaseFrontController
{
use CartTrait;
/**
* set delivery address
* set delivery module
@@ -199,7 +201,11 @@ class OrderController extends BaseFrontController
if (null !== $placedOrder && null !== $placedOrder->getId()) {
/* order has been placed */
$this->redirect(URL::getInstance()->absoluteUrl($this->getRoute('order.placed', array('order_id' => $orderEvent->getPlacedOrder()->getId()))));
if($orderEvent->hasResponse()) {
return $orderEvent->getResponse();
} else {
$this->redirect(URL::getInstance()->absoluteUrl($this->getRoute('order.placed', array('order_id' => $orderEvent->getPlacedOrder()->getId()))));
}
} else {
/* order has not been placed */
$this->redirectToRoute('cart.view');
@@ -222,6 +228,10 @@ class OrderController extends BaseFrontController
if (null === $customer || $placedOrder->getCustomerId() !== $customer->getId()) {
throw new TheliaProcessException("Received placed order id does not belong to the current customer", TheliaProcessException::PLACED_ORDER_ID_BAD_CURRENT_CUSTOMER, $placedOrder);
}
$session = $this->getRequest()->getSession();
$this->createCart($this->getRequest()->getSession());
$session->setOrder(new Order());
$this->getParserContext()->set("placed_order_id", $placedOrder->getId());
}