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(false)) !== $this->generateSignature()) { |
||
31 | if (strtolower($this->getSignatureValue(true) !== $this->generateSignature())) { |
||
32 | throw new InvalidResponseException('Invalid hash'); |
||
33 | } |
||
34 | } |
||
35 | } |
||
36 | |||
37 | public function generateSignature(bool $includeCurrency = false): string |
||
38 | { |
||
39 | $params = [ |
||
40 | $this->getAmount(), |
||
41 | $this->getTransactionReference(), |
||
42 | $this->request->getSecretKey2() |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
43 | ]; |
||
44 | |||
45 | foreach ($this->getCustomFields($includeCurrency) as $field => $value) { |
||
46 | $params[] = "$field=$value"; |
||
47 | } |
||
48 | |||
49 | return md5(implode(':', $params)); |
||
50 | } |
||
51 | |||
52 | public function getCustomFields(bool $includeCurrency = false): array |
||
53 | { |
||
54 | $fields = array_filter([ |
||
55 | 'Shp_TransactionId' => $this->getTransactionId(), |
||
56 | 'Shp_Client' => $this->getClient(), |
||
57 | 'Shp_Currency' => $includeCurrency ? $this->getCurrency() : null, |
||
58 | ]); |
||
59 | |||
60 | ksort($fields); |
||
61 | |||
62 | return $fields; |
||
63 | } |
||
64 | |||
65 | public function getSignatureValue() |
||
66 | { |
||
67 | return $this->data['SignatureValue']; |
||
68 | } |
||
69 | |||
70 | public function getClient() |
||
71 | { |
||
72 | return $this->data['Shp_Client']; |
||
73 | } |
||
74 | |||
75 | public function getAmount() |
||
76 | { |
||
77 | return $this->data['OutSum']; |
||
78 | } |
||
79 | |||
80 | public function getPayer() |
||
81 | { |
||
82 | return $this->data['PaymentMethod']; |
||
83 | } |
||
84 | |||
85 | public function getTransactionId() |
||
86 | { |
||
87 | return $this->data['Shp_TransactionId']; |
||
88 | } |
||
89 | |||
90 | public function getCurrency() |
||
91 | { |
||
92 | return $this->data['OutSumCurrency'] ?? 'RUB'; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * RoboKassa does not provide real transaction reference (ID of payment on RoboKassa side) 😕 |
||
97 | * |
||
98 | * @return string The InvId property, or 0 (zero) if InvId is not set. |
||
99 | */ |
||
100 | public function getTransactionReference() |
||
101 | { |
||
102 | return $this->data['InvId'] ?? '0'; |
||
103 | } |
||
104 | |||
105 | public function getInvId() |
||
106 | { |
||
107 | return $this->getTransactionReference(); |
||
108 | } |
||
109 | |||
110 | public function isSuccessful() |
||
111 | { |
||
112 | return true; |
||
113 | } |
||
114 | } |
||
115 |