forest_liana 1.3.28 → 1.3.29

Sign up to get free protection for your applications and to get access to all the features.
Files changed (26) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/forest_liana/intercom_controller.rb +8 -1
  3. data/app/controllers/forest_liana/stripe_controller.rb +33 -0
  4. data/app/models/forest_liana/model/collection.rb +1 -1
  5. data/app/serializers/forest_liana/collection_serializer.rb +3 -0
  6. data/app/serializers/forest_liana/intercom_attribute_serializer.rb +1 -1
  7. data/app/serializers/forest_liana/intercom_conversation_serializer.rb +1 -1
  8. data/app/serializers/forest_liana/serializer_factory.rb +44 -8
  9. data/app/serializers/forest_liana/stripe_bank_account_serializer.rb +43 -0
  10. data/app/serializers/forest_liana/stripe_card_serializer.rb +1 -1
  11. data/app/serializers/forest_liana/stripe_invoice_serializer.rb +1 -1
  12. data/app/serializers/forest_liana/stripe_payment_serializer.rb +1 -1
  13. data/app/serializers/forest_liana/stripe_subscription_serializer.rb +45 -0
  14. data/app/services/forest_liana/intercom_attributes_getter.rb +3 -11
  15. data/app/services/forest_liana/intercom_conversations_getter.rb +3 -10
  16. data/app/services/forest_liana/operator_value_parser.rb +1 -1
  17. data/app/services/forest_liana/schema_adapter.rb +61 -31
  18. data/app/services/forest_liana/stripe_bank_accounts_getter.rb +86 -0
  19. data/app/services/forest_liana/stripe_cards_getter.rb +26 -22
  20. data/app/services/forest_liana/stripe_invoices_getter.rb +28 -26
  21. data/app/services/forest_liana/stripe_payments_getter.rb +28 -25
  22. data/app/services/forest_liana/stripe_subscriptions_getter.rb +97 -0
  23. data/config/routes.rb +5 -2
  24. data/lib/forest_liana/bootstraper.rb +205 -30
  25. data/lib/forest_liana/version.rb +1 -1
  26. metadata +6 -2
@@ -0,0 +1,86 @@
1
+ module ForestLiana
2
+ class StripeBankAccountsGetter
3
+ attr_accessor :records
4
+
5
+ def initialize(params, secret_key, reference)
6
+ @params = params
7
+ Stripe.api_key = ForestLiana.integrations[:stripe][:api_key]
8
+ end
9
+
10
+ def count
11
+ @cards.try(:total_count) || 0
12
+ end
13
+
14
+ def perform
15
+ params = {
16
+ limit: limit,
17
+ starting_after: starting_after,
18
+ ending_before: ending_before,
19
+ object: 'bank_account'
20
+ }
21
+ params['include[]'] = 'total_count'
22
+
23
+ resource = collection.find(@params[:id])
24
+ customer = resource[field]
25
+
26
+ if customer.blank?
27
+ @records = []
28
+ else
29
+ fetch_bank_accounts(customer, params)
30
+ end
31
+ end
32
+
33
+ def fetch_bank_accounts(customer, params)
34
+ @cards = Stripe::Customer.retrieve(customer).sources.all(params)
35
+ if @cards.blank?
36
+ @records = []
37
+ return
38
+ end
39
+
40
+ @records = @cards.data.map do |d|
41
+ query = {}
42
+ query[field] = d.customer
43
+ d.customer = collection.find_by(query)
44
+
45
+ d
46
+ end
47
+ end
48
+
49
+ def starting_after
50
+ if pagination? && @params[:starting_after]
51
+ @params[:starting_after]
52
+ end
53
+ end
54
+
55
+ def ending_before
56
+ if pagination? && @params[:ending_before]
57
+ @params[:ending_before]
58
+ end
59
+ end
60
+
61
+ def limit
62
+ return 10 unless pagination?
63
+
64
+ if @params[:page][:size]
65
+ @params[:page][:size].to_i
66
+ else
67
+ 10
68
+ end
69
+ end
70
+
71
+ def pagination?
72
+ @params[:page]
73
+ end
74
+
75
+ def collection
76
+ @params[:collection].singularize.capitalize.constantize
77
+ end
78
+
79
+ def field
80
+ ForestLiana.integrations[:stripe][:mapping].select { |value|
81
+ value.split('.')[0] == ForestLiana::SchemaUtils
82
+ .find_model_from_table_name(@params[:collection]).try(:name)
83
+ }.first.split('.')[1]
84
+ end
85
+ end
86
+ end
@@ -12,11 +12,16 @@ module ForestLiana
12
12
  end
