lib/app_manager/model.rb in app_manager-1.5.1 vs lib/app_manager/model.rb in app_manager-1.6.0
- old
+ new
@@ -150,9 +150,145 @@
rescue Exception => e
return "#{e.inspect}"
end
end
+ def get_active_charge_app_manager
+ active_charge = self.get_charge rescue nil
+ return active_charge && active_charge['active_charge'].present? && !active_charge['active_charge'].nil? ? active_charge['active_charge'] : nil
+ end
+
+ def has_valid_app_manager_plan_id
+ shop_plan_id = self[AppManager.configuration.plan_id_or_name_field]
+ shop_domain = self[AppManager.configuration.shopify_domain_field]
+ plan_obj = AppManager::Client.new
+ plans = plan_obj.get_plans(shop_domain) rescue nil
+ return shop_plan_id && (plans && plans.any? && plans.collect{|e| e['id']}.include?(shop_plan_id)) ? true : false
+ end
+
+ def update_app_manager_charge(current_shopify_charge_id=nil)
+ charge_updated = nil
+ begin
+ shop_plan_id = self[AppManager.configuration.plan_id_or_name_field]
+ shop_domain = self[AppManager.configuration.shopify_domain_field]
+ if get_active_charge_app_manager.nil?
+ current_charge_id = current_shopify_charge_id.present? ? current_shopify_charge_id : active_shopify_charge_id
+ if current_charge_id.present?
+ if has_valid_app_manager_plan_id
+ if charge_update_call(self,shop_plan_id,current_charge_id)
+ charge_updated = true
+ end
+ end
+ end
+ end
+ return charge_updated
+ rescue Exception => e
+ Rollbar.error("Warning in APP MANAGER, trying to get active shopify charge id >>>> #{e.inspect}")
+ return nil
+ end
+ end
+
+ def active_shopify_charge_id
+ begin
+ gq_obj = AppManager::GraphqlHelper.new(self.shopify_domain, self.shopify_token)
+ data = gq_obj.recurring_charge_current_api_call(self)
+ if !data["errors"].present? && (data["data"].present? && data["data"]["currentAppInstallation"].present? && data["data"]["currentAppInstallation"]["activeSubscriptions"].present?)
+ current_shopify_charge_id = data["data"]["currentAppInstallation"]["activeSubscriptions"].first["id"]
+ if current_shopify_charge_id
+ return current_shopify_charge_id.split('/')[-1] rescue nil
+ else
+ return nil
+ end
+ else
+ return nil
+ end
+ rescue Exception => e
+ Rollbar.error("Warning in APP MANAGER, trying to get active shopify charge id >>>> #{e.inspect}")
+ return nil
+ end
+ end
+
+ def app_manager_charge_update(shop,shopify_current_charge_obj)
+ if shop.present? && shop.plan_id.present? && shopify_current_charge_obj.present?
+ charge_update_call(shop,shop.plan_id,shopify_current_charge_obj['id'],shopify_current_charge_obj)
+ end
+ end
+
+ def charge_update_call(shop,shop_plan_id,current_charge_id,charge_obj={})
+ charge_callback_done = nil
+ # begin
+ @shop = shop
+ @field_names = AppManager.configuration.field_names
+ shopify_token = @field_names['shopify_token']
+ shopify_domain = @field_names['name']
+ grandfathered_field = @field_names['grandfathered']
+ shop_domain = @shop[AppManager.configuration.shopify_domain_field]
+ if !@shop.nil?
+ if charge_obj.present? && !charge_obj.nil?
+ shopify_charge_obj = charge_obj
+ else
+ headers = {"X-Shopify-Access-Token" => @shop[shopify_token]}
+ charges = HTTParty.get('https://' + shop_domain + '/admin/api/' + AppManager.configuration.shopify_api_version + '/recurring_application_charges/' + current_charge_id + '.json', :headers => headers)
+ shopify_charge_obj = charges.parsed_response && charges.parsed_response.is_a?(Hash) && charges.parsed_response.has_key?('recurring_application_charge') ? charges.parsed_response['recurring_application_charge'] : {}
+ end
+
+ if shopify_charge_obj.present?
+ plan_obj = AppManager::Client.new
+ plan_data = plan_obj.get_plan(shop_plan_id, shop_domain)
+ charge = shopify_charge_obj
+ charge['charge_id'] = charge['id']
+ charge['type'] = 'recurring'
+ charge['plan_id'] = shop_plan_id
+ charge['shop_domain'] = shop_domain
+ charge['interval'] = plan_data['interval']['value']
+ ['api_client_id', 'return_url', 'decorated_return_url','id','id','currency'].each { |k| charge.delete k }
+ charge_ob = AppManager::Client.new(nil, json_req = true)
+ response = charge_ob.store_charge(charge.to_json)
+
+ if response['message'] == "success"
+ AppManager.clear_cache
+
+ update_info = {AppManager.configuration.plan_id_or_name_field => shop_plan_id,grandfathered_field => 0}
+ if !config_trial_days.nil? && !plan_data.nil?
+ trial_days = plan_data['trial_days'] || 0
+ update_info[config_trial_days] = trial_days
+ end
+
+ if @shop.update(update_info)
+ charge_callback_done = true
+ end
+ Thread.new do
+ charge_data = plan_obj.get_charge(@shop[shopify_domain])
+ begin
+ AppManager::EventHandler.new('charge_created', {
+ "plan" => plan_data,
+ "charge" => charge,
+ "previous_charge" => charge_data ? (charge_data['cancelled_charge'] || nil) : nil,
+ "shopify_domain" => shop_domain
+ })
+ rescue Exception => e
+ Rollbar.error("Warning in APP MANAGER model Charge Created Callback Event Fire>>>> #{e.inspect}")
+ end
+ end
+ end
+ end
+ end
+ return charge_callback_done
+ # rescue Exception => e
+ # Rollbar.error("Warning in APP MANAGER model charge_callback, trying to update active shopify charge in app manager>>>> #{e.inspect}")
+ # return nil
+ # end
+ end
+
+ def config_trial_days
+ @field_names = AppManager.configuration.field_names
+ if !@field_names.nil? && @field_names.has_key?('total_trial_days') && !@field_names['total_trial_days'].nil? && !@field_names['total_trial_days'].blank?
+ return @field_names['total_trial_days']
+ else
+ return nil
+ 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