Code Duplication    Length = 145-145 lines in 2 locations

Controller/Webhooks/Accept.php 1 location

@@ 29-173 (lines=145) @@
26
/**
27
 * Class Refund - Receives communication for refunded payment.
28
 */
29
class Accept extends Action implements CsrfAwareActionInterface
30
{
31
    /**
32
     * createCsrfValidationException.
33
     *
34
     * @param RequestInterface $request
35
     *
36
     * @return null
37
     */
38
    public function createCsrfValidationException(RequestInterface $request): InvalidRequestException
39
    {
40
        if ($request) {
41
            return null;
42
        }
43
    }
44
45
    /**
46
     * validateForCsrf.
47
     *
48
     * @param RequestInterface $request
49
     *
50
     * @return bool true
51
     */
52
    public function validateForCsrf(RequestInterface $request): bool
53
    {
54
        if ($request) {
55
            return true;
56
        }
57
    }
58
59
    /**
60
     * @var logger
61
     */
62
    protected $logger;
63
64
    /**
65
     * @var orderFactory
66
     */
67
    protected $orderFactory;
68
69
    /**
70
     * @var resultJsonFactory
71
     */
72
    protected $resultJsonFactory;
73
74
    /**
75
     * @var storeManager
76
     */
77
    protected $storeManager;
78
79
    /**
80
     * @param Context               $context
81
     * @param logger                $logger
82
     * @param Config                $config
83
     * @param OrderInterfaceFactory $orderFactory
84
     * @param JsonFactory           $resultJsonFactory
85
     */
86
    public function __construct(
87
        Context $context,
88
        Config $config,
89
        Logger $logger,
90
        OrderInterfaceFactory $orderFactory,
91
        CreditmemoFactory $creditmemoFactory,
92
        CreditmemoService $creditmemoService,
93
        Invoice $invoice,
94
        StoreManagerInterface $storeManager,
95
        JsonFactory $resultJsonFactory
96
    ) {
97
        parent::__construct($context);
98
        $this->config = $config;
99
        $this->logger = $logger;
100
        $this->orderFactory = $orderFactory;
101
        $this->creditmemoFactory = $creditmemoFactory;
102
        $this->creditmemoService = $creditmemoService;
103
        $this->invoice = $invoice;
104
        $this->storeManager = $storeManager;
105
        $this->resultJsonFactory = $resultJsonFactory;
106
    }
107
108
    /**
109
     * Command Accept.
110
     *
111
     * @return json
112
     */
113
    public function execute()
114
    {
115
        if (!$this->getRequest()->isPost()) {
116
            $resultPage = $this->resultJsonFactory->create();
117
            $resultPage->setHttpResponseCode(404);
118
119
            return $resultPage;
120
        }
121
122
        $resultPage = $this->resultJsonFactory->create();
123
        $response = $this->getRequest()->getContent();
124
        $originalNotification = json_decode($response, true);
125
        $authorization = $this->getRequest()->getHeader('Authorization');
126
        $storeId = $this->storeManager->getStore()->getId();
127
        $storeCaptureToken = $this->config->getMerchantGatewayCaptureToken($storeId);
128
        if ($storeCaptureToken === $authorization) {
129
            $order = $this->orderFactory->create()->load($originalNotification['id'], 'ext_order_id');
130
            $this->logger->debug([
131
                'webhook'            => 'accept',
132
                'ext_order_id'       => $originalNotification['id'],
133
                'increment_order_id' => $order->getIncrementId(),
134
            ]);
135
            $payment = $order->getPayment();
136
            if (!$order->getInvoiceCollection()->count()) {
137
                try {
138
                    $payment->accept();
139
                    $payment->save();
140
                    $order->save();
141
                } catch (\Exception $exc) {
142
                    $resultPage->setHttpResponseCode(500);
143
                    $resultPage->setJsonData(
144
                        json_encode([
145
                            'error'   => 400,
146
                            'message' => $exc->getMessage(),
147
                        ])
148
                    );
149
                }
150
151
                return $resultPage->setJsonData(
152
                    json_encode([
153
                        'success'   => 1,
154
                        'status'    => $order->getStatus(),
155
                        'state'     => $order->getState(),
156
                    ])
157
                );
158
            }
159
160
            $resultPage->setHttpResponseCode(400);
161
162
            return $resultPage->setJsonData(
163
                json_encode([
164
                    'error'   => 400,
165
                    'message' => 'The transaction could not be refund',
166
                ])
167
            );
168
        }
169
        $resultPage->setHttpResponseCode(401);
170
171
        return $resultPage;
172
    }
173
}
174

