Skip to content

Commit

Permalink
Allow use of oauth token
Browse files Browse the repository at this point in the history
  • Loading branch information
Sudipto Choudhury committed Sep 11, 2020
1 parent eefdc1a commit f64ab27
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
54 changes: 52 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ use SudiptoChoudhury\Zoho\Subscriptions\Api;

...

$subscriptions = new Api([
'oauthtoken' => '<<Zoho Subscriptions OAuth Token>>', // https://www.zoho.com/subscriptions/api/v1/#oauth
'zohoOrgId' => '<<Zoho Organization ID>>',
]);


$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' => '<<Zoho Subscriptions Auth Token>>', // https://accounts.zoho.com/apiauthtoken/nb/create
'zohoOrgId' => '<<Zoho Organization ID>>',
Expand Down Expand Up @@ -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' => '<<Zoho Subscriptions OAuth Token>>', // https://www.zoho.com/subscriptions/api/v1/#oauth
'zohoOrgId' => '<<Zoho Organization ID>>',
]);
```


### 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' => '<<Zoho Subscriptions Auth Token>>', // https://accounts.zoho.com/apiauthtoken/nb/create
'zohoOrgId' => '<<Zoho Organization ID>>',
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"autoload": {
"psr-4": {
"SudiptoChoudhury\\": "src/"
"SudiptoChoudhury\\Zoho\\Subscriptions\\": "src/Zoho/Subscriptions"
}
},
"autoload-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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}}",
],
],
Expand All @@ -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);
}

}
File renamed without changes.
File renamed without changes.

0 comments on commit f64ab27

Please sign in to comment.