-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathOpenpay.php
More file actions
232 lines (190 loc) · 5.71 KB
/
Copy pathOpenpay.php
File metadata and controls
232 lines (190 loc) · 5.71 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
declare(strict_types=1);
namespace Openpay\Data;
/**
* Openpay SDK credential holder (Nowo fork).
*
* Credentials are process-global statics (upstream SDK design). Callers must use
* {@see configure()} / {@see getInstance()} per logical operation and {@see reset()}
* afterwards so merchant keys never leak across HTTP requests — including under
* php-fpm workers and FrankenPHP ZTS / worker mode.
*/
class Openpay
{
private static $instance = null;
private static $id = null;
private static $apiKey = null;
private static $userAgent = '';
private static $country = null;
private static string $apiEndpoint = '';
private static string $apiSandboxEndpoint = '';
private static bool $sandboxMode = true;
private static $classification = '';
private static $publicIp = null;
/**
* Clears all merchant credentials and endpoint state.
* Safe to call between requests / after each API session.
*/
public static function reset(): void
{
self::$instance = null;
self::$id = null;
self::$apiKey = null;
self::$userAgent = '';
self::$country = null;
self::$apiEndpoint = '';
self::$apiSandboxEndpoint = '';
self::$sandboxMode = true;
self::$classification = '';
self::$publicIp = null;
}
/**
* Overwrites static credentials for the next API calls (no merge with previous merchant).
*/
public static function configure(string $id, string $apiKey, string $country = 'MX', string $publicIp = '127.0.0.1'): void
{
self::$id = $id;
self::$apiKey = $apiKey;
self::$country = $country;
self::$publicIp = $publicIp;
self::setEndpointUrl($country);
}
public static function getInstance(string $id, string $apiKey, ?string $country = '', ?string $publicIp = null)
{
$country = ($country !== null && $country !== '') ? $country : 'MX';
$publicIp = $publicIp ?? '127.0.0.1';
self::configure($id, $apiKey, $country, $publicIp);
return OpenpayApi::createRoot();
}
public static function setUserAgent($userAgent): void
{
if ($userAgent !== '')
{
self::$userAgent = $userAgent;
}
}
public static function getUserAgent()
{
return self::$userAgent;
}
public static function setClassificationMerchant($classification): void
{
if ($classification !== '')
{
self::$classification = $classification;
}
}
public static function getClassificationMerchant()
{
return self::$classification;
}
public static function setApiKey($key = ''): void
{
if ($key !== '')
{
self::$apiKey = $key;
}
}
public static function getApiKey()
{
$key = self::$apiKey;
if (!$key)
{
return getenv('OPENPAY_API_KEY');
}
return $key;
}
public static function setId($id = ''): void
{
if ($id !== '')
{
self::$id = $id;
}
}
public static function setCountry($country = ''): void
{
if ($country !== '')
{
self::$country = $country;
}
}
public static function getCountry()
{
return self::$country;
}
public static function getId()
{
$id = self::$id;
if (!$id)
{
return getenv('OPENPAY_MERCHANT_ID');
}
return $id;
}
public static function setPublicIp($publicIp = null): void
{
if (null !== $publicIp)
{
self::$publicIp = $publicIp;
}
}
public static function getPublicIp()
{
return self::$publicIp;
}
public static function getSandboxMode()
{
if (getenv('OPENPAY_PRODUCTION_MODE'))
{
return strtoupper(getenv('OPENPAY_PRODUCTION_MODE')) === 'FALSE';
}
return self::$sandboxMode;
}
public static function setSandboxMode($mode): void
{
self::$sandboxMode = (bool) $mode;
}
public static function getProductionMode(): bool
{
$sandbox = self::$sandboxMode;
if (getenv('OPENPAY_PRODUCTION_MODE'))
{
$sandbox = (strtoupper(getenv('OPENPAY_PRODUCTION_MODE')) === 'FALSE');
}
return !$sandbox;
}
public static function setProductionMode($mode): void
{
self::$sandboxMode = !(bool) $mode;
}
public static function setEndpointUrl($country): void
{
if ($country === 'MX')
{
if (self::getClassificationMerchant() !== 'eglobal')
{
self::$apiEndpoint = 'https://api.openpay.mx/v1';
self::$apiSandboxEndpoint = 'https://sandbox-api.openpay.mx/v1';
}
else
{
self::$apiEndpoint = 'https://api.ecommercebbva.com/v1';
self::$apiSandboxEndpoint = 'https://sand-api.ecommercebbva.com/v1';
}
}
elseif ($country === 'CO')
{
self::$apiEndpoint = 'https://api.openpay.co/v1';
self::$apiSandboxEndpoint = 'https://sandbox-api.openpay.co/v1';
}
elseif ($country === 'PE')
{
self::$apiEndpoint = 'https://api.openpay.pe/v1';
self::$apiSandboxEndpoint = 'https://sandbox-api.openpay.pe/v1';
}
}
public static function getEndpointUrl(): string
{
return self::getSandboxMode() ? self::$apiSandboxEndpoint : self::$apiEndpoint;
}
}