Controller/Webhooks/Deny.php 1 location

@@ 29-173 (lines=145) @@
26
/**
27
 * Class Refund - Receives communication for refunded payment.
28
 */
29
class Deny extends Action implements CsrfAwareActionInterface
30
{
31
    /**
32
     * createCsrfValidationException.
33
     *
34
     * @param RequestInterface $request
35
     *
36
     * @return null
37
     */
38
    public function createCsrfValidationException(RequestInterface $request): InvalidRequestException
39
    {
40
        if ($request) {
41
            return null;
42
        }
43
    }
44
45
    /**
46
     * validateForCsrf.
47
     *
48
     * @param RequestInterface $request
49
     *
50
     * @return bool true
51
     */
52
    public function validateForCsrf(RequestInterface $request): bool
53
    {
54
        if ($request) {
55
            return true;
56
        }
57
    }
58
59
    /**
60
     * @var logger
61
     */
62
    protected $logger;
63
64
    /**
65
     * @var orderFactory
66
     */
67
    protected $orderFactory;
68
69
    /**
70
     * @var resultJsonFactory
71
     */
72
    protected $resultJsonFactory;
73
74
    /**
75
     * @var storeManager
76
     */
77
    protected $storeManager;
78
79
    /**
80
     * @param Context               $context
81
     * @param logger                $logger
82
     * @param Config                $config
83
     * @param OrderInterfaceFactory $orderFactory
84
     * @param JsonFactory           $resultJsonFactory
85
     */
86
    public function __construct(
87
        Context $context,
88
        Config $config,
89
        Logger $logger,
90
        OrderInterfaceFactory $orderFactory,
91
        CreditmemoFactory $creditmemoFactory,
92
        CreditmemoService $creditmemoService,
93
        Invoice $invoice,
94
        StoreManagerInterface $storeManager,
95
        JsonFactory $resultJsonFactory
96
    ) {
97
        parent::__construct($context);
98
        $this->config = $config;
99
        $this->logger = $logger;
100
        $this->orderFactory = $orderFactory;
101
        $this->creditmemoFactory = $creditmemoFactory;
102
        $this->creditmemoService = $creditmemoService;
103
        $this->invoice = $invoice;
104
        $this->storeManager = $storeManager;
105
        $this->resultJsonFactory = $resultJsonFactory;
106
    }
107
108
    /**
109
     * Command Accept.
110
     *
111
     * @return json
112
     */
113
    public function execute()
114
    {
115
        if (!$this->getRequest()->isPost()) {
116
            $resultPage = $this->resultJsonFactory->create();
117
            $resultPage->setHttpResponseCode(404);
118
119
            return $resultPage;
120
        }
121
122
        $resultPage = $this->resultJsonFactory->create();
123
        $response = $this->getRequest()->getContent();
124
        $originalNotification = json_decode($response, true);
125
        $authorization = $this->getRequest()->getHeader('Authorization');
126
        $storeId = $this->storeManager->getStore()->getId();
127
        $storeCaptureToken = $this->config->getMerchantGatewayCancelToken($storeId);
128
        if ($storeCaptureToken === $authorization) {
129
            $order = $this->orderFactory->create()->load($originalNotification['id'], 'ext_order_id');
130
            $this->logger->debug([
131
                'webhook'            => 'deny',
132
                'ext_order_id'       => $originalNotification['id'],
133
                'increment_order_id' => $order->getIncrementId(),
134
            ]);
135
            $payment = $order->getPayment();
136
            if (!$order->canCancel()) {
137
                try {
138
                    $payment->deny();
139
                    $payment->save();
140
                    $order->save();
141
                } catch (\Exception $exc) {
142
                    $resultPage->setHttpResponseCode(500);
143
                    $resultPage->setJsonData(
144
                        json_encode([
145
                            'error'   => 400,
146
                            'message' => $exc->getMessage(),
147
                        ])
148
                    );
149
                }
150
151
                return $resultPage->setJsonData(
152
                    json_encode([
153
                        'success'   => 1,
154
                        'status'    => $order->getStatus(),
155
                        'state'     => $order->getState(),
156
                    ])
157
                );
158
            }
159
160
            $resultPage->setHttpResponseCode(201);
161
162
            return $resultPage->setJsonData(
163
                json_encode([
164
                    'error'   => 400,
165
                    'message' => 'The transaction could not be refund',
166
                ])
167
            );
168
        }
169
        $resultPage->setHttpResponseCode(401);
170
171
        return $resultPage;
172
    }
173
}
174