Skip to content

Commit f64ab27

Browse files
author
Sudipto Choudhury
committed
Allow use of oauth token
1 parent eefdc1a commit f64ab27

File tree

8 files changed

+82
-4
lines changed

8 files changed

+82
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
### Changelog
22

3+
### 0.3.0 - 11 September 2020
4+
- Allow use of oauth token
5+
6+
37
### 0.2.3 - 27 March 2020
48
- Allow option to set `tld` property - top level domain `com` (default), `eu`, `in`, `com.au` for API host
59

README.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@ use SudiptoChoudhury\Zoho\Subscriptions\Api;
1515

1616
...
1717

18+
$subscriptions = new Api([
19+
'oauthtoken' => '<<Zoho Subscriptions OAuth Token>>', // https://www.zoho.com/subscriptions/api/v1/#oauth
20+
'zohoOrgId' => '<<Zoho Organization ID>>',
21+
]);
22+
23+
24+
$resultJson = $subscriptions->addCustomer($data); // create a customer
25+
26+
$resultJson = $subscriptions->addSubscription($data); // create a subscription
27+
28+
29+
```
30+
31+
> Auth Token (Will be deprecated soon), but you can still use it.
32+
33+
```php
34+
35+
use SudiptoChoudhury\Zoho\Subscriptions\Api;
36+
37+
...
38+
1839
$subscriptions = new Api([
1940
'authtoken' => '<<Zoho Subscriptions Auth Token>>', // https://accounts.zoho.com/apiauthtoken/nb/create
2041
'zohoOrgId' => '<<Zoho Organization ID>>',
@@ -52,10 +73,39 @@ composer require sudiptochoudhury/php-zoho-subscriptions
5273
```
5374

5475

55-
## Setting up
76+
## Setting up Authentication
77+
78+
> **Get Organization ID** Before you start, read https://www.zoho.com/subscriptions/api/v1/#organization-id to know your Organization ID.
79+
80+
81+
### Setup OAuth and use oauth token
82+
83+
Follow instructions from https://www.zoho.com/subscriptions/api/v1/#oauth and get the `oathtoken`.
84+
85+
Pass the oauth token via the constructor.
86+
87+
```php
88+
89+
use SudiptoChoudhury\Zoho\Subscriptions\Api;
90+
91+
92+
new Api([
93+
'oauthtoken' => '<<Zoho Subscriptions OAuth Token>>', // https://www.zoho.com/subscriptions/api/v1/#oauth
94+
'zohoOrgId' => '<<Zoho Organization ID>>',
95+
]);
96+
```
97+
98+
99+
### Or Use AuthToken
100+
> will be deprecated soon
101+
102+
Crete and retrieve AuthToken from https://accounts.zoho.com/apiauthtoken/nb/create and set the AuthToken and Organization ID in the constructor.
56103

57-
All you need to do is to retrieve from your Zoho account and set the AuthToken and Organization ID in the constructor.
58104
```php
105+
106+
use SudiptoChoudhury\Zoho\Subscriptions\Api;
107+
108+
59109
new Api([
60110
'authtoken' => '<<Zoho Subscriptions Auth Token>>', // https://accounts.zoho.com/apiauthtoken/nb/create
61111
'zohoOrgId' => '<<Zoho Organization ID>>',

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"autoload": {
2525
"psr-4": {
26-
"SudiptoChoudhury\\": "src/"
26+
"SudiptoChoudhury\\Zoho\\Subscriptions\\": "src/Zoho/Subscriptions"
2727
}
2828
},
2929
"autoload-dev": {

src/zoho/subscriptions/Api.php renamed to src/Zoho/Subscriptions/Api.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@
185185
*/
186186
class Api extends ApiForge
187187
{
188+
// deprecated Authorization header
189+
const AUTHTOKEN_HEADER = 'Zoho-authtoken {{authtoken}}';
190+
188191
protected $loggerFile = __DIR__ . '/zoho-subscriptions-api-calls.log';
189192

190193
protected $DEFAULT_API_JSON_PATH = './config/subscriptions.json';
@@ -193,13 +196,14 @@ class Api extends ApiForge
193196

194197
protected $DEFAULTS = [
195198
'authtoken' => '',
199+
'oauthtoken' => '',
196200
'zohoOrgId' => '',
197201
'tld' => 'com', // eu, in, com.au
198202
'client' => [
199203
'base_uri' => 'https://subscriptions.zoho.{{tld}}/api/v1/',
200204
'verify' => false,
201205
'headers' => [
202-
'Authorization' => 'Zoho-authtoken {{authtoken}}',
206+
'Authorization' => 'Zoho-oauthtoken {{oauthtoken}}',
203207
'X-com-zoho-subscriptions-organizationid' => "{{zohoOrgId}}",
204208
],
205209
],
@@ -210,5 +214,25 @@ class Api extends ApiForge
210214
],
211215
];
212216

217+
/**
218+
* @param array $options
219+
*
220+
* @return \SudiptoChoudhury\Zoho\Subscriptions\Api
221+
*/
222+
protected function setOptions($options = [])
223+
{
224+
// authtoken is deprecated, apply it until discontinued
225+
$authtoken = $options['authtoken'] ?? null;
226+
if ($authtoken) {
227+
$headers = $this->DEFAULTS['client']['headers'] ?? null;
228+
if ($headers) {
229+
$auth = $headers['Authorization'] ?? null;
230+
if ($auth) {
231+
$this->DEFAULTS['client']['headers']['Authorization'] = static::AUTHTOKEN_HEADER;
232+
}
233+
}
234+
}
235+
return parent::setOptions($options);
236+
}
213237

214238
}
File renamed without changes.

0 commit comments

Comments
 (0)