app_manager 1.1.8 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 49b23403c65f2eb57f2d3a3a54d1c966e3810773aea9822c8625f3026fec93b9
4
- data.tar.gz: 3625d02deadd46fbc14f27227cc49d0ffb774fa285d971df90a4d91dcef06255
3
+ metadata.gz: 9e3986aadebfd51cccf040bfd0a5ad36041de436ac3ab4c074fb0a2174a9da10
4
+ data.tar.gz: 83e772b1847481e9e91745f91569973d16e1809eb7732affe4f6f46ab3385c89
5
5
  SHA512:
6
- metadata.gz: 2e755621b1a8dea6d0f24a8135bd562cad9451ca1917a5c011be9e30654529f3dd55f8b9dc60908d7460b2ab8f6ca90823ec779726b277f1e35e45eec711621c
7
- data.tar.gz: 45f80655e7125a4b00740be0b286867edc755101bdb2d3c6fe5a52ed70ae7f798f56a87b371783be6b3b4cda678917165cf49ffe16fe2aa0ed6cc437b22ff7fb
6
+ metadata.gz: 8793bb25fdfbff2ed36e2c0f4f24f63d6c40c1bc6df999ef101cfa6f6b66d2732729743f1ab2298d66debfbd6785ea47bddcdacf7fa3d21efcebdd17bcda5b3b
7
+ data.tar.gz: 1b040a0afaedeb15cf4892f9010105e4f1ea1e6f7a92b75c5d65dc5e1c3a5870fd3eb4a01f825738a5c8fbc722a8f3dd63fe3be8eb217bf19ac4859bc1d716bd
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- app_manager (1.1.8)
4
+ app_manager (1.2.2)
5
5
  httparty
6
6
  kaminari (>= 0.16.3)
7
7
  rails (>= 5.2.0)
@@ -119,7 +119,7 @@ GEM
119
119
  mime-types-data (3.2022.0105)
120
120
  mini_mime (1.1.2)
121
121
  mini_portile2 (2.8.0)
122
- minitest (5.16.1)
122
+ minitest (5.16.2)
123
123
  multi_xml (0.6.0)
124
124
  net-imap (0.2.3)
125
125
  digest
@@ -146,7 +146,7 @@ GEM
146
146
  method_source (~> 1.0)
147
147
  public_suffix (4.0.6)
148
148
  racc (1.6.0)
149
- rack (2.2.3.1)
149
+ rack (2.2.4)
150
150
  rack-test (2.0.2)
151
151
  rack (>= 1.3)
152
152
  rails (7.0.3)
data/README.md CHANGED
@@ -171,7 +171,26 @@ and then you can use follwing methods with your shop objects.
171
171
 
172
172
  @shop.get_plan # return current plan hash
173
173
 
