NotifyAction::getPosData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusPrzelewy24Plugin\Action;
14
15
use BitBag\SyliusPrzelewy24Plugin\Bridge\Przelewy24BridgeInterface;
16
use Payum\Core\Action\ActionInterface;
17
use Payum\Core\ApiAwareInterface;
18
use Payum\Core\Bridge\Spl\ArrayObject;
19
use Payum\Core\Exception\InvalidArgumentException;
20
use Payum\Core\Exception\RequestNotSupportedException;
21
use Payum\Core\Exception\UnsupportedApiException;
22
use Payum\Core\GatewayAwareInterface;
23
use Payum\Core\GatewayAwareTrait;
24
use Payum\Core\Request\GetHttpRequest;
25
use Payum\Core\Request\Notify;
26
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
27
28
final class NotifyAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
29
{
30
    use GatewayAwareTrait;
31
32
    /** @var Przelewy24BridgeInterface */
33
    private $przelewy24Bridge;
34
35
    public function __construct(Przelewy24BridgeInterface $przelewy24Bridge)
36
    {
37
        $this->przelewy24Bridge = $przelewy24Bridge;
38
    }
39
40
    public function setApi($api): void
41
    {
42
        if (false === is_array($api)) {
43
            throw new UnsupportedApiException('Not supported.Expected to be set as array.');
44
        }
45
46
        $this->przelewy24Bridge->setAuthorizationData($api['merchant_id'], $api['crc_key'], $api['environment']);
47
    }
48
49
    public function execute($request): void
50
    {
51
        RequestNotSupportedException::assertSupports($this, $request);
52
53
        $details = ArrayObject::ensureArrayObject($request->getModel());
54
55
        $this->gateway->execute($httpRequest = new GetHttpRequest());
56
57
        if (!isset($httpRequest->request['p24_session_id']) || $details['p24_session_id'] !== $httpRequest->request['p24_session_id']) {
58
            throw new NotFoundHttpException();
59
        }
60
61
        if (false === $this->verifySign($httpRequest)) {
62
            throw new InvalidArgumentException('Invalid sign.');
63
        }
64
65
        $details['p24_order_id'] = $httpRequest->request['p24_order_id'];
66
67
        if (true === $this->przelewy24Bridge->trnVerify($this->getPosData($details))) {
68
            $details['p24_status'] = Przelewy24BridgeInterface::COMPLETED_STATUS;
69
70
            return;
71
        }
72
73
        $details['p24_status'] = Przelewy24BridgeInterface::FAILED_STATUS;
74
    }
75
76
    public function supports($request): bool
77
    {
78
        return
79
            $request instanceof Notify &&
80
            $request->getModel() instanceof \ArrayAccess
81
        ;
82
    }
83
84
    private function getPosData(ArrayObject $details): array
85
    {
86
        $posData = [];
87
88
        $posData['p24_session_id'] = $details['p24_session_id'];
89
        $posData['p24_amount'] = $details['p24_amount'];
90
        $posData['p24_currency'] = $details['p24_currency'];
91
        $posData['p24_order_id'] = $details['p24_order_id'];
92
93
        return $posData;
94
    }
95
96
    private function verifySign(GetHttpRequest $request): bool
97
    {
98
        $sign = $this->przelewy24Bridge->createSign([
99
            $request->request['p24_session_id'],
100
            $request->request['p24_order_id'],
101
            $request->request['p24_amount'],
102
            $request->request['p24_currency'],
103
        ]);
104
105
        return $sign === $request->request['p24_sign'];
106
    }
107
}
108