Passed
Push — dev ( 6076ca...bd4ba6 )
by Jan
05:12
created

PaymentManagement::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 10
c 1
b 0
f 1
nc 1
nop 10
dl 0
loc 22
rs 9.9332

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Getloy\GetloyMagentoGateway\Model;
4
5
use Getloy\GetloyMagentoGateway\Api\PaymentManagementInterface;
6
use Getloy\GetloyMagentoGateway\Gateway\Config\Config as GatewayConfig;
7
use Getloy\GetloyMagentoGateway\Model\CallbackResponse;
8
use Magento\Framework\UrlInterface;
9
use Magento\Sales\Api\OrderRepositoryInterface;
10
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
11
use Magento\Payment\Gateway\Data\PaymentDataObjectFactory;
12
use Magento\Quote\Model\QuoteIdMaskFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Model\QuoteIdMaskFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Magento\Quote\Api\BillingAddressManagementInterface;
14
use Magento\Quote\Api\CartManagementInterface;
15
use Magento\Quote\Api\CartRepositoryInterface;
16
use Magento\Store\Model\StoreManagerInterface;
17
18
class PaymentManagement implements PaymentManagementInterface
19
{
20
    /**
21
     * @var GatewayConfig
22
     */
23
    protected $gatewayConfig;
24
25
    /**
26
     * @var CartRepositoryInterface
27
     */
28
    protected $quoteRepository;
29
30
    /**
31
     * @var PaymentDataObjectFactory
32
     */
33
    protected $paymentDataObjectFactory;
34
35
    /**
36
     * @var QuoteIdMaskFactory
37
     */
38
    protected $quoteIdMaskFactory;
39
40
    /**
41
     * @var InvoiceSender
42
     */
43
    protected $invoiceSender;
44
45
    /**
46
     * @var OrderRepositoryInterface
47
     */
48
    protected $orderRepository;
49
50
    /**
51
     * @var BillingAddressManagementInterface
52
     */
53
    protected $billingAddressManagement;
54
55
    /**
56
     * @var StoreManagerInterface
57
     */
58
    protected $storeManager;
59
60
    /**
61
     * @var CartManagementInterface
62
     */
63
    protected $cartManagement;
64
    /**
65
     * @var UrlInterface
66
     */
67
    protected $url;
68
69
    /**
70
     * SessionInformationManagement constructor.
71
     *
72
     * @param GatewayConfig                     $gatewayConfig
73
     * @param CartRepositoryInterface           $quoteRepository
74
     * @param OrderRepositoryInterface          $orderRepository
75
     * @param PaymentDataObjectFactory          $paymentDataObjectFactory
76
     * @param QuoteIdMaskFactory                $quoteIdMaskFactory
77
     * @param InvoiceSender                     $invoiceSender
78
     * @param BillingAddressManagementInterface $billingAddressManagement
79
     * @param StoreManagerInterface             $storeManager
80
     * @param CartManagementInterface           $cartManagement
81
     * @param UrlInterface                      $url
82
     */
83
    public function __construct(
84
        GatewayConfig $gatewayConfig,
85
        CartRepositoryInterface $quoteRepository,
86
        OrderRepositoryInterface $orderRepository,
87
        PaymentDataObjectFactory $paymentDataObjectFactory,
88
        QuoteIdMaskFactory $quoteIdMaskFactory,
89
        InvoiceSender $invoiceSender,
90
        BillingAddressManagementInterface $billingAddressManagement,
91
        StoreManagerInterface $storeManager,
92
        CartManagementInterface $cartManagement,
93
        UrlInterface $url
94
    ) {
95
        $this->gatewayConfig = $gatewayConfig;
96
        $this->quoteRepository = $quoteRepository;
97
        $this->orderRepository = $orderRepository;
98
        $this->paymentDataObjectFactory = $paymentDataObjectFactory;
99
        $this->quoteIdMaskFactory = $quoteIdMaskFactory;
100
        $this->invoiceSender = $invoiceSender;
101
        $this->billingAddressManagement = $billingAddressManagement;
102
        $this->storeManager = $storeManager;
103
        $this->cartManagement = $cartManagement;
104
        $this->url = $url;
105
    }
106
107
    protected function generateTransactionId($orderId)
108
    {
109
        $tstamp = base_convert(time(), 10, 36);
110
        return sprintf('MG-%s-%s', $orderId, $tstamp);
111
    }
112
113
    protected function validateTransactionId($transactionId, $orderId)
114
    {
115
        return 1 === preg_match(
116
            sprintf('/^MG-%d-[0-9a-z]{6}$/', $orderId),
117
            $transactionId
118
        );
119
    }
120
121
    protected function generatePayload(
122
        \Magento\Quote\Model\Quote $quote,
123
        $orderId
124
    ) {
125
        $transactionId  = $this->generateTransactionId($orderId);
126
        $totalAmount    = round($quote->getGrandTotal(), 2);
127
        $currency       = $this->storeManager
128
            ->getStore()->getBaseCurrency()->getCode();
129
130
        /* @var \Magento\Quote\Api\Data\AddressInterface */
131
        $billingAddress = $quote->getBillingAddress();
132
        
133
        $getloyMerchantKey = $this->gatewayConfig->getGetloyMerchantKey();
134
        $testMode          = $this->gatewayConfig->isSandbox();
135
        $paywayMerchantId  = $this->gatewayConfig->getPaywayMerchantId();
136
        $paywayMerchantKey = $this->gatewayConfig->getPaywayApiKey();
137
138
        $getloyGateway = new \Getloy\Gateway($getloyMerchantKey);
139
        $getloyGateway->registerPaymentProvider(
140
            \Getloy\PaymentProviders::PAYWAY_KH,
141
            [
142
                'testMode' => $testMode,
143
                'merchantId' => $paywayMerchantId,
144
                'merchantKey' => $paywayMerchantKey,
145
            ]
146
        );
147
148
        $orderItems = new \Getloy\TransactionDetails\OrderItems();
149
150
        foreach ($quote->getAllVisibleItems() as $item) {
151
            $orderItems->add(
152
                new \Getloy\TransactionDetails\OrderItem(
153
                    $item->getName() ?: '',
154
                    (int) round($item->getQty(), 0),
155
                    $item->getRowTotal(),
156
                    $item->getPrice()
157
                )
158
            );
159
        }
160
161
        $order = new \Getloy\TransactionDetails\OrderDetails(
162
            $totalAmount,
163
            $currency,
164
            null,
165
            $orderItems
166
        );
167
168
169
        $payee = new \Getloy\TransactionDetails\PayeeDetails(
170
            $billingAddress->getFirstname(),
171
            $billingAddress->getLastname(),
172
            $billingAddress->getEmail() ? $billingAddress->getEmail() : '',
173
            $billingAddress->getTelephone() ? $billingAddress->getTelephone() : ''
174
        );
175
        return $getloyGateway->widgetPayload(
176
            $transactionId,
177
            \Getloy\PaymentProviders::PAYWAY_KH,
178
            $order,
179
            $payee,
180
            $this->url->getUrl('rest/default/V1/getloy/payment/callback/' . $orderId)
181
        );
182
    }
183
184
    /**
185
     * {@inheritDoc}
186
     *
187
     * @throws \Magento\Framework\Exception\LocalizedException
188
     */
189
    public function createPayment(
190
        $cartId,
191
        \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
192
        \Magento\Quote\Api\Data\AddressInterface $billingAddress
193
    ) {
194
        /**
195
         * @var \Magento\Quote\Model\Quote $quote 
196
         */
197
        $quote = $this->quoteRepository->getActive($cartId);
198
199
        return $this->placeOrderAndGetPayload(
200
            $quote,
201
            $paymentMethod,
202
            $billingAddress
203
        );
204
    }
205
206
    /**
207
     * {@inheritDoc}
208
     */
209
    public function createGuestPayment(
210
        $cartId,
211
        $email,
212
        \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
213
        \Magento\Quote\Api\Data\AddressInterface $billingAddress
214
    ) {
215
        $quoteIdMask = $this->quoteIdMaskFactory
216
            ->create()
217
            ->load($cartId, 'masked_id');
218
219
        $billingAddress->setEmail($email);
220
221
        /**
222
         * @var \Magento\Quote\Model\Quote $quote 
223
         */
224
        $quote = $this->quoteRepository->getActive($quoteIdMask->getQuoteId());
225
226
        $quote->setBillingAddress($billingAddress);
227
228
        $quote->setCheckoutMethod(
229
            \Magento\Quote\Api\CartManagementInterface::METHOD_GUEST
230
        );
231
232
        $this->quoteRepository->save($quote);
233
       
234
        return $this->placeOrderAndGetPayload(
235
            $quote,
236
            $paymentMethod,
237
            $billingAddress
238
        );
239
    }
240
241
    protected function placeOrderAndGetPayload(
242
        \Magento\Quote\Model\Quote $quote,
243
        \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
244
        \Magento\Quote\Api\Data\AddressInterface $billingAddress
245
    ) {
246
        $quote->setBillingAddress($billingAddress);
247
248
        $this->quoteRepository->save($quote);
249
250
        $orderId = $this->cartManagement->placeOrder(
251
            $quote->getId(),
252
            $paymentMethod
253
        );
254
255
        /**
256
         * @var \Magento\Sales\Model\Order
257
         */
258
        $order = $this->orderRepository->get($orderId);
259
260
        $order->addStatusHistoryComment(__('PayWay payment session started.'))
261
            ->save();
262
263
        return [[
264
            'payload' => $this->generatePayload($quote, $orderId),
265
        ]];
266
    }
267
268
    /**
269
     * @param string $orderId
270
     * @param string $tid
271
     * @param string $status
272
     * @param string $amount_paid
273
     * @param string $currency
274
     * @param string $auth_hash_ext
275
     *
276
     * @return \Getloy\GetloyMagentoGateway\Model\CallbackResponse
277
     */
278
    public function handleCallback(
279
        $orderId,
280
        $tid,
281
        $status,
282
        $amount_paid,
283
        $currency,
284
        $auth_hash_ext
285
    ): CallbackResponse {
286
        $gateway = new \Getloy\Gateway($this->gatewayConfig->getGetloyMerchantKey());
287
288
        try {
289
            $callbackDetails = $gateway->validateCallback(
290
                [
291
                    'tid' => $tid,
292
                    'status' => $status,
293
                    'amount_paid' => $amount_paid,
294
                    'currency' => $currency,
295
                    'auth_hash_ext' => $auth_hash_ext
296
                ]
297
            );
298
        } catch (\Exception $e) {
299
            return new CallbackResponse('failed', 'malformed callback');
300
        }
301
302
        if (\Getloy\CallbackDetails::STATUS_SUCCESS !== $callbackDetails->status()) {
303
            return new CallbackResponse('failed', 'invalid transaction status');
304
        }
305
306
        if (!$this->validateTransactionId($tid, $orderId)) {
307
            return new CallbackResponse('failed', 'order ID mismatch');
308
        }
309
310
        try {
311
            /**
312
             * @var \Magento\Sales\Model\Order 
313
             */
314
            $order = $this->orderRepository->get((int) $orderId);
315
        } catch (\Exception $e) {
316
            return new CallbackResponse('failed', 'quote does not exist');
317
        }
318
319
        $totalAmount = round($order->getGrandTotal(), 2);
320
        $currency    = $order->getOrderCurrencyCode();
321
322
        if (abs($totalAmount - $callbackDetails->amountPaid()) > 0.01
323
            || $currency !== $callbackDetails->currency()
324
        ) {
325
            return new CallbackResponse('failed', 'invalid callback');
326
        }
327
328
        $order->addStatusHistoryComment(
329
            __(
330
                'PayWay payment complete. PayWay transaction ID: #%1.', 
331
                $tid
332
            )
333
        );
334
335
        /**
336
         * @var \Magento\Sales\Model\Order\Payment 
337
         */
338
        $payment = $order->getPayment();
339
340
        $payment->setTransactionId($tid);
341
        $payment->setCurrencyCode($callbackDetails->currency());
342
        $payment->registerCaptureNotification($callbackDetails->amountPaid());
343
        $payment->setAdditionalInformation('getloy_transaction_id', $tid);
344
        $payment->setAdditionalInformation('getloy_payment_method', 'PayWay');
345
        $payment->setAdditionalInformation('getloy_method_variant', 'default');
346
        $payment->save();
347
348
        /**
349
         * @var \Magento\Sales\Model\Order\Invoice 
350
         */
351
        $invoice = $payment->getCreatedInvoice();
352
        $this->invoiceSender->send($invoice);
353
        $order->addStatusHistoryComment(
354
            __(
355
                'You notified customer about invoice #%1.',
356
                $invoice->getIncrementId()
357
            )
358
        )
359
            ->setIsCustomerNotified(true)
360
            ->save();
361
362
        return new CallbackResponse('complete', 'callback processed successfully');
363
    }
364
}
365