1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Korobovn\CloudPayments\Message\Request; |
6
|
|
|
|
7
|
|
|
use Korobovn\CloudPayments\Message\Request\Exception\ClientCannotBeNull; |
8
|
|
|
use Tarampampam\Wrappers\Json; |
9
|
|
|
use Korobovn\CloudPayments\Client\CloudPaymentClientInterface; |
10
|
|
|
use Korobovn\CloudPayments\Message\Response\ResponseInterface; |
11
|
|
|
use Korobovn\CloudPayments\Message\Strategy\StrategyInterface; |
12
|
|
|
use Korobovn\CloudPayments\Message\Request\Model\ModelInterface; |
13
|
|
|
|
14
|
|
|
abstract class AbstractRequest implements RequestInterface |
15
|
|
|
{ |
16
|
|
|
protected const API_CLOUD_PAYMENT_DOMAIN = 'https://api.cloudpayments.ru/'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ModelInterface|null |
20
|
|
|
*/ |
21
|
|
|
protected $model; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $method = 'POST'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $headers = [ |
32
|
|
|
'Content-Type' => 'application/json', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var CloudPaymentClientInterface|null |
37
|
|
|
*/ |
38
|
|
|
protected $client; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns relative url of request (without domain part) |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
abstract protected function getRelativeUrl(): string; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
*/ |
50
|
|
|
public function getUrl(): string |
51
|
|
|
{ |
52
|
|
|
return rtrim($this->getDomain(), '/') . '/' . ltrim($this->getRelativeUrl(), '/'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Main domain name of cloud payments api server |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
protected function getDomain(): string |
61
|
|
|
{ |
62
|
|
|
return static::API_CLOUD_PAYMENT_DOMAIN; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
*/ |
68
|
|
|
public function getModel(): ModelInterface |
69
|
|
|
{ |
70
|
|
|
if ($this->model === null) { |
71
|
|
|
$this->model = $this->createModel(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->model; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return ModelInterface |
79
|
|
|
*/ |
80
|
|
|
abstract protected function createModel(): ModelInterface; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
*/ |
85
|
|
|
abstract public function getStrategy(): StrategyInterface; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritDoc} |
89
|
|
|
*/ |
90
|
|
|
public function getBody(): ?string |
91
|
|
|
{ |
92
|
|
|
return Json::encode($this->getModel()->toArray()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritDoc} |
97
|
|
|
*/ |
98
|
|
|
public function getMethod(): string |
99
|
|
|
{ |
100
|
|
|
return $this->method; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritDoc} |
105
|
|
|
*/ |
106
|
|
|
public function getHeaders(): array |
107
|
|
|
{ |
108
|
|
|
return $this->headers; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritDoc} |
113
|
|
|
*/ |
114
|
|
|
public function setClient(CloudPaymentClientInterface $client): RequestInterface |
115
|
|
|
{ |
116
|
|
|
$this->client = $client; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return CloudPaymentClientInterface |
123
|
|
|
* @throws \LogicException |
124
|
|
|
*/ |
125
|
|
|
protected function getClient(): CloudPaymentClientInterface |
126
|
|
|
{ |
127
|
|
|
if ($this->client === null) { |
128
|
|
|
throw new ClientCannotBeNull('The client cannot be null'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->client; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritDoc} |
136
|
|
|
*/ |
137
|
|
|
public function send(): ResponseInterface |
138
|
|
|
{ |
139
|
|
|
return $this->getClient()->send($this); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|