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