1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Korobovn\CloudPayments\Message\Request; |
4
|
|
|
|
5
|
|
|
use Tarampampam\Wrappers\Json; |
6
|
|
|
use Korobovn\CloudPayments\Client\CloudPaymentClientInterface; |
7
|
|
|
use Korobovn\CloudPayments\Message\Response\ResponseInterface; |
8
|
|
|
use Korobovn\CloudPayments\Message\Strategy\StrategyInterface; |
9
|
|
|
use Korobovn\CloudPayments\Message\Request\Model\ModelInterface; |
10
|
|
|
|
11
|
|
|
abstract class AbstractRequest implements RequestInterface |
12
|
|
|
{ |
13
|
|
|
/** @var string */ |
14
|
|
|
protected $domain = 'https://api.cloudpayments.ru/'; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $url; |
18
|
|
|
|
19
|
|
|
/** @var ModelInterface */ |
20
|
|
|
protected $model; |
21
|
|
|
|
22
|
|
|
/** @var StrategyInterface */ |
23
|
|
|
protected $strategy; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $method = 'POST'; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
protected $headers = [ |
30
|
|
|
'Content-Type' => 'application/json', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** @var CloudPaymentClientInterface */ |
34
|
|
|
protected $client; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function getUrl(): string |
40
|
|
|
{ |
41
|
|
|
return rtrim($this->domain, '/') . '/' . ltrim($this->url, '/'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return ModelInterface |
46
|
|
|
*/ |
47
|
|
|
public function getModel(): ModelInterface |
48
|
|
|
{ |
49
|
|
|
return $this->model; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return StrategyInterface |
54
|
|
|
*/ |
55
|
|
|
public function getStrategy(): StrategyInterface |
56
|
|
|
{ |
57
|
|
|
return $this->strategy; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string|null |
62
|
|
|
* @throws \Tarampampam\Wrappers\Exceptions\JsonEncodeDecodeException |
63
|
|
|
* |
64
|
|
|
*/ |
65
|
|
|
public function getBody(): ?string |
66
|
|
|
{ |
67
|
|
|
return Json::encode($this->getModel()->toArray()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getMethod(): string |
74
|
|
|
{ |
75
|
|
|
return $this->method; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function getHeaders(): array |
82
|
|
|
{ |
83
|
|
|
return $this->headers; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param CloudPaymentClientInterface $client |
88
|
|
|
* |
89
|
|
|
* @return self |
90
|
|
|
*/ |
91
|
|
|
public function setClient(CloudPaymentClientInterface $client): self |
92
|
|
|
{ |
93
|
|
|
$this->client = $client; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return ResponseInterface |
100
|
|
|
*/ |
101
|
|
|
public function send(): ResponseInterface |
102
|
|
|
{ |
103
|
|
|
return $this->client->send($this); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|