99use Magento \Framework \App \Action \Action ;
1010use Magento \Framework \App \Action \Context ;
1111use Magento \Framework \App \ResponseInterface ;
12+ use Magento \Framework \Controller \Result \Redirect ;
1213use Magento \Framework \Controller \ResultFactory ;
1314use Magento \Framework \Controller \ResultInterface ;
1415use Magento \Framework \Exception \LocalizedException ;
1516use Magento \Framework \Exception \NotFoundException ;
17+ use Magento \Sales \Model \Order ;
1618use Psr \Log \LoggerInterface ;
1719use Worldline \Connect \Model \Config ;
1820use Worldline \Connect \Model \ConfigInterface ;
21+ use Worldline \Connect \Model \Order \OrderServiceInterface ;
1922use Worldline \Connect \Model \Worldline \Action \GetInlinePaymentStatus ;
23+ use Worldline \Connect \Model \Worldline \Api \ClientInterface ;
2024use Worldline \Connect \Model \Worldline \StatusInterface ;
2125
26+ use function __ ;
27+ use function mb_strtolower ;
28+
2229class ProcessReturn extends Action
2330{
2431 /**
@@ -45,6 +52,13 @@ class ProcessReturn extends Action
4552 // phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
4653 private $ inlinePaymentStatus ;
4754
55+ /**
56+ * @var ClientInterface
57+ */
58+ // phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
59+ protected $ worldlineClient ;
60+ private OrderServiceInterface $ orderService ;
61+
4862 /**
4963 * ProcessReturn constructor.
5064 *
@@ -59,27 +73,64 @@ public function __construct(
5973 Session $ checkoutSession ,
6074 ConfigInterface $ config ,
6175 LoggerInterface $ logger ,
62- GetInlinePaymentStatus $ getInlinePaymentStatus
76+ GetInlinePaymentStatus $ getInlinePaymentStatus ,
77+ ClientInterface $ worldlineClient ,
78+ OrderServiceInterface $ orderService
6379 ) {
6480 parent ::__construct ($ context );
6581
6682 $ this ->checkoutSession = $ checkoutSession ;
6783 $ this ->ePaymentsConfig = $ config ;
6884 $ this ->logger = $ logger ;
6985 $ this ->inlinePaymentStatus = $ getInlinePaymentStatus ;
86+ $ this ->worldlineClient = $ worldlineClient ;
87+ $ this ->orderService = $ orderService ;
7088 }
7189
7290 /**
7391 * Executes when a customer returns from an inline payment that caused a redirect.
7492 *
7593 * @return ResponseInterface|ResultInterface
7694 */
95+ // phpcs:ignore SlevomatCodingStandard.Functions.FunctionLength.FunctionLength
7796 public function execute ()
7897 {
98+ $ payment = $ this ->worldlineClient ->worldlinePayment ($ this ->retrievePaymentRefId ());
99+
100+ if ($ payment ->status === StatusInterface::REJECTED ) {
101+ /** @var Order $order */
102+ $ order = $ this ->orderService ->getByIncrementId ($ payment ->paymentOutput ->references ->merchantReference );
103+
104+ $ this ->inlinePaymentStatus ->process ($ order , $ payment );
105+
106+ $ order ->setState (Order::STATE_PAYMENT_REVIEW );
107+
108+ /** @var Order\Payment $orderPayment */
109+ $ orderPayment = $ order ->getPayment ();
110+ $ orderPayment ->setIsTransactionClosed (true );
111+ $ orderPayment ->setIsTransactionDenied (true );
112+ $ orderPayment ->update ();
113+
114+ $ this ->orderService ->save ($ order );
115+
116+ $ message = $ this ->ePaymentsConfig ->getPaymentStatusInfo ($ payment ->status );
117+
118+ $ this ->messageManager ->addErrorMessage ($ message );
119+ $ this ->logger ->error (__ ($ message ));
120+ $ this ->checkoutSession ->restoreQuote ();
121+
122+ return $ this ->redirect ('checkout/cart ' );
123+ }
124+
79125 try {
80- $ paymentRefId = $ this ->retrievePaymentRefId ();
126+ $ payment = $ this ->worldlineClient ->worldlinePayment ($ this ->retrievePaymentRefId ());
127+
128+ /** @var Order $order */
129+ $ order = $ this ->orderService ->getByIncrementId ($ payment ->paymentOutput ->references ->merchantReference );
81130
82- $ order = $ this ->inlinePaymentStatus ->process ($ paymentRefId );
131+ $ this ->inlinePaymentStatus ->process ($ order , $ payment );
132+
133+ $ this ->orderService ->save ($ order );
83134
84135 /** @var string $worldlinePaymentStatus */
85136 $ worldlinePaymentStatus = $ order ->getPayment ()->getAdditionalInformation (Config::PAYMENT_STATUS_KEY );
@@ -91,10 +142,38 @@ public function execute()
91142 // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName
92143 throw new LocalizedException ($ info ? __ ($ info ) : __ ('Unknown status ' ));
93144 }
145+
94146 // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName
95147 $ this ->messageManager ->addSuccessMessage (__ ('Payment status: ' ) . ' ' . ($ info ?: 'Unknown status ' ));
96148
97- return $ this ->redirect ('checkout/onepage/success ' );
149+ /** @var Redirect $resultRedirect */
150+ $ resultRedirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
151+
152+ $ redirectUrl = $ order ->getPayment ()->getAdditionalInformation (Config::REDIRECT_URL_KEY );
153+
154+ if ($ redirectUrl ) {
155+ $ order ->getPayment ()->setAdditionalInformation (Config::REDIRECT_URL_KEY , null );
156+
157+ $ this ->orderService ->save ($ order );
158+
159+ $ resultRedirect ->setUrl ($ redirectUrl );
160+
161+ return $ resultRedirect ;
162+ } else {
163+ $ resultRedirect ->setPath ('checkout/onepage/success ' );
164+
165+ $ message = $ this ->ePaymentsConfig ->getPaymentStatusInfo (mb_strtolower ($ worldlinePaymentStatus ));
166+ if ($ message ) {
167+ // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName, Squiz.Strings.DoubleQuoteUsage.ContainsVar
168+ $ this ->messageManager ->addSuccessMessage (__ ('Payment status: ' ) . " $ message " );
169+ }
170+ $ resultsMessage = $ order ->getPayment ()->getAdditionalInformation (Config::TRANSACTION_RESULTS_KEY );
171+ if ($ resultsMessage ) {
172+ $ this ->messageManager ->addNoticeMessage ($ resultsMessage );
173+ }
174+
175+ return $ this ->redirect ('checkout/onepage/success ' );
176+ }
98177 } catch (Exception $ e ) {
99178 $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
100179 $ this ->logger ->error ($ e ->getMessage ());
0 commit comments