Completed
Push — main ( 7136de...6d50be )
by Leonard
03:35
created

ServerNotifyRequest::getMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Omnipay\YooMoney\Message;
4
5
use Omnipay\Common\Exception\InvalidResponseException;
6
use Omnipay\Common\Http\ClientInterface;
7
use Omnipay\Common\Message\NotificationInterface;
8
use Omnipay\YooMoney\Traits\ResponseFieldsTrait;
9
use Symfony\Component\HttpFoundation\Request as HttpRequest;
10
11
class ServerNotifyRequest extends AbstractRequest implements NotificationInterface
12
{
13
    use ResponseFieldsTrait;
14
15
    protected $data;
16
17
    public function __construct(ClientInterface $httpClient, HttpRequest $httpRequest)
18
    {
19
        parent::__construct($httpClient, $httpRequest);
20
21
        $this->data = $httpRequest->request->all();
22
    }
23
24
    public function getData()
25
    {
26
        if (!$this->isValid()) {
27
            throw new InvalidResponseException("Callback hash does not match expected value");
28
        }
29
30
        return $this->data;
31
    }
32
33
    public function sendData($data)
34
    {
35
        return $this->data;
36
    }
37
38
    public function isValid()
39
    {
40
        return $this->getSignature() === $this->buildSignature();
41
    }
42
43
    private function getSignature()
44
    {
45
        return $this->getSha1Hash();
46
    }
47
48
    private function buildSignature()
49
    {
50
        $params = [
51
            $this->getDataItem('notification_type'),
52
            $this->getDataItem('operation_id'),
53
            $this->getDataItem('amount'),
54
            $this->getDataItem('currency'),
55
            $this->getDataItem('datetime'),
56
            $this->getDataItem('sender'),
57
            $this->getDataItem('codepro'),
58
            $this->getSecret(),
59
            $this->getDataItem('label', ''),
60
        ];
61
62
        return hash('sha1', implode("&", $params));
63
    }
64
65
    /**
66
     * Was the transaction successful?
67
     *
68
     * @return string Transaction status, one of {@see STATUS_COMPLETED}, {@see #STATUS_PENDING},
69
     * or {@see #STATUS_FAILED}.
70
     */
71
    public function getTransactionStatus()
72
    {
73
        if (!$this->isValid()) {
74
            return self::STATUS_FAILED;
75
        }
76
77
        $isUnaccepted = $this->getUnaccepted();
78
79
        return filter_var($isUnaccepted, FILTER_VALIDATE_BOOL) ?
80
            self::STATUS_PENDING : self::STATUS_COMPLETED;
81
    }
82
83
    public function getMessage()
84
    {
85
        if (!$this->isValid()) {
86
            return sprintf('Callback hash does not match expected value');
87
        }
88
89
        return '';
90
    }
91
92
}
93