|
2 | 2 |
|
3 | 3 | namespace OxidEsales\MonduPayment\Controller;
|
4 | 4 |
|
| 5 | +use OxidEsales\Eshop\Application\Model\Basket; |
| 6 | +use OxidEsales\Eshop\Application\Model\Order; |
| 7 | +use OxidEsales\Eshop\Application\Model\User; |
| 8 | +use OxidEsales\Eshop\Core\Exception\StandardException; |
5 | 9 | use OxidEsales\Eshop\Core\Registry;
|
| 10 | +use OxidEsales\Eshop\Core\Request; |
| 11 | +use OxidEsales\Eshop\Core\UtilsView; |
| 12 | +use OxidEsales\MonduPayment\Core\Http\MonduClient; |
6 | 13 | use OxidEsales\MonduPayment\Core\Utils\MonduHelper;
|
| 14 | +use Symfony\Component\Config\Definition\Exception\Exception; |
7 | 15 |
|
8 | 16 | class OrderController extends OrderController_parent
|
9 | 17 | {
|
10 |
| - public function isMonduPayment() |
11 |
| - { |
12 |
| - $session = Registry::getSession(); |
13 |
| - $paymentId = $session->getVariable("paymentid"); |
14 |
| - |
15 |
| - return MonduHelper::isMonduPayment($paymentId); |
16 |
| - } |
17 |
| - |
18 |
| - public function getPaymentPageUrl() |
19 |
| - { |
20 |
| - $shopUrl = $this->getConfig()->getShopSecureHomeURL(); |
21 |
| - return $shopUrl . '&cl=payment&payerror=2'; |
22 |
| - } |
| 18 | + private MonduClient $_client; |
| 19 | + private User|null|false $_oUser; |
| 20 | + |
| 21 | + public function __construct() |
| 22 | + { |
| 23 | + parent::__construct(); |
| 24 | + |
| 25 | + $this->_client = oxNew(MonduClient::class); |
| 26 | + $this->_oUser = $this->getUser(); |
| 27 | + } |
| 28 | + |
| 29 | + public function isMonduPayment() |
| 30 | + { |
| 31 | + $session = Registry::getSession(); |
| 32 | + $paymentId = $session->getVariable('paymentid'); |
| 33 | + |
| 34 | + return MonduHelper::isMonduPayment($paymentId); |
| 35 | + } |
| 36 | + |
| 37 | + public function getPaymentPageUrl() |
| 38 | + { |
| 39 | + $shopUrl = \OxidEsales\Eshop\Core\Registry::getConfig()->getShopSecureHomeURL(); |
| 40 | + return $shopUrl . '&cl=payment&payerror=2'; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @throws \Exception |
| 45 | + */ |
| 46 | + public function execute() |
| 47 | + { |
| 48 | + if($this->isMonduPayment()){ |
| 49 | + $orderUuid = Registry::get(Request::class)->getRequestEscapedParameter('order_uuid'); |
| 50 | + |
| 51 | + if (!$orderUuid) { |
| 52 | + throw new \Exception('Mondu: Not found'); |
| 53 | + } |
| 54 | + |
| 55 | + $monduOrder = $this->_client->getMonduOrder($orderUuid); |
| 56 | + $response = $this->_client->confirmOrder($orderUuid); |
| 57 | + |
| 58 | + if (isset($response['state']) && $response['state'] == 'confirmed') { |
| 59 | + try { |
| 60 | + $iSuccess = $this->monduExecute($this->getBasket()); |
| 61 | + |
| 62 | + return $this->_getNextStep($iSuccess); |
| 63 | + } catch (Exception $e) { |
| 64 | + throw new \Exception('Mondu: Error during the order process'); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // if user is not logged in set the user |
| 70 | + if(!$this->getUser() && isset($this->_oUser)){ |
| 71 | + $this->setUser($this->_oUser); |
| 72 | + } |
| 73 | + |
| 74 | + return parent::execute(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Save order to database, delete order_id from session and redirect to thank you page |
| 79 | + * |
| 80 | + * @param Basket $oBasket |
| 81 | + * |
| 82 | + * @return bool|int|mixed |
| 83 | + */ |
| 84 | + protected function monduExecute(Basket $oBasket) |
| 85 | + { |
| 86 | + if (!Registry::getSession()->getVariable('sess_challenge')) { |
| 87 | + Registry::getSession()->setVariable('sess_challenge', Registry::getUtilsObject()->generateUID()); |
| 88 | + } |
| 89 | + |
| 90 | + $iSuccess = 0; |
| 91 | + $oBasket->calculateBasket(true); |
| 92 | + $oOrder = oxNew(Order::class); |
| 93 | + |
| 94 | + try { |
| 95 | + $iSuccess = $oOrder->finalizeOrder($oBasket, $oBasket->getUser()); |
| 96 | + } catch (StandardException $e) { |
| 97 | + Registry::get(UtilsView::class)->addErrorToDisplay($e); |
| 98 | + } |
| 99 | + |
| 100 | + if ($iSuccess === 1) { |
| 101 | + // performing special actions after user finishes order (assignment to special user groups) |
| 102 | + $this->_oUser->onOrderExecute($oBasket, $iSuccess); |
| 103 | + } |
| 104 | + |
| 105 | + return $iSuccess; |
| 106 | + } |
23 | 107 | }
|
0 commit comments