13
13
 
14
14
  def perform
15
- params = { limit: limit, offset: offset, object: 'card' }
15
+ params = {
16
+ limit: limit,
17
+ starting_after: starting_after,
18
+ ending_before: ending_before,
19
+ object: 'card'
20
+ }
16
21
  params['include[]'] = 'total_count'
17
22
 
18
- resource = user_collection.find(@params[:id])
19
- customer = resource[user_field]
23
+ resource = collection.find(@params[:id])
24
+ customer = resource[field]
20
25
 
21
26
  if customer.blank?
22
27
  @records = []
@@ -34,21 +39,22 @@ module ForestLiana
34
39
 
35
40
  @records = @cards.data.map do |d|
36
41
  query = {}
37
- query[user_field] = d.customer
38
- d.customer = user_collection.find_by(query)
42
+ query[field] = d.customer
43
+ d.customer = collection.find_by(query)
39
44
 
40
45
  d
41
46
  end
42
47
  end
43
48
 
44
- def offset
45
- return 0 unless pagination?
49
+ def starting_after
50
+ if pagination? && @params[:starting_after]
51
+ @params[:starting_after]
52
+ end
53
+ end
46
54
 
47
- number = @params[:page][:number]
48
- if number && number.to_i > 0
49
- (number.to_i - 1) * limit
50
- else
51
- 0
55
+ def ending_before
56
+ if pagination? && @params[:ending_before]
57
+ @params[:ending_before]
52
58
  end
53
59
  end
54
60
 
@@ -63,20 +69,18 @@ module ForestLiana
63
69
  end
64
70
 
65
71
  def pagination?
66
- @params[:page] && @params[:page][:number]
72
+ @params[:page]
67
73
  end
68
74
 
69
- def user_collection
70
- ForestLiana.integrations
71
- .try(:[], :stripe)
72
- .try(:[], :user_collection)
73
- .try(:constantize)
75
+ def collection
76
+ @params[:collection].singularize.capitalize.constantize
74
77
  end
75
78
 
76
- def user_field
77
- ForestLiana.integrations
78
- .try(:[], :stripe)
79
- .try(:[], :user_field)
79
+ def field
80
+ ForestLiana.integrations[:stripe][:mapping].select { |value|
81
+ value.split('.')[0] == ForestLiana::SchemaUtils
82
+ .find_model_from_table_name(@params[:collection]).try(:name)
83
+ }.first.split('.')[1]
80
84
  end
81
85
  end
82
86
  end
@@ -12,11 +12,15 @@ module ForestLiana
12
12
  end
13
13
 
14
14
  def perform
15
- query = { limit: limit, offset: offset }
16
-
17
- if @params[:id] && user_collection && user_field
18
- resource = user_collection.find(@params[:id])
19
- query[:customer] = resource[user_field]
15
+ query = {
16
+ limit: limit,
17
+ starting_after: starting_after,
18
+ ending_before: ending_before
19
+ }
20
+
21
+ if @params[:id] && collection && field
22
+ resource = collection.find(@params[:id])
23
+ query[:customer] = resource[field]
20
24
  end
21
25
 
22
26
  query['include[]'] = 'total_count'
@@ -35,9 +39,9 @@ module ForestLiana
35
39
  d.amount_due /= 100.00
36
40
 
37
41
  query = {}
38
- query[user_field] = d.customer
39
- if user_collection
40
- d.customer = user_collection.find_by(query)
42
+ query[field] = d.customer
43
+ if collection
44
+ d.customer = collection.find_by(query)
41
45
  else
42
46
  d.customer = nil
43
47
  end
@@ -51,14 +55,15 @@ module ForestLiana
51
55
  Stripe::Invoice.all(params)
52
56
  end
53
57
 
