Skip to content

Commit 02882b9

Browse files
Merge pull request #4 from nofrixion/PLU-14-integrated-checkout-page
PLU-14 integrated checkout page
2 parents cf2719b + 2d41f22 commit 02882b9

26 files changed

+811
-236
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
vendor/
2-
.vscode/
2+
.vscode/
3+
localdev/
4+
*.code-workspace
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nofrixion\Payments\Controller\Payment;
6+
7+
use Magento\Checkout\Model\Session;
8+
use Magento\Customer\Model\Session as CustomerSession;
9+
use Magento\Framework\App\RequestInterface;
10+
use Magento\Framework\Controller\Result\RedirectFactory;
11+
use Magento\Framework\Message\ManagerInterface;
12+
use Magento\Framework\Session\SessionManagerInterface;
13+
use Magento\Framework\UrlInterface;
14+
use Magento\Framework\View\Result\PageFactory;
15+
use Magento\Sales\Model\Order;
16+
use Nofrixion\Payments\Helper\Data as NofrixionHelper;
17+
use Nofrixion\Payments\Model\OrderStatuses;
18+
use Nofrixion\Util\PreciseNumber;
19+
use Psr\Log\LoggerInterface;
20+
21+
/**
22+
* InitiatePayment - controller that creates Magento Order, NoFrixion payment request and initializes payment.
23+
* @todo add card handling
24+
* @todo error handling around payment request creation.
25+
*/
26+
class InitiatePayment implements \Magento\Framework\App\ActionInterface
27+
{
28+
private NofrixionHelper $nofrixionHelper;
29+
private CustomerSession $customerSession;
30+
private CookieManagerInterface $cookieManager;
31+
private CookieMetadataFactory $cookieMetadataFactory;
32+
private LoggerInterface $logger;
33+
private ManagerInterface $messageManager;
34+
private PageFactory $resultPageFactory;
35+
private RedirectFactory $resultRedirectFactory;
36+
private RequestInterface $request;
37+
private SessionManagerInterface $sessionManager;
38+
private Session $checkoutSession;
39+
private UrlInterface $url;
40+
41+
public function __construct(
42+
CustomerSession $customerSession,
43+
LoggerInterface $logger,
44+
ManagerInterface $messageManager,
45+
NofrixionHelper $helper,
46+
PageFactory $resultPageFactory,
47+
RedirectFactory $resultRedirectFactory,
48+
RequestInterface $request,
49+
Session $checkoutSession,
50+
SessionManagerInterface $sessionManager,
51+
UrlInterface $url
52+
) {
53+
$this->customerSession = $customerSession;
54+
$this->logger = $logger;
55+
$this->messageManager = $messageManager;
56+
$this->nofrixionHelper = $helper;
57+
$this->request = $request;
58+
$this->resultPageFactory = $resultPageFactory;
59+
$this->resultRedirectFactory = $resultRedirectFactory;
60+
$this->checkoutSession = $checkoutSession;
61+
$this->sessionManager = $sessionManager;
62+
$this->url = $url;
63+
}
64+
65+
/**
66+
* Summary of execute
67+
* @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
68+
*/
69+
public function execute()
70+
{
71+
//xdebug_break();
72+
73+
$resultRedirect = $this->resultRedirectFactory->create();
74+
$bankId = rawurldecode($this->request->getParam('bankId'));
75+
76+
$order = $this->checkoutSession->getLastRealOrder();
77+
if ($order && $order->getId()) {
78+
$paymentRequest = $this->nofrixionHelper->createPaymentRequest($order);
79+
80+
$pendingPaymentStatus = OrderStatuses::STATUS_CODE_PENDING_PAYMENT;
81+
$order->addCommentToStatusHistory('Forwarded customer to payment page', $pendingPaymentStatus);
82+
$order->save();
83+
84+
// need to call: https://api-sandbox.nofrixion.com/api/v1/paymentrequests/{id}/pisp
85+
// with body field 'ProviderID' = $bankId
86+
// $amount = PreciseNumber::parseString((string) $paymentRequest['amount']);
87+
$failureRedirectUrl = $this->url->getUrl('checkout/cart', ['_secure' => true]);
88+
$paymentInitialization = $this->nofrixionHelper->initiatePayByBank($paymentRequest['id'], $bankId, $failureRedirectUrl, null);
89+
90+
// Can't handle exception caused by null URL in controller so check with 'if' and 'filter_var'
91+
if (filter_var($paymentInitialization->redirectUrl, FILTER_VALIDATE_URL)) {
92+
$resultRedirect->setUrl($paymentInitialization->redirectUrl);
93+
try {
94+
// $resultRedirect used if forwarding to PIS provider
95+
return $resultRedirect;
96+
} catch (Exception $e) {
97+
$this->logger->error('Error initializing payment.', ['exception' => $e]);
98+
$this->initializationFailureAction($order, $paymentRequest, 'Order cancelled due to payment initialization error.');
99+
}
100+
} else {
101+
// or $paymentInitialization->redirectUrl is invalid.
102+
$msg = 'A valid payment URL was not received for provider ' . $bankId;
103+
$this->logger->error($msg);
104+
$this->initializationFailureAction($order, $paymentRequest, $msg);
105+
}
106+
} else {
107+
// If we get to here $order was not valid
108+
$this->messageManager->addWarningMessage('An error occurred creating your order.');
109+
}
110+
// if we haven't successfully directed to the payment URL return to cart.
111+
$resultRedirect->setPath('checkout/cart');
112+
return $resultRedirect;
113+
}
114+
115+
public function initializationFailureAction($order, $paymentRequest, $message){
116+
// restore cart
117+
$this->nofrixionHelper->restoreCart($order);
118+
$this->messageManager->addWarningMessage($message);
119+
120+
// Can't delete payment request as it has events (pisp_initiate).
121+
// $this->nofrixionHelper->deletePaymentRequest($paymentRequest['id']);
122+
}
123+
}

