|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | namespace Openpay\Data; |
4 | 6 |
|
| 7 | +/** |
| 8 | + * Openpay SDK credential holder (Nowo fork). |
| 9 | + * |
| 10 | + * Credentials are process-global statics (upstream SDK design). Callers must use |
| 11 | + * {@see configure()} / {@see getInstance()} per logical operation and {@see reset()} |
| 12 | + * afterwards so merchant keys never leak across HTTP requests — including under |
| 13 | + * php-fpm workers and FrankenPHP ZTS / worker mode. |
| 14 | + */ |
5 | 15 | class Openpay |
6 | 16 | { |
7 | | - |
8 | 17 | private static $instance = null; |
9 | | - private static $id; |
10 | | - private static $apiKey; |
| 18 | + |
| 19 | + private static $id = null; |
| 20 | + |
| 21 | + private static $apiKey = null; |
| 22 | + |
11 | 23 | private static $userAgent = ''; |
12 | | - private static $country; |
13 | | - private static $apiEndpoint = ''; |
14 | | - private static $apiSandboxEndpoint = ''; |
15 | | - private static $sandboxMode = true; |
| 24 | + |
| 25 | + private static $country = null; |
| 26 | + |
| 27 | + private static string $apiEndpoint = ''; |
| 28 | + |
| 29 | + private static string $apiSandboxEndpoint = ''; |
| 30 | + |
| 31 | + private static bool $sandboxMode = true; |
| 32 | + |
16 | 33 | private static $classification = ''; |
17 | | - private static $publicIp; |
18 | 34 |
|
19 | | - public function __construct() |
| 35 | + private static $publicIp = null; |
| 36 | + |
| 37 | + /** |
| 38 | + * Clears all merchant credentials and endpoint state. |
| 39 | + * Safe to call between requests / after each API session. |
| 40 | + */ |
| 41 | + public static function reset(): void |
20 | 42 | { |
| 43 | + self::$instance = null; |
| 44 | + self::$id = null; |
| 45 | + self::$apiKey = null; |
| 46 | + self::$userAgent = ''; |
| 47 | + self::$country = null; |
| 48 | + self::$apiEndpoint = ''; |
| 49 | + self::$apiSandboxEndpoint = ''; |
| 50 | + self::$sandboxMode = true; |
| 51 | + self::$classification = ''; |
| 52 | + self::$publicIp = null; |
| 53 | + } |
21 | 54 |
|
| 55 | + /** |
| 56 | + * Overwrites static credentials for the next API calls (no merge with previous merchant). |
| 57 | + */ |
| 58 | + public static function configure(string $id, string $apiKey, string $country = 'MX', string $publicIp = '127.0.0.1'): void |
| 59 | + { |
| 60 | + self::$id = $id; |
| 61 | + self::$apiKey = $apiKey; |
| 62 | + self::$country = $country; |
| 63 | + self::$publicIp = $publicIp; |
| 64 | + self::setEndpointUrl($country); |
22 | 65 | } |
23 | 66 |
|
24 | | - public static function getInstance($id, $apiKey, $country, $publicIp) |
| 67 | + public static function getInstance(string $id, string $apiKey, ?string $country = '', ?string $publicIp = null) |
25 | 68 | { |
26 | | - if ($id != '') { |
27 | | - self::setId($id); |
28 | | - } |
29 | | - if ($apiKey != '') { |
30 | | - self::setApiKey($apiKey); |
31 | | - } |
32 | | - if ($country != '') { |
33 | | - self::setCountry($country); |
34 | | - self::setEndpointUrl($country); |
35 | | - } |
36 | | - if(!is_null($publicIp)){ |
37 | | - self::setPublicIp($publicIp); |
38 | | - } |
39 | | - $instance = OpenpayApi::getInstance(null); |
40 | | - return $instance; |
| 69 | + $country = ($country !== null && $country !== '') ? $country : 'MX'; |
| 70 | + $publicIp = $publicIp ?? '127.0.0.1'; |
| 71 | + |
| 72 | + self::configure($id, $apiKey, $country, $publicIp); |
| 73 | + |
| 74 | + return OpenpayApi::createRoot(); |
41 | 75 | } |
42 | 76 |
|
43 | | - public static function setUserAgent($userAgent) |
| 77 | + public static function setUserAgent($userAgent): void |
44 | 78 | { |
45 | | - if ($userAgent != '') { |
| 79 | + if ($userAgent !== '') |
| 80 | + { |
46 | 81 | self::$userAgent = $userAgent; |
47 | 82 | } |
48 | 83 | } |
49 | 84 |
|
50 | 85 | public static function getUserAgent() |
51 | 86 | { |
52 | | - $userAgent = self::$userAgent; |
53 | | - return $userAgent; |
| 87 | + return self::$userAgent; |
54 | 88 | } |
55 | 89 |
|
56 | | - public static function setClassificationMerchant($classification) |
| 90 | + public static function setClassificationMerchant($classification): void |
57 | 91 | { |
58 | | - if ($classification != '') { |
| 92 | + if ($classification !== '') |
| 93 | + { |
59 | 94 | self::$classification = $classification; |
60 | 95 | } |
61 | 96 | } |
62 | 97 |
|
63 | 98 | public static function getClassificationMerchant() |
64 | 99 | { |
65 | | - $classification = self::$classification; |
66 | | - return $classification; |
| 100 | + return self::$classification; |
67 | 101 | } |
68 | 102 |
|
69 | | - public static function setApiKey($key = '') |
| 103 | + public static function setApiKey($key = ''): void |
70 | 104 | { |
71 | | - if ($key != '') { |
| 105 | + if ($key !== '') |
| 106 | + { |
72 | 107 | self::$apiKey = $key; |
73 | 108 | } |
74 | 109 | } |
75 | 110 |
|
76 | 111 | public static function getApiKey() |
77 | 112 | { |
78 | 113 | $key = self::$apiKey; |
79 | | - if (!$key) { |
80 | | - $key = getenv('OPENPAY_API_KEY'); |
| 114 | + |
| 115 | + if (!$key) |
| 116 | + { |
| 117 | + return getenv('OPENPAY_API_KEY'); |
81 | 118 | } |
| 119 | + |
82 | 120 | return $key; |
83 | 121 | } |
84 | 122 |
|
85 | | - public static function setId($id = '') |
| 123 | + public static function setId($id = ''): void |
86 | 124 | { |
87 | | - if ($id != '') { |
| 125 | + if ($id !== '') |
| 126 | + { |
88 | 127 | self::$id = $id; |
89 | 128 | } |
90 | 129 | } |
91 | 130 |
|
92 | | - public static function setCountry($country = '') |
| 131 | + public static function setCountry($country = ''): void |
93 | 132 | { |
94 | | - if ($country != '') { |
| 133 | + if ($country !== '') |
| 134 | + { |
95 | 135 | self::$country = $country; |
96 | 136 | } |
97 | 137 | } |
98 | 138 |
|
99 | 139 | public static function getCountry() |
100 | 140 | { |
101 | | - $country = self::$country; |
102 | | - return $country; |
| 141 | + return self::$country; |
103 | 142 | } |
104 | 143 |
|
105 | 144 | public static function getId() |
106 | 145 | { |
107 | 146 | $id = self::$id; |
108 | | - if (!$id) { |
109 | | - $id = getenv('OPENPAY_MERCHANT_ID'); |
| 147 | + |
| 148 | + if (!$id) |
| 149 | + { |
| 150 | + return getenv('OPENPAY_MERCHANT_ID'); |
110 | 151 | } |
| 152 | + |
111 | 153 | return $id; |
112 | 154 | } |
113 | 155 |
|
114 | | - public static function setPublicIp($publicIp = null) |
| 156 | + public static function setPublicIp($publicIp = null): void |
115 | 157 | { |
116 | | - if (!is_null($publicIp)) { |
| 158 | + if (null !== $publicIp) |
| 159 | + { |
117 | 160 | self::$publicIp = $publicIp; |
118 | 161 | } |
119 | 162 | } |
120 | 163 |
|
121 | | - public static function getPublicIp() { |
| 164 | + public static function getPublicIp() |
| 165 | + { |
122 | 166 | return self::$publicIp; |
123 | | - |
124 | 167 | } |
125 | 168 |
|
126 | 169 | public static function getSandboxMode() |
127 | 170 | { |
128 | | - $sandbox = self::$sandboxMode; |
129 | | - if (getenv('OPENPAY_PRODUCTION_MODE')) { |
130 | | - $sandbox = (strtoupper(getenv('OPENPAY_PRODUCTION_MODE')) == 'FALSE'); |
| 171 | + if (getenv('OPENPAY_PRODUCTION_MODE')) |
| 172 | + { |
| 173 | + return strtoupper(getenv('OPENPAY_PRODUCTION_MODE')) === 'FALSE'; |
131 | 174 | } |
132 | | - return $sandbox; |
| 175 | + |
| 176 | + return self::$sandboxMode; |
133 | 177 | } |
134 | 178 |
|
135 | | - public static function setSandboxMode($mode) |
| 179 | + public static function setSandboxMode($mode): void |
136 | 180 | { |
137 | | - self::$sandboxMode = $mode ? true : false; |
| 181 | + self::$sandboxMode = (bool) $mode; |
138 | 182 | } |
139 | 183 |
|
140 | | - public static function getProductionMode() |
| 184 | + public static function getProductionMode(): bool |
141 | 185 | { |
142 | 186 | $sandbox = self::$sandboxMode; |
143 | | - if (getenv('OPENPAY_PRODUCTION_MODE')) { |
144 | | - $sandbox = (strtoupper(getenv('OPENPAY_PRODUCTION_MODE')) == 'FALSE'); |
| 187 | + |
| 188 | + if (getenv('OPENPAY_PRODUCTION_MODE')) |
| 189 | + { |
| 190 | + $sandbox = (strtoupper(getenv('OPENPAY_PRODUCTION_MODE')) === 'FALSE'); |
145 | 191 | } |
| 192 | + |
146 | 193 | return !$sandbox; |
147 | 194 | } |
148 | 195 |
|
149 | | - public static function setProductionMode($mode) |
| 196 | + public static function setProductionMode($mode): void |
150 | 197 | { |
151 | | - self::$sandboxMode = $mode ? false : true; |
| 198 | + self::$sandboxMode = !(bool) $mode; |
152 | 199 | } |
153 | 200 |
|
154 | | - public static function setEndpointUrl($country) |
| 201 | + public static function setEndpointUrl($country): void |
155 | 202 | { |
156 | | - if ($country == 'MX') { |
157 | | - if (self::getClassificationMerchant() != 'eglobal') { |
158 | | - self::$apiEndpoint = 'https://api.openpay.mx/v1'; |
| 203 | + if ($country === 'MX') |
| 204 | + { |
| 205 | + if (self::getClassificationMerchant() !== 'eglobal') |
| 206 | + { |
| 207 | + self::$apiEndpoint = 'https://api.openpay.mx/v1'; |
159 | 208 | self::$apiSandboxEndpoint = 'https://sandbox-api.openpay.mx/v1'; |
160 | | - } else { |
161 | | - self::$apiEndpoint = 'https://api.ecommercebbva.com/v1'; |
| 209 | + } |
| 210 | + else |
| 211 | + { |
| 212 | + self::$apiEndpoint = 'https://api.ecommercebbva.com/v1'; |
162 | 213 | self::$apiSandboxEndpoint = 'https://sand-api.ecommercebbva.com/v1'; |
163 | 214 | } |
164 | | - } elseif ($country == 'CO') { |
165 | | - self::$apiEndpoint = 'https://api.openpay.co/v1'; |
| 215 | + } |
| 216 | + elseif ($country === 'CO') |
| 217 | + { |
| 218 | + self::$apiEndpoint = 'https://api.openpay.co/v1'; |
166 | 219 | self::$apiSandboxEndpoint = 'https://sandbox-api.openpay.co/v1'; |
167 | | - } elseif ($country == 'PE') { |
168 | | - self::$apiEndpoint = 'https://api.openpay.pe/v1'; |
| 220 | + } |
| 221 | + elseif ($country === 'PE') |
| 222 | + { |
| 223 | + self::$apiEndpoint = 'https://api.openpay.pe/v1'; |
169 | 224 | self::$apiSandboxEndpoint = 'https://sandbox-api.openpay.pe/v1'; |
170 | 225 | } |
171 | 226 | } |
172 | 227 |
|
173 | | - public static function getEndpointUrl() |
| 228 | + public static function getEndpointUrl(): string |
174 | 229 | { |
175 | | - return (self::getSandboxMode() ? self::$apiSandboxEndpoint : self::$apiEndpoint); |
| 230 | + return self::getSandboxMode() ? self::$apiSandboxEndpoint : self::$apiEndpoint; |
176 | 231 | } |
177 | | - |
178 | 232 | } |
0 commit comments