Passed
Push — master ( 380eac...dfa1a6 )
by Dmitry
12:01
created

CompletePurchaseResponse   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 91
ccs 0
cts 62
cp 0
rs 10
c 0
b 0
f 0
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getClient() 0 3 1
A getPayer() 0 3 1
A getSignatureValue() 0 3 1
A getCurrency() 0 3 1
A getTransactionId() 0 3 1
A generateSignature() 0 13 2
A __construct() 0 7 2
A getAmount() 0 3 1
A getCustomFields() 0 11 1
A isSuccessful() 0 3 1
A getTransactionReference() 0 3 1
A getInvId() 0 3 1
1
<?php
2
/**
3
 * RoboKassa driver for Omnipay PHP payment library.
4
 *
5
 * @link      https://github.com/hiqdev/omnipay-robokassa
6
 * @package   omnipay-robokassa
7
 * @license   MIT
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace Omnipay\RoboKassa\Message;
12
13
use Omnipay\Common\Exception\InvalidResponseException;
14
use Omnipay\Common\Message\AbstractResponse;
15
use Omnipay\Common\Message\RequestInterface;
16
17
/**
18
 * RoboKassa Complete Purchase Response.
19
 */
20
class CompletePurchaseResponse extends AbstractResponse
21
{
22
    /** @var RequestInterface|CompletePurchaseRequest */
23
    protected $request;
24
25
    public function __construct(RequestInterface $request, $data)
26
    {
27
        $this->request = $request;
28
        $this->data    = $data;
29
30
        if (strtolower($this->getSignatureValue()) !== $this->generateSignature()) {
31
            throw new InvalidResponseException('Invalid hash');
32
        }
33
    }
34
35
    public function generateSignature()
36
    {
37
        $params = [
38
            $this->getAmount(),
39
            $this->getTransactionReference(),
40
            $this->request->getSecretKey2()
0 ignored issues
show
Bug introduced by
The method getSecretKey2() does not exist on Omnipay\Common\Message\RequestInterface. It seems like you code against a sub-type of Omnipay\Common\Message\RequestInterface such as Omnipay\RoboKassa\Message\AbstractRequest. ( Ignorable by Annotation )

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

40
            $this->request->/** @scrutinizer ignore-call */ 
41
                            getSecretKey2()
Loading history...
41
        ];
42
43
        foreach ($this->getCustomFields() as $field => $value) {
44
            $params[] = "$field=$value";
45
        }
46
47
        return md5(implode(':', $params));
48
    }
49
50
    public function getCustomFields()
51
    {
52
        $fields = array_filter([
53
            'Shp_TransactionId' => $this->getTransactionId(),
54
            'Shp_Client' => $this->getClient(),
55
            'Shp_Currency' => $this->getCurrency(),
56
        ]);
57
58
        ksort($fields);
59
60
        return $fields;
61
    }
62
63
    public function getSignatureValue()
64
    {
65
        return $this->data['SignatureValue'];
66
    }
67
68
    public function getClient()
69
    {
70
        return $this->data['Shp_Client'];
71
    }
72
73
    public function getAmount()
74
    {
75
        return $this->data['OutSum'];
76
    }
77
78
    public function getPayer()
79
    {
80
        return $this->data['PaymentMethod'];
81
    }
82
83
    public function getTransactionId()
84
    {
85
        return $this->data['Shp_TransactionId'];
86
    }
87
88
    public function getCurrency()
89
    {
90
        return $this->data['Shp_Currency'];
91
    }
92
93
    /**
94
     * RoboKassa does not provide real transaction reference (ID of payment on RoboKassa side) 😕
95
     *
96
     * @return string The InvId property, or 0 (zero) if InvId is not set.
97
     */
98
    public function getTransactionReference()
99
    {
100
        return $this->data['InvId'] ?? '0';
101
    }
102
103
    public function getInvId()
104
    {
105
        return $this->getTransactionReference();
106
    }
107
108
    public function isSuccessful()
109
    {
110
        return true;
111
    }
112
}
113