1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nxmad\Larapay\Abstracts; |
6
|
|
|
|
7
|
|
|
use Illuminate\Config\Repository; |
8
|
|
|
use Nxmad\Larapay\Units\Redirect; |
9
|
|
|
use Nxmad\Larapay\Contracts\Fields; |
10
|
|
|
use Nxmad\Larapay\Traits\HasMutations; |
11
|
|
|
use Illuminate\Http\Request as HttpRequest; |
12
|
|
|
|
13
|
|
|
abstract class Request implements Fields |
14
|
|
|
{ |
15
|
|
|
use HasMutations; |
16
|
|
|
|
17
|
|
|
const GET = 'GET'; |
18
|
|
|
const POST = 'POST'; |
19
|
|
|
const CUSTOM = '_'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The gateway instance. |
23
|
|
|
* |
24
|
|
|
* @var Gateway |
25
|
|
|
*/ |
26
|
|
|
protected $gateway; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Original HTTP request if presented. |
30
|
|
|
* |
31
|
|
|
* @var null|HttpRequest |
32
|
|
|
*/ |
33
|
|
|
protected $http = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The list of required fields by request. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $required = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The list of fields to transform. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $aliases = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The HTTP method of request. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $method = Redirect::GET; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Request constructor. |
58
|
|
|
* |
59
|
|
|
* @param Gateway $gateway |
60
|
|
|
* @param array|HttpRequest $data |
61
|
|
|
*/ |
62
|
|
|
public function __construct(Gateway $gateway, $data = []) |
63
|
|
|
{ |
64
|
|
|
$this->gateway = $gateway; |
65
|
|
|
$this->query = new Repository; |
66
|
|
|
|
67
|
|
|
if (is_array($data)) { |
68
|
|
|
$this->fill($data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($data instanceof HttpRequest) |
72
|
|
|
{ |
73
|
|
|
$this->http = $data; |
74
|
|
|
|
75
|
|
|
$this->setRaw($data->all()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get original http request. |
81
|
|
|
* |
82
|
|
|
* @return HttpRequest |
83
|
|
|
*/ |
84
|
|
|
public function getHttpRequest() |
85
|
|
|
{ |
86
|
|
|
return $this->http; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* You can override endpoint method. |
91
|
|
|
* |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
public function endpoint() |
95
|
|
|
{ |
96
|
|
|
return $this->gateway->getEndpoint($this); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* You can override sign method. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function sign() |
105
|
|
|
{ |
106
|
|
|
return $this->gateway->sign($this); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Transform request to redirect. |
111
|
|
|
* |
112
|
|
|
* @return Redirect |
113
|
|
|
*/ |
114
|
|
|
public function redirect() |
115
|
|
|
{ |
116
|
|
|
return new Redirect($this->endpoint(), $this->method, $this->all()); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Fires before send. |
121
|
|
|
*/ |
122
|
|
|
public function prepare() |
123
|
|
|
{ |
124
|
|
|
// ... |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function send() |
131
|
|
|
{ |
132
|
|
|
if (in_array(self::SIGNATURE, $this->required)) { |
133
|
|
|
$this->set(self::SIGNATURE, $this->sign()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
foreach ($this->required as $field) { |
137
|
|
|
if (! $this->has($field)) { |
138
|
|
|
throw new \RuntimeException("Required field [{$field}] is not presented."); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->prepare(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Convert no_one_should_use_this_case to CamelCase. |
147
|
|
|
* |
148
|
|
|
* @param string $str |
149
|
|
|
* @param string $sep |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
protected function camelize(string $str, string $sep = '_'): string |
154
|
|
|
{ |
155
|
|
|
return str_replace($sep, '', ucwords($str, $sep)); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|