diff --git a/app/models/license.rb b/app/models/license.rb new file mode 100755 index 00000000..b2682213 --- /dev/null +++ b/app/models/license.rb @@ -0,0 +1,192 @@ +class License + include HTTParty + + base_uri "secure.smartsales.asia/api" + + attr_accessor :name, :address_1, :address_2, :township, :city, :country, :email, :phone, :fax, :logo, :subdomain, + :plan_activation_date, :plan_next_renewal_date, :plan_max_products,:plan_max_customers, :plan_active_connections, + :dbhost, :dbschema, :dbusername, :dbpassword, :exchange_unqiue_id, :localqueue_host,:server_mode,:localhost_address, + :localqueue_user, :localqueue_password, :remotequeue_host, :remotequeue_user, :remotequeue_password, :api_token, :app_token + + @license = nil + @secret = nil + + def initialize(server = "", lookup = "") + #this code is hard-code to reflect server mode - Very important. + self.server_mode = "cloud" + + if (server != "") + self.class.base_uri server + end + + @secret = SecureRandom.hex(10) + @params = { query: { device: "SXlite", token: SECRETS_CONFIG['provision_key'] } } + end + + def shop_code + if ( self.subdomain.length > 3) + return self.subdomain[0,3].upcase + else + return self.subdomain.upcase + end + end + + def detail_with_local_cache(lookup, key) + ##Check from local redis - if available load local otherwise get from remote + cache_key = "store:license:#{key}:hostname" + + # No Needs for current + # @secret = key + + cache_license = nil + + ##Get redis connection from connection pool + Redis.current do |conn| + cache_license = conn.get(cache_key) + end + + Rails.logger.info "Cache key - " + cache_key.to_s + + if cache_license.nil? + ##change the d/e key + # @options = { query: {device: "SXlite", lookup: lookup, skey: @secret, token: SECRETS_CONFIG['provision_key']} } + @params = { query: { device: "SXlite", token: SECRETS_CONFIG['provision_key']} } + + response = self.class.get("/request_license", @params) + @license = response.parsed_response + + if (@license["status"] == true) + + assign() + + Rails.logger.info "License - " + response.parsed_response.to_s + + Redis.current do |conn| + ##Remote - store the remote response in local redis cache + conn.set(cache_key, Marshal.dump(@license)) + ##ADD to List to remove later + conn.sadd("License:cache:keys", cache_key) + end + + return true + end + + Rails.logger.info 'API License' + + else + + @license = Marshal.load(cache_license) if cache_license + + Rails.logger.info 'Cache License' + + if (@license["status"] == true) + assign() + return true + end + end + return false + end + + def detail + + response = self.class.get("/subdomain", @options) + @license = response.parsed_response + + Rails.logger.debug "License - " + response.parsed_response.to_s + + + if (@license["status"] == true) + assign() + + return true + end + + return false + end + + def check_remote_license(license_key) + # @options = { query: {device: "cloud", key: license_key, skey: @secret, token: Rails.application.secrets.provision_key} } + @options = { query: {device: "SXlite", key: license_key, skey: @secret, token: SECRETS_CONFIG['provision_key']} } + response = self.class.get("/license", @options) + + @license = response.parsed_response + + Rails.logger.debug "License Remote Response - " + response.parsed_response.to_s + if (@license["status"]) + assign() + end + return @license["status"] + + end + + def verify_by_api_token(api_token) + @options = { query: {device: "SXlite", api_token: api_token, skey: @secret, token: SECRETS_CONFIG['provision_key']} } + response = self.class.get("/verify", @options) + + @license = response.parsed_response + + Rails.logger.debug "License Remote Response - " + response.parsed_response.to_s + if (@license["status"]) + assign() + end + + return @license["status"] + end + #Load License is remove from the cloud license because - this license is must be validated against subdmain instead of license.data from file. + + + def expired? + if (self.plan_next_renewal_date < Date.today) + return true + else + return false + end + end + + private + def assign + # self.name = @license["name"] + # self.address_1 = @license["address_1"] + # self.address_2 = @license["address_2"] + # self.township = @license["township"] + # self.city = @license["city"] + # self.country = @license["country"] + # self.email = @license["email"] + # self.phone = @license["phone"] + # self.fax = @license["fax"] + # self.logo = @license["logo"] + # self.localhost_address = @license["localhost_address"] + # self.subdomain = @license["subdomain"] + # self.plan_activation_date = Date.parse(@license["plan_activation_date"]) + # self.plan_next_renewal_date = Date.parse(@license["plan_next_renewal_date"]) + + ## self.plan_activation_date = Date.strptime(@license["plan_activation_date"], "%Y-%m-%d") + ## self.plan_next_renewal_date = Date.strptime(@license["plan_next_renewal_date"], "%Y-%m-%d") + + + # self.plan_max_products = @license["plan_max_products"].to_i + # self.plan_max_customers = @license["plan_max_customers"].to_i + # self.plan_active_connections = @license["plan_active_connections"].to_i + salt = @license["secret_key"] + + if (@license["dbhost"] || @license["dbschema"] || @license["dbusername"] || @license["dbpassword"] ) + self.dbhost = AESCrypt.decrypt(@license["dbhost"], salt) + self.dbschema = AESCrypt.decrypt(@license["dbschema"], salt) + self.dbusername = AESCrypt.decrypt(@license["dbusername"], salt) + self.dbpassword = AESCrypt.decrypt(@license["dbpassword"], salt) + end + + # self.exchange_unqiue_id = @license["exchange_unqiue_id"] + + # self.localqueue_host= @license["localqueue_host"] + # self.localqueue_user= @license["localqueue_user"] + # self.localqueue_password= @license["localqueue_password"] + + # self.remotequeue_host = @license["remotequeue_host"] + # self.remotequeue_user = @license["remotequeue_user"] + # self.remotequeue_password = @license["remotequeue_password"] + + # self.api_token = @license["api_token"] + # self.app_token = @license["app_token"] + end +end