diff --git a/CHANGELOG.md b/CHANGELOG.md index 352d146..9ede4db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ### Changelog +### 0.3.0 - 11 September 2020 +- Allow use of oauth token + + ### 0.2.3 - 27 March 2020 - Allow option to set `tld` property - top level domain `com` (default), `eu`, `in`, `com.au` for API host diff --git a/README.md b/README.md index b371d8c..a30bb5c 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,27 @@ use SudiptoChoudhury\Zoho\Subscriptions\Api; ... +$subscriptions = new Api([ + 'oauthtoken' => '<>', // https://www.zoho.com/subscriptions/api/v1/#oauth + 'zohoOrgId' => '<>', + ]); + + +$resultJson = $subscriptions->addCustomer($data); // create a customer + +$resultJson = $subscriptions->addSubscription($data); // create a subscription + + +``` + +> Auth Token (Will be deprecated soon), but you can still use it. + +```php + +use SudiptoChoudhury\Zoho\Subscriptions\Api; + +... + $subscriptions = new Api([ 'authtoken' => '<>', // https://accounts.zoho.com/apiauthtoken/nb/create 'zohoOrgId' => '<>', @@ -52,10 +73,39 @@ composer require sudiptochoudhury/php-zoho-subscriptions ``` -## Setting up +## Setting up Authentication + +> **Get Organization ID** Before you start, read https://www.zoho.com/subscriptions/api/v1/#organization-id to know your Organization ID. + + +### Setup OAuth and use oauth token + +Follow instructions from https://www.zoho.com/subscriptions/api/v1/#oauth and get the `oathtoken`. + +Pass the oauth token via the constructor. + +```php + +use SudiptoChoudhury\Zoho\Subscriptions\Api; + + +new Api([ + 'oauthtoken' => '<>', // https://www.zoho.com/subscriptions/api/v1/#oauth + 'zohoOrgId' => '<>', +]); +``` + + +### Or Use AuthToken +> will be deprecated soon + +Crete and retrieve AuthToken from https://accounts.zoho.com/apiauthtoken/nb/create and set the AuthToken and Organization ID in the constructor. -All you need to do is to retrieve from your Zoho account and set the AuthToken and Organization ID in the constructor. ```php + +use SudiptoChoudhury\Zoho\Subscriptions\Api; + + new Api([ 'authtoken' => '<>', // https://accounts.zoho.com/apiauthtoken/nb/create 'zohoOrgId' => '<>', diff --git a/composer.json b/composer.json index 4348a2d..f543c49 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ }, "autoload": { "psr-4": { - "SudiptoChoudhury\\": "src/" + "SudiptoChoudhury\\Zoho\\Subscriptions\\": "src/Zoho/Subscriptions" } }, "autoload-dev": { diff --git a/src/zoho/subscriptions/Api.php b/src/Zoho/Subscriptions/Api.php similarity index 95% rename from src/zoho/subscriptions/Api.php rename to src/Zoho/Subscriptions/Api.php index 8706c98..cbf4931 100644 --- a/src/zoho/subscriptions/Api.php +++ b/src/Zoho/Subscriptions/Api.php @@ -185,6 +185,9 @@ */ class Api extends ApiForge { + // deprecated Authorization header + const AUTHTOKEN_HEADER = 'Zoho-authtoken {{authtoken}}'; + protected $loggerFile = __DIR__ . '/zoho-subscriptions-api-calls.log'; protected $DEFAULT_API_JSON_PATH = './config/subscriptions.json'; @@ -193,13 +196,14 @@ class Api extends ApiForge protected $DEFAULTS = [ 'authtoken' => '', + 'oauthtoken' => '', 'zohoOrgId' => '', 'tld' => 'com', // eu, in, com.au 'client' => [ 'base_uri' => 'https://subscriptions.zoho.{{tld}}/api/v1/', 'verify' => false, 'headers' => [ - 'Authorization' => 'Zoho-authtoken {{authtoken}}', + 'Authorization' => 'Zoho-oauthtoken {{oauthtoken}}', 'X-com-zoho-subscriptions-organizationid' => "{{zohoOrgId}}", ], ], @@ -210,5 +214,25 @@ class Api extends ApiForge ], ]; + /** + * @param array $options + * + * @return \SudiptoChoudhury\Zoho\Subscriptions\Api + */ + protected function setOptions($options = []) + { + // authtoken is deprecated, apply it until discontinued + $authtoken = $options['authtoken'] ?? null; + if ($authtoken) { + $headers = $this->DEFAULTS['client']['headers'] ?? null; + if ($headers) { + $auth = $headers['Authorization'] ?? null; + if ($auth) { + $this->DEFAULTS['client']['headers']['Authorization'] = static::AUTHTOKEN_HEADER; + } + } + } + return parent::setOptions($options); + } } \ No newline at end of file diff --git a/src/zoho/subscriptions/Import.php b/src/Zoho/Subscriptions/Import.php similarity index 100% rename from src/zoho/subscriptions/Import.php rename to src/Zoho/Subscriptions/Import.php diff --git a/src/zoho/subscriptions/config/postman.json b/src/Zoho/Subscriptions/config/postman.json similarity index 100% rename from src/zoho/subscriptions/config/postman.json rename to src/Zoho/Subscriptions/config/postman.json diff --git a/src/zoho/subscriptions/config/subscriptions.json b/src/Zoho/Subscriptions/config/subscriptions.json similarity index 100% rename from src/zoho/subscriptions/config/subscriptions.json rename to src/Zoho/Subscriptions/config/subscriptions.json diff --git a/src/zoho/subscriptions/config/subscriptions.php b/src/Zoho/Subscriptions/config/subscriptions.php similarity index 100% rename from src/zoho/subscriptions/config/subscriptions.php rename to src/Zoho/Subscriptions/config/subscriptions.php