Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve issue of keys passed to method not being used #172

Merged
merged 10 commits into from
Aug 26, 2024
1 change: 0 additions & 1 deletion .github/workflows/coding-standard-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ jobs:
fail-fast: false
matrix:
php-version:
- "7.1"
- "7.2"
- "7.3"
- "7.4"
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"require-dev": {
"phpunit/phpunit": "^5.7 || ^9.5",
"friendsofphp/php-cs-fixer": "^2.19.3 || ^3.9.5",
"phpstan/phpstan": "1.2"
"phpstan/phpstan": "1.11.0",
"vlucas/phpdotenv": "5.5.0"
},
"autoload": {
"classmap": ["lib/omise/"]
Expand Down
45 changes: 45 additions & 0 deletions examples/OmiseCharge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

<?php
require_once 'vendor/autoload.php';
require_once 'lib/Omise.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();

// setting default keys
define('OMISE_PUBLIC_KEY', $_ENV['PUBLIC_KEY_TH']);
define('OMISE_SECRET_KEY', $_ENV['SECRET_KEY_TH']);

$token = OmiseToken::create([
'card' => [
'name' => 'Omise',
'number' => '4242424242424242',
'expiration_month' => 10,
'expiration_year' => 2042,
'city' => 'Bangkok',
'postal_code' => '10320',
'security_code' => 123
]
]);

$chargeCreated = OmiseCharge::create([
'amount' => 100000,
'currency' => 'thb',
'return_uri' => 'http://www.example.com',
'card' => $token->toArray()['id'],
'capture' => false
]);

$chargeFetched = OmiseCharge::retrieve($chargeCreated->toArray()['id']);
// echo print_r($chargeFetched, true);

// Capture the amount
$chargeFetched->capture(['capture_amount' => 100000 / 2]);

// Refund the amount
$chargeRefund = $chargeFetched->refunds()->create(['amount' => 100000]);

echo sprintf('New Charge ID (TH): %s', $chargeCreated->toArray()['id']) . "\n";
echo sprintf('Fetched Charge ID (TH): %s', $chargeFetched->toArray()['id']) . "\n";
// echo print_r($chargeFetched, true);
echo sprintf('Refund ID (TH): %s', $chargeRefund->toArray()['id']) . "\n\n";
82 changes: 82 additions & 0 deletions examples/OmiseSchedule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

<?php
require_once 'vendor/autoload.php';
require_once 'lib/Omise.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();

// setting default keys
define('OMISE_PUBLIC_KEY', $_ENV['PUBLIC_KEY_SG']);
define('OMISE_SECRET_KEY', $_ENV['SECRET_KEY_SG']);

// test customer key
$customerSG = 'cust_xxxx';

$pkeyTH = $_ENV['PUBLIC_KEY_TH'];
$skeyTH = $_ENV['SECRET_KEY_TH'];
$customerTH = 'cust_xxxx';

$pkeyMY = $_ENV['PUBLIC_KEY_MY'];
$skeyMY = $_ENV['SECRET_KEY_MY'];
$customerMY = 'cust_xxxx';

// Create schedule under TH PSP
$createResultTH = OmiseSchedule::create([
'every' => 15,
'period' => 'day',
'start_date' => '2025-01-01',
'end_date' => '2025-03-31',
'charge[customer]' => $customerTH,
'charge[amount]' => 100000,
'charge[description]' => 'Testing schedule',
], $pkeyTH, $skeyTH);

$chargeScheduleIdTH = $createResultTH->toArray()['id'];

echo sprintf('Created schedule ID (TH): %s', $chargeScheduleIdTH) . "\n";

// Create schedule under MY PSP
$createResultMY = OmiseSchedule::create([
'every' => 15,
'period' => 'day',
'start_date' => '2025-01-01',
'end_date' => '2025-03-31',
'charge[customer]' => $customerMY,
'charge[amount]' => 100000,
'charge[description]' => 'Testing schedule',
], $pkeyMY, $skeyMY);

$chargeScheduleIdMY = $createResultMY->toArray()['id'];

echo sprintf('Created schedule ID (MY): %s', $createResultMY->toArray()['id']) . "\n\n";

// Create schedule under SG PSP
$createResultSG = OmiseSchedule::create([
'every' => 15,
'period' => 'day',
'start_date' => '2025-01-01',
'end_date' => '2025-03-31',
'charge[customer]' => $customerSG,
'charge[amount]' => 100000,
'charge[description]' => 'Testing schedule',
]);

$chargeScheduleIdSG = $createResultSG->toArray()['id'];

echo sprintf('Created schedule ID (SG): %s', $createResultSG->toArray()['id']) . "\n";


// Fetching charge scedules

$resultTH = OmiseSchedule::retrieve($chargeScheduleIdTH, $pkeyTH, $skeyTH);

echo sprintf('Fetched schedule ID (TH): %s', $resultTH->toArray()['id']) . "\n\n";

$resultMY = OmiseSchedule::retrieve($chargeScheduleIdMY, $pkeyMY, $skeyMY);

echo sprintf('Fetched schedule ID (MY): %s', $resultMY->toArray()['id']) . "\n\n";

$resultSG = OmiseSchedule::retrieve($chargeScheduleIdSG);

echo sprintf('Fetched schedule ID (SG): %s', $resultSG->toArray()['id']) . "\n\n";
11 changes: 1 addition & 10 deletions lib/omise/OmiseCapabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function ($backend) {
},
$this['payment_backends']
);

