From 182b9fd4be9a4c138906333b4a9e6618bd6fc5a9 Mon Sep 17 00:00:00 2001 From: Aashish Date: Tue, 20 Aug 2024 12:09:16 +0700 Subject: [PATCH 01/10] Updated OmiseObject.php and OmiseApiResource.php to use the keys supplied in the method instead of cached keys. --- composer.json | 3 +- examples/OmiseCharge.php | 37 ++++++++++++++++ examples/OmiseSchedule.php | 65 ++++++++++++++++++++++++++++ lib/omise/OmiseCapabilities.php | 10 ----- lib/omise/res/OmiseApiResource.php | 32 +++++++++----- lib/omise/res/OmiseVaultResource.php | 9 ---- lib/omise/res/obj/OmiseObject.php | 16 +++++-- 7 files changed, 136 insertions(+), 36 deletions(-) create mode 100644 examples/OmiseCharge.php create mode 100644 examples/OmiseSchedule.php diff --git a/composer.json b/composer.json index 3f5306f..ff55427 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.2", + "vlucas/phpdotenv": "^5.6" }, "autoload": { "classmap": ["lib/omise/"] diff --git a/examples/OmiseCharge.php b/examples/OmiseCharge.php new file mode 100644 index 0000000..9e4132c --- /dev/null +++ b/examples/OmiseCharge.php @@ -0,0 +1,37 @@ + +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'] +]); + +$chargeFetched = OmiseCharge::retrieve($chargeCreated->toArray()['id']); +$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 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..4613070 --- /dev/null +++ b/examples/OmiseSchedule.php @@ -0,0 +1,65 @@ + +load(); + +// setting default keys +define('OMISE_PUBLIC_KEY', $_ENV['PUBLIC_KEY_SG']); +define('OMISE_SECRET_KEY', $_ENV['SECRET_KEY_SG']); +$customerSG = 'cust_test_60owl03ul8bzdk0h5ph'; + +$pkeyTH = $_ENV['PUBLIC_KEY_TH']; +$skeyTH = $_ENV['SECRET_KEY_TH']; +$customerTH = 'cust_test_60ilwqkl9khiex66p4s'; + +$pkeyMY = $_ENV['PUBLIC_KEY_MY']; +$skeyMY = $_ENV['SECRET_KEY_MY']; +$customerMY = 'cust_5u4m6n6tlhduy9savs8'; + +// 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); + +// 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); + +// 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', +]); + +$resultTH = OmiseSchedule::retrieve($createResultTH->toArray()['id'], $pkeyTH, $skeyTH); +$resultMY = OmiseSchedule::retrieve($createResultMY->toArray()['id'], $pkeyMY, $skeyMY); +$resultSG = OmiseSchedule::retrieve($createResultSG->toArray()['id']); + +echo sprintf("Created schedule ID (TH): %s", $createResultTH->toArray()['id']) . "\n"; +echo sprintf("Created schedule ID (MY): %s", $createResultMY->toArray()['id']) . "\n"; +echo sprintf("Created schedule ID (SG): %s", $createResultSG->toArray()['id']) . "\n\n"; + +echo sprintf("Fetched schedule ID (TH): %s", $resultTH->toArray()['id']) . "\n"; +echo sprintf("Fetched schedule ID (MY): %s", $resultMY->toArray()['id']) . "\n"; +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..5f9ba0d 100644 --- a/lib/omise/OmiseCapabilities.php +++ b/lib/omise/OmiseCapabilities.php @@ -174,14 +174,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..d7204cc 100644 --- a/lib/omise/res/OmiseApiResource.php +++ b/lib/omise/res/OmiseApiResource.php @@ -37,14 +37,19 @@ class OmiseApiResource extends OmiseObject */ protected static function getInstance($publickey = null, $secretkey = null) { - $resource = new static($publickey, $secretkey); // @phpstan-ignore-line + return $resource = new static($publickey, $secretkey); // @phpstan-ignore-line $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 +65,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 +86,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 +103,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 +117,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 +131,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 +145,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 +159,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 +367,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 = []; } /** From ca4ae5390744a90866987b3acb7a0a836aac973a Mon Sep 17 00:00:00 2001 From: Aashish Date: Wed, 21 Aug 2024 18:11:45 +0700 Subject: [PATCH 02/10] Add methods to update public and secret keys. --- examples/OmiseCharge.php | 3 +++ examples/OmiseRefund.php | 0 examples/OmiseSchedule.php | 28 ++++++++++++++++++---------- lib/omise/res/OmiseApiResource.php | 2 +- 4 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 examples/OmiseRefund.php diff --git a/examples/OmiseCharge.php b/examples/OmiseCharge.php index 9e4132c..18d5a8d 100644 --- a/examples/OmiseCharge.php +++ b/examples/OmiseCharge.php @@ -30,8 +30,11 @@ ]); $chargeFetched = OmiseCharge::retrieve($chargeCreated->toArray()['id']); +echo print_r($chargeFetched, true); +$chargeFetched->capture(['capture_amount' => 100000 / 2]); $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/OmiseRefund.php b/examples/OmiseRefund.php new file mode 100644 index 0000000..e69de29 diff --git a/examples/OmiseSchedule.php b/examples/OmiseSchedule.php index 4613070..fe8961d 100644 --- a/examples/OmiseSchedule.php +++ b/examples/OmiseSchedule.php @@ -1,7 +1,7 @@ load(); @@ -9,6 +9,8 @@ // setting default keys define('OMISE_PUBLIC_KEY', $_ENV['PUBLIC_KEY_SG']); define('OMISE_SECRET_KEY', $_ENV['SECRET_KEY_SG']); + +// test customer key $customerSG = 'cust_test_60owl03ul8bzdk0h5ph'; $pkeyTH = $_ENV['PUBLIC_KEY_TH']; @@ -30,6 +32,12 @@ 'charge[description]' => 'Testing schedule', ], $pkeyTH, $skeyTH); +echo sprintf("Created schedule ID (TH): %s", $createResultTH->toArray()['id']) . "\n"; + +$resultTH = OmiseSchedule::retrieve($createResultTH->toArray()['id'], $pkeyTH, $skeyTH); + +echo sprintf("Fetched schedule ID (TH): %s", $resultTH->toArray()['id']) . "\n\n"; + // Create schedule under MY PSP $createResultMY = OmiseSchedule::create([ 'every' => 15, @@ -41,6 +49,12 @@ 'charge[description]' => 'Testing schedule', ], $pkeyMY, $skeyMY); +echo sprintf("Created schedule ID (MY): %s", $createResultMY->toArray()['id']) . "\n"; + +$resultMY = OmiseSchedule::retrieve($createResultMY->toArray()['id'], $pkeyMY, $skeyMY); + +echo sprintf("Fetched schedule ID (MY): %s", $resultMY->toArray()['id']) . "\n\n"; + // Create schedule under SG PSP $createResultSG = OmiseSchedule::create([ 'every' => 15, @@ -52,14 +66,8 @@ 'charge[description]' => 'Testing schedule', ]); -$resultTH = OmiseSchedule::retrieve($createResultTH->toArray()['id'], $pkeyTH, $skeyTH); -$resultMY = OmiseSchedule::retrieve($createResultMY->toArray()['id'], $pkeyMY, $skeyMY); -$resultSG = OmiseSchedule::retrieve($createResultSG->toArray()['id']); +echo sprintf("Created schedule ID (SG): %s", $createResultSG->toArray()['id']) . "\n"; -echo sprintf("Created schedule ID (TH): %s", $createResultTH->toArray()['id']) . "\n"; -echo sprintf("Created schedule ID (MY): %s", $createResultMY->toArray()['id']) . "\n"; -echo sprintf("Created schedule ID (SG): %s", $createResultSG->toArray()['id']) . "\n\n"; +$resultSG = OmiseSchedule::retrieve($createResultSG->toArray()['id']); -echo sprintf("Fetched schedule ID (TH): %s", $resultTH->toArray()['id']) . "\n"; -echo sprintf("Fetched schedule ID (MY): %s", $resultMY->toArray()['id']) . "\n"; echo sprintf("Fetched schedule ID (SG): %s", $resultSG->toArray()['id']) . "\n\n"; diff --git a/lib/omise/res/OmiseApiResource.php b/lib/omise/res/OmiseApiResource.php index d7204cc..00ddf48 100644 --- a/lib/omise/res/OmiseApiResource.php +++ b/lib/omise/res/OmiseApiResource.php @@ -37,7 +37,7 @@ class OmiseApiResource extends OmiseObject */ protected static function getInstance($publickey = null, $secretkey = null) { - return $resource = new static($publickey, $secretkey); // @phpstan-ignore-line + $resource = new static($publickey, $secretkey); // @phpstan-ignore-line $className = get_class($resource); if (!isset(self::$instances[$className])) { static::$instances[$className] = $resource; From f8f3274b9f558b5d61b622f8649329e2077f8249 Mon Sep 17 00:00:00 2001 From: Aashish Date: Wed, 21 Aug 2024 18:31:19 +0700 Subject: [PATCH 03/10] Update phpunit.xml to ignore examples folder. --- phpunit.xml | 1 + sonar-project.properties | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) 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/** From 1cc4db50bb4112aa21bcf068c7263b9a37b93bc9 Mon Sep 17 00:00:00 2001 From: Aashish Date: Thu, 22 Aug 2024 09:22:11 +0700 Subject: [PATCH 04/10] Resolve issue raised by PHPStan --- examples/OmiseCharge.php | 16 ++++++++++------ examples/OmiseRefund.php | 0 examples/OmiseSchedule.php | 14 +++++++------- lib/omise/OmiseCapabilities.php | 1 + lib/omise/res/OmiseApiResource.php | 1 + 5 files changed, 19 insertions(+), 13 deletions(-) delete mode 100644 examples/OmiseRefund.php diff --git a/examples/OmiseCharge.php b/examples/OmiseCharge.php index 18d5a8d..f6180e4 100644 --- a/examples/OmiseCharge.php +++ b/examples/OmiseCharge.php @@ -1,7 +1,7 @@ load(); @@ -26,15 +26,19 @@ 'amount' => 100000, 'currency' => 'thb', 'return_uri' => 'http://www.example.com', - "card" => $token->toArray()['id'] + 'card' => $token->toArray()['id'] ]); $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 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"; +echo sprintf('Refund ID (TH): %s', $chargeRefund->toArray()['id']) . '\n\n'; diff --git a/examples/OmiseRefund.php b/examples/OmiseRefund.php deleted file mode 100644 index e69de29..0000000 diff --git a/examples/OmiseSchedule.php b/examples/OmiseSchedule.php index fe8961d..1110689 100644 --- a/examples/OmiseSchedule.php +++ b/examples/OmiseSchedule.php @@ -1,7 +1,7 @@ load(); @@ -32,11 +32,11 @@ 'charge[description]' => 'Testing schedule', ], $pkeyTH, $skeyTH); -echo sprintf("Created schedule ID (TH): %s", $createResultTH->toArray()['id']) . "\n"; +echo sprintf('Created schedule ID (TH): %s', $createResultTH->toArray()['id']) . '\n'; $resultTH = OmiseSchedule::retrieve($createResultTH->toArray()['id'], $pkeyTH, $skeyTH); -echo sprintf("Fetched schedule ID (TH): %s", $resultTH->toArray()['id']) . "\n\n"; +echo sprintf('Fetched schedule ID (TH): %s', $resultTH->toArray()['id']) . '\n\n'; // Create schedule under MY PSP $createResultMY = OmiseSchedule::create([ @@ -49,11 +49,11 @@ 'charge[description]' => 'Testing schedule', ], $pkeyMY, $skeyMY); -echo sprintf("Created schedule ID (MY): %s", $createResultMY->toArray()['id']) . "\n"; +echo sprintf('Created schedule ID (MY): %s', $createResultMY->toArray()['id']) . '\n'; $resultMY = OmiseSchedule::retrieve($createResultMY->toArray()['id'], $pkeyMY, $skeyMY); -echo sprintf("Fetched schedule ID (MY): %s", $resultMY->toArray()['id']) . "\n\n"; +echo sprintf('Fetched schedule ID (MY): %s', $resultMY->toArray()['id']) . '\n\n'; // Create schedule under SG PSP $createResultSG = OmiseSchedule::create([ @@ -66,8 +66,8 @@ 'charge[description]' => 'Testing schedule', ]); -echo sprintf("Created schedule ID (SG): %s", $createResultSG->toArray()['id']) . "\n"; +echo sprintf('Created schedule ID (SG): %s', $createResultSG->toArray()['id']) . '\n'; $resultSG = OmiseSchedule::retrieve($createResultSG->toArray()['id']); -echo sprintf("Fetched schedule ID (SG): %s", $resultSG->toArray()['id']) . "\n\n"; +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 5f9ba0d..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; } diff --git a/lib/omise/res/OmiseApiResource.php b/lib/omise/res/OmiseApiResource.php index 00ddf48..0619990 100644 --- a/lib/omise/res/OmiseApiResource.php +++ b/lib/omise/res/OmiseApiResource.php @@ -41,6 +41,7 @@ protected static function getInstance($publickey = null, $secretkey = null) $className = get_class($resource); if (!isset(self::$instances[$className])) { static::$instances[$className] = $resource; + return static::$instances[$className]; } From eb48ade14fb7afc62948c717713b26f5ace9b5d7 Mon Sep 17 00:00:00 2001 From: Aashish Date: Thu, 22 Aug 2024 09:33:21 +0700 Subject: [PATCH 05/10] Drop vlucas/phpdotenv to 5.5 to support PHP 7.1 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ff55427..77b6aab 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "phpunit/phpunit": "^5.7 || ^9.5", "friendsofphp/php-cs-fixer": "^2.19.3 || ^3.9.5", "phpstan/phpstan": "1.2", - "vlucas/phpdotenv": "^5.6" + "vlucas/phpdotenv": "5.5.0" }, "autoload": { "classmap": ["lib/omise/"] From c3af675657736b40f3514bdae9ef9d97ca76e94e Mon Sep 17 00:00:00 2001 From: Aashish Date: Thu, 22 Aug 2024 09:41:49 +0700 Subject: [PATCH 06/10] Updated PHPStan version to resolve issue on PHP 8.0 and 8.1. --- composer.json | 2 +- lib/omise/res/OmiseApiResource.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 77b6aab..d5ac568 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "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", "vlucas/phpdotenv": "5.5.0" }, "autoload": { diff --git a/lib/omise/res/OmiseApiResource.php b/lib/omise/res/OmiseApiResource.php index 0619990..5f49170 100644 --- a/lib/omise/res/OmiseApiResource.php +++ b/lib/omise/res/OmiseApiResource.php @@ -37,7 +37,7 @@ 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; From b684836afeb3e1585e47cd41584bdb459fe3a7ca Mon Sep 17 00:00:00 2001 From: Aashish Date: Thu, 22 Aug 2024 10:16:06 +0700 Subject: [PATCH 07/10] Downgrade PHPStan to support PHP 7.1 --- composer.json | 2 +- examples/OmiseCharge.php | 3 ++- lib/omise/res/OmiseApiResource.php | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index d5ac568..9a0e94b 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "require-dev": { "phpunit/phpunit": "^5.7 || ^9.5", "friendsofphp/php-cs-fixer": "^2.19.3 || ^3.9.5", - "phpstan/phpstan": "^1.11", + "phpstan/phpstan": "1.4.*", "vlucas/phpdotenv": "5.5.0" }, "autoload": { diff --git a/examples/OmiseCharge.php b/examples/OmiseCharge.php index f6180e4..b507f9f 100644 --- a/examples/OmiseCharge.php +++ b/examples/OmiseCharge.php @@ -26,7 +26,8 @@ 'amount' => 100000, 'currency' => 'thb', 'return_uri' => 'http://www.example.com', - 'card' => $token->toArray()['id'] + 'card' => $token->toArray()['id'], + 'capture' => false ]); $chargeFetched = OmiseCharge::retrieve($chargeCreated->toArray()['id']); diff --git a/lib/omise/res/OmiseApiResource.php b/lib/omise/res/OmiseApiResource.php index 5f49170..0619990 100644 --- a/lib/omise/res/OmiseApiResource.php +++ b/lib/omise/res/OmiseApiResource.php @@ -37,7 +37,7 @@ class OmiseApiResource extends OmiseObject */ protected static function getInstance($publickey = null, $secretkey = null) { - $resource = new static($publickey, $secretkey); + $resource = new static($publickey, $secretkey); // @phpstan-ignore-line $className = get_class($resource); if (!isset(self::$instances[$className])) { static::$instances[$className] = $resource; From d74d2aea09b3e14ba3a63449a92972e712e90fa3 Mon Sep 17 00:00:00 2001 From: Aashish Date: Thu, 22 Aug 2024 10:20:40 +0700 Subject: [PATCH 08/10] Removed PHP 7.1 from PHP coding standard worflow. --- .github/workflows/coding-standard-check.yml | 1 - composer.json | 2 +- lib/omise/res/OmiseApiResource.php | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) 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 9a0e94b..cb954e7 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "require-dev": { "phpunit/phpunit": "^5.7 || ^9.5", "friendsofphp/php-cs-fixer": "^2.19.3 || ^3.9.5", - "phpstan/phpstan": "1.4.*", + "phpstan/phpstan": "1.11.0", "vlucas/phpdotenv": "5.5.0" }, "autoload": { diff --git a/lib/omise/res/OmiseApiResource.php b/lib/omise/res/OmiseApiResource.php index 0619990..5f49170 100644 --- a/lib/omise/res/OmiseApiResource.php +++ b/lib/omise/res/OmiseApiResource.php @@ -37,7 +37,7 @@ 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; From 3634eca72d667df24053781c040feff04580500b Mon Sep 17 00:00:00 2001 From: Aashish Date: Thu, 22 Aug 2024 15:10:28 +0700 Subject: [PATCH 09/10] Remove cust ID from examples --- examples/OmiseSchedule.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/OmiseSchedule.php b/examples/OmiseSchedule.php index 1110689..4e2cd08 100644 --- a/examples/OmiseSchedule.php +++ b/examples/OmiseSchedule.php @@ -11,15 +11,15 @@ define('OMISE_SECRET_KEY', $_ENV['SECRET_KEY_SG']); // test customer key -$customerSG = 'cust_test_60owl03ul8bzdk0h5ph'; +$customerSG = 'cust_xxxx'; $pkeyTH = $_ENV['PUBLIC_KEY_TH']; $skeyTH = $_ENV['SECRET_KEY_TH']; -$customerTH = 'cust_test_60ilwqkl9khiex66p4s'; +$customerTH = 'cust_xxxx'; $pkeyMY = $_ENV['PUBLIC_KEY_MY']; $skeyMY = $_ENV['SECRET_KEY_MY']; -$customerMY = 'cust_5u4m6n6tlhduy9savs8'; +$customerMY = 'cust_xxxx'; // Create schedule under TH PSP $createResultTH = OmiseSchedule::create([ From 69cf14e1a838ba306b06d3e24e7698bcf066c762 Mon Sep 17 00:00:00 2001 From: Aashish Date: Fri, 23 Aug 2024 12:12:48 +0700 Subject: [PATCH 10/10] Update examples --- examples/OmiseCharge.php | 10 +++++----- examples/OmiseSchedule.php | 31 ++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/examples/OmiseCharge.php b/examples/OmiseCharge.php index b507f9f..40f3654 100644 --- a/examples/OmiseCharge.php +++ b/examples/OmiseCharge.php @@ -31,7 +31,7 @@ ]); $chargeFetched = OmiseCharge::retrieve($chargeCreated->toArray()['id']); -echo print_r($chargeFetched, true); +// echo print_r($chargeFetched, true); // Capture the amount $chargeFetched->capture(['capture_amount' => 100000 / 2]); @@ -39,7 +39,7 @@ // 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'; +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 index 4e2cd08..22214b8 100644 --- a/examples/OmiseSchedule.php +++ b/examples/OmiseSchedule.php @@ -32,11 +32,9 @@ 'charge[description]' => 'Testing schedule', ], $pkeyTH, $skeyTH); -echo sprintf('Created schedule ID (TH): %s', $createResultTH->toArray()['id']) . '\n'; +$chargeScheduleIdTH = $createResultTH->toArray()['id']; -$resultTH = OmiseSchedule::retrieve($createResultTH->toArray()['id'], $pkeyTH, $skeyTH); - -echo sprintf('Fetched schedule ID (TH): %s', $resultTH->toArray()['id']) . '\n\n'; +echo sprintf('Created schedule ID (TH): %s', $chargeScheduleIdTH) . "\n"; // Create schedule under MY PSP $createResultMY = OmiseSchedule::create([ @@ -49,11 +47,9 @@ 'charge[description]' => 'Testing schedule', ], $pkeyMY, $skeyMY); -echo sprintf('Created schedule ID (MY): %s', $createResultMY->toArray()['id']) . '\n'; - -$resultMY = OmiseSchedule::retrieve($createResultMY->toArray()['id'], $pkeyMY, $skeyMY); +$chargeScheduleIdMY = $createResultMY->toArray()['id']; -echo sprintf('Fetched schedule ID (MY): %s', $resultMY->toArray()['id']) . '\n\n'; +echo sprintf('Created schedule ID (MY): %s', $createResultMY->toArray()['id']) . "\n\n"; // Create schedule under SG PSP $createResultSG = OmiseSchedule::create([ @@ -66,8 +62,21 @@ 'charge[description]' => 'Testing schedule', ]); -echo sprintf('Created schedule ID (SG): %s', $createResultSG->toArray()['id']) . '\n'; +$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($createResultSG->toArray()['id']); +$resultSG = OmiseSchedule::retrieve($chargeScheduleIdSG); -echo sprintf('Fetched schedule ID (SG): %s', $resultSG->toArray()['id']) . '\n\n'; +echo sprintf('Fetched schedule ID (SG): %s', $resultSG->toArray()['id']) . "\n\n";