Skip to content

Commit 45718c1

Browse files
authored
Wrapper for OAuth URLs (#242)
* Adding wrapper for oauth urls & updating auth mechanism * code for generating onboarding signature * Documentation udpate * Adding test cases for razorpay setup * Adding validations & test cases * adding test cases * Update oauth_token.md * Updating documentation * Update validation_config.rb
1 parent 9423e2b commit 45718c1

15 files changed

+661
-10
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ Ruby 2.6.8 or later
2727

2828
Remember to `require 'razorpay'` before anything else.
2929

30-
Next, you need to setup your key and secret using the following:
30+
Next, you need to setup your auth details. This setup can be done via two ways:
31+
32+
### Using Private Auth
33+
34+
you need to setup your key and secret using the following:
3135

3236
```rb
3337
Razorpay.setup('key_id', 'key_secret')
@@ -38,6 +42,16 @@ You can set customer headers for your requests using the following:
3842
Razorpay.headers = {"CUSTOM_APP_HEADER" => "CUSTOM_VALUE"}
3943
```
4044

45+
### Using Access Token
46+
you need to setup your access token using the following
47+
```rb
48+
Razorpay.setup_with_oauth('access_token')
49+
```
50+
You can set customer headers for your requests using the following:
51+
```rb
52+
Razorpay.headers = {"CUSTOM_APP_HEADER" => "CUSTOM_VALUE"}
53+
```
54+
4155
You can find your API keys at <https://dashboard.razorpay.com/#/app/keys>.
4256

4357
If you are using rails, the right place to do this might be `config/initializers/razorpay.rb`.
@@ -70,6 +84,7 @@ If you are using rails, the right place to do this might be `config/initializers
7084
- [Register NACH and Charge First Payment Together](documents/registerNach.md)
7185
- [Payment Verification](documents/paymentVerification.md)
7286
- [Webhook](documents/webhook.md)
87+
- [OAuthToken](documents/oauth_token.md)
7388

7489
## Development
7590

documents/oauth_token.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
### OAuthToken
2+
3+
```rb
4+
require "razorpay"
5+
```
6+
7+
### Generate Authorize Url
8+
```rb
9+
body = {
10+
submerchant_id: '<SUBMERCHANT_MID>',
11+
timestamp: Time.now.to_i
12+
}
13+
onboarding_signature = Razorpay::Utility.generate_onboarding_signature(body, '<YOUR_CLIENT_SECRET>')
14+
15+
options = {
16+
'client_id' => '<YOUR_CLIENT_ID>',
17+
'redirect_uri' => 'https://example.com/razorpay_callback',
18+
'scopes' => ["read_write"],
19+
'state' => 'NOBYtv8r6c75ex6WZ',
20+
'onboarding_signature' => onboarding_signature
21+
}
22+
authorize_url = Razorpay::OAuthToken.get_auth_url(options)
23+
```
24+
25+
**Parameters:**
26+
27+
| Name | Type | Description |
28+
|----------------------|--------|---------------------------------------------------------------------------------------------------------------------------------------------------------|
29+
| client_id* | string | Unique client identifier. |
30+
| redirect_uri* | string | Callback URL used by Razorpay to redirect after the user approves or denies the authorisation request. The client should whitelist the 'redirect_uri'. |
31+
| scopes* | array | Defines what access your application is requesting from the user. You can request one or multiple scopes by adding them to an array as indicated above. |
32+
| state* | string | A random string generated by your service. This parameter helps prevent cross-site request forgery (CSRF) attacks. |
33+
| onboarding_signature | string | A cryptographic string generated by your service using generateOnboardingSignature method in Utils class. Only applicable for accounts created with pre-fill KYC |
34+
35+
**Response:**
36+
```
37+
"https://auth.razorpay.com/authorize?response_type=code&client_id=<YOUR_CLIENT_ID>&redirect_uri=https:%2F%2Fexample.com%2Frazorpay_callback&scope[]=read_only&scope[]=rx_read_write&state=NOBYtv8r6c75ex6WZ&onboarding_signature=<GENERATED_ONBOARDING_SIGNATURE>"
38+
```
39+
40+
-------------------------------------------------------------------------------------------------------
41+
### Get Access token
42+
```rb
43+
options = {
44+
'client_id' => '<YOUR_CLIENT_ID>',
45+
'client_secret' => '<YOUR_CLIENT_SECRET>',
46+
'redirect_uri' => 'https://example.com/razorpay_callback',
47+
'grant_type' => 'authorization_code',
48+
'code' => '<AUTHORIZATION_CODE>',
49+
'mode' => 'test'
50+
}
51+
oauth_token = Razorpay::OAuthToken.get_access_token(options)
52+
```
53+
54+
**Parameters:**
55+
56+
| Name | Type | Description |
57+
|----------------|--------|------------------------------------------------------------------------------------------------------------------------------|
58+
| client_id* | string | Unique client identifier. |
59+
| client_secret* | string | Client secret string. |
60+
| redirect_uri* | string | Specifies the same redirect_uri used in the authorisation request. |
61+
| grant_type* | string | Defines the grant type for the request. Possible value are:<ul><li>authorization_code</li><li>client_credentials</li></ul> |
62+
| code* | string | Decoded authorisation code received in the last step. Note: Pass this parameter only when grant_type is 'authorization_code' |
63+
| mode | string | The type of mode. Possible values: <ul><li>test</li><li>live (default)</li></ul> |
64+
65+
**Response:**
66+
```json
67+
{
68+
"public_token": "rzp_test_oauth_9xu1rkZqoXlClS",
69+
"token_type": "Bearer",
70+
"expires_in": 7862400,
71+
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IkY1Z0NQYkhhRzRjcUpnIn0.eyJhdWQiOiJGNFNNeEgxanMxbkpPZiIsImp0aSI6IkY1Z0NQYkhhRzRjcUpnIiwiaWF0IjoxNTkyODMxMDExLCJuYmYiOjE1OTI4MzEwMTEsInN1YiI6IiIsImV4cCI6MTYwMDc3OTgxMSwidXNlcl9pZCI6IkYycVBpejJEdzRPRVFwIiwibWVyY2hhbnRfaWQiOiJGMnFQaVZ3N0lNV01GSyIsInNjb3BlcyI6WyJyZWFkX29ubHkiXX0.Wwqt5czhoWpVzP5_aoiymKXoGj-ydo-4A_X2jf_7rrSvk4pXdqzbA5BMrHxPdPbeFQWV6vsnsgbf99Q3g-W4kalHyH67LfAzc3qnJ-mkYDkFY93tkeG-MCco6GJW-Jm8xhaV9EPUak7z9J9jcdluu9rNXYMtd5qxD8auyRYhEgs",
72+
"refresh_token": "def50200f42e07aded65a323f6c53181d802cc797b62cc5e78dd8038d6dff253e5877da9ad32f463a4da0ad895e3de298cbce40e162202170e763754122a6cb97910a1f58e2378ee3492dc295e1525009cccc45635308cce8575bdf373606c453ebb5eb2bec062ca197ac23810cf9d6cf31fbb9fcf5b7d4de9bf524c89a4aa90599b0151c9e4e2fa08acb6d2fe17f30a6cfecdfd671f090787e821f844e5d36f5eacb7dfb33d91e83b18216ad0ebeba2bef7721e10d436c3984daafd8654ed881c581d6be0bdc9ebfaee0dc5f9374d7184d60aae5aa85385690220690e21bc93209fb8a8cc25a6abf1108d8277f7c3d38217b47744d7",
73+
"razorpay_account_id": "acc_Dhk2qDbmu6FwZH"
74+
}
75+
```
76+
77+
-------------------------------------------------------------------------------------------------------
78+
79+
### Get Access token using refresh token
80+
```rb
81+
options = {
82+
'client_id' => '<YOUR_CLIENT_ID>',
83+
'client_secret' => '<YOUR_CLIENT_SECRET>',
84+
'refresh_token' => 'def5020096e1c470c901d34cd60fa53abdaf3662sa0'
85+
}
86+
oauth_token = Razorpay::OAuthToken.refresh_token(options)
87+
```
88+
89+
**Parameters:**
90+
91+
| Name | Type | Description |
92+
|----------------|-----------|--------------------------------------------|
93+
| client_id* | string | Unique client identifier. |
94+
| client_secret* | string | Client secret string. |
95+
| refresh_token* | string | The previously-stored refresh token value. |
96+
97+
**Response:**
98+
```json
99+
{
100+
"public_token": "rzp_test_oauth_9xu1rkZqoXlClS",
101+
"token_type": "Bearer",
102+
"expires_in": 7862400,
103+
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6Ijl4dTF",
104+
"refresh_token": "def5020096e1c470c901d34cd60fa53abdaf36620e823ffa53"
105+
}
106+
```
107+
108+
-------------------------------------------------------------------------------------------------------
109+
110+
### Revoke a token
111+
```rb
112+
options = {
113+
'client_id' => '<YOUR_CLIENT_ID>',
114+
'client_secret' => '<YOUR_CLIENT_SECRET>',
115+
'token' => 'def5020096e1c470c901d34cd60fa53abdaf36620e823ffa53'
116+
'token_type_hint' => 'access_token'
117+
}
118+
response = Razorpay::OAuthToken.revoke_token(options)
119+
```
120+
121+
**Parameters:**
122+
123+
| Name | Type | Description |
124+
|------------------|----------|----------------------------------------------------------------------------------------------------------|
125+
| client_id* | string | Unique client identifier. |
126+
| client_secret* | string | Client secret string. |
127+
| token_type_hint* | string | The type of token for the request. Possible values: <ul><li>access_token</li><li>refresh_token</li></ul> |
128+
| token* | string | The token whose access should be revoked. |
129+
130+
**Response:**
131+
```json
132+
{
133+
"message": "Token Revoked"
134+
}
135+
```
136+
-------------------------------------------------------------------------------------------------------
137+
138+
**PN: * indicates mandatory fields**
139+
<br>
140+
<br>
141+
**For reference click [here](https://razorpay.com/docs/partners/platform/onboard-businesses/integrate-oauth/integration-steps)**
142+

lib/razorpay.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,22 @@
2626
require 'razorpay/product'
2727
require 'razorpay/stakeholder'
2828
require 'razorpay/account'
29+
require 'razorpay/oauth_token'
2930

3031
# Base Razorpay module
3132
module Razorpay
3233
class << self
33-
attr_accessor :auth, :custom_headers
34+
attr_accessor :auth, :custom_headers, :access_token, :auth_type
3435
end
3536

3637
def self.setup(key_id, key_secret)
3738
self.auth = { username: key_id, password: key_secret }
39+
self.auth_type = Razorpay::PRIVATE_AUTH
40+
end
41+
42+
def self.setup_with_oauth(access_token)
43+
self.access_token = access_token
44+
self.auth_type = Razorpay::OAUTH
3845
end
3946

4047
def self.headers=(headers = {})

lib/razorpay/constants.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
module Razorpay
33
BASE_URI = 'https://api.razorpay.com'.freeze
44
TEST_URL = 'https://api.razorpay.com/'.freeze
5-
VERSION = '3.2.1'.freeze
5+
VERSION = '3.2.1'.freeze
6+
AUTH_URL = 'https://auth.razorpay.com'.freeze
7+
API_HOST = 'API'.freeze
8+
AUTH_HOST = 'AUTH'.freeze
9+
PRIVATE_AUTH = 'Private'.freeze
10+
OAUTH = 'OAuth'.freeze
611
end

lib/razorpay/oauth_token.rb

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
require 'razorpay/request'
2+
require 'razorpay/entity'
3+
require 'razorpay/payload_validator'
4+
require 'razorpay/validation_config'
5+
6+
module Razorpay
7+
# OAuth APIs allow to you create and manage access tokens
8+
class OAuthToken < Entity
9+
def self.request
10+
Razorpay::Request.new('token', Razorpay::AUTH_HOST)
11+
end
12+
13+
def self.get_auth_url(options)
14+
validate_auth_url_request(options)
15+
uri = URI.join(Razorpay::AUTH_URL, '/authorize')
16+
17+
query_params = {
18+
'response_type' => 'code',
19+
'client_id' => options['client_id'],
20+
'redirect_uri' => options['redirect_uri'],
21+
'state' => options['state']
22+
}
23+
24+
options['scopes'].each { |scope| query_params["scope[]"] = scope }
25+
26+
if options.has_key?('onboarding_signature')
27+
query_params['onboarding_signature'] = options['onboarding_signature']
28+
end
29+
uri.query = URI.encode_www_form(query_params)
30+
uri.to_s
31+
end
32+
33+
def self.get_access_token(options)
34+
validate_access_token_request(options)
35+
r = request
36+
r.request :post, "/token", options
37+
end
38+
39+
def self.refresh_token(options)
40+
options['grant_type'] = 'refresh_token'
41+
validate_refresh_token_request(options)
42+
r = request
43+
r.request :post, "/token", options
44+
end
45+
46+
def self.revoke_token(options)
47+
validate_revoke_token_request(options)
48+
r = request
49+
r.request :post, "/revoke", options
50+
end
51+
52+
class << self
53+
54+
private
55+
56+
def validate_auth_url_request(options)
57+
Razorpay::PayloadValidator.validate(options, get_validations_for_auth_request_url)
58+
end
59+
60+
def validate_access_token_request(options)
61+
Razorpay::PayloadValidator.validate(options, get_validations_for_access_token_request)
62+
end
63+
64+
def validate_refresh_token_request(options)
65+
Razorpay::PayloadValidator.validate(options, get_validations_for_refresh_token_request)
66+
end
67+
68+
def validate_revoke_token_request(options)
69+
Razorpay::PayloadValidator.validate(options, get_validations_for_revoke_token_request)
70+
end
71+
72+
def get_validations_for_auth_request_url
73+
[
74+
Razorpay::ValidationConfig.new('client_id', [:id]),
75+
Razorpay::ValidationConfig.new('redirect_uri', [:non_empty_string, :url]),
76+
Razorpay::ValidationConfig.new('scopes', [:non_null]),
77+
Razorpay::ValidationConfig.new('state', [:non_empty_string])
78+
]
79+
end
80+
81+
def get_validations_for_access_token_request
82+
[
83+
Razorpay::ValidationConfig.new('client_id', [:id]),
84+
Razorpay::ValidationConfig.new('client_secret', [:non_empty_string]),
85+
Razorpay::ValidationConfig.new('redirect_uri', [:non_empty_string, :url]),
86+
Razorpay::ValidationConfig.new('grant_type', [:token_grant])
87+
]
88+
end
89+
90+
def get_validations_for_refresh_token_request
91+
[
92+
Razorpay::ValidationConfig.new('client_id', [:id]),
93+
Razorpay::ValidationConfig.new('client_secret', [:non_empty_string]),
94+
Razorpay::ValidationConfig.new('refresh_token', [:non_empty_string]),
95+
Razorpay::ValidationConfig.new('grant_type', [:token_grant])
96+
]
97+
end
98+
99+
def get_validations_for_revoke_token_request
100+
[
101+
Razorpay::ValidationConfig.new('client_id', [:id]),
102+
Razorpay::ValidationConfig.new('client_secret', [:non_empty_string]),
103+
Razorpay::ValidationConfig.new('token', [:non_empty_string]),
104+
Razorpay::ValidationConfig.new('token_type_hint', [:non_empty_string])
105+
]
106+
end
107+
end
108+
end
109+
end

0 commit comments

Comments
 (0)