54
- def offset
55
- return 0 unless pagination?
58
+ def starting_after
59
+ if pagination? && @params[:starting_after]
60
+ @params[:starting_after]
61
+ end
62
+ end
56
63
 
57
- number = @params[:page][:number]
58
- if number && number.to_i > 0
59
- (number.to_i - 1) * limit
60
- else
61
- 0
64
+ def ending_before
65
+ if pagination? && @params[:ending_before]
66
+ @params[:ending_before]
62
67
  end
63
68
  end
64
69
 
@@ -73,21 +78,18 @@ module ForestLiana
73
78
  end
74
79
 
75
80
  def pagination?
76
- @params[:page] && @params[:page][:number]
81
+ @params[:page]
77
82
  end
78
83
 
79
- def user_collection
80
- ForestLiana.integrations
81
- .try(:[], :stripe)
82
- .try(:[], :user_collection)
83
- .try(:constantize)
84
+ def collection
85
+ @params[:collection].singularize.capitalize.constantize
84
86
  end
85
87
 
86
- def user_field
87
- ForestLiana.integrations
88
- .try(:[], :stripe)
89
- .try(:[], :user_field)
88
+ def field
89
+ ForestLiana.integrations[:stripe][:mapping].select { |value|
90
+ value.split('.')[0] == ForestLiana::SchemaUtils
91
+ .find_model_from_table_name(@params[:collection]).try(:name)
92
+ }.first.split('.')[1]
90
93
  end
91
94
  end
92
95
  end
93
-
@@ -12,11 +12,15 @@ module ForestLiana
12
12
  end
13
13
 
14
14
  def perform
15
- query = { limit: limit, offset: offset }
16
-
17
- if @params[:id] && user_collection && user_field
18
- resource = user_collection.find(@params[:id])
19
- query[:customer] = resource[user_field]
15
+ query = {
16
+ limit: limit,
17
+ starting_after: starting_after,
18
+ ending_before: ending_before
19
+ }
20
+
21
+ if @params[:id] && collection && field
22
+ resource = collection.find(@params[:id])
23
+ query[:customer] = resource[field]
20
24
  end
21
25
 
22
26
  query['source'] = { object: :card }
@@ -33,9 +37,9 @@ module ForestLiana
33
37
  d.amount /= 100.00
34
38
 
35
39
  query = {}
36
- query[user_field] = d.customer
37
- if user_collection
38
- d.customer = user_collection.find_by(query)
40
+ query[field] = d.customer
41
+ if collection
42
+ d.customer = collection.find_by(query)
39
43
  else
40
44
  d.customer = nil
41
45
  end
@@ -49,14 +53,15 @@ module ForestLiana
49
53
  Stripe::Charge.all(params)
50
54
  end
51
55
 
52
- def offset
53
- return 0 unless pagination?
56
+ def starting_after
57
+ if pagination? && @params[:starting_after]
58
+ @params[:starting_after]
59
+ end
60
+ end
54
61
 
55
- number = @params[:page][:number]
56
- if number && number.to_i > 0
57
- (number.to_i - 1) * limit
58
- else
59
- 0
62
+ def ending_before
63
+ if pagination? && @params[:ending_before]
64
+ @params[:ending_before]
60
65
  end
61
66
  end
62
67
 
@@ -71,20 +76,18 @@ module ForestLiana
71
76
  end
72
77
 
73
78
  def pagination?
74
- @params[:page] && @params[:page][:number]
79
+ @params[:page]
75
80
  end
76
81
 
77
- def user_collection
78
- ForestLiana.integrations
79
- .try(:[], :stripe)
80
- .try(:[], :user_collection)
81
- .try(:constantize)
82
+ def collection
83
+ @params[:collection].singularize.capitalize.constantize
82
84
  end
83
85
 
84
- def user_field
85
- ForestLiana.integrations
86
- .try(:[], :stripe)
87
- .try(:[], :user_field)
86
+ def field
87
+ ForestLiana.integrations[:stripe][:mapping].select { |value|
88
+ value.split('.')[0] == ForestLiana::SchemaUtils
89
+ .find_model_from_table_name(@params[:collection]).try(:name)
90
+ }.first.split('.')[1]
88
91
  end
89
92
  end
