lib/app_manager/model.rb in app_manager-1.1.8 vs lib/app_manager/model.rb in app_manager-1.2.0

- old
+ new

@@ -29,11 +29,12 @@ def plan_features plan_id = self[AppManager.configuration.plan_id_or_name_field] if !plan_id - raise Error, "Plan id not found" + Rails.logger.info "Plan id found nil or not set in DB" + return [] end plan_obj = AppManager::Client.new plans = plan_obj.get_plan(plan_id) if plans && plans['features'].blank? return [] @@ -62,38 +63,90 @@ nil end end def get_remaining_days - shop_domain = self[AppManager.configuration.shopify_domain_field] - trial_activated_at_field = AppManager.configuration.field_names['trial_activated_at'] - trial_activated_at_val = self[trial_activated_at_field] - plan_field = AppManager.configuration.field_names['plan_id'] - plan_id = self[plan_field] - plan_obj = AppManager::Client.new - shop_domain ? plan_obj.get_remaining_days(shop_domain,trial_activated_at_val,plan_id) : 0 + begin + shop_domain = self[AppManager.configuration.shopify_domain_field] + trial_activated_at_field = AppManager.configuration.field_names['trial_activated_at'] + trial_activated_at_val = self[trial_activated_at_field] + plan_field = AppManager.configuration.field_names['plan_id'] + plan_id = self[plan_field] + plan_obj = AppManager::Client.new + shop_domain ? plan_obj.get_remaining_days(shop_domain,trial_activated_at_val,plan_id) : 0 + rescue Exception => e + return "#{e.inspect}" + end end - def get_plan - if !self[AppManager.configuration.plan_id_or_name_field] - raise Error, "Invalid params, must have charge_id,shop && plan" + def get_plan(plan_id=nil) + req_plan_id = nil + if !plan_id.nil? + req_plan_id = plan_id + else + shop_plan_id = self[AppManager.configuration.plan_id_or_name_field] + if shop_plan_id && !shop_plan_id.nil? + req_plan_id = shop_plan_id + else + Rails.logger.info "Invalid params, must have charge_id,shop && plan" + return {} + end end - plan_obj = AppManager::Client.new - return plan_obj.get_plan(self[AppManager.configuration.plan_id_or_name_field]) + plan_obj = AppManager::Client.new if req_plan_id + return req_plan_id ? plan_obj.get_plan(req_plan_id) : {} end def get_charge + shop_plan_id = self[AppManager.configuration.plan_id_or_name_field] + found_free_plan = false + if shop_plan_id && !shop_plan_id.nil? + plan_obj = AppManager::Client.new + plan = plan_obj.get_plan(shop_plan_id) rescue nil + if plan && plan['price'] == 0 && plan['public'] == true + found_free_plan = true + return nil + end + end + if !found_free_plan + begin + shop_domain = self[AppManager.configuration.shopify_domain_field] + plan_obj = AppManager::Client.new + return plan_obj.get_charge(shop_domain) + rescue Exception => e + Rails.logger.info "#{e.inspect}" + return nil + end + end + end + + def get_all_plans begin shop_domain = self[AppManager.configuration.shopify_domain_field] + shop_plan_id = self[AppManager.configuration.plan_id_or_name_field] + active_plan_id = shop_plan_id && shop_plan_id.nil? ? nil : shop_plan_id plan_obj = AppManager::Client.new - return plan_obj.get_charge(shop_domain) + return plan_obj.get_plans(shop_domain,active_plan_id) rescue Exception => e - return "#{e.inspect}" + Rails.logger.info "#{e.inspect}" + return [] end end + def cancel_charge + begin + shop_plan_id = self[AppManager.configuration.plan_id_or_name_field] + shop_domain = self[AppManager.configuration.shopify_domain_field] + if shop_plan_id && shop_domain + plan_obj = AppManager::Client.new + plan_obj.cancel_charge(shop_domain,shop_plan_id) + end + rescue Exception => e + return "#{e.inspect}" + end + end + def set_default_plan(plan_id=nil) begin if plan_id.nil? shop_domain = self[AppManager.configuration.shopify_domain_field] plan_obj = AppManager::Client.new @@ -115,19 +168,21 @@ end - def get_plans_by_features(feature_slugs) + def get_plans_by_features(feature_slugs,params="false") if feature_slugs.any? + shop_plan_id = self[AppManager.configuration.plan_id_or_name_field] plan_data = [] shop_domain = self[AppManager.configuration.shopify_domain_field] plan_obj = AppManager::Client.new plans = plan_obj.get_plans(shop_domain) plans = plans.select{|x| x['interval'] == 'EVERY_30_DAYS' && x['public'] == true} plans.each do |plan| - if plan['features'].any? && plan['features'].values.any? && plan['features'].values.collect{|c| c['slug']}.any? + next if (params && !params.nil? && params == "exclude_current_plan" && !shop_plan_id.nil? && plan["id"] == shop_plan_id) + if plan && (plan['features'] && plan['features'].any?) && (plan['features'].values && plan['features'].values.any?) && (plan['features'].values.collect{|c| c['slug']} && plan['features'].values.collect{|c| c['slug']}.any?) plan_feature_slugs = plan['features'].values.collect{|c| c['slug']}.sort if (feature_slugs & plan_feature_slugs).any? && ((feature_slugs & plan_feature_slugs).sort == feature_slugs.sort) plan_data.push(plan) end end @@ -135,9 +190,11 @@ return plan_data else return [] end end + + private def casted_value(value,value_type) case value_type \ No newline at end of file