174
- @shop.get_charge # return current charge hash
174
+ if you pass @shop.get_plan(plan_id) it will return that particular plan hash like @shop.get_plan(311)
175
+
176
+ @shop.get_charge # return current charge hash, it will return nil if current plan is free
177
+
178
+ @shop.set_default_plan(plan_id=nil) #if plan id is passed nil, then it will set the free(public and $0 price) to active and if passed it will set that plan to active plan
179
+
180
+ @shop.get_plans_by_features(feature_slugs) # pass feature_slugs as an array even if one slug and it will return plans array which have that slug found. Example:
181
+
182
+ @shop.get_plans_by_features(['quantity-based-discount'])
183
+ @shop.get_plans_by_features(['quantity-based-discount','multiple-discount-types'])
184
+
185
+
186
+ you can pass string argument 'exclude_current_plan' which will return all plans that matching features except current plan
187
+
188
+ @shop.get_plans_by_features(['quantity-based-discount'],'exclude_current_plan')
189
+ @shop.get_plans_by_features(['quantity-based-discount','multiple-discount-types'],'exclude_current_plan')
190
+
191
+
192
+ @shop.get_all_plans # return all app plans array
193
+
175
194
  ```
176
195
 
177
196
 
@@ -13,17 +13,33 @@ module AppManager
13
13
  if !@shop.nil?
14
14
  plan_obj = AppManager::Client.new
15
15
  plan_data = plan_obj.get_plan(params[:plan_id],params[:shop])
16
+ # render json: plan_data and return #use this to test plan failsafe api
16
17
  if plan_data.present? && plan_data.is_a?(Hash)
17
18
  if plan_data['price'] == 0
19
+ active_charge_data = plan_obj.get_charge(params[:shop])
20
+ begin
21
+ if active_charge_data && active_charge_data.any? && !active_charge_data['active_charge'].nil? && !active_charge_data['active_charge']['charge_id'].nil?
22
+ gq_obj = AppManager::GraphqlHelper.new(@shop.shopify_domain,@shop.shopify_token)
23
+ rec_cancel_data = gq_obj.recurring_charge_cancel_api_call(active_charge_data['active_charge']['charge_id'],@shop)
24
+ if !rec_cancel_data["errors"].present? && (rec_cancel_data["data"].present? && rec_cancel_data["data"]["appSubscriptionCancel"].present? && !rec_cancel_data["data"]["appSubscriptionCancel"]["userErrors"].any? && (rec_cancel_data["data"]["appSubscriptionCancel"]["appSubscription"]["status"] == 'CANCELLED'))
25
+ cancelled_charge_id = rec_cancel_data["data"]["appSubscriptionCancel"]["appSubscription"]["id"].split('/')[-1]
26
+ plan_obj.cancel_charge(params[:shop],cancelled_charge_id)
27
+ end
28
+ end
29
+ rescue Exception => e
30
+ Rails.logger.info "APP MANAGER >>>> #{e.inspect}"
31
+ end
32
+
18
33
  if @shop.update(@plan_field => params[:plan_id])
19
34
  begin
20
35
  AppManager::EventHandler.new('charge_created',{
21
36
  "plan" => plan_data,
22
37
  "charge" => nil,
23
- "previous_charge" => nil
38
+ "previous_charge" => nil,
39
+ "shopify_domain" => params[:shop]
24
40
  })
25
41
  rescue Exception => e
26
- Rails.logger.info "APP MANGAGER >>>> #{e.inspect}"
42
+ Rails.logger.info "APP MANAGER >>>> #{e.inspect}"
27
43
  end
28
44
  AppManager.clear_cache
29
45
  render json: {'redirect_url' => "#{app_url}?shop=#{params[:shop]}"} and return true
@@ -68,17 +84,17 @@ module AppManager
68
84
  plan_data = plan_obj.get_plan(params[:plan],params[:shop])
69
85
 
70
86
  charge = charges.parsed_response['recurring_application_charge']
71
- charge['charge_id'] = charge['id'];
72
- charge['type'] = 'recurring';
73
- charge['plan_id'] = params[:plan];
74
- charge['shop_domain'] = params[:shop];
75
- charge['interval'] = plan_data['interval']['value'];
87
+ charge['charge_id'] = charge['id']
88
+ charge['type'] = 'recurring'
89
+ charge['plan_id'] = params[:plan]
90
+ charge['shop_domain'] = params[:shop]
91
+ charge['interval'] = plan_data['interval']['value']
76
92
 
77
93
  if !@plan_field.nil?
78
94
  begin
79
95
  plan_obj.cancel_charge(@shop[shopify_domain],@shop[@plan_field])
80
96
  rescue Exception => e
81
- Rails.logger.info "APP MANGAGER >>>> #{e.inspect}"
97
+ Rails.logger.info "APP MANAGER >>>> #{e.inspect}"
82
98
  end
83
99
  end
84
100
 
@@ -93,10 +109,11 @@ module AppManager
93
109
  AppManager::EventHandler.new('charge_created',{
94
110
  "plan" => plan_data,
95
111
  "charge" => charge,
96
- "previous_charge" => charge_data ? (charge_data['cancelled_charge'] || nil) : nil
112
+ "previous_charge" => charge_data ? (charge_data['cancelled_charge'] || nil) : nil,
113
+ "shopify_domain" => params[:shop]
97
114
  })
98
115
  rescue Exception => e
99
- Rails.logger.info "APP MANGAGER >>>> #{e.inspect}"
116
+ Rails.logger.info "APP MANAGER >>>> #{e.inspect}"
100
117
  end
101
118
  end
102
119
  redirect_to "#{app_url}?shop=#{params[:shop]}", :status => 301 and return
@@ -13,6 +13,7 @@ module AppManager
13
13
 
14
14
  def plans
15
15
  active_plan_id_or_name = shopify_plan = plan = nil
16
+ active_charge = nil
16
17
  default_plan_id = nil
17
18
  choose_later = false
18
19
  plan_obj = AppManager::Client.new
@@ -30,16 +31,16 @@ module AppManager
30
31
  @trial_activated_field = AppManager.configuration.field_names['trial_activated_at']
31
32
  trial_activated_at = @shop[@trial_activated_field] rescue nil
32
33
  active_charge = plan_obj.get_charge(params[:shop_domain])
33
- if active_charge && active_charge.any? && active_charge['active_charge'].nil? && active_charge['cancelled_charge'].nil? && trial_activated_at
34
+ if active_charge && active_charge.any? && active_charge['active_charge'].nil? && active_charge['cancelled_charge'].nil? && !trial_activated_at
34
35
  choose_later = true
35
36
  end
36
37
 
37
38
  end
38
39
  else
39
- Rails.logger.info "APP MANGAGER >>>> Either model is defined wrong or config.plan_id_or_name_field is nil in initializer ==="
40
+ Rails.logger.info "APP MANAGER >>>> Either model is defined wrong or config.plan_id_or_name_field is nil in initializer ==="
40
41
  end
41
42
  else
42
- Rails.logger.info "APP MANGAGER >>>> Either params missing store_domain or config.plan_features is nil in initializer ==="
43
+ Rails.logger.info "APP MANAGER >>>> Either params missing store_domain or config.plan_features is nil in initializer ==="
43
44
  end
44
45
 
45
46
 
@@ -64,7 +65,8 @@ module AppManager
64
65
  'shopify_plan' => shopify_plan,
65
66
  'plan' => plan,
66
67
  'default_plan_id' => default_plan_id,
67
- 'choose_later' => choose_later
68
+ 'choose_later' => choose_later,
69
+ 'has_active_charge' => (!active_charge['active_charge'].nil? ? true : false)
68
70
  }
69
71
  render json: response
70
72
  end
@@ -119,8 +121,24 @@ module AppManager
119
121
  if params[:shop_domain].present? && params[:plan_id].present? && model
120
122
  @shop = shop_data
121
123
  @trial_activated_field = AppManager.configuration.field_names['trial_activated_at']
122
- @shop.update(@plan_field => params[:plan_id],@trial_activated_field => DateTime.now)
123
- render json: {'status' => true}
124
+ if @shop.update(@plan_field => params[:plan_id],@trial_activated_field => DateTime.now)
125
+ begin
126
+ plan_obj = AppManager::Client.new
127
+ plan_data = plan_obj.get_plan(params[:plan_id],params[:shop_domain]) rescue nil
128
+ AppManager::EventHandler.new('charge_created',{
129
+ "plan" => plan_data,
130
+ "charge" => nil,
131
+ "previous_charge" => nil,
132
+ "choose_later" => true,
133
+ "shopify_domain" => params[:shop_domain]
134
+ })
135
+ rescue Exception => e
136
+ Rails.logger.info "APP MANAGER >>>> #{e.inspect}"
137
+ end
138
+ render json: {'status' => true}
139
+ else
140
+ render json: {'status' => false,'error' => "#{@shop.errors.full_messages.to_sentence}"}, status: 422
141
+ end
124
142
  else
125
143
  render json: {'status' => false,'error' => 'Shop not found or missing shop'}, status: 422
126
144
  end
@@ -140,13 +158,13 @@ module AppManager
140
158
  begin
141
159
  @fs.sync_app_manager
142
160
  rescue Exception => e
143
- Rails.logger.info "APP MANGAGER >>>> LOCAL DB couldn't sync with POTAL DB #{e.inspect}"
161
+ Rails.logger.info "APP MANAGER >>>> LOCAL DB couldn't sync with POTAL DB #{e.inspect}"
144
162
  end
145
163
 
146
164
  begin
147
165
  @fs.save_api_data(params)
148
166
  rescue Exception => e
149
- Rails.logger.info "APP MANGAGER >>>> #{e.inspect}"
167
+ Rails.logger.info "APP MANAGER >>>> #{e.inspect}"
150
168
  end
151
169
  AppManager.clear_cache
152
170
  end
@@ -18,6 +18,10 @@ module AppManager
18
18
  post("/cancel-charge", {shop_domain: shop_domain,plan_id: plan_id})
19
19
  end
20
20
 
21
+ def update_charge(shop_domain,plan_id)
22
+ post("/update-charge", {shop_domain: shop_domain,plan_id: plan_id})
23
+ end
24
+
21
25
  def sync_charge(options = {})
22
26
  post("/sync-charge", options)
23
27
  end
@@ -28,37 +28,91 @@ module AppManager
28
28
  begin
29
29
  save_api_plans(params["plans"])
30
30
  rescue Exception => e
31
- Rails.logger.info "APP MANGAGER >>>> #{e.inspect}"
31
+ Rails.logger.info "APP MANAGER >>>> #{e.inspect}"
32
+ begin
33
+ create_plan_table
34
+ save_api_plans(params["plans"])
35
+ rescue Exception => e
36
+ Rails.logger.info "APP MANAGER FailSafe >>>> #{e.inspect}"
37
+ end
32
38
  end
39
+
40
+
33
41
  begin
34
42
  save_api_charges(params["charges"])
35
43
  rescue Exception => e
36
- Rails.logger.info "APP MANGAGER >>>> #{e.inspect}"
44
+ Rails.logger.info "APP MANAGER >>>> #{e.inspect}"
45
+ begin
46
+ create_charges_table
47
+ save_api_charges(params["charges"])
48
+ rescue Exception => e
49
+ Rails.logger.info "APP MANAGER FailSafe >>>> #{e.inspect}"
50
+ end
37
51
  end
52
+
53
+
38
54
  begin
39
55
  save_api_apps(params["apps"])
40
56
  rescue Exception => e
41
- Rails.logger.info "APP MANGAGER >>>> #{e.inspect}"
57
+ Rails.logger.info "APP MANAGER >>>> #{e.inspect}"
58
+ begin
59
+ create_apps_table
60
+ save_api_apps(params["apps"])
61
+ rescue Exception => e
62
+ Rails.logger.info "APP MANAGER FailSafe >>>> #{e.inspect}"
63
+ end
42
64
  end
65
+
66
+
43
67
  begin
44
68
  save_api_app_structures(params["app_structures"])
45
69
  rescue Exception => e
46
- Rails.logger.info "APP MANGAGER >>>> #{e.inspect}"
70
+ Rails.logger.info "APP MANAGER >>>> #{e.inspect}"
71
+ begin
72
+ create_app_structures_table
73
+ save_api_app_structures(params["app_structures"])
74
+ rescue Exception => e
75
+ Rails.logger.info "APP MANAGER FailSafe >>>> #{e.inspect}"
76
+ end
47
77
  end
78
+
79
+
48
80
  begin
49
81
  save_api_discount_plans(params["discount_plans"])
50
82
  rescue Exception => e
51
- Rails.logger.info "APP MANGAGER >>>> #{e.inspect}"
83
+ Rails.logger.info "APP MANAGER >>>> #{e.inspect}"
84
+ begin
85
+ create_discount_plans_table
86
+ save_api_discount_plans(params["discount_plans"])
87
+ rescue Exception => e
88
+ Rails.logger.info "APP MANAGER FailSafe >>>> #{e.inspect}"
89
+ end
52
90
  end
91
+
92
+
53
93
  begin
54
94
  save_api_extend_trials(params["extend_trials"])
55
95
  rescue Exception => e
56
- Rails.logger.info "APP MANGAGER >>>> #{e.inspect}"
96
+ Rails.logger.info "APP MANAGER >>>> #{e.inspect}"
97
+ begin
98
+ create_extend_trials_table
99
+ save_api_extend_trials(params["extend_trials"])
100
+ rescue Exception => e
101
+ Rails.logger.info "APP MANAGER FailSafe >>>> #{e.inspect}"
102
+ end
57
103
  end
104
+
105
+
58
106
  begin
59
107
  save_api_plan_users(params["plan_users"])
60
108
  rescue Exception => e
61
- Rails.logger.info "APP MANGAGER >>>> #{e.inspect}"
109
+ Rails.logger.info "APP MANAGER >>>> #{e.inspect}"
110
+ begin
111
+ create_plan_users_table
112
+ save_api_plan_users(params["plan_users"])
113
+ rescue Exception => e
114
+ Rails.logger.info "APP MANAGER FailSafe >>>> #{e.inspect}"
115
+ end
62
116
  end
63
117
  end
64
118
 
@@ -194,11 +248,22 @@ module AppManager
194
248
 
195
249
 
196
250
  def get_local_plans(params)
251
+
197
252
  plans_data = []
198
253
 
199
254
  active_plan_id = nil
255
+ active_charge_price = nil
256
+
200
257
  charges = @apm_db.execute( "SELECT * FROM charges WHERE shop_domain = ? ",params['shop_domain'])
201
- active_plan_id = charges.first['plan_id'] if charges.any?
258
+
259
+ if charges.any?
260
+ active_plan_id = charges.first['plan_id']
261
+ active_charge_price = charges.first['price']
262
+ elsif params['active_plan_id'].present? && !params['active_plan_id'].nil?
263
+ active_plan_id = params['active_plan_id']
264
+ plan_data = @apm_db.execute( "SELECT * FROM plans WHERE id = ?", active_plan_id)
265
+ active_charge_price = plan_data.first['price'] if plan_data.any?
266
+ end
202
267
 
203
268
  custom_plan_ids = []
204
269
  plan_users = @apm_db.execute( "SELECT * FROM plan_users WHERE shop_domain = ? ",params['shop_domain'])
@@ -232,7 +297,7 @@ module AppManager
232
297
  new_plan[key] = val.collect{|e|e['value']}
233
298
  elsif ['affiliate'].include?(key)
234
299
  new_plan[key] = eval(value)
235
- elsif ['is_custom','public','store_base_plan'].include?(key)
300
+ elsif ['is_custom','public','store_base_plan','choose_later_plan'].include?(key)
236
301
  new_plan[key] = (value == 0 ? false : true)
237
302
  elsif ['test'].include?(key)
238
303
  new_plan[key] = (value == 0 ? nil : true)
@@ -255,12 +320,41 @@ module AppManager
255
320
  features_by_plan.each do |fp|
256
321
  fp['name'] = features.find{|e| e['uuid'] == fp['feature_id']}['name'] rescue nil
257
322
  fp['format'] = features.find{|e| e['uuid'] == fp['feature_id']}['format'] rescue nil
323
+ fp['slug'] = features.find{|e| e['uuid'] == fp['feature_id']}['slug'] rescue nil
258
324
  features_by_plans_data.push(fp)
259
325
  end
260
326
  end
261
327
  end
262
328
 
329
+
330
+ custom_discounts_data = []
331
+ if params["shop_domain"].present? && plan_data
332
+ custom_discounts = @apm_db.execute( "SELECT * FROM discount_plans WHERE shop_domain = ? ORDER BY created_at DESC", params["shop_domain"])
333
+ if custom_discounts.any?
334
+ custom_discounts.each do |custom_discount|
335
+ new_custom_discount = {}
336
+ custom_discount.each_with_index do |(key, value), index|
337
+ new_custom_discount[key] = value unless key.class == Integer
338
+ end
339
+ custom_discounts_data.push(new_custom_discount)
340
+ end
341
+ end
342
+ end
343
+
263
344
  plans_data.each do |plan|
345
+ if (!active_plan_id.nil? && plan['id'] == active_plan_id)
346
+ plan['price'] = active_charge_price
347
+ end
348
+
349
+ if custom_discounts_data.any? && custom_discounts_data.select{|e| e['plan_id'] == plan['id']}.size > 0
350
+ cd_hash = {}
351
+ custom_discounts_data.select{|e| e['plan_id'] == plan['id']}.each do |cd|
352
+ plan['discount'] = cd['discount']
353
+ plan['discount_type'] = cd['discount_type']
354
+ plan['cycle_count'] = cd['cycle_count']
355
+ end
356
+ end
357
+
264
358
  if features_by_plans_data.select{|e| e['plan_id'] == plan['id']}.size > 0
265
359
  feature_hash = {}
266
360
  features_by_plans_data.select{|e| e['plan_id'] == plan['id']}.each do |fp|
@@ -271,11 +365,14 @@ module AppManager
271
365
  features = nil
272
366
  end
273
367
  plan['features'] = features
368
+ plan['old_plan_id'] = nil
369
+
274
370
  end
275
371
 
276
372
  plans = plans_data
277
373
  end
278
374
 
375
+
279
376
  return plans
280
377
  end
281
378
 
@@ -291,7 +388,7 @@ module AppManager
291
388
  new_plan[key] = val
292
389
  elsif ['shopify_plans','affiliate','features'].include?(key)
293
390
  new_plan[key] = eval(value)
294
- elsif ['is_custom','public','store_base_plan'].include?(key)
391
+ elsif ['is_custom','public','store_base_plan','choose_later_plan'].include?(key)
295
392
  new_plan[key] = (value == 0 ? false : true)
296
393
  elsif ['test'].include?(key)
297
394
  new_plan[key] = (value == 0 ? nil : true)
@@ -308,7 +405,7 @@ module AppManager
308
405
  end
309
406
  plan_data['app'] = app_data
310
407
  end
311
-
408
+ plan_data['old_plan_id'] = nil # Temporary for migration puspose only
312
409
  if params["shop_domain"].present? && plan_data
313
410
  @apm_db.execute( "SELECT * FROM discount_plans WHERE plan_id = ? AND shop_domain = ? ", params["plan_id"],params["shop_domain"]) do |cd|
314
411
  plan_data['discount'] = cd['discount'] if cd rescue plan_data['discount']
@@ -322,6 +419,7 @@ module AppManager
322
419
  end
323
420
 
324
421
  end
422
+
325
423
  return plan_data
326
424
  end
327
425
 
@@ -122,6 +122,32 @@ module AppManager
122
122
  return data
123
123
  end
124
124
 
125
+
126
+ def recurring_charge_cancel_api_call(charge_id,shop)
127
+ query = 'mutation(
128
+ $id: ID!
129
+ ){
130
+ appSubscriptionCancel(
131
+ id: $id
132
+ ) {
133
+ userErrors {
134
+ field
135
+ message
136
+ }
137
+ appSubscription {
138
+ id
139
+ status
140
+ }
141
+ }
142
+ }
143
+ '
144
+ variables = {
145
+ "id": "gid://shopify/AppSubscription/#{charge_id}"
146
+ }
147
+ data = run_graph_api(query,variables)
148
+ return data
149
+ end
150
+
125
151
  end
126
152
  end
127
153
 
@@ -31,7 +31,8 @@ module AppManager
31
31
  def plan_features
32
32
  plan_id = self[AppManager.configuration.plan_id_or_name_field]
33
33
  if !plan_id
34
- raise Error, "Plan id not found"
34
+ Rails.logger.info "Plan id found nil or not set in DB"
35
+ return []
35
36
  end
36
37
  plan_obj = AppManager::Client.new
37
38
  plans = plan_obj.get_plan(plan_id)
@@ -64,34 +65,86 @@ module AppManager
64
65
  end
65
66
 
66
67
  def get_remaining_days
67
- shop_domain = self[AppManager.configuration.shopify_domain_field]
68
- trial_activated_at_field = AppManager.configuration.field_names['trial_activated_at']
69
- trial_activated_at_val = self[trial_activated_at_field]
70
- plan_field = AppManager.configuration.field_names['plan_id']
71
- plan_id = self[plan_field]
72
- plan_obj = AppManager::Client.new
73
- shop_domain ? plan_obj.get_remaining_days(shop_domain,trial_activated_at_val,plan_id) : 0
68
+ begin
69
+ shop_domain = self[AppManager.configuration.shopify_domain_field]
70
+ trial_activated_at_field = AppManager.configuration.field_names['trial_activated_at']
71
+ trial_activated_at_val = self[trial_activated_at_field]
72
+ plan_field = AppManager.configuration.field_names['plan_id']
73
+ plan_id = self[plan_field]
74
+ plan_obj = AppManager::Client.new
75
+ shop_domain ? plan_obj.get_remaining_days(shop_domain,trial_activated_at_val,plan_id) : 0
76
+ rescue Exception => e
77
+ return "#{e.inspect}"
78
+ end
74
79
  end
75
80
 
76
81
 
77
- def get_plan
78
- if !self[AppManager.configuration.plan_id_or_name_field]
79
- raise Error, "Invalid params, must have charge_id,shop && plan"
82
+ def get_plan(plan_id=nil)
83
+ req_plan_id = nil
84
+ if !plan_id.nil?
85
+ req_plan_id = plan_id
86
+ else
87
+ shop_plan_id = self[AppManager.configuration.plan_id_or_name_field]
88
+ if shop_plan_id && !shop_plan_id.nil?
89
+ req_plan_id = shop_plan_id
90
+ else
91
+ Rails.logger.info "Invalid params, must have charge_id,shop && plan"
92
+ return {}
93
+ end
80
94
  end
81
- plan_obj = AppManager::Client.new
82
- return plan_obj.get_plan(self[AppManager.configuration.plan_id_or_name_field])
95
+ plan_obj = AppManager::Client.new if req_plan_id
96
+ return req_plan_id ? plan_obj.get_plan(req_plan_id) : {}
83
97
  end
84
98
 
85
99
  def get_charge
100
+ shop_plan_id = self[AppManager.configuration.plan_id_or_name_field]
101
+ found_free_plan = false
102
+ if shop_plan_id && !shop_plan_id.nil?
103
+ plan_obj = AppManager::Client.new
104
+ plan = plan_obj.get_plan(shop_plan_id) rescue nil
105
+ if plan && plan['price'] == 0 && plan['public'] == true
106
+ found_free_plan = true
107
+ return nil
108
+ end
109
+ end
110
+ if !found_free_plan
111
+ begin
112
+ shop_domain = self[AppManager.configuration.shopify_domain_field]
113
+ plan_obj = AppManager::Client.new
114
+ return plan_obj.get_charge(shop_domain)
115
+ rescue Exception => e
116
+ Rails.logger.info "#{e.inspect}"
117
+ return nil
118
+ end
119
+ end
120
+ end
121
+
122
+ def get_all_plans
86
123
  begin
87
124
  shop_domain = self[AppManager.configuration.shopify_domain_field]
125
+ shop_plan_id = self[AppManager.configuration.plan_id_or_name_field]
126
+ active_plan_id = shop_plan_id && shop_plan_id.nil? ? nil : shop_plan_id
88
127
  plan_obj = AppManager::Client.new
89
- return plan_obj.get_charge(shop_domain)
128
+ return plan_obj.get_plans(shop_domain,active_plan_id)
90
129
  rescue Exception => e
91
- return "#{e.inspect}"
130
+ Rails.logger.info "#{e.inspect}"
131
+ return []
92
132
  end
93
133
  end
94
134
 
135
+ def cancel_charge
136
+ begin
137
+ shop_plan_id = self[AppManager.configuration.plan_id_or_name_field]
138
+ shop_domain = self[AppManager.configuration.shopify_domain_field]
139
+ if shop_plan_id && shop_domain
140
+ plan_obj = AppManager::Client.new
141
+ plan_obj.cancel_charge(shop_domain,shop_plan_id)
142
+ end
143
+ rescue Exception => e
144
+ return "#{e.inspect}"
145
+ end
146
+ end
147
+
95
148
  def set_default_plan(plan_id=nil)
96
149
  begin
97
150
  if plan_id.nil?
@@ -117,15 +170,17 @@ module AppManager
117
170
 
118
171
 
119
172
 
120
- def get_plans_by_features(feature_slugs)
173
+ def get_plans_by_features(feature_slugs,params="false")
121
174
  if feature_slugs.any?
175
+ shop_plan_id = self[AppManager.configuration.plan_id_or_name_field]
122
176
  plan_data = []
123
177
  shop_domain = self[AppManager.configuration.shopify_domain_field]
124
178
  plan_obj = AppManager::Client.new
125
179
  plans = plan_obj.get_plans(shop_domain)
126
180
  plans = plans.select{|x| x['interval'] == 'EVERY_30_DAYS' && x['public'] == true}
127
181
  plans.each do |plan|
128
- if plan['features'].any? && plan['features'].values.any? && plan['features'].values.collect{|c| c['slug']}.any?
182
+ next if (params && !params.nil? && params == "exclude_current_plan" && !shop_plan_id.nil? && plan["id"] == shop_plan_id)
183
+ 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?)
129
184
  plan_feature_slugs = plan['features'].values.collect{|c| c['slug']}.sort
130
185
  if (feature_slugs & plan_feature_slugs).any? && ((feature_slugs & plan_feature_slugs).sort == feature_slugs.sort)
131
186
  plan_data.push(plan)
@@ -138,6 +193,8 @@ module AppManager
138
193
  end
139
194
  end
140
195
 
196
+
197
+
141
198
  private
142
199
 
143
200
  def casted_value(value,value_type)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AppManager
4
- VERSION = "1.1.8"
4
+ VERSION = "1.2.2"
5
5
  end
@@ -84,5 +84,6 @@ end
84
84
  ActiveSupport::Notifications.subscribe "charge_created" do |name, start, finish, id, payload|
85
85
  Rails.logger.debug "========APP MANAGER========="
86
86
  Rails.logger.debug "DATA : #{payload}"
87
+ #payload hash contains 3 keys, "plan", "charge", "previous_charge" and an extra key "choose_later" => "true" renders when clicked on choose later
87
88
  Rails.logger.debug "========APP MANAGER========="
88
89
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.8
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hulkapps
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-30 00:00:00.000000000 Z
11
+ date: 2022-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty