Passed
Branch 2.3 (92ba2c)
by Alexander
02:54
created

Index   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 64
dl 0
loc 155
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getStateDesc() 0 18 1
A execute() 0 61 5
A createCsrfValidationException() 0 4 1
A __construct() 0 13 1
A validateForCsrf() 0 3 1
1
<?php
2
3
namespace B2Binpay\Payment\Controller\Callback;
4
5
use Magento\Sales\Model\Order;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order 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...
6
use Magento\Framework\App\Action\Context;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Action\Context 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...
7
use Magento\Framework\App\Action\Action;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Action\Action 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...
8
use Magento\Framework\App\CsrfAwareActionInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\CsrfAwareActionInterface 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...
9
use Magento\Framework\App\Request\InvalidRequestException;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Re...InvalidRequestException 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...
10
use Magento\Framework\App\RequestInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\RequestInterface 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...
11
use Magento\Framework\Controller\Result\RawFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Controller\Result\RawFactory 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...
12
use B2Binpay\Payment\Gateway\Validator\CallbackValidator;
13
use B2Binpay\AmountFactory;
0 ignored issues
show
Bug introduced by
The type B2Binpay\AmountFactory 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...
14
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface 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...
15
16
class Index extends Action implements CsrfAwareActionInterface
17
{
18
    /**
19
     * @var RawFactory
20
     */
21
    protected $rawResultFactory;
22
23
    /**
24
     * @var Context
25
     */
26
    protected $context;
27
28
    /**
29
     * @var CallbackValidator
30
     */
31
    protected $validator;
32
33
    /**
34
     * @var LoggerInterface
35
     */
36
    protected $logger;
37
38
    /**
39
     * @var AmountFactory
40
     */
41
    private $amountFactory;
42
43
    /**
44
     * Callback controller.
45
     *
46
     * @param Context $context
47
     * @param RawFactory $rawResultFactory
48
     * @param CallbackValidator $validator
49
     * @param AmountFactory $amountFactory
50
     * @param LoggerInterface $logger
51
     */
52
    public function __construct(
53
        Context $context,
54
        RawFactory $rawResultFactory,
55
        CallbackValidator $validator,
56
        AmountFactory $amountFactory,
57
        LoggerInterface $logger
58
    ) {
59
        $this->context = $context;
60
        $this->rawResultFactory = $rawResultFactory;
61
        $this->validator = $validator;
62
        $this->amountFactory = $amountFactory;
63
        $this->logger = $logger;
64
        parent::__construct($context);
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70
    public function createCsrfValidationException(
71
        RequestInterface $request
72
    ): ?InvalidRequestException {
73
        return null;
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function validateForCsrf(RequestInterface $request): ?bool
80
    {
81
        return true;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function execute()
88
    {
89
        $request = $this->context->getRequest();
90
        $result = $this->rawResultFactory->create();
91
92
        $validationResult = $this->validator->validate(['request' => $request]);
93
94
        $validation = $validationResult->getFailsDescription();
95
96
        if (!$validationResult->isValid()) {
97
            $result->setStatusHeader($validation['status'], '1.1', $validation['message']);
98
99
            return $result;
100
        }
101
102
        $order = $validation['order'];
103
        $payment = $order->getPayment();
104
        $params = $request->getParams();
105
106
        $billStatus = (string)$params['status'];
107
108
        if ('2' === $billStatus) {
109
            if ($params['amount'] === $params['actual_amount']) {
110
                $totalDue = $order->getTotalDue();
111
112
                $payment->authorize(false, $totalDue);
113
                $payment->registerCaptureNotification($totalDue);
114
115
                $order->addStatusToHistory(
116
                    $order::STATE_PROCESSING,
117
                    __('B2BinPay payment complete!')
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
                    /** @scrutinizer ignore-call */ 
118
                    __('B2BinPay payment complete!')
Loading history...
118
                );
119
            } else {
120
                $actualAmount = $this->amountFactory->create(
121
                    $params['actual_amount'],
122
                    $params['currency']['iso'],
123
                    $params['pow']
124
                )->getValue();
125
126
                $order->addStatusToHistory(
127
                    $order::STATE_PAYMENT_REVIEW,
128
                    __('B2BinPay received payment: ' . $actualAmount . $params['currency']['alpha'])
129
                );
130
            }
131
        }
132
133
        $stateList = $this->getStateDesc();
134
135
        if (!empty($stateList[$billStatus])) {
136
            $order->addStatusToHistory(
137
                $stateList[$billStatus]['state'],
138
                $stateList[$billStatus]['message']
139
            );
140
        }
141
142
        $order->save();
143
144
        $result->setStatusHeader('200', '1.1', 'OK');
145
        $result->setContents('OK');
146
147
        return $result;
148
    }
149
150
    /**
151
     * @return array
152
     */
153
    private function getStateDesc()
154
    {
155
        return [
156
            '-2' => [
157
                'state' => Order::STATE_CLOSED,
158
                'message' => __('B2BinPay payment error!')
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
                'message' => /** @scrutinizer ignore-call */ __('B2BinPay payment error!')
Loading history...
159
            ],
160
            '-1' => [
161
                'state' => Order::STATE_CANCELED,
162
                'message' => __('B2BinPay payment expired!')
163
            ],
164
            '3' => [
165
                'state' => Order::STATE_PAYMENT_REVIEW,
166
                'message' => __('B2BinPay payment freeze!')
167
            ],
168
            '4' => [
169
                'state' => Order::STATE_CLOSED,
170
                'message' => __('B2BinPay payment closed!')
171
            ]
172
        ];
173
    }
174
}
175