Request.php
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Lackoxygen\MinPayment\Request;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use Lackoxygen\MinPayment\Utils\Signature;
use ReflectionProperty;
class Request
{
/**
* @var string
*/
protected $mchtId;
/**
* @var string
*/
protected $service;
/**
* @var string
*/
protected $ver;
/**
* @var string
*/
protected $token;
/**
* @return string
*/
public function getMethod(): string
{
return 'POST';
}
/**
* @return string
*/
public function getPath(): string
{
return 'Gateway';
}
/**
* @return string
*/
public function getOption(): string
{
return RequestOptions::JSON;
}
/**
* @param array $config
*
* @return array
*/
public function __invoke(array $config = []): array
{
$this->paddingHead($config);
$body = $this->extract();
$this->signature($body, Arr::get($config, 'secret'));
return $body;
}
private function signature(&$body, string $secret)
{
$body['sign'] = Signature::make($body, $secret);
}
private function paddingHead(array $config)
{
$this->mchtId = Arr::get($config, 'merchant_id');
$this->token = Signature::encode(microtime(true) . uniqid(''));
$this->ver = Arr::get($config, 'version', 'V3.1');
}
/**
* @return array
*/
private function extract(): array
{
$propertiesArray = [];
$ref = new \ReflectionClass($this);
$properties = $ref->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED);
foreach ($properties as $property) {
$property->setAccessible(true);
$propertiesArray[$property->getName()] = $property->getValue($this);
}
return $propertiesArray;
}
}