Controller/Redirect/ForwardToPayment.php

-78
This file was deleted.

Controller/Redirect/ReturnAfterPayment.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@ class ReturnAfterPayment implements \Magento\Framework\App\ActionInterface
2626
private LoggerInterface $logger;
2727
private RedirectFactory $resultRedirectFactory;
2828

29-
public function __construct(RequestInterface $request, LoggerInterface $logger, UrlInterface $url, RedirectFactory $resultRedirectFactory, PageFactory $resultPageFactory, Data $nofrixionHelper, OrderFactory $orderFactory, Session $checkoutSession)
30-
{
29+
public function __construct(
30+
RequestInterface $request,
31+
LoggerInterface $logger,
32+
UrlInterface $url,
33+
RedirectFactory $resultRedirectFactory,
34+
PageFactory $resultPageFactory,
35+
Data $nofrixionHelper,
36+
OrderFactory $orderFactory,
37+
Session $checkoutSession
38+
) {
3139
$this->request = $request;
3240
$this->resultRedirectFactory = $resultRedirectFactory;
3341
$this->url = $url;
@@ -64,4 +72,4 @@ public function execute()
6472

6573
return $resultRedirect;
6674
}
67-
}
75+
}

Controller/Webhook/In.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
use Magento\Framework\Controller\Result\JsonFactory;
1010
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
1111
use Magento\Store\Model\StoreManagerInterface;
12-
use Nofrixion\Payments\Helper\Data as NoFrixionHelper;
12+
use Nofrixion\Payments\Helper\Data as NofrixionHelper;
1313
use Psr\Log\LoggerInterface;
1414

1515
class In implements ActionInterface
1616
{
1717
private LoggerInterface $logger;
18-
private NoFrixionHelper $nofrixionHelper;
18+
private NofrixionHelper $nofrixionHelper;
1919
private OrderSender $orderSender;
2020
private RequestInterface $request;
2121
private StoreManagerInterface $storeManager;
2222
private JsonFactory $resultJsonFactory;
2323

24-
public function __construct(RequestInterface $request, NoFrixionHelper $helper, OrderSender $orderSender, StoreManagerInterface $storeManager, JsonFactory $resultJsonFactory, LoggerInterface $logger)
24+
public function __construct(RequestInterface $request, NofrixionHelper $helper, OrderSender $orderSender, StoreManagerInterface $storeManager, JsonFactory $resultJsonFactory, LoggerInterface $logger)
2525
{
2626
$this->request = $request;
2727
$this->nofrixionHelper = $helper;

0 commit comments

Comments
 (0)