diff --git a/.github/workflows/coding-standard-check.yml b/.github/workflows/coding-standard-check.yml
index a31d24a..708cfc3 100644
--- a/.github/workflows/coding-standard-check.yml
+++ b/.github/workflows/coding-standard-check.yml
@@ -29,7 +29,6 @@ jobs:
fail-fast: false
matrix:
php-version:
- - "7.1"
- "7.2"
- "7.3"
- "7.4"
diff --git a/composer.json b/composer.json
index 3f5306f..cb954e7 100644
--- a/composer.json
+++ b/composer.json
@@ -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/"]
diff --git a/examples/OmiseCharge.php b/examples/OmiseCharge.php
new file mode 100644
index 0000000..40f3654
--- /dev/null
+++ b/examples/OmiseCharge.php
@@ -0,0 +1,45 @@
+
+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";
diff --git a/examples/OmiseSchedule.php b/examples/OmiseSchedule.php
new file mode 100644
index 0000000..22214b8
--- /dev/null
+++ b/examples/OmiseSchedule.php
@@ -0,0 +1,82 @@
+
+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";
diff --git a/lib/omise/OmiseCapabilities.php b/lib/omise/OmiseCapabilities.php
index 48fca8d..7f133de 100644
--- a/lib/omise/OmiseCapabilities.php
+++ b/lib/omise/OmiseCapabilities.php
@@ -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;
}
@@ -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();
- }
}
diff --git a/lib/omise/res/OmiseApiResource.php b/lib/omise/res/OmiseApiResource.php
index 0738db2..5f49170 100644
--- a/lib/omise/res/OmiseApiResource.php
+++ b/lib/omise/res/OmiseApiResource.php
@@ -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;
}
/**
@@ -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;
@@ -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;
@@ -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);
}
@@ -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);
}
@@ -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);
}
@@ -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);
}
@@ -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);
}
@@ -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;
}
diff --git a/lib/omise/res/OmiseVaultResource.php b/lib/omise/res/OmiseVaultResource.php
index b1cdbbd..32221d4 100644
--- a/lib/omise/res/OmiseVaultResource.php
+++ b/lib/omise/res/OmiseVaultResource.php
@@ -2,13 +2,4 @@
class OmiseVaultResource extends OmiseApiResource
{
- /**
- * Returns the public key.
- *
- * @return string
- */
- protected static function getResourceKey()
- {
- return parent::getResourceKey();
- }
}
diff --git a/lib/omise/res/obj/OmiseObject.php b/lib/omise/res/obj/OmiseObject.php
index d1ab23f..92a8d80 100644
--- a/lib/omise/res/obj/OmiseObject.php
+++ b/lib/omise/res/obj/OmiseObject.php
@@ -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 = [];
}
/**
diff --git a/phpunit.xml b/phpunit.xml
index 42225bb..b4c9f67 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -54,6 +54,7 @@
./lib/Omise.php
tests
vendor
+ examples
diff --git a/sonar-project.properties b/sonar-project.properties
index a62a7f2..943fe16 100644
--- a/sonar-project.properties
+++ b/sonar-project.properties
@@ -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/**