Permalink
Browse files

Added return handling

  • Loading branch information...
nehalist committed Jul 10, 2018
1 parent 53adb0e commit 78ec5008f5699e7c6334a580f682c96ef940590b
@@ -0,0 +1,77 @@
<?php
/**
* Shop System Plugins - Terms of Use
*
* The plugins offered are provided free of charge by Wirecard AG and are explicitly not part
* of the Wirecard AG range of products and services.
*
* They have been tested and approved for full functionality in the standard configuration
* (status on delivery) of the corresponding shop system. They are under General Public
* License version 3 (GPLv3) and can be used, developed and passed on to third parties under
* the same terms.
*
* However, Wirecard AG does not provide any guarantee or accept any liability for any errors
* occurring when used in an enhanced, customized shop system configuration.
*
* Operation in an enhanced, customized configuration is at your own risk and requires a
* comprehensive test phase by the user of the plugin.
*
* Customers use the plugins at their own risk. Wirecard AG does not guarantee their full
* functionality neither does Wirecard AG assume liability for any disadvantages related to
* the use of the plugins. Additionally, Wirecard AG does not guarantee the full functionality
* for customized shop systems or installed plugins of other vendors of plugins within the same
* shop system.
*
* Customers are responsible for testing the plugin's functionality before starting productive
* operation.
*
* By installing the plugin into the shop system the customer agrees to these terms of use.
* Please do not use the plugin if you do not agree to these terms of use!
*/
namespace WirecardShopwareElasticEngine\Components\Actions;
class ErrorAction implements Action
{
const PROCESSING_FAILED = 1;
const FAILURE_RESPONSE = 2;
const PAYMENT_CANCELED = 3;
/**
* @var int
*/
protected $code;
/**
* @var string
*/
protected $message;
/**
* ErrorAction constructor.
*
* @param int $code
* @param string $message
*/
public function __construct($code, $message)
{
$this->code = $code;
$this->message = $message;
}
/**
* @return int
*/
public function getCode()
{
return $this->code;
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
}
@@ -0,0 +1,67 @@
<?php
/**
* Shop System Plugins - Terms of Use
*
* The plugins offered are provided free of charge by Wirecard AG and are explicitly not part
* of the Wirecard AG range of products and services.
*
* They have been tested and approved for full functionality in the standard configuration
* (status on delivery) of the corresponding shop system. They are under General Public
* License version 3 (GPLv3) and can be used, developed and passed on to third parties under
* the same terms.
*
* However, Wirecard AG does not provide any guarantee or accept any liability for any errors
* occurring when used in an enhanced, customized shop system configuration.
*
* Operation in an enhanced, customized configuration is at your own risk and requires a
* comprehensive test phase by the user of the plugin.
*
* Customers use the plugins at their own risk. Wirecard AG does not guarantee their full
* functionality neither does Wirecard AG assume liability for any disadvantages related to
* the use of the plugins. Additionally, Wirecard AG does not guarantee the full functionality
* for customized shop systems or installed plugins of other vendors of plugins within the same
* shop system.
*
* Customers are responsible for testing the plugin's functionality before starting productive
* operation.
*
* By installing the plugin into the shop system the customer agrees to these terms of use.
* Please do not use the plugin if you do not agree to these terms of use!
*/
namespace WirecardShopwareElasticEngine\Components\Services;
use Wirecard\PaymentSdk\Response\Response;
abstract class Handler
{
protected $devEnvironments = ['dev', 'development'];
/**
* @param int $orderNumber
* @return string
*/
protected function getOrderNumberForTransaction($orderNumber)
{
if (in_array(getenv('SHOPWARE_ENV'), $this->devEnvironments)) {
$orderNumber = uniqid() . '-' . $orderNumber;
}
return $orderNumber;
}
/**
* @param Response $response
* @return string
*/
protected function getOrderNumberFromResponse(Response $response)
{
$orderNumber = $response->findElement('order-number');
if (in_array(getenv('SHOPWARE_ENV'), $this->devEnvironments) && strpos($orderNumber, '-') >= 0) {
$orderNumber = explode('-', $orderNumber)[1];
}
return $orderNumber;
}
}
@@ -33,16 +33,22 @@
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Shopware\Models\Order\Order;
use Wirecard\PaymentSdk\Entity\Redirect;
use Wirecard\PaymentSdk\Response\FailureResponse;
use Wirecard\PaymentSdk\Response\InteractionResponse;
use Wirecard\PaymentSdk\Response\Response;
use Wirecard\PaymentSdk\Response\SuccessResponse;
use Wirecard\PaymentSdk\TransactionService;
use WirecardShopwareElasticEngine\Components\Actions\Action;
use WirecardShopwareElasticEngine\Components\Actions\ErrorAction;
use WirecardShopwareElasticEngine\Components\Actions\RedirectAction;
use WirecardShopwareElasticEngine\Components\Data\OrderSummary;
use WirecardShopwareElasticEngine\Exception\ArrayKeyNotFoundException;
use WirecardShopwareElasticEngine\Exception\OrderNotFoundException;
use WirecardShopwareElasticEngine\Models\Transaction;
class PaymentHandler
class PaymentHandler extends Handler
{
/**
* @var OrderSummary
@@ -121,17 +127,70 @@ public function execute(
]);
switch (true) {
case $response instanceof SuccessResponse:
case $response instanceof InteractionResponse:
$this->updateOrder($response->getTransactionId(), $orderSummary);
$this->createTransaction($response, $orderSummary);
return new RedirectAction($response->getRedirectUrl());
case $response instanceof FailureResponse:
// todo: handle failure
exit();
$this->logger->error('Failure response', $response->getData());
return new ErrorAction(ErrorAction::FAILURE_RESPONSE, 'Failure response');
default:
// todo: throw exception
return null;
$this->logger->error('Processing failed', $response->getData());
return new ErrorAction(ErrorAction::PROCESSING_FAILED, 'Payment processing failed');
}
}
/**
* Updates the shopware order by setting the proper transaction ID.
*
* @param $transactionId
* @param OrderSummary $orderSummary
*/
private function updateOrder($transactionId, OrderSummary $orderSummary)
{
$order = $this->em->getRepository(Order::class)
->findOneBy([
'number' => $orderSummary->getOrderNumber()
]);
if (! $order) {
throw new OrderNotFoundException($orderSummary->getOrderNumber(), $transactionId);
}
$order->setTransactionId($transactionId);
$this->em->flush();
}
/**
* Creates a new transaction with available data from our response and order summary.
*
* @param Response $response
* @param OrderSummary $orderSummary
*/
private function createTransaction(Response $response, OrderSummary $orderSummary)
{
$transaction = new Transaction();
if ($response instanceof SuccessResponse || $response instanceof InteractionResponse) {
$transaction->setTransactionId($response->getTransactionId());
}
$transaction->setOrderNumber($orderSummary->getOrderNumber());
$transaction->setResponse($response->getData());
$transaction->setAmount($orderSummary->getAmount()->getValue());
$transaction->setCurrency($orderSummary->getAmount()->getCurrency());
$transaction->setTransactionType($orderSummary->getPayment()->getPaymentConfig()->getTransactionType());
$transaction->setCreatedAt(new \DateTime());
$this->em->persist($transaction);
$this->em->flush();
}
/**
@@ -149,11 +208,12 @@ private function prepareTransaction(OrderSummary $orderSummary, Redirect $redire
$payment = $orderSummary->getPayment();
$paymentConfig = $payment->getPaymentConfig();
$transaction = $payment->getTransaction();
$orderNumber = $this->getOrderNumberForTransaction($orderSummary->getOrderNumber());
$transaction->setRedirect($redirect);
$transaction->setAmount($orderSummary->getAmount());
$transaction->setNotificationUrl($notificationUrl);
$transaction->setOrderNumber($orderSummary->getOrderNumber());
$transaction->setOrderNumber($orderNumber);
if ($paymentConfig->sendBasket()) {
$transaction->setBasket($orderSummary->getBasketMapper()->getWirecardBasket());
@@ -166,8 +226,8 @@ private function prepareTransaction(OrderSummary $orderSummary, Redirect $redire
$transaction->setLocale($orderSummary->getUserMapper()->getLocale());
}
if ($paymentConfig->sendDescriptor() && ! in_array(getenv('SHOPWARE_ENV'), ['dev', 'development'])) {
$transaction->setDescriptor($this->getDescriptor($orderSummary->getOrderNumber()));
if ($paymentConfig->sendDescriptor()) {
$transaction->setDescriptor($this->getDescriptor($orderNumber));
}
}
@@ -33,14 +33,19 @@
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Shopware\Components\Routing\RouterInterface;
use Shopware\Models\Order\Order;
use Wirecard\PaymentSdk\Response\FailureResponse;
use Wirecard\PaymentSdk\Response\Response;
use Wirecard\PaymentSdk\Response\SuccessResponse;
use WirecardShopwareElasticEngine\Components\Actions\Action;
use WirecardShopwareElasticEngine\Components\Actions\ErrorAction;
use WirecardShopwareElasticEngine\Components\Actions\RedirectAction;
use WirecardShopwareElasticEngine\Exception\OrderNotFoundException;
use WirecardShopwareElasticEngine\Exception\ParentTransactionNotFoundException;
use WirecardShopwareElasticEngine\Models\Transaction;
class ReturnHandler
class ReturnHandler extends Handler
{
/**
* @var EntityManagerInterface
@@ -52,8 +57,14 @@ class ReturnHandler
*/
protected $logger;
public function __construct(EntityManagerInterface $em, LoggerInterface $logger)
/**
* @var RouterInterface
*/
protected $router;
public function __construct(RouterInterface $router, EntityManagerInterface $em, LoggerInterface $logger)
{
$this->router = $router;
$this->em = $em;
$this->logger = $logger;
}
@@ -80,9 +91,9 @@ public function execute(Response $response)
*/
protected function handleSuccess(SuccessResponse $response)
{
$transactionId = $response->getTransactionId();
$paymentUniqueId = $response->getProviderTransactionId();
$orderNumber = $response->findElement('order-number');
$transactionId = $response->getTransactionId();
$parentTransactionId = $response->getParentTransactionId();
$orderNumber = $this->getOrderNumberFromResponse($response);
$order = $this->em
->getRepository(Order::class)
@@ -91,10 +102,41 @@ protected function handleSuccess(SuccessResponse $response)
]);
if (! $order) {
// todo: error handling
throw new OrderNotFoundException($orderNumber, $transactionId);
}
// TemporaryID is set to the order number, since the returned `RedirectAction` will contain this ID
// as `sUniqueID` to get information about what order has been processed and show proper information.
$order->setTemporaryId($orderNumber);
$parentTransaction = $this->em
->getRepository(Transaction::class)
->findOneBy([
'transactionId' => $response->getParentTransactionId(),
]);
if (! $parentTransaction) {
throw new ParentTransactionNotFoundException($parentTransactionId, $transactionId);
}
return new RedirectAction(null);
$transaction = new Transaction();
$transaction->setTransactionId($response->getTransactionId());
$transaction->setProviderTransactionId($response->getProviderTransactionId());
$transaction->setCurrency($response->getRequestedAmount()->getCurrency());
$transaction->setAmount($response->getRequestedAmount()->getValue());
$transaction->setTransactionType($response->getTransactionType());
$transaction->setResponse($response->getData());
$transaction->setCreatedAt(new \DateTime());
$this->em->persist($transaction);
$this->em->flush();
return new RedirectAction($this->router->assemble([
'module' => 'frontend',
'controller' => 'checkout',
'action' => 'finish',
'sUniqueID' => $order->getTemporaryId()
]));
}
/**
@@ -103,6 +145,8 @@ protected function handleSuccess(SuccessResponse $response)
*/
protected function handleFailure(FailureResponse $response)
{
return new RedirectAction(null);
$this->logger->error('Return handling failed', $response->getData());
return new ErrorAction(ErrorAction::FAILURE_RESPONSE, 'Failure response');
}
}
Oops, something went wrong.

0 comments on commit 78ec500

Please sign in to comment.