-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathThepeer.php
More file actions
189 lines (159 loc) · 5.6 KB
/
Thepeer.php
File metadata and controls
189 lines (159 loc) · 5.6 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace Thepeer\Sdk;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
use Thepeer\Sdk\Exceptions\ForbiddenException;
use Thepeer\Sdk\Exceptions\InvalidPayloadException;
use Thepeer\Sdk\Exceptions\InvalidResourceException;
use Thepeer\Sdk\Exceptions\NotAcceptableException;
use Thepeer\Sdk\Exceptions\ServiceUnavailableException;
use Thepeer\Sdk\Exceptions\UnauthorizedException;
use Thepeer\Sdk\Exceptions\ServerErrorException;
class Thepeer
{
/**
* @var string
*/
private $secretKey;
/**
* @var Client
*/
private $client;
/**
* Thepeer constructor.
* @param $secretKey
*/
public function __construct($secretKey)
{
$this->secretKey = $secretKey;
$this->client = new Client([
'base_uri' => 'https://api.thepeer.co',
'headers' => [
'x-api-key' => $secretKey,
'Content-Type' => 'application/json',
'Accept' => 'application/json'
],
'http_errors' => false
]);
}
public function validateSignature(Request $payload)
{
$headerSignature = $payload->header('X-Thepeer-Signature');
$calculatedSignature = hash_hmac('sha1', json_encode($payload->all()), $this->secretKey);
return $headerSignature === $calculatedSignature;
}
public function indexUser(string $name, string $email, string $identifier)
{
try {
$request = $this->client->post("/users", [
"body" => json_encode([
'name' => $name,
'email' => $email,
'identifier' => $identifier
])
]);
$payload = json_decode($request->getBody()->getContents());
return $this->processResponse($payload, $request);
} catch (GuzzleException $e) {
throw new ServerErrorException($e->getMessage());
}
}
public function updateUser(string $reference, string $identifier)
{
try {
$request = $this->client->put("/users/{$reference}", [
"body" => json_encode([
'identifier' => $identifier
])
]);
$payload = json_decode($request->getBody()->getContents());
return $this->processResponse($payload, $request);
} catch (GuzzleException $e) {
throw new ServerErrorException($e->getMessage());
}
}
public function deleteUser(string $reference)
{
try {
$request = $this->client->delete("/users/{$reference}");
$payload = json_decode($request->getBody()->getContents());
return $this->processResponse($payload, $request);
} catch (GuzzleException $e) {
throw new ServerErrorException($e->getMessage());
}
}
public function getLink(string $link)
{
try {
$request = $this->client->get("/link/{$link}");
$payload = json_decode($request->getBody()->getContents());
return $this->processResponse($payload, $request);
} catch (GuzzleException $e) {
throw new ServerErrorException($e->getMessage());
}
}
public function chargeLink(string $link, int $amount, string $remark)
{
try {
$request = $this->client->post("/link/{$link}/charge", [
"body" => json_encode([
'amount' => $amount,
'remark' => $remark
])
]);
$payload = json_decode($request->getBody()->getContents());
return $this->processResponse($payload, $request);
} catch (GuzzleException $e) {
throw new ServerErrorException($e->getMessage());
}
}
public function authorizeCharge(string $reference, string $event)
{
try {
$request = $this->client->post("/authorization/{$reference}", [
"body" => json_encode([
'event' => $event,
])
]);
$payload = json_decode($request->getBody()->getContents());
return $this->processResponse($payload, $request);
} catch (GuzzleException $e) {
throw new ServerErrorException($e->getMessage());
}
}
public function getBusinesses(string $channel)
{
try {
$request = $this->client->get("/businesses?channel={$channel}");
$payload = json_decode($request->getBody()->getContents());
return $this->processResponse($payload, $request);
} catch (GuzzleException $e) {
throw new ServerErrorException($e->getMessage());
}
}
private function processResponse($payload, $request)
{
switch ($request->getStatusCode()) {
case 201:
case 200:
return $payload;
case 401:
throw new UnauthorizedException($payload->message);
case 403:
throw new ForbiddenException($payload->message);
case 404:
throw new InvalidResourceException($payload->message);
case 422:
foreach ($payload->errors as $error) {
throw new InvalidPayloadException($error[0]);
}
case 406:
throw new NotAcceptableException($payload->message);
case 503:
throw new ServiceUnavailableException($payload->message);
default:
throw new ServerErrorException($payload->message);
}
}
}