1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xsolla\SDK\Webhook; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Xsolla\SDK\Exception\Webhook\InvalidParameterException; |
7
|
|
|
use Xsolla\SDK\Exception\Webhook\XsollaWebhookException; |
8
|
|
|
|
9
|
|
|
class WebhookRequest |
10
|
|
|
{ |
11
|
|
|
protected static $codes = [ |
12
|
|
|
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded.', |
13
|
|
|
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded.', |
14
|
|
|
JSON_ERROR_NONE => 'No error has occurred.', |
15
|
|
|
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON.', |
16
|
|
|
JSON_ERROR_SYNTAX => 'Syntax error.', |
17
|
|
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded.', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $body; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $headers; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $clientIp; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws XsollaWebhookException |
37
|
|
|
* @return WebhookRequest |
38
|
|
|
*/ |
39
|
|
|
public static function fromGlobals() |
40
|
|
|
{ |
41
|
|
|
$request = Request::createFromGlobals(); |
42
|
|
|
$headers = []; |
43
|
|
|
foreach ($request->headers->all() as $header => $arrayValue) { |
44
|
|
|
$headers[$header] = $arrayValue[0]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return new static($headers, $request->getContent(), $request->getClientIp()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $headers |
52
|
|
|
* @param string $body |
53
|
|
|
* @param string $clientIp |
54
|
|
|
*/ |
55
|
|
|
public function __construct(array $headers, $body, $clientIp = null) |
56
|
|
|
{ |
57
|
|
|
$this->headers = $headers; |
58
|
|
|
$this->body = $body; |
59
|
|
|
$this->clientIp = $clientIp; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getBody() |
66
|
|
|
{ |
67
|
|
|
return $this->body; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @throws XsollaWebhookException |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function toArray() |
75
|
|
|
{ |
76
|
|
|
$data = json_decode($this->body, true); |
77
|
|
|
$jsonLastError = json_last_error(); |
78
|
|
|
if (JSON_ERROR_NONE !== $jsonLastError) { |
79
|
|
|
$message = 'Unknown error.'; |
80
|
|
|
if (array_key_exists($jsonLastError, self::$codes)) { |
81
|
|
|
$message = self::$codes[$jsonLastError]; |
82
|
|
|
} |
83
|
|
|
throw new InvalidParameterException('Unable to parse Xsolla webhook request into JSON: '.$message); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return null === $data ? [] : $data; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function getHeaders() |
93
|
|
|
{ |
94
|
|
|
return $this->headers; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getClientIp() |
101
|
|
|
{ |
102
|
|
|
return $this->clientIp; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|