require_dependency "app_manager/application_controller" module AppManager class ChargesController < ApplicationController include HTTParty skip_before_action :verify_authenticity_token, :only => [:process_plan] before_action :params_permit require 'time' require 'rack' def process_plan if params[:shop].present? && params[:plan_id].present? @shop = shop_data if !@shop.nil? plan_obj = AppManager::Client.new plan_data = plan_obj.get_plan(params[:plan_id],params[:shop]) if plan_data.present? && plan_data.is_a?(Hash) request_data = {'shop' => @shop.shopify_domain, 'timestamp' => Time.now.to_i, 'plan' => params[:plan_id]} return_url = "#{app_url}#{plan_callback_path}?#{Rack::Utils.build_query(request_data)}" gq_obj = AppManager::GraphqlHelper.new(@shop.shopify_domain,@shop.shopify_token) data = gq_obj.recurring_charge_api_call(plan_data,return_url,@shop) if !data["errors"].present? && (data["data"].present? && data["data"]["appSubscriptionCreate"].present? && ( !data["data"]["appSubscriptionCreate"]["userErrors"].any? && data["data"]["appSubscriptionCreate"]["confirmationUrl"])) redirect_charge = data["data"]["appSubscriptionCreate"]["confirmationUrl"] render json: {'redirect_url' => redirect_charge} else render json: {'error' => data["errors"].as_json} end else render json: {'error' => data["errors"]} end else render json: {'error' => 'Shop not found'} end else render json: {'error' => 'Missing Shop domain or plan id in params'} end end def callback if params[:charge_id].present? && params[:shop].present? && params[:plan].present? @shop = shop_data shopify_token = @field_names['shopify_token'] shopify_domain = @field_names['name'] if !@shop.nil? headers = {"X-Shopify-Access-Token" => @shop[shopify_token]} charges = HTTParty.get('https://'+@shop[shopify_domain]+'/admin/api/'+@api_version+'/recurring_application_charges/'+params[:charge_id]+'.json', :headers => headers) if charges.parsed_response && charges.parsed_response.is_a?(Hash) && charges.parsed_response.has_key?('recurring_application_charge') plan_obj = AppManager::Client.new plan_data = plan_obj.get_plan(params[:plan],params[:shop]) charge = charges.parsed_response['recurring_application_charge'] charge['charge_id'] = charge['id']; charge['type'] = 'recurring'; charge['plan_id'] = params[:plan]; charge['shop_domain'] = params[:shop]; charge['interval'] = plan_data['interval']['value']; if !@plan_field.nil? begin plan_obj.cancel_charge(@shop[shopify_domain],@shop[@plan_field]) rescue Exception => e end end charge_ob = AppManager::Client.new(nil,json_req=true) response = charge_ob.store_charge(charge.to_json) if response['message'] == "success" model.update(@plan_field => params[:plan]) end redirect_to "/?shop=#{params[:shop]}" else render json: {'error' => 'Invalid shopify charge'} end else render json: {'error' => 'Shop not found'} end else render json: {'error' => 'Invalid params'} end end private def params_permit params.permit! end def model @models = ActiveRecord::Base.connection.tables @config_table = AppManager.configuration.shopify_table_name @shopify_domain = AppManager.configuration.shopify_domain_field @plan_field = AppManager.configuration.plan_id_or_name_field @field_names = AppManager.configuration.field_names @api_version = AppManager.configuration.shopify_api_version if @models.include?(@config_table) && !@plan_field.nil? return @config_table.classify.constantize else return nil end end def shop_data if model return model.where(@shopify_domain => params[:shop]).first rescue nil else return nil end end def app_url AppManager.configuration.app_url end end end