Skip to content

Commit d27bd63

Browse files
committed
Make Openpay credentials request-safe for php-fpm and FrankenPHP.
Add configure()/reset() and createRoot() so merchant statics can be cleared between requests in long-lived workers (FrankenPHP) and FPM.
1 parent 5a922d4 commit d27bd63

3 files changed

Lines changed: 168 additions & 101 deletions

File tree

Openpay/Data/Openpay.php

Lines changed: 127 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,232 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Openpay\Data;
46

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+
*/
515
class Openpay
616
{
7-
817
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+
1123
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+
1633
private static $classification = '';
17-
private static $publicIp;
1834

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
2042
{
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+
}
2154

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);
2265
}
2366

24-
public static function getInstance($id, $apiKey, $country, $publicIp)
67+
public static function getInstance(string $id, string $apiKey, ?string $country = '', ?string $publicIp = null)
2568
{
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();
4175
}
4276

43-
public static function setUserAgent($userAgent)
77+
public static function setUserAgent($userAgent): void
4478
{
45-
if ($userAgent != '') {
79+
if ($userAgent !== '')
80+
{
4681
self::$userAgent = $userAgent;
4782
}
4883
}
4984

5085
public static function getUserAgent()
5186
{
52-
$userAgent = self::$userAgent;
53-
return $userAgent;
87+
return self::$userAgent;
5488
}
5589

56-
public static function setClassificationMerchant($classification)
90+
public static function setClassificationMerchant($classification): void
5791
{
58-
if ($classification != '') {
92+
if ($classification !== '')
93+
{
5994
self::$classification = $classification;
6095
}
6196
}
6297

6398
public static function getClassificationMerchant()
6499
{
65-
$classification = self::$classification;
66-
return $classification;
100+
return self::$classification;
67101
}
68102

69-
public static function setApiKey($key = '')
103+
public static function setApiKey($key = ''): void
70104
{
71-
if ($key != '') {
105+
if ($key !== '')
106+
{
72107
self::$apiKey = $key;
73108
}
74109
}
75110

76111
public static function getApiKey()
77112
{
78113
$key = self::$apiKey;
79-
if (!$key) {
80-
$key = getenv('OPENPAY_API_KEY');
114+
115+
if (!$key)
116+
{
117+
return getenv('OPENPAY_API_KEY');
81118
}
119+
82120
return $key;
83121
}
84122

85-
public static function setId($id = '')
123+
public static function setId($id = ''): void
86124
{
87-
if ($id != '') {
125+
if ($id !== '')
126+
{
88127
self::$id = $id;
89128
}
90129
}
91130

92-
public static function setCountry($country = '')
131+
public static function setCountry($country = ''): void
93132
{
94-
if ($country != '') {
133+
if ($country !== '')
134+
{
95135
self::$country = $country;
96136
}
97137
}
98138

99139
public static function getCountry()
100140
{
101-
$country = self::$country;
102-
return $country;
141+
return self::$country;
103142
}
104143

105144
public static function getId()
106145
{
107146
$id = self::$id;
108-
if (!$id) {
109-
$id = getenv('OPENPAY_MERCHANT_ID');
147+
148+
if (!$id)
149+
{
150+
return getenv('OPENPAY_MERCHANT_ID');
110151
}
152+
111153
return $id;
112154
}
113155

114-
public static function setPublicIp($publicIp = null)
156+
public static function setPublicIp($publicIp = null): void
115157
{
116-
if (!is_null($publicIp)) {
158+
if (null !== $publicIp)
159+
{
117160
self::$publicIp = $publicIp;
118161
}
119162
}
120163

121-
public static function getPublicIp() {
164+
public static function getPublicIp()
165+
{
122166
return self::$publicIp;
123-
124167
}
125168

126169
public static function getSandboxMode()
127170
{
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';
131174
}
132-
return $sandbox;
175+
176+
return self::$sandboxMode;
133177
}
134178

135-
public static function setSandboxMode($mode)
179+
public static function setSandboxMode($mode): void
136180
{
137-
self::$sandboxMode = $mode ? true : false;
181+
self::$sandboxMode = (bool) $mode;
138182
}
139183

140-
public static function getProductionMode()
184+
public static function getProductionMode(): bool
141185
{
142186
$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');
145191
}
192+
146193
return !$sandbox;
147194
}
148195

149-
public static function setProductionMode($mode)
196+
public static function setProductionMode($mode): void
150197
{
151-
self::$sandboxMode = $mode ? false : true;
198+
self::$sandboxMode = !(bool) $mode;
152199
}
153200

154-
public static function setEndpointUrl($country)
201+
public static function setEndpointUrl($country): void
155202
{
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';
159208
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';
162213
self::$apiSandboxEndpoint = 'https://sand-api.ecommercebbva.com/v1';
163214
}
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';
166219
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';
169224
self::$apiSandboxEndpoint = 'https://sandbox-api.openpay.pe/v1';
170225
}
171226
}
172227

173-
public static function getEndpointUrl()
228+
public static function getEndpointUrl(): string
174229
{
175-
return (self::getSandboxMode() ? self::$apiSandboxEndpoint : self::$apiEndpoint);
230+
return self::getSandboxMode() ? self::$apiSandboxEndpoint : self::$apiEndpoint;
176231
}
177-
178232
}

Openpay/Data/OpenpayApi.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Openpay\Data;
46

7+
use Override;
8+
59
class OpenpayApi extends OpenpayApiResourceBase
610
{
7-
8-
protected $derivedResources = array(
9-
'Bine' => array(),
10-
'Customer' => array(),
11-
'Card' => array(),
12-
'Charge' => array(),
13-
'Pse' => array(),
14-
'Payout' => array(),
15-
'Fee' => array(),
16-
'Plan' => array(),
17-
'Webhook' => array(),
18-
'Token' => array());
19-
20-
public static function getInstance($r, $p = null)
11+
protected $derivedResources = [
12+
'Bine' => [],
13+
'Customer' => [],
14+
'Card' => [],
15+
'Charge' => [],
16+
'Pse' => [],
17+
'Payout' => [],
18+
'Fee' => [],
19+
'Plan' => [],
20+
'Webhook' => [],
21+
'Token' => []];
22+
23+
#[Override]
24+
protected static function getInstance($r, $p = null)
2125
{
22-
if(version_compare(phpversion(), '8.3.0', '<')){
23-
$resourceName = get_class();
24-
} else {
25-
$resourceName = self::class;
26-
}
26+
$resourceName = self::class;
27+
2728
return parent::getInstance($resourceName);
2829
}
2930

30-
public function getMerchantInfo()
31+
/**
32+
* Public entry used by {@see Openpay::getInstance()} (credentials already configured).
33+
*/
34+
public static function createRoot(): self
35+
{
36+
return self::getInstance(null);
37+
}
38+
39+
#[Override]
40+
protected function getMerchantInfo()
3141
{
3242
return parent::getMerchantInfo();
3343
}
3444

35-
protected function getResourceUrlName($p = true)
45+
#[Override]
46+
protected function getResourceUrlName($p = true): string
3647
{
3748
return '';
3849
}
@@ -41,5 +52,4 @@ public function getFullURL()
4152
{
4253
return $this->getUrl();
4354
}
44-
4555
}

0 commit comments

Comments
 (0)