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\Bridge; |
14
|
|
|
|
15
|
|
|
use GuzzleHttp\ClientInterface; |
16
|
|
|
|
17
|
|
|
final class Przelewy24Bridge implements Przelewy24BridgeInterface |
18
|
|
|
{ |
19
|
|
|
/** @var string */ |
20
|
|
|
private $merchantId; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $crcKey; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $environment = self::SANDBOX_ENVIRONMENT; |
27
|
|
|
|
28
|
|
|
/** @var ClientInterface */ |
29
|
|
|
private $client; |
30
|
|
|
|
31
|
|
|
public function __construct(ClientInterface $client) |
32
|
|
|
{ |
33
|
|
|
$this->client = $client; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setAuthorizationData( |
37
|
|
|
string $merchantId, |
38
|
|
|
string $crcKey, |
39
|
|
|
string $environment = self::SANDBOX_ENVIRONMENT |
40
|
|
|
): void { |
41
|
|
|
$this->merchantId = $merchantId; |
42
|
|
|
$this->crcKey = $crcKey; |
43
|
|
|
$this->environment = $environment; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getTrnRegisterUrl(): string |
47
|
|
|
{ |
48
|
|
|
return $this->getHostForEnvironment() . 'trnRegister'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getTrnRequestUrl(string $token): string |
52
|
|
|
{ |
53
|
|
|
return $this->getHostForEnvironment() . 'trnRequest/' . $token; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getTrnVerifyUrl(): string |
57
|
|
|
{ |
58
|
|
|
return $this->getHostForEnvironment() . 'trnVerify'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getHostForEnvironment(): string |
62
|
|
|
{ |
63
|
|
|
return self::SANDBOX_ENVIRONMENT === $this->environment ? |
64
|
|
|
self::SANDBOX_HOST : self::PRODUCTION_HOST |
65
|
|
|
; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function createSign(array $parameters): string |
69
|
|
|
{ |
70
|
|
|
return md5(implode('|', array_merge($parameters, [$this->crcKey]))); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function trnRegister(array $posData): string |
74
|
|
|
{ |
75
|
|
|
$posData['p24_merchant_id'] = $this->merchantId; |
76
|
|
|
$posData['p24_pos_id'] = $this->merchantId; |
77
|
|
|
$posData['p24_api_version'] = self::P24_API_VERSION; |
78
|
|
|
|
79
|
|
|
$sign = $this->createSign([ |
80
|
|
|
$posData['p24_session_id'], |
81
|
|
|
$posData['p24_merchant_id'], |
82
|
|
|
$posData['p24_amount'], |
83
|
|
|
$posData['p24_currency'], |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
$posData['p24_sign'] = $sign; |
87
|
|
|
|
88
|
|
|
return $this->request($posData, $this->getTrnRegisterUrl())['token']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function trnVerify(array $posData): bool |
92
|
|
|
{ |
93
|
|
|
$posData['p24_merchant_id'] = $this->merchantId; |
94
|
|
|
$posData['p24_pos_id'] = $this->merchantId; |
95
|
|
|
$posData['p24_api_version'] = self::P24_API_VERSION; |
96
|
|
|
|
97
|
|
|
$sign = $this->createSign([ |
98
|
|
|
$posData['p24_session_id'], |
99
|
|
|
$posData['p24_order_id'], |
100
|
|
|
$posData['p24_amount'], |
101
|
|
|
$posData['p24_currency'], |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
$posData['p24_sign'] = $sign; |
105
|
|
|
|
106
|
|
|
return (int) $this->request($posData, $this->getTrnVerifyUrl())['error'] === 0; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function request(array $posData, string $url): array |
110
|
|
|
{ |
111
|
|
|
$response = (string) $this->client->request('POST', $url, ['form_params' => $posData])->getBody(); |
112
|
|
|
|
113
|
|
|
$result = []; |
114
|
|
|
|
115
|
|
|
foreach (explode('&', $response) as $value) { |
116
|
|
|
$value = explode('=', $value); |
117
|
|
|
|
118
|
|
|
$result[trim($value[0])] = $value[1] ?? null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!isset($result['error']) || $result['error'] > 0) { |
122
|
|
|
throw new \Exception($response); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|