87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'app/controllers/app_manager/charges_controller.rb', line 87
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']
grandfathered_field = @field_names['grandfathered']
discounted_plans = []
if !@shop.nil?
old_plan_id = @shop[@plan_field]
old_plan_data = nil
= {"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 => )
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']
['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 = {@plan_field => params[:plan],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
@shop.update(update_info)
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" => params[:shop]
})
rescue Exception => e
Rollbar.error("Error in APP MANAGER Charge Created Callback >>>> #{e.inspect}-#{e.backtrace}")
end
begin
plan_obj = AppManager::Client.new
if params[:discount].present? && !params[:discount].nil? && params[:discount] != '0'
discounted_plans = plan_obj.get_related_discounted_plans(params[:discount])
if discounted_plans.empty? || discounted_plans.include?(params[:plan].to_i)
plan_obj.discount_used(@shop[shopify_domain], params[:discount])
end
end
rescue Exception => e
Rollbar.error("Error in APP MANAGER Discount used API call >>>> #{e.inspect}-#{e.backtrace}")
end
end
end
embed_host = Base64.encode64(params[:shop] + "/admin")
if !old_plan_id.nil?
plan_obj = AppManager::Client.new
old_plan_data = plan_obj.get_plan(old_plan_id, params[:shop])
end
current_plan_string = !plan_data.nil? ? "#{plan_data['name'].gsub(" ", "-")}-#{plan_data['interval']['label']}" : "none" rescue ""
old_plan_string = !old_plan_data.nil? ? "#{old_plan_data['name'].gsub(" ", "-")}-#{old_plan_data['interval']['label']}" : "none" rescue ""
plan_query_string = "new_plan=#{current_plan_string}&old_plan=#{old_plan_string}"
if app_slug.present? && plan_page_route_value.present?
redirect_to "https://#{params[:shop]}/admin/apps/#{app_slug}/#{plan_page_route_value}?#{plan_query_string}"
elsif app_slug.present? && !plan_page_route_value.present?
redirect_to "https://#{params[:shop]}/admin/apps/#{app_slug}"
else
redirect_to "#{app_url}?host=#{embed_host}&shop=#{params[:shop]}", :status => 301 and return
end
else
raise Error, "Invalid shopify charge #{charges.inspect}"
end
else
raise ModelNotFound, "Shop not found"
end
else
if params[:shop].present?
embed_host = Base64.encode64(params[:shop] + "/admin")
if app_slug.present?
redirect_to "https://#{params[:shop]}/admin/apps/#{app_slug}"
else
redirect_to "#{app_url}?host=#{embed_host}&shop=#{params[:shop]}", :status => 301 and return
end
else
raise Error, "Invalid params, must have charge_id,shop && plan in charge controller"
end
end
end
|