-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApi.php
112 lines (99 loc) · 3.45 KB
/
Api.php
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
<?php
namespace SlimDash\Payeezy;
use Payum\Core\Bridge\Guzzle\HttpClientFactory;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception;
use Payum\Core\Exception\Http\HttpException;
use Payum\Core\HttpClientInterface;
class Api
{
/**
* @var HttpClientInterface
*/
protected $client;
/**
* @var array
*/
protected $options = array(
'apiKey' => null,
'apiSecret' => null,
'merchantToken' => null,
'sandbox' => null,
);
/**
* @param array $options
* @param HttpClientInterface $client
* @throws \Payum\Core\Exception\InvalidArgumentException if an option is invalid
*/
public function __construct(array $options, HttpClientInterface $client)
{
$options = ArrayObject::ensureArrayObject($options);
$options->defaults($this->options);
$options->validateNotEmpty(array(
'apiKey',
'apiSecret',
'merchantToken',
));
if (false == is_bool($options['sandbox'])) {
throw new LogicException('The boolean sandbox option must be set.');
}
$this->options = $options;
$this->client = $client ?: HttpClientFactory::create();
}
/**
* @return string
*/
protected function getApiEndpoint()
{
return
$this->options['sandbox']
? 'https://api-cert.payeezy.com/v1/transactions'
: 'https://api.payeezy.com/v1/transactions'
;
}
/**
* @param string $payload
* @return array
*/
public function getHeaders($payload)
{
$nonce = strval(hexdec(bin2hex(openssl_random_pseudo_bytes(4, $cstrong))));
$timestamp = strval(time() * 1000); //time stamp in milli seconds
$data = $this->options['apiKey'] . $nonce . $timestamp . $this->options['merchantToken'] . $payload;
$hashAlgorithm = "sha256";
$hmac = hash_hmac($hashAlgorithm, $data, $this->options['apiSecret'], false); // HMAC Hash in hex
$authorization = base64_encode($hmac);
return array(
'Authorization' => $authorization,
'nonce' => $nonce,
'timestamp' => $timestamp,
'apikey' => $this->options['apiKey'],
'token' => $this->options['merchantToken'],
'Content-Type' => 'application/json',
);
}
/**
* @param array $fields
* @param string $transaction_id
* @return array
*/
public function doRequest($fields = array(), $transaction_id = null)
{
$url = $this->getApiEndpoint();
if (isset($transaction_id)) {
$url = $url . '/' . $transaction_id;
}
$payload = json_encode($fields, JSON_FORCE_OBJECT);
$headers = $this->getHeaders($payload);
$request = new \GuzzleHttp\Psr7\Request('POST', $url, $headers, $payload);
$response = $this->client->send($request);
if (false == ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300)) {
throw HttpException::factory($request, $response);
}
$result = json_decode($response->getBody()->getContents());
if (null === $result) {
throw new LogicException("Response content is not valid json: \n\n{$response->getBody()->getContents()}");
}
return $result;
}
}