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