-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixResource.php
More file actions
79 lines (68 loc) · 2.37 KB
/
Copy pathPixResource.php
File metadata and controls
79 lines (68 loc) · 2.37 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
<?php
declare(strict_types=1);
namespace Basement\AbacatePay\Pix;
use Basement\AbacatePay\Exception\AbacatePayException;
use Basement\AbacatePay\Pix\Http\Request\CreatePixQrCodeRequest;
use Basement\AbacatePay\Pix\Http\Response\CheckStatusPixQrCodeResponse;
use Basement\AbacatePay\Pix\Http\Response\CreatePixQrCodeResponse;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use JsonException;
use Symfony\Component\HttpFoundation\Response;
final readonly class PixResource
{
public const string BASE_PATH = 'pixQrCode';
public function __construct(
private Client $client,
) {}
/**
* @throws AbacatePayException
* @throws JsonException
*/
public function createQrCode(CreatePixQrCodeRequest $request): CreatePixQrCodeResponse
{
try {
$response = $this->client->post(sprintf('%s/create', self::BASE_PATH), [
'json' => $request->toArray(),
]);
$responsePayload = json_decode(
$response->getBody()->getContents(),
true,
512,
JSON_THROW_ON_ERROR
);
return CreatePixQrCodeResponse::fromArray($responsePayload);
} catch (GuzzleException $e) {
match ($e->getCode()) {
Response::HTTP_UNAUTHORIZED => throw AbacatePayException::unauthorized(),
default => throw new AbacatePayException($e->getMessage(), $e->getCode()),
};
}
}
/**
* @throws AbacatePayException
* @throws JsonException
*/
public function checkStatus(string $id): CheckStatusPixQrCodeResponse
{
try {
$response = $this->client->post(sprintf('%s/check', self::BASE_PATH), [
'query' => [
'id' => $id,
],
]);
$responsePayload = json_decode(
$response->getBody()->getContents(),
true,
512,
JSON_THROW_ON_ERROR
);
return CheckStatusPixQrCodeResponse::fromArray($responsePayload);
} catch (GuzzleException $e) {
match ($e->getCode()) {
Response::HTTP_UNAUTHORIZED => throw AbacatePayException::unauthorized(),
default => throw new AbacatePayException($e->getMessage(), $e->getCode()),
};
}
}
}