17
17
18
18
namespace MultiSafepay \ConnectCore \Service ;
19
19
20
+ use Exception ;
20
21
use Magento \Framework \App \Area ;
21
22
use Magento \Framework \App \State ;
22
23
use Magento \Framework \Exception \LocalizedException ;
23
24
use Magento \Framework \Exception \NoSuchEntityException ;
24
- use Magento \Framework \Phrase ;
25
25
use Magento \Sales \Api \Data \OrderInterface ;
26
26
use Magento \Sales \Api \OrderRepositoryInterface ;
27
27
use Magento \Sales \Model \Order \Payment ;
28
28
use Magento \Sales \Model \Order \Payment \Transaction ;
29
29
use MultiSafepay \ConnectCore \Model \Api \Initializer \OrderRequestInitializer ;
30
30
use MultiSafepay \Exception \ApiException ;
31
31
use Psr \Http \Client \ClientExceptionInterface ;
32
+ use MultiSafepay \ConnectCore \Logger \Logger ;
32
33
34
+ /**
35
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
36
+ */
33
37
class PaymentLink
34
38
{
35
39
public const MULTISAFEPAY_PAYMENT_LINK_PARAM_NAME = 'payment_link ' ;
@@ -49,21 +53,29 @@ class PaymentLink
49
53
*/
50
54
private $ state ;
51
55
56
+ /**
57
+ * @var Logger
58
+ */
59
+ private $ logger ;
60
+
52
61
/**
53
62
* PaymentLink constructor.
54
63
*
55
64
* @param OrderRequestInitializer $orderRequestInitializer
56
65
* @param OrderRepositoryInterface $orderRepository
57
66
* @param State $state
67
+ * @param Logger $logger
58
68
*/
59
69
public function __construct (
60
70
OrderRequestInitializer $ orderRequestInitializer ,
61
71
OrderRepositoryInterface $ orderRepository ,
62
- State $ state
72
+ State $ state ,
73
+ Logger $ logger
63
74
) {
64
75
$ this ->orderRequestInitializer = $ orderRequestInitializer ;
65
76
$ this ->orderRepository = $ orderRepository ;
66
77
$ this ->state = $ state ;
78
+ $ this ->logger = $ logger ;
67
79
}
68
80
69
81
/**
@@ -85,10 +97,13 @@ public function getPaymentLinkByOrder(OrderInterface $order): string
85
97
}
86
98
87
99
/**
100
+ * Add the payment link to the order
101
+ *
88
102
* @param OrderInterface $order
89
103
* @param string $paymentLink
90
104
* @return void
91
105
* @throws LocalizedException
106
+ * @throws Exception
92
107
*/
93
108
public function addPaymentLink (OrderInterface $ order , string $ paymentLink ): void
94
109
{
@@ -128,30 +143,35 @@ private function addToAdditionalInformation(Payment $payment, string $paymentUrl
128
143
}
129
144
130
145
/**
146
+ * Add the Payment link to the order comments if the order was placed in the admin backend
147
+ *
131
148
* @param OrderInterface $order
132
149
* @param string $paymentUrl
133
150
* @return void
151
+ * @throws Exception
134
152
*/
135
153
private function addPaymentLinkToOrderComments (OrderInterface $ order , string $ paymentUrl ): void
136
154
{
137
- $ order ->addCommentToStatusHistory ($ this ->getOrderCommentByAreaCode ($ paymentUrl ));
138
- $ this ->orderRepository ->save ($ order );
155
+ if ($ this ->isAreaCodeAdminHtml ()) {
156
+ $ order ->addCommentToStatusHistory (__ ('Payment link for this transaction: %1 ' , $ paymentUrl )->render ());
157
+ $ this ->orderRepository ->save ($ order );
158
+ }
139
159
}
140
160
141
161
/**
142
- * @param string $paymentUrl
143
- * @return Phrase
162
+ * Check if this is being executed from the backend
163
+ *
164
+ * @return bool
144
165
*/
145
- private function getOrderCommentByAreaCode ( string $ paymentUrl ): Phrase
166
+ private function isAreaCodeAdminHtml ( ): bool
146
167
{
147
168
try {
148
169
$ areaCode = $ this ->state ->getAreaCode ();
149
170
} catch (LocalizedException $ localizedException ) {
150
- $ areaCode = Area::AREA_ADMINHTML ;
171
+ $ this ->logger ->logException ($ localizedException );
172
+ return false ;
151
173
}
152
174
153
- return $ areaCode === Area::AREA_ADMINHTML
154
- ? __ ('Payment link for this transaction: %1 ' , $ paymentUrl )
155
- : __ ('The user has been redirected to the following page: %1 ' , $ paymentUrl );
175
+ return $ areaCode === Area::AREA_ADMINHTML ;
156
176
}
157
177
}
0 commit comments