90
93
  end
@@ -0,0 +1,97 @@
1
+ module ForestLiana
2
+ class StripeSubscriptionsGetter
3
+ attr_accessor :records
4
+
5
+ def initialize(params, secret_key, reference)
6
+ @params = params
7
+ Stripe.api_key = ForestLiana.integrations[:stripe][:api_key]
8
+ end
9
+
10
+ def count
11
+ @subscriptions.try(:total_count) || 0
12
+ end
13
+
14
+ def perform
15
+ query = {
16
+ limit: limit,
17
+ starting_after: starting_after,
18
+ ending_before: ending_before
19
+ }
20
+
21
+ if @params[:id] && collection && field
22
+ resource = collection.find(@params[:id])
23
+ query[:customer] = resource[field]
24
+ end
25
+
26
+ query['include[]'] = 'total_count'
27
+ @subscriptions = fetch_subscriptions(query)
28
+ if @subscriptions.blank?
29
+ @records = []
30
+ return
31
+ end
32
+
33
+ @records = @subscriptions.data.map do |d|
34
+ d.date = Time.at(d.date).to_datetime
35
+ d.period_start = Time.at(d.period_start).to_datetime
36
+ d.period_end = Time.at(d.period_end).to_datetime
37
+ d.amount_due /= 100.00
38
+ d.subtotal /= 100.00
39
+ d.total /= 100.00
40
+ d.application_fee /= 100.00
41
+ d.tax /= 100.00
42
+
43
+ query = {}
44
+ query[field] = d.customer
45
+ if collection
46
+ d.customer = collection.find_by(query)
47
+ else
48
+ d.customer = nil
49
+ end
50
+
51
+ d
52
+ end
53
+ end
54
+
55
+ def fetch_subscriptions(params)
56
+ return if @params[:id] && params[:customer].blank?
57
+ Stripe::Subscription.all(params)
58
+ end
59
+
60
+ def starting_after
61
+ if pagination? && @params[:starting_after]
62
+ @params[:starting_after]
63
+ end
64
+ end
65
+
66
+ def ending_before
67
+ if pagination? && @params[:ending_before]
68
+ @params[:ending_before]
69
+ end
70
+ end
71
+
72
+ def limit
73
+ return 10 unless pagination?
74
+
75
+ if @params[:page][:size]
76
+ @params[:page][:size].to_i
77
+ else
78
+ 10
79
+ end
80
+ end
81
+
82
+ def pagination?
83
+ @params[:page]
84
+ end
85
+
86
+ def collection
87
+ @params[:collection].singularize.capitalize.constantize
88
+ end
89
+
90
+ def field
91
+ ForestLiana.integrations[:stripe][:mapping].select { |value|
92
+ value.split('.')[0] == ForestLiana::SchemaUtils
93
+ .find_model_from_table_name(@params[:collection]).try(:name)
94
+ }.first.split('.')[1]
95
+ end
96
+ end
97
+ end
data/config/routes.rb CHANGED
@@ -3,12 +3,15 @@ ForestLiana::Engine.routes.draw do
3
3
  post 'sessions' => 'sessions#create'
4
4
 
5
5
  # Stripe Integration
6
- get 'stripe_payments' => 'stripe#payments'
6
+ get '(:collection)_stripe_payments' => 'stripe#payments'
7
7
  get ':collection/:id/stripe_payments' => 'stripe#payments'
8
8
  post 'stripe_payments/refunds' => 'stripe#refund'
9
- get 'stripe_invoices' => 'stripe#invoices'
9
+ get '(:collection)_stripe_invoices' => 'stripe#invoices'
10
10
  get ':collection/:id/stripe_invoices' => 'stripe#invoices'
11
11
  get ':collection/:id/stripe_cards' => 'stripe#cards'
12
+ get '(:collection)_stripe_subscriptions' => 'stripe#subscriptions'
13
+ get ':collection/:id/stripe_subscriptions' => 'stripe#subscriptions'
14
+ get ':collection/:id/stripe_bank_accounts' => 'stripe#bank_accounts'
12
15
 
13
16
  # Intercom Integration
14
17
  get ':collection/:id/intercom_conversations' => 'intercom#user_conversations'