Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 127 additions & 73 deletions Openpay/Data/Openpay.php
Original file line number Diff line number Diff line change
@@ -1,178 +1,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;
private static $apiKey;

private static $id = null;

private static $apiKey = null;

private static $userAgent = '';
private static $country;
private static $apiEndpoint = '';
private static $apiSandboxEndpoint = '';
private static $sandboxMode = true;

private static $country = null;

private static string $apiEndpoint = '';

private static string $apiSandboxEndpoint = '';

private static bool $sandboxMode = true;

private static $classification = '';
private static $publicIp;

public function __construct()
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($id, $apiKey, $country, $publicIp)
public static function getInstance(string $id, string $apiKey, ?string $country = '', ?string $publicIp = null)
{
if ($id != '') {
self::setId($id);
}
if ($apiKey != '') {
self::setApiKey($apiKey);
}
if ($country != '') {
self::setCountry($country);
self::setEndpointUrl($country);
}
if(!is_null($publicIp)){
self::setPublicIp($publicIp);
}
$instance = OpenpayApi::getInstance(null);
return $instance;
$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)
public static function setUserAgent($userAgent): void
{
if ($userAgent != '') {
if ($userAgent !== '')
{
self::$userAgent = $userAgent;
}
}

public static function getUserAgent()
{
$userAgent = self::$userAgent;
return $userAgent;
return self::$userAgent;
}

public static function setClassificationMerchant($classification)
public static function setClassificationMerchant($classification): void
{
if ($classification != '') {
if ($classification !== '')
{
self::$classification = $classification;
}
}

public static function getClassificationMerchant()
{
$classification = self::$classification;
return $classification;
return self::$classification;
}

public static function setApiKey($key = '')
public static function setApiKey($key = ''): void
{
if ($key != '') {
if ($key !== '')
{
self::$apiKey = $key;
}
}

public static function getApiKey()
{
$key = self::$apiKey;
if (!$key) {
$key = getenv('OPENPAY_API_KEY');

if (!$key)
{
return getenv('OPENPAY_API_KEY');
}

return $key;
}

public static function setId($id = '')
public static function setId($id = ''): void
{
if ($id != '') {
if ($id !== '')
{
self::$id = $id;
}
}

public static function setCountry($country = '')
public static function setCountry($country = ''): void
{
if ($country != '') {
if ($country !== '')
{
self::$country = $country;
}
}

public static function getCountry()
{
$country = self::$country;
return $country;
return self::$country;
}

public static function getId()
{
$id = self::$id;
if (!$id) {
$id = getenv('OPENPAY_MERCHANT_ID');

if (!$id)
{
return getenv('OPENPAY_MERCHANT_ID');
}

return $id;
}

public static function setPublicIp($publicIp = null)
public static function setPublicIp($publicIp = null): void
{
if (!is_null($publicIp)) {
if (null !== $publicIp)
{
self::$publicIp = $publicIp;
}
}

public static function getPublicIp() {
public static function getPublicIp()
{
return self::$publicIp;

}

public static function getSandboxMode()
{
$sandbox = self::$sandboxMode;
if (getenv('OPENPAY_PRODUCTION_MODE')) {
$sandbox = (strtoupper(getenv('OPENPAY_PRODUCTION_MODE')) == 'FALSE');
if (getenv('OPENPAY_PRODUCTION_MODE'))
{
return strtoupper(getenv('OPENPAY_PRODUCTION_MODE')) === 'FALSE';
}
return $sandbox;

return self::$sandboxMode;
}

public static function setSandboxMode($mode)
public static function setSandboxMode($mode): void
{
self::$sandboxMode = $mode ? true : false;
self::$sandboxMode = (bool) $mode;
}

public static function getProductionMode()
public static function getProductionMode(): bool
{
$sandbox = self::$sandboxMode;
if (getenv('OPENPAY_PRODUCTION_MODE')) {
$sandbox = (strtoupper(getenv('OPENPAY_PRODUCTION_MODE')) == 'FALSE');

if (getenv('OPENPAY_PRODUCTION_MODE'))
{
$sandbox = (strtoupper(getenv('OPENPAY_PRODUCTION_MODE')) === 'FALSE');
}

return !$sandbox;
}

public static function setProductionMode($mode)
public static function setProductionMode($mode): void
{
self::$sandboxMode = $mode ? false : true;
self::$sandboxMode = !(bool) $mode;
}

public static function setEndpointUrl($country)
public static function setEndpointUrl($country): void
{
if ($country == 'MX') {
if (self::getClassificationMerchant() != 'eglobal') {
self::$apiEndpoint = 'https://api.openpay.mx/v1';
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';
}
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';
}
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';
}
elseif ($country === 'PE')
{
self::$apiEndpoint = 'https://api.openpay.pe/v1';
self::$apiSandboxEndpoint = 'https://sandbox-api.openpay.pe/v1';
}
}

public static function getEndpointUrl()
public static function getEndpointUrl(): string
{
return (self::getSandboxMode() ? self::$apiSandboxEndpoint : self::$apiEndpoint);
return self::getSandboxMode() ? self::$apiSandboxEndpoint : self::$apiEndpoint;
}

}
54 changes: 32 additions & 22 deletions Openpay/Data/OpenpayApi.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
<?php

declare(strict_types=1);

namespace Openpay\Data;

use Override;

class OpenpayApi extends OpenpayApiResourceBase
{

protected $derivedResources = array(
'Bine' => array(),
'Customer' => array(),
'Card' => array(),
'Charge' => array(),
'Pse' => array(),
'Payout' => array(),
'Fee' => array(),
'Plan' => array(),
'Webhook' => array(),
'Token' => array());

public static function getInstance($r, $p = null)
protected $derivedResources = [
'Bine' => [],
'Customer' => [],
'Card' => [],
'Charge' => [],
'Pse' => [],
'Payout' => [],
'Fee' => [],
'Plan' => [],
'Webhook' => [],
'Token' => []];

#[Override]
protected static function getInstance($r, $p = null)
{
if(version_compare(phpversion(), '8.3.0', '<')){
$resourceName = get_class();
} else {
$resourceName = self::class;
}
$resourceName = self::class;

return parent::getInstance($resourceName);
}

public function getMerchantInfo()
/**
* Public entry used by {@see Openpay::getInstance()} (credentials already configured).
*/
public static function createRoot(): self
{
return self::getInstance(null);
}

#[Override]
protected function getMerchantInfo()
{
return parent::getMerchantInfo();
}

protected function getResourceUrlName($p = true)
#[Override]
protected function getResourceUrlName($p = true): string
{
return '';
}
Expand All @@ -41,5 +52,4 @@ public function getFullURL()
{
return $this->getUrl();
}

}
Loading