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
+ }
0 commit comments