// return backends (filtered if requested)
return ($filters = func_get_args()) ? array_filter($backends, self::combineFilters(self::argsToVariadic($filters))) : $backends;
}
Expand Down Expand Up @@ -174,14 +175,4 @@ private static function getUrl()
{
return OMISE_API_URL . self::ENDPOINT;
}

/**
* Returns the public key.
*
* @return string
*/
protected static function getResourceKey()
{
return parent::getResourceKey();
}
}
33 changes: 21 additions & 12 deletions lib/omise/res/OmiseApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,20 @@ class OmiseApiResource extends OmiseObject
*/
protected static function getInstance($publickey = null, $secretkey = null)
{
$resource = new static($publickey, $secretkey); // @phpstan-ignore-line
$resource = new static($publickey, $secretkey);
$className = get_class($resource);

if (!isset(self::$instances[$className])) {
static::$instances[$className] = $resource;

return static::$instances[$className];
}

return static::$instances[$className];
$resource = static::$instances[$className];

$resource->setPublicKey($publickey);
$resource->setSecretKey($secretkey);

return $resource;
}

/**
Expand All @@ -60,7 +66,7 @@ protected static function getInstance($publickey = null, $secretkey = null)
protected static function g_retrieve($url, $publickey = null, $secretkey = null)
{
$resource = self::getInstance($publickey, $secretkey);
$result = $resource->execute($url, self::REQUEST_GET, $resource->getResourceKey());
$result = $resource->execute($url, self::REQUEST_GET, self::getResourceKey($resource));
$resource->refresh($result);

return $resource;
Expand All @@ -81,7 +87,7 @@ protected static function g_retrieve($url, $publickey = null, $secretkey = null)
protected static function g_create($url, $params, $publickey = null, $secretkey = null)
{
$resource = self::getInstance($publickey, $secretkey);
$result = $resource->execute($url, self::REQUEST_POST, $resource->getResourceKey(), $params);
$result = $resource->execute($url, self::REQUEST_POST, self::getResourceKey($resource), $params);
$resource->refresh($result);

return $resource;
Expand All @@ -98,7 +104,7 @@ protected static function g_create($url, $params, $publickey = null, $secretkey
protected static function g_update($url, $params = null)
{
$resource = self::getInstance();
$result = $resource->execute($url, self::REQUEST_PATCH, $resource->getResourceKey(), $params);
$result = $resource->execute($url, self::REQUEST_PATCH, self::getResourceKey($resource), $params);
$resource->refresh($result);
}

Expand All @@ -112,7 +118,7 @@ protected static function g_update($url, $params = null)
protected static function g_expire($url)
{
$resource = self::getInstance();
$result = $resource->execute($url, self::REQUEST_POST, $resource->getResourceKey());
$result = $resource->execute($url, self::REQUEST_POST, self::getResourceKey($resource));
$resource->refresh($result, true);
}

Expand All @@ -126,7 +132,7 @@ protected static function g_expire($url)
protected static function g_destroy($url)
{
$resource = self::getInstance();
$result = $resource->execute($url, self::REQUEST_DELETE, $resource->getResourceKey());
$result = $resource->execute($url, self::REQUEST_DELETE, self::getResourceKey($resource));
$resource->refresh($result, true);
}

Expand All @@ -140,7 +146,7 @@ protected static function g_destroy($url)
protected static function g_revoke($url)
{
$resource = self::getInstance();
$result = $resource->execute($url, self::REQUEST_POST, $resource->getResourceKey());
$result = $resource->execute($url, self::REQUEST_POST, self::getResourceKey($resource));
$resource->refresh($result, true);
}

Expand All @@ -154,7 +160,7 @@ protected static function g_revoke($url)
protected static function g_reload($url)
{
$resource = self::getInstance();
$result = $resource->execute($url, self::REQUEST_GET, $resource->getResourceKey());
$result = $resource->execute($url, self::REQUEST_GET, self::getResourceKey($resource));
$resource->refresh($result);
}

Expand Down Expand Up @@ -362,9 +368,12 @@ protected static function isDestroyed()
*
* @return string
*/
protected static function getResourceKey()
protected static function getResourceKey($resource = null)
{
$resource = self::getInstance();
if (!$resource) {
$resource = self::getInstance();
}

if (in_array(get_class($resource), self::$classesToUsePublicKey)) {
return $resource->_publickey;
}
Expand Down
9 changes: 0 additions & 9 deletions lib/omise/res/OmiseVaultResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,4 @@

class OmiseVaultResource extends OmiseApiResource
{
/**
* Returns the public key.
*
* @return string
*/
protected static function getResourceKey()
{
return parent::getResourceKey();
}
}
16 changes: 12 additions & 4 deletions lib/omise/res/obj/OmiseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,34 @@ class OmiseObject implements ArrayAccess, Iterator, Countable
* @param string $secretkey
*/
protected function __construct($publickey = null, $secretkey = null)
{
$this->setPublicKey($publickey);
$this->setSecretKey($secretkey);
$this->_values = [];
}

protected function setPublicKey($publickey)
{
if ($publickey !== null) {
$this->_publickey = $publickey;
} else {
if (!defined('OMISE_PUBLIC_KEY')) {
define('OMISE_PUBLIC_KEY', 'pkey');
throw new Exception('OMISE_PUBLIC_KEY value is undefined. This is required for authentication');
}
$this->_publickey = OMISE_PUBLIC_KEY;
}
}

protected function setSecretKey($secretkey)
{
if ($secretkey !== null) {
$this->_secretkey = $secretkey;
} else {
if (!defined('OMISE_SECRET_KEY')) {
define('OMISE_SECRET_KEY', 'skey');
throw new Exception('OMISE_SECRET_KEY value is undefined. This is required for authentication');
}
$this->_secretkey = OMISE_SECRET_KEY;
}

$this->_values = [];
}

/**
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<file>./lib/Omise.php</file>
<directory>tests</directory>
<directory>vendor</directory>
<directory>examples</directory>
</exclude>
</coverage>
</phpunit>
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ sonar.organization=omise
sonar.php.coverage.reportPaths=coverage.xml

sonar.exclusions=**/tests/**
sonar.coverage.exclusions=**/tests/**, composer.json, webpack.*.js, **/*.xml, /lib/Omise.php
sonar.coverage.exclusions=**/tests/**, composer.json, webpack.*.js, **/*.xml, /lib/Omise.php, **/examples/**
Loading