1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Larium\Pay\Gateway; |
6
|
|
|
|
7
|
|
|
use Larium\Pay\Client\RestClient; |
8
|
|
|
use Larium\Pay\Response; |
9
|
|
|
|
10
|
|
|
abstract class RestGateway extends Gateway |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Get the base uri for gateway to make the requests. |
14
|
|
|
* |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
abstract protected function getBaseUri(): string; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Authenticate gateway. |
21
|
|
|
* It is common for rest gateways to authenticate with basic auth, bearer |
22
|
|
|
* or custom header authentication. |
23
|
|
|
* So here we can add directly to client the appropriate authentication |
24
|
|
|
* info. |
25
|
|
|
* |
26
|
|
|
* @param RestClient $client |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
abstract protected function authenticate(RestClient $client): void; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns the RestClient for given resource name. |
33
|
|
|
* |
34
|
|
|
* Resource name is usually the uri path for the resource to call. |
35
|
|
|
* In conjuction with base uri will create the full endpoint uri of |
36
|
|
|
* gateway to request. |
37
|
|
|
* |
38
|
|
|
* @param string $resource |
39
|
|
|
* @return RestClient |
40
|
|
|
*/ |
41
|
|
|
protected function getRestClient($resource): RestClient |
42
|
|
|
{ |
43
|
|
|
$client = $this->createClient($this->getBaseUri(), $resource); |
44
|
|
|
$this->authenticate($client); |
45
|
|
|
|
46
|
|
|
return $client; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Factory method for creating the rest client. |
51
|
|
|
* |
52
|
|
|
* @param string $uri The base uri path of gateway. |
53
|
|
|
* @param string $resource The resource path to request. |
54
|
|
|
* @return RestClient |
55
|
|
|
*/ |
56
|
|
|
protected function createClient($uri, $resource): RestClient |
57
|
|
|
{ |
58
|
|
|
return new RestClient($uri, $resource); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the final Response object |
63
|
|
|
* |
64
|
|
|
* @param array $response The response returned from RestClient @see |
65
|
|
|
* RestClient::resolveResponse method |
66
|
|
|
* @return \Larium\Pay\Response|mixed |
67
|
|
|
*/ |
68
|
1 |
|
protected function getResponse(array $response): mixed |
69
|
|
|
{ |
70
|
1 |
|
return $this->createResponse( |
71
|
1 |
|
$this->success($response), |
72
|
1 |
|
$this->message($response), |
73
|
1 |
|
$this->transactionId($response), |
74
|
1 |
|
$this->errorCode($response), |
75
|
1 |
|
$this->responseCode($response), |
76
|
1 |
|
$response['body'], |
77
|
1 |
|
$response['raw_response'], |
78
|
1 |
|
$response['raw_request'] |
79
|
1 |
|
); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|