Created
December 21, 2018 07:01
-
-
Save sabril/bef1b26c34ced836f9f91147a62b951d to your computer and use it in GitHub Desktop.
Zoho Invoide Service using Oauth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'oauth2' | |
require 'json' | |
module ZohoServices | |
class Invoice | |
attr_reader :oauth_auth_code, :client | |
def initialize | |
@customer_id = "CUSTOMER_ID" # define this customer_id | |
@query_root = "https://invoice.zoho.com/api/v3" | |
@organization_id = "ORGANIZATION_ID" # define this organization_id | |
@client = OAuth2::Client.new("CLIENT_ID", "CLIENT_SECRET", { | |
:site => 'https://accounts.zoho.com', authorize_url: 'oauth/v2/auth', token_url: 'oauth/v2/token' | |
}) | |
@redirect_url = 'https://example.com/oauth2callback' | |
end | |
def get_authorize_url | |
url = @client.auth_code.authorize_url({ | |
:scope => 'ZohoInvoice.invoices.CREATE,ZohoInvoice.invoices.READ,ZohoInvoice.invoices.UPDATE,ZohoInvoice.invoices.DELETE', | |
:redirect_uri => @redirect_url, | |
:access_type => 'offline' | |
}) | |
url #copy this url into browser | |
end | |
def set_access_token(code=nil) | |
@access_token = @client.auth_code.get_token(code || @oauth_auth_code, | |
{:redirect_uri => @redirect_url}) | |
@serialized_access_token = @access_token.to_hash.to_json | |
# save access token | |
your_model.update_columns({ | |
zoho_access_token: JSON.parse(@serialized_access_token)['access_token'], | |
zoho_expires_at: Time.now + JSON.parse(@serialized_access_token)['expires_in_sec'].to_i.seconds | |
}) | |
your_model.zoho_refresh_token = JSON.parse(@serialized_access_token)['refresh_token'] if JSON.parse(@serialized_access_token)['refresh_token'].present? | |
your_model.save | |
nil | |
end | |
def get(query, opts = {}) | |
refresh_access_token if expired? | |
response_json = @access_token.get("#{@query_root}/#{query}", opts).body | |
JSON.parse(response_json) | |
end | |
def post(query, opts = {}) | |
refresh_access_token if expired? | |
response_json = @access_token.post("#{@query_root}/#{query}", opts).body | |
JSON.parse(response_json) | |
end | |
def put(query, opts = {}) | |
response_json = @access_token.put("#{@query_root}/#{query}", opts).body | |
JSON.parse(response_json) | |
end | |
def restore_access_token | |
@access_token = OAuth2::AccessToken.from_hash @client, | |
{ | |
:refresh_token => your_model.zoho_refresh_token, | |
:access_token => your_model.zoho_access_token, | |
:expires_at => your_model.zoho_expires_at | |
} | |
nil | |
end | |
def expired? | |
restore_access_token unless @access_token | |
@access_token.expired? | |
end | |
def access_token_object | |
restore_access_token unless @access_token | |
@access_token | |
end | |
def access_token | |
restore_access_token unless @access_token | |
@access_token.token | |
end | |
def refresh_token | |
restore_access_token unless @access_token | |
@access_token.refresh_token | |
end | |
def refresh_access_token | |
restore_access_token unless @access_token | |
@access_token = @access_token.refresh! | |
@serialized_access_token = @access_token.to_hash.to_json | |
your_model.update_columns({ | |
zoho_access_token: JSON.parse(@serialized_access_token)['access_token'], | |
zoho_expires_at: Time.now + JSON.parse(@serialized_access_token)['expires_in_sec'].to_i.seconds | |
}) | |
your_model.zoho_refresh_token = JSON.parse(@serialized_access_token)['refresh_token'] if JSON.parse(@serialized_access_token)['refresh_token'].present? | |
your_model.save | |
nil | |
end | |
def invoices | |
get('invoices')["invoices"] | |
end | |
def find(invoice_id) | |
get("invoices/#{invoice_id}") | |
end | |
def find_by_reference_number(reference_number) | |
results = get("invoices?search_text=#{reference_number}") | |
results["invoices"].first | |
end | |
def create_invoice(order) | |
return false unless reservation | |
# specify fields to send here, reference https://www.zoho.com/invoice/api/v3/#Invoices_Create_an_invoice | |
body = { | |
"JSONString" => { | |
customer_id: @customer_id, | |
reference_number: order.id, | |
line_items: [ | |
name: order.name, | |
rate: order.total, | |
quantity: 1 | |
] | |
}.to_json | |
} | |
post("invoices", {body: body}) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment