This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOzan.php
146 lines (139 loc) · 4.73 KB
/
Ozan.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace Ozan;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Ozan\Common\OzanUserAgent;
use Ozan\Request\PaymentRequest;
use Ozan\Exceptions\OzanResponseException;
use Ozan\Exceptions\OzanSDKException;
class Ozan
{
/**
* @const string SDK name of the Ozan Pay PHP SDK.
*/
const SDK_NAME = 'Ozan Pay PHP SDK';
/**
* @const string Version number of the Ozan Pay PHP SDK.
*/
const SDK_VERSION = '1.0.0';
/**
* @const string The live base authorization URL.
*/
const BASE_API_URL_LIVE = 'https://ozan.com/api/';
/**
* @const string The sandbox base authorization URL.
*/
const BASE_API_URL_SANDBOX = 'https://sandbox.ozan.com/api/';
/**
* @const string The token url.
*/
const TOKEN_URL = 'oauth/token?grant_type=client_credentials';
/**
* @const string The token url.
*/
const PAYMENT_URL = 'payments';
/**
* @const int Unknown error code.
*/
const UNKNOWN_ERROR_CODE = 1000;
/**
* @const string Unknown error description.
*/
const UNKNOWN_ERROR_DESCRIPTION = 'Unknown error from Ozan API';
/**
* @var string The api key provided by Ozan.
*/
protected $apiKey;
/**
* @var string The secret key provided by Ozan.
*/
protected $secretKey;
/**
* @var bool Sandbox/Live mode.
*/
protected $isLive = false;
/**
* @var Client The Ozan Guzzle client service.
*/
protected $guzzleClient;
/**
* Instantiates a new Ozan super-class object.
*
* @param array $config
*
* @throws OzanSDKException
*/
public function __construct(array $config = [])
{
if (!isset($config['api_key'])) {
throw new OzanSDKException('Required "api_key" key not supplied in config and could not find fallback environment variable.');
}
if (!isset($config['secret_key'])) {
throw new OzanSDKException('Required "secret_key" key not supplied in config and could not find fallback environment variable.');
}
if (!isset($config['is_live'])) {
throw new OzanSDKException('Required "is_live" key not supplied in config and could not find fallback environment variable.');
}
$this->apiKey = $config['api_key'];
$this->secretKey = $config['secret_key'];
$this->isLive = $config['is_live'];
$this->guzzleClient = new Client(['base_uri' => $this->isLive ? static::BASE_API_URL_LIVE : static::BASE_API_URL_SANDBOX]);
}
/**
* Creates a payment.
*
* @param PaymentRequest $paymentRequest
*
* @return \OzanResponse
*
* @throws OzanResponseException
* @throws OzanSDKException
*/
public function createPayment($paymentRequest)
{
$accessToken = $this->getAccessToken();
try {
$response = $this->guzzleClient->post(static::PAYMENT_URL, [
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
'User-Agent' => OzanUserAgent::getValue(static::SDK_NAME, static::SDK_VERSION),
'Content-Type' => 'application/json'
],
'json' => $paymentRequest->fromJson()
]);
$paymentResponse = new OzanResponse($response->getBody(), $response->getStatusCode(), $response->getHeaders());
return $paymentResponse;
} catch (RequestException $e) {
$response = $e->getResponse();
throw OzanResponseException::create($response);
} catch (\Exception $e) {
return new OzanSDKException(static::UNKNOWN_ERROR_DESCRIPTION, static::UNKNOWN_ERROR_CODE);
}
}
/**
* Retrieves access token.
*
* @return string
* @throws OzanSDKException
* @throws OzanResponseException
*/
private function getAccessToken()
{
$credentials = base64_encode($this->apiKey . ':' . $this->secretKey);
try {
$response = $this->guzzleClient->post(static::TOKEN_URL, [
'headers' => [
'Authorization' => 'Basic ' . $credentials,
'User-Agent' => OzanUserAgent::getValue(static::SDK_NAME, static::SDK_VERSION)
]
]);
$tokenResponse = new OzanResponse($response->getBody(), $response->getStatusCode(), $response->getHeaders());
return $tokenResponse->getField('access_token');
} catch (RequestException $e) {
$response = $e->getResponse();
throw OzanResponseException::create($response);
} catch (\Exception $e) {
return new OzanSDKException(static::UNKNOWN_ERROR_DESCRIPTION, static::UNKNOWN_ERROR_CODE);
}
}
}