From 0d3592e8ebb4f355ee8b86c168217d89da7ba27b Mon Sep 17 00:00:00 2001 From: guzzilar Date: Fri, 21 Jun 2019 19:53:35 +0800 Subject: [PATCH] This is a draft commit and should not be merged without properly rebase. --- lib/Account.php | 24 +-- lib/Balance.php | 24 +-- lib/Capabilities.php | 30 ++-- lib/Charge.php | 88 +++++----- lib/Collection.php | 64 +++++++ lib/Customer.php | 90 +++++----- lib/Dispute.php | 50 +++--- lib/Event.php | 44 ++--- lib/Forex.php | 24 +-- lib/Link.php | 52 +++--- lib/Omise.php | 140 ++++++++++------ lib/Refund.php | 14 +- lib/Schedule.php | 4 +- lib/Search.php | 124 ++++++++------ lib/Source.php | 18 +- lib/Token.php | 43 +++-- lib/Transaction.php | 42 ++--- lib/Transfer.php | 38 ++--- lib/omise/ApiRequestor.php | 157 ++---------------- lib/omise/ApiResource.php | 118 +++++++++++++ lib/omise/Client/ClientInterface.php | 17 ++ lib/omise/Client/CurlClient.php | 97 +++++++++++ lib/omise/Client/UnitTestClient.php | 51 ++++++ lib/omise/OmiseObject.php | 90 ++++++++++ lib/omise/Resource.php | 46 +++++ lib/omise/res/OmiseApiResource.php | 5 + .../2019-05-29/api.omise.co/account-get.json | 18 ++ .../2019-05-29/api.omise.co/balance-get.json | 10 ++ .../api.omise.co/capability-get.json | 50 ++++++ .../2019-05-29/api.omise.co/charges-post.json | 4 + .../charges/chrg_test_fixture-get.json | 5 + .../charges/chrg_test_fixture-patch.json | 5 + .../customers/cust_test_fixture-delete.json | 8 + .../2019-05-29/api.omise.co/events-get.json | 23 +++ .../api.omise.co/forex/usd-get.json | 8 + tests/omise/AccountTest.php | 29 +--- tests/omise/ApiRequestorTest.php | 43 +++-- tests/omise/BalanceTest.php | 31 ++-- tests/omise/CapabilitiesTest.php | 16 +- tests/omise/ChargeTest.php | 48 ++---- tests/omise/ForexTest.php | 4 +- tests/omise/TestConfig.php | 16 +- tests/omise/TransactionTest.php | 11 +- 43 files changed, 1115 insertions(+), 708 deletions(-) create mode 100644 lib/Collection.php create mode 100644 lib/omise/ApiResource.php create mode 100644 lib/omise/Client/ClientInterface.php create mode 100644 lib/omise/Client/CurlClient.php create mode 100644 lib/omise/Client/UnitTestClient.php create mode 100644 lib/omise/OmiseObject.php create mode 100644 lib/omise/Resource.php create mode 100644 tests/fixtures/2019-05-29/api.omise.co/account-get.json create mode 100644 tests/fixtures/2019-05-29/api.omise.co/balance-get.json create mode 100644 tests/fixtures/2019-05-29/api.omise.co/capability-get.json create mode 100644 tests/fixtures/2019-05-29/api.omise.co/charges-post.json create mode 100644 tests/fixtures/2019-05-29/api.omise.co/charges/chrg_test_fixture-get.json create mode 100644 tests/fixtures/2019-05-29/api.omise.co/charges/chrg_test_fixture-patch.json create mode 100644 tests/fixtures/2019-05-29/api.omise.co/customers/cust_test_fixture-delete.json create mode 100644 tests/fixtures/2019-05-29/api.omise.co/events-get.json create mode 100644 tests/fixtures/2019-05-29/api.omise.co/forex/usd-get.json diff --git a/lib/Account.php b/lib/Account.php index e8a90b77..380cce97 100644 --- a/lib/Account.php +++ b/lib/Account.php @@ -1,37 +1,25 @@ ['currency', 'type', 'chargeAmount'] ]; - protected function __construct() + public function __construct() { parent::__construct(); $this->setupFilterShortcuts(); @@ -48,22 +48,22 @@ protected function setupFilterShortcuts() */ public static function retrieve() { - return parent::g_retrieve(get_class(), self::getUrl()); + return parent::resourceRetrieve(); } /** - * @see Omise\Res\OmiseApiResource::g_reload() + * @see Omise\ApiResource::resourceReload() */ public function reload() { - parent::g_reload(self::getUrl()); + parent::resourceReload(); } /** * Retrieves array of payment backends. Optionally pass in as many filter functions as you want * (muliple arguments, or a single array) * - * @param [func1,fun2,...] OR func1, func2,... + * @param [func1,fun2,...] OR func1, func2,... * * @return array */ @@ -165,18 +165,8 @@ private static function argsToVariadic($argArray) /** * @return string */ - private static function getUrl() + protected function credential() { - return \Omise\ApiRequestor::OMISE_API_URL . self::ENDPOINT; - } - - /** - * Returns the public key. - * - * @return string - */ - protected function getResourceKey() - { - return $this->_publickey; + return 'pkey_test_5fcnkn4378pmguq5zpy'; } } diff --git a/lib/Charge.php b/lib/Charge.php index 344486ee..c5ace9ed 100644 --- a/lib/Charge.php +++ b/lib/Charge.php @@ -1,51 +1,60 @@ query($query); + } + + /** + * Retrieves a collection of charge objects. * - * @return Omise\Charge + * @param array $query + * + * @return Omise\Collection */ - public static function retrieve($id = '') + public static function all($query = array()) { - return parent::g_retrieve(get_class(), self::getUrl($id)); + $resource = Resource::newObject(static::OBJECT_NAME); + $result = $resource->request()->get($resource->url(), $resource->credential(), $query); + + return new Collection($result); } /** - * Search for charges. + * Retrieves a charge. * - * @param string $query + * @param string $id * - * @return Omise\Search + * @return Omise\Charge */ - public static function search($query = '') + public static function retrieve($id) { - return Search::scope('charge')->query($query); + return parent::resourceRetrieve($id); } /** - * @see Omise\Res\OmiseApiResource::g_reload() + * @see Omise\ApiResource::resourceReload() */ public function reload() { - if ($this['object'] === 'charge') { - parent::g_reload(self::getUrl($this['id'])); - } else { - parent::g_reload(self::getUrl()); - } + parent::resourceReload(); } /** @@ -69,15 +78,15 @@ public static function schedule($params) */ public static function create($params) { - return parent::g_create(get_class(), self::getUrl(), $params); + return parent::resourceCreate($params); } /** - * @see Omise\Res\OmiseApiResource::g_update() + * @see Omise\ApiResource::resourceUpdate() */ public function update($params) { - parent::g_update(self::getUrl($this['id']), $params); + parent::resourceUpdate($params); } /** @@ -87,9 +96,7 @@ public function update($params) */ public function capture() { - $result = $this->apiRequestor->post(self::getUrl($this['id']).'/capture', parent::getResourceKey()); - $this->refresh($result); - + $this->refresh($this->chainRequest('post', 'capture')); return $this; } @@ -100,7 +107,9 @@ public function capture() */ public function refund($params) { - $result = $this->apiRequestor->post(self::getUrl($this['id']) . '/refunds', parent::getResourceKey(), $params); + $result = $this->chainRequest('post', 'refunds', $params); + + $this->reload(); return new Refund($result); } @@ -111,26 +120,19 @@ public function refund($params) */ public function reverse() { - $result = $this->apiRequestor->post(self::getUrl($this['id']).'/reverse', parent::getResourceKey()); - $this->refresh($result); - + $this->refresh($this->chainRequest('post', 'reverse')); return $this; } /** * list refunds * - * @return Omise\RefundList + * @return \Omise\Collection Of Omise\Refund objects. */ public function refunds($options = array()) { - if (is_array($options) && ! empty($options)) { - $refunds = $this->apiRequestor->get(self::getUrl($this['id']) . '/refunds?' . http_build_query($options), parent::getResourceKey()); - } else { - $refunds = $this['refunds']; - } - - return new RefundList($refunds, $this['id']); + $refunds = is_array($options) && ! empty($options) ? $this->chainRequest('get', 'refunds', $options) : $this['refunds']; + return new Collection($refunds); } /** @@ -148,14 +150,4 @@ public static function schedules($options = array()) return parent::g_retrieve('\Omise\ScheduleList', self::getUrl('schedules' . $options)); } - - /** - * @param string $id - * - * @return string - */ - private static function getUrl($id = '') - { - return \Omise\ApiRequestor::OMISE_API_URL . self::ENDPOINT . '/' . $id; - } } diff --git a/lib/Collection.php b/lib/Collection.php new file mode 100644 index 00000000..624f4152 --- /dev/null +++ b/lib/Collection.php @@ -0,0 +1,64 @@ +mapAttribute($items); + + foreach ($items['data'] as $key => $value) { + $this->items[$key] = Resource::newObject($value['object'], $value); + } + + $this->collection = $this->first()['object']; + } + + /** + * @return Omise\Object In a first position of an array ($items). + */ + public function first() + { + return $this->items[0]; + } + + /** + * @implements \Countable + * + * @return int + */ + public function count() + { + return $this->attributes['total']; + } + + protected function mapAttribute($items) + { + $this->attributes = array( + 'from' => $items['from'], + 'to' => $items['to'], + 'offset' => $items['offset'], + 'limit' => $items['limit'], + 'total' => $items['total'], + 'order' => $items['order'], + 'location' => $items['location'], + ); + } +} diff --git a/lib/Customer.php b/lib/Customer.php index 4309e905..ec00a7bb 100644 --- a/lib/Customer.php +++ b/lib/Customer.php @@ -1,37 +1,51 @@ query($query); + } + + /** + * Retrieves customer objects. * * @param string $id * - * @return Omise\Customer + * @return Omise\Collection */ - public static function retrieve($id = '') + public static function all($query = array()) { - return parent::g_retrieve(get_class(), self::getUrl($id)); + $resource = Resource::newObject(static::OBJECT_NAME); + $result = $resource->request()->get($resource->url(), $resource->credential(), $query); + + return new Collection($result); } /** - * Search for customers. + * Retrieves a customer. * - * @param string $query + * @param string $id * - * @return Omise\Search + * @return Omise\Customer */ - public static function search($query = '') + public static function retrieve($id) { - return Search::scope('customer')->query($query); + return parent::resourceRetrieve($id); } /** @@ -43,19 +57,15 @@ public static function search($query = '') */ public static function create($params) { - return parent::g_create(get_class(), self::getUrl(), $params); + return parent::resourceCreate($params); } /** - * @see Omise\Res\OmiseApiResource::g_reload() + * @see Omise\ApiResource::g_reload() */ public function reload() { - if ($this['object'] === 'customer') { - parent::g_reload(self::getUrl($this['id'])); - } else { - parent::g_reload(self::getUrl()); - } + parent::resourceReload(); } /** @@ -63,15 +73,15 @@ public function reload() */ public function update($params) { - parent::g_update(self::getUrl($this['id']), $params); + parent::resourceUpdate($params); } /** - * @see Omise\Res\OmiseApiResource::g_destroy() + * @see Omise\xApiResource::g_destroy() */ public function destroy() { - parent::g_destroy(self::getUrl($this['id'])); + parent::resourceDestroy(); } /** @@ -87,17 +97,12 @@ public function isDestroyed() * * @param array $options * - * @return Omise\CardList + * @return Omise\Collection */ public function cards($options = array()) { - if (is_array($options) && ! empty($options)) { - $cards = $this->apiRequestor->get(self::getUrl($this['id']) . '/cards?' . http_build_query($options), parent::getResourceKey()); - } else { - $cards = $this['cards']; - } - - return new CardList($cards, $this['id']); + $cards = is_array($options) && ! empty($options) ? $this->chainRequest('get', 'cards', $options) : $this['cards']; + return new Collection($cards); } /** @@ -105,7 +110,7 @@ public function cards($options = array()) * * @deprecated deprecated since version 2.0.0 use '$customer->cards()' * - * @return Omise\CardList + * @return Omise\Collection */ public function getCards($options = array()) { @@ -121,22 +126,7 @@ public function getCards($options = array()) */ public function schedules($options = array()) { - if ($this['object'] === 'customer') { - if (is_array($options)) { - $options = '?' . http_build_query($options); - } - - return parent::g_retrieve('\Omise\ScheduleList', self::getUrl($this['id'] . '/schedules' . $options)); - } - } - - /** - * @param string $id - * - * @return string - */ - private static function getUrl($id = '') - { - return \Omise\ApiRequestor::OMISE_API_URL . self::ENDPOINT . '/' . $id; + $schedules = $this->chainRequest('get', 'schedules', $options); + return new Collection($schedules); } } diff --git a/lib/Dispute.php b/lib/Dispute.php index 5dd64b16..c86211a0 100644 --- a/lib/Dispute.php +++ b/lib/Dispute.php @@ -1,23 +1,27 @@ request()->get($resource->url(), $resource->credential(), $query); + + return new Collection($result); } /** @@ -33,34 +37,30 @@ public static function search($query = '') } /** - * @see Omise\Res\OmiseApiResource::g_reload() + * Retrieves a dispute. + * + * @param string $id + * + * @return Omise\Dispute */ - public function reload() + public static function retrieve($id) { - if ($this['object'] === 'dispute') { - parent::g_reload(self::getUrl($this['id'])); - } else { - parent::g_reload(self::getUrl()); - } + return parent::resourceRetrieve($id); } /** - * @see Omise\Res\OmiseApiResource::g_update() + * @see Omise\ApiResource::resourceReload() */ - public function update($params) + public function reload() { - parent::g_update(self::getUrl($this['id']), $params); + parent::resourceReload(); } /** - * Generate request url. - * - * @param string $id - * - * @return string + * @see Omise\ApiResource::resourceUpdate() */ - private static function getUrl($id = '') + public function update($params) { - return \Omise\ApiRequestor::OMISE_API_URL . self::ENDPOINT . '/' . $id; + parent::resourceUpdate($params); } } diff --git a/lib/Event.php b/lib/Event.php index c3a95846..5e1e5546 100644 --- a/lib/Event.php +++ b/lib/Event.php @@ -1,45 +1,45 @@ request()->get($resource->url(), $resource->credential(), $query); + + return new Collection($result); } /** - * @see Omise\Res\OmiseApiResource::g_reload() + * Retrieves an event. + * + * @param string $id + * + * @return Omise\Event */ - public function reload() + public static function retrieve($id) { - if ($this['object'] === 'event') { - parent::g_reload(self::getUrl($this['id'])); - } else { - parent::g_reload(self::getUrl()); - } + return parent::resourceRetrieve($id); } /** - * Generate request url. - * - * @param string $id - * - * @return string + * @see Omise\ApiResource::resourceReload() */ - private static function getUrl($id = '') + public function reload() { - return \Omise\ApiRequestor::OMISE_API_URL . self::ENDPOINT . '/' . $id; + parent::resourceReload(); } } diff --git a/lib/Forex.php b/lib/Forex.php index dddd3b3f..7a09b275 100644 --- a/lib/Forex.php +++ b/lib/Forex.php @@ -1,11 +1,9 @@ request()->get($resource->url(), $resource->credential(), $query); + + return new Collection($result); } /** @@ -33,15 +37,23 @@ public static function search($query = '') } /** - * @see Omise\Res\OmiseApiResource::g_reload() + * Retrieves a link object. + * + * @param string $id + * + * @return Omise\Link + */ + public static function retrieve($id) + { + return parent::resourceRetrieve($id); + } + + /** + * @see Omise\ApiResource::resourceReload() */ public function reload() { - if ($this['object'] === 'link') { - parent::g_reload(self::getUrl($this['id'])); - } else { - parent::g_reload(self::getUrl()); - } + parent::resourceReload(); } /** @@ -53,16 +65,6 @@ public function reload() */ public static function create($params) { - return parent::g_create(get_class(), self::getUrl(), $params); - } - - /** - * @param string $id - * - * @return string - */ - private static function getUrl($id = '') - { - return \Omise\ApiRequestor::OMISE_API_URL . self::ENDPOINT . '/' . $id; + return parent::resourceCreate($params); } } diff --git a/lib/Omise.php b/lib/Omise.php index 7cc39772..cbeb4412 100644 --- a/lib/Omise.php +++ b/lib/Omise.php @@ -6,15 +6,19 @@ * @method public static secretKey * @method public static apiVersion * @method public static userAgent + * @method public static client * @method public static setPublicKey * @method public static setSecretKey * @method public static setApiVersion * @method public static setUserAgent + * @method public static setClient * * @since 3.0.0 */ class Omise { + const VERSION = '3.0.0-dev'; + /** * @var string Of Omise credentials (public key, secret key). */ @@ -31,6 +35,11 @@ class Omise */ protected static $userAgent; + /** + * @var Omise\Client\ClientInterface + */ + protected static $client = '\Omise\Client\CurlClient'; + /** * @return string */ @@ -83,6 +92,14 @@ public static function userAgent() return static::$userAgent; } + /** + * @return \Omise\Client\ClientInterface A new instance of a ClientInterface-implemented object. + */ + public static function client() + { + return new static::$client; + } + /** * @param string $key */ @@ -119,66 +136,81 @@ public static function setUserAgent($userAgent) { static::$userAgent = $userAgent; } + + /** + * @param string $client Client class name + */ + public static function setClient($client) + { + static::$client = $client; + } } // Cores and utilities. -require_once dirname(__FILE__).'/omise/ApiRequestor.php'; -require_once dirname(__FILE__).'/omise/res/obj/OmiseObject.php'; -require_once dirname(__FILE__).'/omise/res/OmiseApiResource.php'; -require_once dirname(__FILE__).'/omise/res/OmiseVaultResource.php'; -require_once dirname(__FILE__).'/omise/Http/Response/Handler.php'; +require_once __DIR__ . '/omise/Client/ClientInterface.php'; +require_once __DIR__ . '/omise/Client/CurlClient.php'; +require_once __DIR__ . '/omise/Client/UnitTestClient.php'; +require_once __DIR__ . '/omise/Http/Response/Handler.php'; +require_once __DIR__ . '/omise/OmiseObject.php'; +require_once __DIR__ . '/omise/ApiRequestor.php'; +require_once __DIR__ . '/omise/ApiResource.php'; +require_once __DIR__ . '/omise/Resource.php'; +require_once __DIR__ . '/Collection.php'; +// require_once __DIR__ . '/omise/res/obj/OmiseObject.php'; +// require_once __DIR__ . '/omise/res/OmiseApiResource.php'; +// require_once __DIR__ . '/omise/res/OmiseVaultResource.php'; // Errors -require_once dirname(__FILE__).'/omise/exception/OmiseExceptions.php'; +require_once __DIR__ . '/omise/exception/OmiseExceptions.php'; // API Resources. -require_once dirname(__FILE__).'/Account.php'; -require_once dirname(__FILE__).'/Balance.php'; -require_once dirname(__FILE__).'/Capabilities.php'; -require_once dirname(__FILE__).'/Card.php'; -require_once dirname(__FILE__).'/CardList.php'; -require_once dirname(__FILE__).'/Charge.php'; -require_once dirname(__FILE__).'/Customer.php'; -require_once dirname(__FILE__).'/Dispute.php'; -require_once dirname(__FILE__).'/Event.php'; -require_once dirname(__FILE__).'/Forex.php'; -require_once dirname(__FILE__).'/Link.php'; -require_once dirname(__FILE__).'/Occurrence.php'; -require_once dirname(__FILE__).'/OccurrenceList.php'; -require_once dirname(__FILE__).'/Recipient.php'; -require_once dirname(__FILE__).'/Refund.php'; -require_once dirname(__FILE__).'/RefundList.php'; -require_once dirname(__FILE__).'/Schedule.php'; -require_once dirname(__FILE__).'/ScheduleList.php'; -require_once dirname(__FILE__).'/Scheduler.php'; -require_once dirname(__FILE__).'/Search.php'; -require_once dirname(__FILE__).'/Source.php'; -require_once dirname(__FILE__).'/Token.php'; -require_once dirname(__FILE__).'/Transaction.php'; -require_once dirname(__FILE__).'/Transfer.php'; +require_once __DIR__ . '/Account.php'; +require_once __DIR__ . '/Balance.php'; +require_once __DIR__ . '/Capabilities.php'; +// require_once __DIR__ . '/Card.php'; +// require_once __DIR__ . '/CardList.php'; +require_once __DIR__ . '/Charge.php'; +require_once __DIR__ . '/Customer.php'; +require_once __DIR__ . '/Dispute.php'; +require_once __DIR__ . '/Event.php'; +require_once __DIR__ . '/Forex.php'; +require_once __DIR__ . '/Link.php'; +// require_once __DIR__ . '/Occurrence.php'; +// require_once __DIR__ . '/OccurrenceList.php'; +// require_once __DIR__ . '/Recipient.php'; +require_once __DIR__ . '/Refund.php'; +// require_once __DIR__ . '/RefundList.php'; +require_once __DIR__ . '/Schedule.php'; +// require_once __DIR__ . '/ScheduleList.php'; +// require_once __DIR__ . '/Scheduler.php'; +require_once __DIR__ . '/Search.php'; +require_once __DIR__ . '/Source.php'; +require_once __DIR__ . '/Token.php'; +require_once __DIR__ . '/Transaction.php'; +require_once __DIR__ . '/Transfer.php'; // API Resources - Legacy classes. -require_once dirname(__FILE__).'/omise/OmiseAccount.php'; -require_once dirname(__FILE__).'/omise/OmiseBalance.php'; -require_once dirname(__FILE__).'/omise/OmiseCapabilities.php'; -require_once dirname(__FILE__).'/omise/OmiseCard.php'; -require_once dirname(__FILE__).'/omise/OmiseCardList.php'; -require_once dirname(__FILE__).'/omise/OmiseDispute.php'; -require_once dirname(__FILE__).'/omise/OmiseEvent.php'; -require_once dirname(__FILE__).'/omise/OmiseForex.php'; -require_once dirname(__FILE__).'/omise/OmiseToken.php'; -require_once dirname(__FILE__).'/omise/OmiseCharge.php'; -require_once dirname(__FILE__).'/omise/OmiseCustomer.php'; -require_once dirname(__FILE__).'/omise/OmiseOccurrence.php'; -require_once dirname(__FILE__).'/omise/OmiseOccurrenceList.php'; -require_once dirname(__FILE__).'/omise/OmiseRefund.php'; -require_once dirname(__FILE__).'/omise/OmiseRefundList.php'; -require_once dirname(__FILE__).'/omise/OmiseSearch.php'; -require_once dirname(__FILE__).'/omise/OmiseSchedule.php'; -require_once dirname(__FILE__).'/omise/OmiseScheduleList.php'; -require_once dirname(__FILE__).'/omise/OmiseScheduler.php'; -require_once dirname(__FILE__).'/omise/OmiseSource.php'; -require_once dirname(__FILE__).'/omise/OmiseTransfer.php'; -require_once dirname(__FILE__).'/omise/OmiseTransaction.php'; -require_once dirname(__FILE__).'/omise/OmiseRecipient.php'; -require_once dirname(__FILE__).'/omise/OmiseLink.php'; +require_once __DIR__ . '/omise/OmiseAccount.php'; +require_once __DIR__ . '/omise/OmiseBalance.php'; +require_once __DIR__ . '/omise/OmiseCapabilities.php'; +// require_once __DIR__ . '/omise/OmiseCard.php'; +// require_once __DIR__ . '/omise/OmiseCardList.php'; +require_once __DIR__ . '/omise/OmiseCharge.php'; +require_once __DIR__ . '/omise/OmiseCustomer.php'; +require_once __DIR__ . '/omise/OmiseDispute.php'; +require_once __DIR__ . '/omise/OmiseEvent.php'; +require_once __DIR__ . '/omise/OmiseForex.php'; +require_once __DIR__ . '/omise/OmiseLink.php'; +// require_once __DIR__ . '/omise/OmiseOccurrence.php'; +// require_once __DIR__ . '/omise/OmiseOccurrenceList.php'; +// require_once __DIR__ . '/omise/OmiseRecipient.php'; +require_once __DIR__ . '/omise/OmiseRefund.php'; +// require_once __DIR__ . '/omise/OmiseRefundList.php'; +require_once __DIR__ . '/omise/OmiseSchedule.php'; +// require_once __DIR__ . '/omise/OmiseScheduleList.php'; +// require_once __DIR__ . '/omise/OmiseScheduler.php'; +require_once __DIR__ . '/omise/OmiseSearch.php'; +require_once __DIR__ . '/omise/OmiseSource.php'; +require_once __DIR__ . '/omise/OmiseToken.php'; +require_once __DIR__ . '/omise/OmiseTransaction.php'; +require_once __DIR__ . '/omise/OmiseTransfer.php'; diff --git a/lib/Refund.php b/lib/Refund.php index 5932b926..8cdec84d 100644 --- a/lib/Refund.php +++ b/lib/Refund.php @@ -1,23 +1,13 @@ refresh($refund); - } + const OBJECT_NAME = 'refund'; /** - * Search for refunds. - * * @param string $query * * @return Omise\Search diff --git a/lib/Schedule.php b/lib/Schedule.php index 84d6d2c1..3ee5a221 100644 --- a/lib/Schedule.php +++ b/lib/Schedule.php @@ -1,10 +1,10 @@ mergeAttributes('scope', $scope); } /** * Create an instance of `OmiseSearch` with the given scope. * - * This constructor is `protected` thus not intended to be used directly. + * @param string $scope See supported scope at [Search API](https://www.omise.co/search-api) page. * - * @param string $scope See supported scope at [Search API](https://www.omise.co/search-api) page. + * @return Omise\Search The created instance. */ - protected function __construct($scope) + public static function scope($scope) { - parent::__construct(); - $this->mergeAttributes('scope', $scope); + return new static($scope); } /** @@ -147,45 +146,14 @@ public function isDirty() public function reload() { $this->dirty = false; - $this->g_reload($this->getUrl()); - } + parent::resourceReload(); - /** - * Reload search data from Omise server if this instance is in dirty state. - */ - private function reloadIfDirty() - { - if ($this->isDirty()) { - $this->reload(); + $mappedObject = array(); + foreach ($this['data'] as $key => $value) { + $mappedObject[$key] = Resource::newObject($value['object'], $value); } - } - - /** - * Merge the given key and value to search attributes, and set instance state - * as dirty. - * - * @param string $key Search attribute key. - * @param mixed $value Search attribute value. - * - * @return Omise\Search This instance. - */ - private function mergeAttributes($key, $value) - { - $this->dirty = true; - $this->attributes[$key] = $value; - return $this; - } - - /** - * Generate request url. - * - * @return string - */ - private function getUrl() - { - $querystring = http_build_query($this->attributes); - return \Omise\ApiRequestor::OMISE_API_URL . self::ENDPOINT . '/?' . $querystring; + $this['data'] = $mappedObject; } // Override methods of ArrayAccess @@ -233,4 +201,58 @@ public function offsetGet($key) $this->reloadIfDirty(); return parent::offsetGet($key); } + + protected function mapObject($objectName, $values) + { + $knownObject = array( + 'charge' => '\Omise\Charge', + 'card' => '\Omise\Card', + 'customer' => '\Omise\Customer', + 'refund' => '\Omise\Refund', + 'schedule' => '\Omise\Schedule', + 'transaction' => '\Omise\Transaction' + ); + + $object = $knownObject[$objectName]; + + return new $object($values); + } + + /** + * Generate request url. + * + * @return string + */ + protected function url() + { + $querystring = http_build_query($this->searchAttributes); + return \Omise\ApiRequestor::OMISE_API_URL . Resource::getEndpoint(static::OBJECT_NAME) . '/?' . $querystring; + } + + /** + * Merge the given key and value to search attributes, and set instance state + * as dirty. + * + * @param string $key Search attribute key. + * @param mixed $value Search attribute value. + * + * @return Omise\Search This instance. + */ + private function mergeAttributes($key, $value) + { + $this->dirty = true; + $this->searchAttributes[$key] = $value; + + return $this; + } + + /** + * Reload search data from Omise server if this instance is in dirty state. + */ + private function reloadIfDirty() + { + if ($this->isDirty()) { + $this->reload(); + } + } } diff --git a/lib/Source.php b/lib/Source.php index 635a1b3f..228622a6 100644 --- a/lib/Source.php +++ b/lib/Source.php @@ -1,29 +1,19 @@ id; } /** - * @param string $id - * * @return string */ - private static function getUrl($id = '') + protected function credential() { - return \Omise\ApiRequestor::OMISE_VAULT_URL . self::ENDPOINT . '/' . $id; + return Omise::publicKey(); } } diff --git a/lib/Transaction.php b/lib/Transaction.php index cb67b36c..6951e7a4 100644 --- a/lib/Transaction.php +++ b/lib/Transaction.php @@ -1,45 +1,45 @@ request()->get($resource->url(), $resource->credential(), $query); + + return new Collection($result); } /** - * (non-PHPdoc) + * Retrieves a link object. + * + * @param string $id * - * @see OmiseApiResource::g_reload() + * @return Omise\Transaction */ - public function reload() + public static function retrieve($id) { - if ($this['object'] === 'transaction') { - parent::reload(self::getUrl($this['id'])); - } else { - parent::g_reload(self::getUrl()); - } + return parent::resourceRetrieve($id); } /** - * @param string $id - * - * @return string + * @see Omise\ApiResource::resourceReload() */ - private static function getUrl($id = '') + public function reload() { - return \Omise\ApiRequestor::OMISE_API_URL . self::ENDPOINT . '/' . $id; + parent::resourceReload(); } } diff --git a/lib/Transfer.php b/lib/Transfer.php index b1cb5c93..ab2b814c 100644 --- a/lib/Transfer.php +++ b/lib/Transfer.php @@ -1,12 +1,12 @@ get test mode. - if (preg_match('/phpunit/', $_SERVER['SCRIPT_NAME'])) { - $result = $this->_executeTest($url, $requestMethod, $key, $params); - } else { - $result = $this->_executeCurl($url, $requestMethod, $key, $params); - } + $client = $this->client(); + $client->setCredential($key); + + $result = $client->execute($url, $method, $params); $responseHandler = new ResponseHandler; return $responseHandler->handle($result); } /** - * @param string $url - * @param string $requestMethod - * @param array $params - * - * @throws OmiseException - * - * @return string + * @return \Omise\Client\ClientInterface An instance of a ClientInterface-implemented object. */ - private function _executeCurl($url, $requestMethod, $key, $params = null) + public function client() { - $ch = curl_init($url); - - curl_setopt_array($ch, $this->genOptions($requestMethod, $key.':', $params)); - - // Make a request or thrown an exception. - if (($result = curl_exec($ch)) === false) { - $error = curl_error($ch); - curl_close($ch); - - throw new Exception($error); - } - - // Close. - curl_close($ch); - - return $result; - } - - /** - * @param string $url - * @param string $requestMethod - * @param array $params - * - * @throws OmiseException - * - * @return string - */ - private function _executeTest($url, $requestMethod, $key, $params = null) - { - // Extract only hostname and URL path without trailing slash. - $parsed = parse_url($url); - $requestUrl = $parsed['host'] . rtrim($parsed['path'], '/'); - - // Convert query string into filename friendly format. - if (!empty($parsed['query'])) { - $query = base64_encode($parsed['query']); - $query = str_replace(array('+', '/', '='), array('-', '_', ''), $query); - $requestUrl = $requestUrl . '-' . $query; - } - - // Finally. - $requestUrl = dirname(__FILE__) . '/../../tests/fixtures/' . $requestUrl . '-' . strtolower($requestMethod) . '.json'; - - // Make a request from Curl if json file was not exists. - if (!file_exists($requestUrl)) { - // Get a directory that's file should contain. - $request_dir = explode('/', $requestUrl); - unset($request_dir[count($request_dir) - 1]); - $request_dir = implode('/', $request_dir); - - // Create directory if it not exists. - if (! file_exists($request_dir)) { - mkdir($request_dir, 0777, true); - } - - $result = $this->_executeCurl($url, $requestMethod, $key, $params); - - $f = fopen($requestUrl, 'w'); - if ($f) { - fwrite($f, $result); - fclose($f); - } - } else { - // Or get response from json file. - $result = file_get_contents($requestUrl); - } - - return $result; - } - - /** - * Creates an option for php-curl from the given request method and parameters in an associative array. - * - * @param string $requestMethod - * @param array $params - * - * @return array - */ - private function genOptions($requestMethod, $userpassword, $params) - { - $certificateFileLocation = dirname(__FILE__) . '/../../data/ca_certificates.pem'; - $userAgent = 'OmisePHP/' . self::OMISE_PHP_LIB_VERSION . ' PHP/' . phpversion(); - - $options = array( - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, // Set the HTTP version to 1.1. - CURLOPT_CUSTOMREQUEST => $requestMethod, // Set the request method. - CURLOPT_RETURNTRANSFER => true, // Make php-curl returns the data as string. - CURLOPT_HEADER => false, // Do not include the header in the output. - CURLINFO_HEADER_OUT => true, // Track the header request string and set the referer on redirect. - CURLOPT_AUTOREFERER => true, - CURLOPT_TIMEOUT => $this->OMISE_TIMEOUT, // Time before the request is aborted. - CURLOPT_CONNECTTIMEOUT => $this->OMISE_CONNECTTIMEOUT, // Time before the request is aborted when attempting to connect. - CURLOPT_USERPWD => $userpassword, // Authentication. - CURLOPT_CAINFO => $certificateFileLocation // CA bundle. - ); - - // Config Omise API Version - if ($apiVersion = \Omise\Omise::apiVersion()) { - $options += array(CURLOPT_HTTPHEADER => array('Omise-Version: ' . $apiVersion)); - - $userAgent .= ' OmiseAPI/' . $apiVersion; - } - - // Config UserAgent - if ($suffixUserAgent = \Omise\Omise::userAgent()) { - $userAgent .= ' ' . $suffixUserAgent; - } - - $options += array(CURLOPT_USERAGENT => $userAgent); - - // Also merge POST parameters with the option. - if (is_array($params) && count($params) > 0) { - $httpQuery = http_build_query($params); - $httpQuery = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $httpQuery); - - $options += array(CURLOPT_POSTFIELDS => $httpQuery); - } - - return $options; + static::$client = static::$client ?: \Omise\Omise::client(); + return static::$client; } } diff --git a/lib/omise/ApiResource.php b/lib/omise/ApiResource.php new file mode 100644 index 00000000..915e11af --- /dev/null +++ b/lib/omise/ApiResource.php @@ -0,0 +1,118 @@ +setId($id); + + $result = $resource->request()->get($resource->url(), $resource->credential()); + + $resource->refresh($result); + return $resource; + } + + /** + * @param array $params + * + * @return Omise\OmiseObject + * + * @throws Omise\Exception + */ + public static function resourceCreate($params) + { + $resource = Resource::newObject(static::OBJECT_NAME); + + $result = $resource->request()->post($resource->url(), $resource->credential(), $params); + + $resource->setId($result['id']); + $resource->refresh($result); + return $resource; + } + + /** + * @param array $params + * + * @return Omise\OmiseObject + * + * @throws Omise\Exception + */ + public function resourceUpdate($params) + { + $result = $this->request()->patch($this->url(), $this->credential(), $params); + $this->refresh($result); + } + + /** + * @throws Omise\Exception + */ + public function resourceReload() + { + $result = $this->request()->get($this->url(), $this->credential()); + $this->refresh($result); + } + + /** + * @throws Omise\Exception + */ + public function resourceDestroy() + { + $result = $this->request()->delete($this->url(), $this->credential()); + $this->refresh($result); + } + + /** + * @return \Omise\ApiRequestor An instance. + */ + public function request() + { + return new ApiRequestor; + } + + public function chainRequest($method, $endpoint, $params = array()) + { + return $this->request()->$method($this->url() . '/' . $endpoint, $this->credential(), $params); + } + + /** + * @return string + */ + protected function url() + { + return ApiRequestor::OMISE_API_URL . Resource::getEndpoint(static::OBJECT_NAME) . '/' . $this->id; + } + + /** + * @return string + */ + protected function credential() + { + return Omise::secretKey(); + } +} diff --git a/lib/omise/Client/ClientInterface.php b/lib/omise/Client/ClientInterface.php new file mode 100644 index 00000000..d8644673 --- /dev/null +++ b/lib/omise/Client/ClientInterface.php @@ -0,0 +1,17 @@ +genOptions($method, static::$credential . ':', $params); + $curl = curl_init($url); + + curl_setopt_array($curl, $options); + + // Make a request or thrown an exception. + if (($result = curl_exec($curl)) === false) { + $error = curl_error($curl); + curl_close($curl); + + throw new Exception($error); + } + + curl_close($curl); + return $result; + } + + /** + * Creates an option for php-curl from the given request method and parameters in an associative array. + * + * @param string $requestMethod + * @param array $params + * + * @return array + */ + private function genOptions($requestMethod, $userpassword, $params) + { + $certificateFileLocation = dirname(__FILE__) . '/../../../data/ca_certificates.pem'; + $userAgent = 'OmisePHP/' . \Omise\Omise::VERSION . ' PHP/' . phpversion(); + + $options = array( + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, // Set the HTTP version to 1.1. + CURLOPT_CUSTOMREQUEST => $requestMethod, // Set the request method. + CURLOPT_RETURNTRANSFER => true, // Make php-curl returns the data as string. + CURLOPT_HEADER => false, // Do not include the header in the output. + CURLINFO_HEADER_OUT => true, // Track the header request string and set the referer on redirect. + CURLOPT_AUTOREFERER => true, + CURLOPT_TIMEOUT => $this->OMISE_TIMEOUT, // Time before the request is aborted. + CURLOPT_CONNECTTIMEOUT => $this->OMISE_CONNECTTIMEOUT, // Time before the request is aborted when attempting to connect. + CURLOPT_USERPWD => $userpassword, // Authentication. + CURLOPT_CAINFO => $certificateFileLocation // CA bundle. + ); + + // Config Omise API Version + if ($apiVersion = \Omise\Omise::apiVersion()) { + $options += array(CURLOPT_HTTPHEADER => array('Omise-Version: ' . $apiVersion)); + + $userAgent .= ' OmiseAPI/' . $apiVersion; + } + + // Config UserAgent + if ($suffixUserAgent = \Omise\Omise::userAgent()) { + $userAgent .= ' ' . $suffixUserAgent; + } + + $options += array(CURLOPT_USERAGENT => $userAgent); + + // Also merge POST parameters with the option. + if (is_array($params) && count($params) > 0) { + $httpQuery = http_build_query($params); + $httpQuery = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $httpQuery); + + $options += array(CURLOPT_POSTFIELDS => $httpQuery); + } + + return $options; + } +} diff --git a/lib/omise/Client/UnitTestClient.php b/lib/omise/Client/UnitTestClient.php new file mode 100644 index 00000000..878351e3 --- /dev/null +++ b/lib/omise/Client/UnitTestClient.php @@ -0,0 +1,51 @@ +apiVersion; + $requestUrl = $this->extractRequestUrl($url); + $fixtureDirectory = __DIR__ . '/../../../tests/fixtures/'; + + $fixture = $fixtureDirectory . $apiVersion . '/' . $requestUrl . '-' . strtolower($method) . '.json'; + + if (! file_exists($fixture)) { + throw new Exception('Fixture not found: ' . $fixture); + } + + return file_get_contents($fixture); + } + + protected function extractRequestUrl($url) + { + // Extract only hostname and URL path without trailing slash. + $parsed = parse_url($url); + $requestUrl = $parsed['host'] . rtrim($parsed['path'], '/'); + + // Convert query string into filename friendly format. + if (! empty($parsed['query'])) { + $query = base64_encode($parsed['query']); + $query = str_replace(array('+', '/', '='), array('-', '_', ''), $query); + $requestUrl = $requestUrl . '-' . $query; + } + + return $requestUrl; + } +} diff --git a/lib/omise/OmiseObject.php b/lib/omise/OmiseObject.php new file mode 100644 index 00000000..bd6dd942 --- /dev/null +++ b/lib/omise/OmiseObject.php @@ -0,0 +1,90 @@ +refresh($values); + + if ($values['id']) { + $this->setId($values['id']); + } + + return $this; + } + + /** + * @param string $id Of a given resource. + */ + public function setId($id) + { + $this->id = $id; + } + + /** + * @param array $values + */ + public function refresh($values) + { + $this->attributes = array_merge($this->attributes, $values); + } + + /** + * @return array + */ + public function toArray() + { + return (array) $this->attributes; + } + + /** + * @return array + */ + public function toJson() + { + return (string) json_encode($this->attributes); + } + + /** + * Override methods of ArrayAccess + */ + public function offsetSet($key, $value) + { + $this->attributes[$key] = $value; + } + + /** + * Override methods of ArrayAccess + */ + public function offsetExists($key) + { + return isset($this->attributes[$key]); + } + + /** + * Override methods of ArrayAccess + */ + public function offsetUnset($key) + { + unset($this->attributes[$key]); + } + + /** + * Override methods of ArrayAccess + */ + public function offsetGet($key) + { + return isset($this->attributes[$key]) ? $this->attributes[$key] : null; + } +} diff --git a/lib/omise/Resource.php b/lib/omise/Resource.php new file mode 100644 index 00000000..5f88df38 --- /dev/null +++ b/lib/omise/Resource.php @@ -0,0 +1,46 @@ + array('endpoint' => 'account', 'class' => 'Omise\Account'), + 'balance' => array('endpoint' => 'balance', 'class' => 'Omise\Balance'), + 'capability' => array('endpoint' => 'capability', 'class' => 'Omise\Capabilities'), + 'card' => array('endpoint' => 'cards', 'class' => 'Omise\Card'), + 'charge' => array('endpoint' => 'charges', 'class' => 'Omise\Charge'), + 'customer' => array('endpoint' => 'customers', 'class' => 'Omise\Customer'), + 'dispute' => array('endpoint' => 'disputes', 'class' => 'Omise\Dispute'), + 'event' => array('endpoint' => 'events', 'class' => 'Omise\Event'), + 'forex' => array('endpoint' => 'forex', 'class' => 'Omise\Forex'), + 'link' => array('endpoint' => 'links', 'class' => 'Omise\Link'), + 'occurrence' => array('endpoint' => 'occurrences', 'class' => 'Omise\Occurrence'), + 'recipient' => array('endpoint' => 'recipients', 'class' => 'Omise\Recipient'), + 'refund' => array('endpoint' => 'refunds', 'class' => 'Omise\Refund'), + 'schedule' => array('endpoint' => 'schedules', 'class' => 'Omise\Schedule'), + 'search' => array('endpoint' => 'search', 'class' => 'Omise\Search'), + 'source' => array('endpoint' => 'sources', 'class' => 'Omise\Source'), + 'token' => array('endpoint' => 'tokens', 'class' => 'Omise\Token'), + 'transaction' => array('endpoint' => 'transactions', 'class' => 'Omise\Transaction'), + 'transfer' => array('endpoint' => 'transfers', 'class' => 'Omise\Transfer'), + ); + + public static function getEndpoint($object) + { + return isset(static::$knownResources[$object]) ? static::$knownResources[$object]['endpoint'] : null; + } + + public static function getClassName($object) + { + return isset(static::$knownResources[$object]) ? static::$knownResources[$object]['class'] : null; + } + + public static function newObject($object, $value = array()) + { + $classname = static::getClassName($object); + return new $classname($value); + } +} diff --git a/lib/omise/res/OmiseApiResource.php b/lib/omise/res/OmiseApiResource.php index 4dbd9be9..3192b31e 100644 --- a/lib/omise/res/OmiseApiResource.php +++ b/lib/omise/res/OmiseApiResource.php @@ -133,4 +133,9 @@ protected function getResourceKey() { return $this->_secretkey; } + + protected function client() + { + return \Omise\Omise::client(); + } } diff --git a/tests/fixtures/2019-05-29/api.omise.co/account-get.json b/tests/fixtures/2019-05-29/api.omise.co/account-get.json new file mode 100644 index 00000000..69f6fc21 --- /dev/null +++ b/tests/fixtures/2019-05-29/api.omise.co/account-get.json @@ -0,0 +1,18 @@ +{ + "object": "account", + "id": "account_test_fixture", + "livemode": "false", + "location": "/account", + "created_at": "2015-02-06T09:45:48Z", + "team": "acct_59fixture", + "email": "omisephp-fixture@omise.co", + "currency": "THB", + "supported_currencies": [ + "THB", + "JPY", + "USD", + "EUR", + "GBP", + "SGD" + ] +} diff --git a/tests/fixtures/2019-05-29/api.omise.co/balance-get.json b/tests/fixtures/2019-05-29/api.omise.co/balance-get.json new file mode 100644 index 00000000..8fe94ed9 --- /dev/null +++ b/tests/fixtures/2019-05-29/api.omise.co/balance-get.json @@ -0,0 +1,10 @@ +{ + "object": "balance", + "livemode": false, + "location": "/balance", + "created_at": "2017-11-01T17:10:42Z", + "transferable": 51861467, + "total": 51861467, + "reserve": 0, + "currency": "thb" +} diff --git a/tests/fixtures/2019-05-29/api.omise.co/capability-get.json b/tests/fixtures/2019-05-29/api.omise.co/capability-get.json new file mode 100644 index 00000000..1f140bc4 --- /dev/null +++ b/tests/fixtures/2019-05-29/api.omise.co/capability-get.json @@ -0,0 +1,50 @@ +{ + "object":"capability", + "location":"/capability", + "banks":["test","bbl","kbank","rbs","ktb","jpm","mufg","tmb","scb","citi","smbc","sc","cimb","uob","bay","mega","boa","cacib","gsb","hsbc","db","ghb","baac","mb","bnp","tbank","ibank","tisco","kk","icbc","tcrb","lhb"], + "payment_methods":[ + { + "object":"payment_method", + "name":"card", + "currencies":["THB","JPY","USD","EUR","GBP","SGD"], + "card_brands":["JCB","Visa","MasterCard","American Express","Diners Club"], + "installment_terms":null + }, + { + "object":"payment_method", + "name":"installment_bay", + "currencies":["THB"], + "card_brands":null, + "installment_terms":[3,4,6,9,10] + }, + { + "object":"payment_method", + "name":"installment_first_choice", + "currencies":["THB"], + "card_brands":null, + "installment_terms":[3,4,6,9,10,12,18,24,36] + }, + { + "object":"payment_method", + "name":"installment_kbank", + "currencies":["THB"], + "card_brands":null, + "installment_terms":[3,4,6,10] + }, + { + "object":"payment_method", + "name":"installment_bbl", + "currencies":["THB"], + "card_brands":null, + "installment_terms":[4,6,8,9,10] + }, + { + "object":"payment_method", + "name":"installment_ktc", + "currencies":["THB"], + "card_brands":null, + "installment_terms":[3,4,5,6,7,8,9,10] + } + ], + "zero_interest_installments":false +} diff --git a/tests/fixtures/2019-05-29/api.omise.co/charges-post.json b/tests/fixtures/2019-05-29/api.omise.co/charges-post.json new file mode 100644 index 00000000..15a1de25 --- /dev/null +++ b/tests/fixtures/2019-05-29/api.omise.co/charges-post.json @@ -0,0 +1,4 @@ +{ + "object": "charge", + "id": "chrg_test_fixture" +} diff --git a/tests/fixtures/2019-05-29/api.omise.co/charges/chrg_test_fixture-get.json b/tests/fixtures/2019-05-29/api.omise.co/charges/chrg_test_fixture-get.json new file mode 100644 index 00000000..20f6bcf4 --- /dev/null +++ b/tests/fixtures/2019-05-29/api.omise.co/charges/chrg_test_fixture-get.json @@ -0,0 +1,5 @@ +{ + "object": "charge", + "id": "chrg_test_fixture", + "description": "mock" +} diff --git a/tests/fixtures/2019-05-29/api.omise.co/charges/chrg_test_fixture-patch.json b/tests/fixtures/2019-05-29/api.omise.co/charges/chrg_test_fixture-patch.json new file mode 100644 index 00000000..20f6bcf4 --- /dev/null +++ b/tests/fixtures/2019-05-29/api.omise.co/charges/chrg_test_fixture-patch.json @@ -0,0 +1,5 @@ +{ + "object": "charge", + "id": "chrg_test_fixture", + "description": "mock" +} diff --git a/tests/fixtures/2019-05-29/api.omise.co/customers/cust_test_fixture-delete.json b/tests/fixtures/2019-05-29/api.omise.co/customers/cust_test_fixture-delete.json new file mode 100644 index 00000000..e693f2c9 --- /dev/null +++ b/tests/fixtures/2019-05-29/api.omise.co/customers/cust_test_fixture-delete.json @@ -0,0 +1,8 @@ +{ + "object": "customer", + "id": "cust_test_fixture", + "livemode": false, + "location": "/customers/cust_test_5gbl3cn1rii7j2g2yzz", + "created_at": "2019-06-22T13:28:03Z", + "deleted": true +} diff --git a/tests/fixtures/2019-05-29/api.omise.co/events-get.json b/tests/fixtures/2019-05-29/api.omise.co/events-get.json new file mode 100644 index 00000000..d7e10624 --- /dev/null +++ b/tests/fixtures/2019-05-29/api.omise.co/events-get.json @@ -0,0 +1,23 @@ +{ + "object": "list", + "from": "1970-01-01T00:00:00Z", + "to": "2019-06-23T14:08:45Z", + "offset": 0, + "limit": 3, + "total": 1, + "order": "chronological", + "location": "/events", + "data": [ + { + "object": "event", + "id": "evnt_test_531gmadallmweult89k", + "livemode": false, + "location": "/events/evnt_test_531gmadallmweult89k", + "created_at": "2017-11-01T18:16:24Z", + "key": "charge.create", + "data": { + "object": "charge" + } + } + ] +} diff --git a/tests/fixtures/2019-05-29/api.omise.co/forex/usd-get.json b/tests/fixtures/2019-05-29/api.omise.co/forex/usd-get.json new file mode 100644 index 00000000..4ec2de96 --- /dev/null +++ b/tests/fixtures/2019-05-29/api.omise.co/forex/usd-get.json @@ -0,0 +1,8 @@ +{ + "object": "forex", + "location": "/forex/usd", + "livemode": false, + "base": "USD", + "quote": "THB", + "rate": 29.55656335 +} diff --git a/tests/omise/AccountTest.php b/tests/omise/AccountTest.php index 75143099..caff3f7a 100644 --- a/tests/omise/AccountTest.php +++ b/tests/omise/AccountTest.php @@ -1,41 +1,30 @@ assertTrue(method_exists('OmiseAccount', 'retrieve')); - $this->assertTrue(method_exists('OmiseAccount', 'reload')); - $this->assertTrue(method_exists('OmiseAccount', 'getUrl')); - } +use Omise\Account; +class AccountTest extends TestConfig +{ /** * @test - * Assert that an account object is returned after a successful retrieve. */ - public function retrieve_omise_account_object() + public function retrieve() { - $account = OmiseAccount::retrieve(); + $account = Account::retrieve(); - $this->assertArrayHasKey('object', $account); - $this->assertEquals('account', $account['object']); + $this->assertSame('account', $account['object']); + $this->assertSame('account_test_fixture', $account['id']); } /** * @test - * Assert that an account object is returned after a successful reload. */ public function reload() { $account = OmiseAccount::retrieve(); $account->reload(); - $this->assertArrayHasKey('object', $account); - $this->assertEquals('account', $account['object']); + $this->assertSame('account', $account['object']); + $this->assertSame('account_test_fixture', $account['id']); } } diff --git a/tests/omise/ApiRequestorTest.php b/tests/omise/ApiRequestorTest.php index c568599d..a1f49f0c 100644 --- a/tests/omise/ApiRequestorTest.php +++ b/tests/omise/ApiRequestorTest.php @@ -1,16 +1,18 @@ apiRequestor = new \Omise\ApiRequestor; + $this->request = new ApiRequestor; } /** @@ -19,14 +21,13 @@ public function setUp() public function make_get_request() { // This endpoint will be resolved to a json-mock file, - // test/fixtures/api.omise.co/account-get.json + // test/fixtures/*api-version*/api.omise.co/account-get.json $url = \Omise\ApiRequestor::OMISE_API_URL . 'account'; - $result = $this->apiRequestor->get($url, 'secretkey'); + $result = $this->request->get($url, 'skey'); - $this->assertArrayHasKey('id', $result); - $this->assertSame($result['object'], 'account'); - $this->assertSame($result['id'], 'acct_4yyvy93tmab34q2ywlo'); + $this->assertSame('account', $result['object']); + $this->assertSame('account_test_fixture', $result['id']); } /** @@ -35,14 +36,13 @@ public function make_get_request() public function make_post_request() { // This endpoint will be resolved to a json-mock file, - // test/fixtures/api.omise.co/charges-post.json + // test/fixtures/*api-version*/api.omise.co/charges-post.json $url = \Omise\ApiRequestor::OMISE_API_URL . 'charges'; - $result = $this->apiRequestor->post($url, 'secretkey', array('amount' => 100000)); + $result = $this->request->post($url, 'skey', array('amount' => 100000)); - $this->assertArrayHasKey('id', $result); - $this->assertSame($result['object'], 'charge'); - $this->assertSame($result['id'], 'chrg_test_4zmrjgxdh4ycj2qncoj'); + $this->assertSame('charge', $result['object']); + $this->assertSame('chrg_test_fixture', $result['id']); } /** @@ -51,14 +51,13 @@ public function make_post_request() public function make_patch_request() { // This endpoint will be resolved to a json-mock file, - // test/fixtures/api.omise.co/charges/chrg_test_4zmrjgxdh4ycj2qncoj-patch.json - $url = \Omise\ApiRequestor::OMISE_API_URL . 'charges/chrg_test_4zmrjgxdh4ycj2qncoj'; + // test/fixtures/*api-version*/api.omise.co/charges/chrg_test_fixture-patch.json + $url = \Omise\ApiRequestor::OMISE_API_URL . 'charges/chrg_test_fixture'; - $result = $this->apiRequestor->patch($url, 'secretkey', array('description' => 'mock')); + $result = $this->request->patch($url, 'skey', array('description' => 'mock')); - $this->assertArrayHasKey('id', $result); - $this->assertSame($result['object'], 'charge'); - $this->assertSame($result['id'], 'chrg_test_4zmrjgxdh4ycj2qncoj'); + $this->assertSame('charge', $result['object']); + $this->assertSame('chrg_test_fixture', $result['id']); } /** @@ -67,10 +66,10 @@ public function make_patch_request() public function make_delete_request() { // This endpoint will be resolved to a json-mock file, - // test/fixtures/api.omise.co/customers/cust_test_4zmrjg2hct06ybwobqc-delete.json - $url = \Omise\ApiRequestor::OMISE_API_URL . 'customers/cust_test_4zmrjg2hct06ybwobqc'; + // test/fixtures/api.omise.co/*api-version*/customers/cust_test_fixture-delete.json + $url = \Omise\ApiRequestor::OMISE_API_URL . 'customers/cust_test_fixture'; - $result = $this->apiRequestor->delete($url, 'secretkey'); + $result = $this->request->delete($url, 'skey'); $this->assertArrayHasKey('id', $result); $this->assertSame($result['object'], 'customer'); @@ -84,6 +83,6 @@ public function make_delete_request() */ public function make_unsupported_method_request() { - $this->apiRequestor->got('http://test', 'secretkey'); + $this->request->got('http://test', 'skey'); } } diff --git a/tests/omise/BalanceTest.php b/tests/omise/BalanceTest.php index ec4b627f..d19aba0a 100644 --- a/tests/omise/BalanceTest.php +++ b/tests/omise/BalanceTest.php @@ -1,41 +1,30 @@ assertTrue(method_exists('OmiseBalance', 'retrieve')); - $this->assertTrue(method_exists('OmiseBalance', 'reload')); - $this->assertTrue(method_exists('OmiseBalance', 'getUrl')); - } +use Omise\Balance; +class BalanceTest extends TestConfig +{ /** * @test - * Assert that a balance object is returned after a successful retrieve. */ - public function retrieve_omise_balance_object() + public function retrieve() { - $balance = OmiseBalance::retrieve(); + $balance = Balance::retrieve(); - $this->assertArrayHasKey('object', $balance); - $this->assertEquals('balance', $balance['object']); + $this->assertSame('balance', $balance['object']); + $this->assertSame(51861467, $balance['transferable']); } /** * @test - * Assert that a balance object is returned after a successful reload. */ public function reload() { - $balance = OmiseBalance::retrieve(); + $balance = Balance::retrieve(); $balance->reload(); - $this->assertArrayHasKey('object', $balance); - $this->assertEquals('balance', $balance['object']); + $this->assertSame('balance', $balance['object']); + $this->assertSame(51861467, $balance['transferable']); } } diff --git a/tests/omise/CapabilitiesTest.php b/tests/omise/CapabilitiesTest.php index 1329f056..e256d26e 100644 --- a/tests/omise/CapabilitiesTest.php +++ b/tests/omise/CapabilitiesTest.php @@ -1,6 +1,8 @@ capabilities = OmiseCapabilities::retrieve(); - } - - /** - * @test - * OmiseCapabilities class must be contain some method below. - */ - public function method_exists() - { - $this->assertTrue(method_exists('OmiseCapabilities', 'retrieve')); - $this->assertTrue(method_exists('OmiseCapabilities', 'reload')); + $this->capabilities = Capabilities::retrieve(); } /** * @test - * Assert that a capabilities object is returned after a successful retrieve. */ public function retrieve_omise_capabilities_object() { @@ -34,7 +25,6 @@ public function retrieve_omise_capabilities_object() /** * @test - * Assert that a capabilities object is returned after a successful reload. */ public function reload() { diff --git a/tests/omise/ChargeTest.php b/tests/omise/ChargeTest.php index 26648b91..d623c916 100644 --- a/tests/omise/ChargeTest.php +++ b/tests/omise/ChargeTest.php @@ -2,32 +2,15 @@ class ChargeTest extends TestConfig { - /** - * @test - * OmiseCharge class must be contain some method below. - */ - public function method_exists() - { - $this->assertTrue(method_exists('OmiseCharge', 'reload')); - $this->assertTrue(method_exists('OmiseCharge', 'create')); - $this->assertTrue(method_exists('OmiseCharge', 'update')); - $this->assertTrue(method_exists('OmiseCharge', 'capture')); - $this->assertTrue(method_exists('OmiseCharge', 'reverse')); - $this->assertTrue(method_exists('OmiseCharge', 'refund')); - $this->assertTrue(method_exists('OmiseCharge', 'refunds')); - $this->assertTrue(method_exists('OmiseCharge', 'getUrl')); - } - /** * @test * Assert that a list of charge object could be successfully retrieved. */ - public function retrieve_charge_list_object() + public function retrieve_charge_collection() { - $charge = OmiseCharge::retrieve(); + $charge = OmiseCharge::all(); - $this->assertArrayHasKey('object', $charge); - $this->assertEquals('list', $charge['object']); + $this->assertEquals('charge', $charge['collection']); } /** @@ -36,11 +19,15 @@ public function retrieve_charge_list_object() */ public function create() { - $charge = OmiseCharge::create(array('amount' => 100000, - 'currency' => 'thb', - 'description' => 'Order-384', - 'ip' => '127.0.0.1', - 'card' => 'tokn_test_4zmrjhuk2rndz24a6x0')); + $params = array( + 'amount' => 100000, + 'currency' => 'thb', + 'description' => 'Order-384', + 'ip' => '127.0.0.1', + 'card' => 'tokn_test_fixture' + ); + + $charge = OmiseCharge::create($params); $this->assertArrayHasKey('object', $charge); $this->assertEquals('charge', $charge['object']); @@ -52,7 +39,7 @@ public function create() */ public function retrieve_specific_charge_object() { - $charge = OmiseCharge::retrieve('chrg_test_4zmrjgxdh4ycj2qncoj'); + $charge = OmiseCharge::retrieve('chrg_test_fixture'); $this->assertArrayHasKey('object', $charge); $this->assertEquals('charge', $charge['object']); @@ -64,11 +51,12 @@ public function retrieve_specific_charge_object() */ public function update() { - $charge = OmiseCharge::retrieve('chrg_test_4zmrjgxdh4ycj2qncoj'); - $charge->update(array('description' => 'Another description')); + $charge = OmiseCharge::retrieve('chrg_test_fixture'); + $charge->update(array('description' => 'mock')); $this->assertArrayHasKey('object', $charge); $this->assertEquals('charge', $charge['object']); + $this->assertEquals('mock', $charge['description']); } /** @@ -80,12 +68,12 @@ public function update() */ public function capture() { - $charge = OmiseCharge::retrieve('chrg_test_4zmrjgxdh4ycj2qncoj'); + $charge = OmiseCharge::retrieve('chrg_test_fixture'); $charge->capture(); $this->assertArrayHasKey('object', $charge); $this->assertEquals('charge', $charge['object']); - $this->assertTrue($charge['captured']); + $this->assertTrue($charge['paid']); } /** diff --git a/tests/omise/ForexTest.php b/tests/omise/ForexTest.php index 16b510d8..3cd93a08 100644 --- a/tests/omise/ForexTest.php +++ b/tests/omise/ForexTest.php @@ -22,7 +22,7 @@ public function retrieve() $this->assertArrayHasKey('object', $forex); $this->assertEquals('forex', $forex['object']); - $this->assertEquals('usd', $forex['from']); + $this->assertEquals('USD', $forex['base']); } /** @@ -35,6 +35,6 @@ public function reload() $this->assertArrayHasKey('object', $forex); $this->assertEquals('forex', $forex['object']); - $this->assertEquals('usd', $forex['from']); + $this->assertEquals('USD', $forex['base']); } } diff --git a/tests/omise/TestConfig.php b/tests/omise/TestConfig.php index bab6e2a6..8892bc76 100644 --- a/tests/omise/TestConfig.php +++ b/tests/omise/TestConfig.php @@ -1,17 +1,13 @@ = 0 && file_exists(dirname(__FILE__).'/../../vendor/autoload.php')) { - require_once dirname(__FILE__).'/../../vendor/autoload.php'; -} else { - require_once dirname(__FILE__).'/../../lib/Omise.php'; -} +\Omise\Omise::setPublicKey('pkey'); +\Omise\Omise::setSecretKey('skey'); +\Omise\Omise::setApiVersion('2019-05-29'); +\Omise\Omise::setClient('\Omise\Client\UnitTestClient'); abstract class TestConfig extends PHPUnit_Framework_TestCase { - + // ... } diff --git a/tests/omise/TransactionTest.php b/tests/omise/TransactionTest.php index 8675a7d2..817a4e68 100644 --- a/tests/omise/TransactionTest.php +++ b/tests/omise/TransactionTest.php @@ -1,17 +1,18 @@ assertTrue(method_exists('OmiseTransaction', 'retrieve')); - $this->assertTrue(method_exists('OmiseTransaction', 'reload')); - $this->assertTrue(method_exists('OmiseTransaction', 'getUrl')); + $this->assertTrue(method_exists('Omise\Transaction', 'retrieve')); + $this->assertTrue(method_exists('Omise\Transaction', 'reload')); + $this->assertTrue(method_exists('Omise\Transaction', 'getUrl')); } /**