1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CMPayments\PaymentSdk; |
4
|
|
|
|
5
|
|
|
use CMPayments\GuzzlePSPAuthenticationMiddleware\AuthenticationMiddleware; |
6
|
|
|
use CMPayments\PaymentSdk\Requests\RequestInterface; |
7
|
|
|
use GuzzleHttp\Client as HttpClient; |
8
|
|
|
use GuzzleHttp\HandlerStack; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Gateway to the payments API. |
12
|
|
|
* |
13
|
|
|
* @package CMPayments\PaymentSdk |
14
|
|
|
* @author Jory Geerts <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Gateway |
17
|
|
|
{ |
18
|
|
|
const API_URL = 'https://api.cmpayments.com/'; |
19
|
|
|
|
20
|
|
|
const REQUEST_METHOD_POST = 'POST'; |
21
|
|
|
const REQUEST_METHOD_GET = 'GET'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var HttpClient |
25
|
|
|
*/ |
26
|
|
|
private $httpClient; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Gateway constructor. |
30
|
|
|
* @param Credentials $credentials |
31
|
|
|
* @param HttpClient|null $httpClient |
32
|
|
|
*/ |
33
|
5 |
|
public function __construct(Credentials $credentials, HttpClient $httpClient = null) |
34
|
|
|
{ |
35
|
5 |
|
if (!$httpClient) { |
36
|
1 |
|
$httpClient = new HttpClient([ |
37
|
|
|
'base_uri' => static::API_URL |
38
|
1 |
|
]); |
39
|
1 |
|
} |
40
|
5 |
|
$this->httpClient = $httpClient; |
41
|
|
|
|
42
|
|
|
/** @var HandlerStack $handlerStack */ |
43
|
5 |
|
$handlerStack = $httpClient->getConfig('handler'); |
44
|
5 |
|
$handlerStack->push( |
45
|
5 |
|
new AuthenticationMiddleware( |
46
|
5 |
|
$credentials->getMerchantKey(), |
47
|
5 |
|
$credentials->getMerchantSecret() |
48
|
5 |
|
) |
49
|
5 |
|
); |
50
|
5 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Execute a request against the Payments API. |
54
|
|
|
* |
55
|
|
|
* @param RequestInterface $request |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
4 |
|
public function execute(RequestInterface $request) |
59
|
|
|
{ |
60
|
4 |
|
$guzzleResponse = $this->httpClient->request( |
61
|
4 |
|
$request->getRequestMethod(), |
62
|
4 |
|
$request->getEndpoint(), |
63
|
|
|
[ |
64
|
4 |
|
'json' => $request->getPayload() |
65
|
4 |
|
] |
66
|
4 |
|
); |
67
|
|
|
|
68
|
1 |
|
return \GuzzleHttp\json_decode($guzzleResponse->getBody()->getContents()); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|