chargebee_rails 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -14
- data/app/controllers/chargebee_rails/webhooks_controller.rb +5 -1
- data/lib/chargebee_rails.rb +9 -7
- data/lib/chargebee_rails/hosted_page_subscription_manager.rb +4 -1
- data/lib/chargebee_rails/subscription.rb +15 -0
- data/lib/chargebee_rails/sync_plans.rb +92 -0
- data/lib/chargebee_rails/version.rb +1 -1
- data/lib/chargebee_rails/webhook_handler.rb +76 -12
- data/lib/tasks/sync_plans.rake +4 -75
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11ff4b416fd4203067991d079471d7368cab7d60
|
4
|
+
data.tar.gz: 98ed912ceacbc95089c1f12e8289a8ae649e7d5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e476b76a87343f79a4a1b8afea8fb1b0b5c667ccbb9db5aac92fbb28640debb17281ffc3c31c703bd79a9030bac292657285fa63f18797774bcb70dbb0745db3
|
7
|
+
data.tar.gz: 41ad4b3e6163f875d3f5b605f27d4a04efc6098735a9f88c8e0283eeb0ba7e5925ba7c7ab32566864ba13b24673d38f1c3c4960257034a53c4a94fd6fb7e3047
|
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
# Chargebee Rails
|
2
|
+
|
3
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/2f9b7a79d17605b51f5e/maintainability)](https://codeclimate.com/github/spritlesoftware/chargebee-rails-subscriptions/maintainability)
|
4
|
+
|
2
5
|
This is the Rails gem for integrating with Chargebee. If you're new to Chargebee, sign up for an account [here](https://www.chargebee.com).
|
3
6
|
|
4
7
|
[Introduction](#introduction)
|
@@ -67,7 +70,7 @@ If the rest-client version is less than 1.8.0, update the latest version by runn
|
|
67
70
|
|
68
71
|
# Installation
|
69
72
|
|
70
|
-
|
73
|
+
### Step 1: Install the “chargebee_rails” gem to your application
|
71
74
|
|
72
75
|
Add the below line to your Gemfile:
|
73
76
|
|
@@ -87,7 +90,7 @@ And, run
|
|
87
90
|
```
|
88
91
|
|
89
92
|
|
90
|
-
|
93
|
+
### Step 2: Add Subscription models to your app
|
91
94
|
|
92
95
|
The entity that uniquely identifies a customer account within your application is referred to as a subscription owner module. For example, if you are building a CRM application, the entity that represents the customer’s account will be your subscription owner entity.
|
93
96
|
|
@@ -119,7 +122,7 @@ Now, you will have models and database tables set for subscriptions, plans, paym
|
|
119
122
|
|
120
123
|
|
121
124
|
|
122
|
-
|
125
|
+
### Step 3: Set up Chargebee
|
123
126
|
|
124
127
|
|
125
128
|
Configure your Chargebee site name and API key in the `config/initializers/chargebee_rails.rb` file.
|
@@ -137,12 +140,12 @@ Configure your Chargebee site name and API key in the `config/initializers/charg
|
|
137
140
|
```
|
138
141
|
|
139
142
|
|
140
|
-
##Gateway credentials
|
143
|
+
## Gateway credentials
|
141
144
|
|
142
145
|
Payment Gateway credentials have to be set up in Chargebee under *Settings> Site Settings> Gateway Settings*.
|
143
146
|
|
144
147
|
|
145
|
-
##Webhook notifications
|
148
|
+
## Webhook notifications
|
146
149
|
|
147
150
|
You can set up basic authentication for your incoming webhook notifications in `config/initializers/chargebee_rails.rb` file.
|
148
151
|
|
@@ -156,7 +159,7 @@ You can set up basic authentication for your incoming webhook notifications in `
|
|
156
159
|
|
157
160
|
|
158
161
|
|
159
|
-
##Set the controller name used to handle webhooks
|
162
|
+
## Set the controller name used to handle webhooks
|
160
163
|
|
161
164
|
If you’d like to use a different controller to handle webhooks, you can extend the `ChargebeeRails::WebhookController` and add the controller name in config.webhook_handler.
|
162
165
|
|
@@ -178,7 +181,7 @@ For instance, if you have a controller *MyAppEventsController* in the `my_app_ev
|
|
178
181
|
|
179
182
|
```
|
180
183
|
|
181
|
-
##Configure the webhook url in Chargebee
|
184
|
+
## Configure the webhook url in Chargebee
|
182
185
|
|
183
186
|
Configure the webhook url in Chargebee under *API & Webhooks> Webhook* Settings. The path can be specified as shown below:
|
184
187
|
|
@@ -193,7 +196,7 @@ Configure the webhook url in Chargebee under *API & Webhooks> Webhook* Settings.
|
|
193
196
|
The webhook url for your site will be _http(s)://<your-domain>.com/chargebee_rails_event_.
|
194
197
|
|
195
198
|
|
196
|
-
##Sync plans
|
199
|
+
## Sync plans
|
197
200
|
|
198
201
|
Currently Chargebee does not support webhook notifications for addition, update and removal of Plans. However, this gem comes with a rake task to sync plans between Chargebee and your application. Hence, each time a plan is created in Chargebee, it will automatically be synced with your application. In the future, we will have webhooks events in place to support plan related operations. Once that's done, the rake task’s code will be included as part of the event handler.
|
199
202
|
|
@@ -209,7 +212,7 @@ The plans can be synced to your application using the following command:
|
|
209
212
|
**Note**: The archived plans will also be synced in this method.
|
210
213
|
|
211
214
|
|
212
|
-
##Sync failed events
|
215
|
+
## Sync failed events
|
213
216
|
|
214
217
|
Chargebee attempts to send webhook notifications for upto 2 days. After 2 days, if the webhook event has failed due to some reason, the webhook’s status is marked as “Failed” and further attempts are stopped. Once the error has been fixed at your end, the rake task will sync the failed events with your application. The failed events will be selectively sent to the webhook handler as well as hook methods, provided the event does not have an outdated update.
|
215
218
|
|
@@ -222,7 +225,7 @@ Chargebee attempts to send webhook notifications for upto 2 days. After 2 days,
|
|
222
225
|
|
223
226
|
|
224
227
|
|
225
|
-
##Sync events with your application
|
228
|
+
## Sync events with your application
|
226
229
|
|
227
230
|
The event types listed below are synced with the application by this gem
|
228
231
|
|
@@ -256,7 +259,7 @@ The event types listed below are synced with the application by this gem
|
|
256
259
|
|
257
260
|
|
258
261
|
|
259
|
-
##Configure your default plan Id
|
262
|
+
## Configure your default plan Id#
|
260
263
|
|
261
264
|
When a customer signs up for a trial account, you will associate the subscription with a particular plan in Chargebee. This plan can be configured as the default plan in the gem, so that the the plan name is automatically passed during subscription creation.
|
262
265
|
This way, when calling the [create a subscription](https://apidocs.chargebee.com/docs/api/subscriptions#create_a_subscription) API, if the plan id is not passed in the subscription method, it will be taken from `config.default_plan_id`.
|
@@ -270,7 +273,7 @@ This way, when calling the [create a subscription](https://apidocs.chargebee.com
|
|
270
273
|
|
271
274
|
|
272
275
|
|
273
|
-
##Advanced settings
|
276
|
+
## Advanced settings
|
274
277
|
|
275
278
|
If you would like to control the subscription upgrade/downgrade behaviour, you can specify this in:
|
276
279
|
|
@@ -309,7 +312,7 @@ If you’d like to include delayed charges during [update_subscription_estimate]
|
|
309
312
|
|
310
313
|
```
|
311
314
|
|
312
|
-
##Customer
|
315
|
+
## Customer
|
313
316
|
|
314
317
|
**Retrieve as Chargebee Customer**
|
315
318
|
|
@@ -346,7 +349,7 @@ If you’d like to include delayed charges during [update_subscription_estimate]
|
|
346
349
|
```
|
347
350
|
|
348
351
|
|
349
|
-
##Subscription
|
352
|
+
## Subscription
|
350
353
|
|
351
354
|
**Create a Subscription**
|
352
355
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module ChargebeeRails
|
2
2
|
class WebhooksController < ActionController::Base
|
3
3
|
include WebhookHandler
|
4
|
-
|
4
|
+
before_action :authenticate, if: :chargebee_configuration_webhook
|
5
5
|
|
6
6
|
# Handle ChargeBee webhook events
|
7
7
|
# From the post request received from chargebee, the event for which the
|
@@ -20,6 +20,10 @@ module ChargebeeRails
|
|
20
20
|
|
21
21
|
private
|
22
22
|
|
23
|
+
def chargebee_configuration_webhook
|
24
|
+
ChargebeeRails.configuration.secure_webhook_api
|
25
|
+
end
|
26
|
+
|
23
27
|
def log_errors(e)
|
24
28
|
logger.error e.message
|
25
29
|
e.backtrace.each { |line| logger.error " #{line}" }
|
data/lib/chargebee_rails.rb
CHANGED
@@ -11,13 +11,15 @@ require "chargebee_rails/subscription_builder"
|
|
11
11
|
require "chargebee_rails/hosted_page_subscription_manager"
|
12
12
|
require "chargebee_rails/customer"
|
13
13
|
require "chargebee_rails/metered_billing"
|
14
|
+
require "chargebee_rails/sync_plans"
|
15
|
+
|
14
16
|
|
15
17
|
module ChargebeeRails
|
16
18
|
# This method is used to update the chargebee customer and also reflects the
|
17
19
|
# changes to the subscription owner in the application.
|
18
20
|
# * *Args* :
|
19
21
|
# - +customer+ -> the subscription owner
|
20
|
-
# - +options+ -> the options hash allowed for customer update in chargebee
|
22
|
+
# - +options+ -> the options hash allowed for customer update in chargebee
|
21
23
|
# For more details on the options hash, refer the input parameters for
|
22
24
|
# https://apidocs.chargebee.com/docs/api/customers?lang=ruby#update_a_customer
|
23
25
|
# * *Returns* :
|
@@ -32,11 +34,11 @@ module ChargebeeRails
|
|
32
34
|
end
|
33
35
|
|
34
36
|
# Update the billing information of the chargebee customer. The changes in the
|
35
|
-
# billing details are also reflected in the subscription owner's
|
36
|
-
# +chargebee_customer_data+
|
37
|
+
# billing details are also reflected in the subscription owner's
|
38
|
+
# +chargebee_customer_data+
|
37
39
|
# * *Args* :
|
38
40
|
# - +customer+ -> the subscription owner
|
39
|
-
# - +options+ -> the options hash allowed for updating customer billing info in chargebee
|
41
|
+
# - +options+ -> the options hash allowed for updating customer billing info in chargebee
|
40
42
|
# For more details on the options hash, refer the input parameters for
|
41
43
|
# https://apidocs.chargebee.com/docs/api/customers?lang=ruby#update_billing_info_for_a_customer
|
42
44
|
# * *Returns* :
|
@@ -50,11 +52,11 @@ module ChargebeeRails
|
|
50
52
|
customer
|
51
53
|
end
|
52
54
|
|
53
|
-
# Add contacts to a chargebee customer, the subscription owner
|
55
|
+
# Add contacts to a chargebee customer, the subscription owner
|
54
56
|
# and the contact details hash is given as options.
|
55
57
|
# * *Args* :
|
56
58
|
# - +customer+ -> the subscription owner
|
57
|
-
# - +options+ -> the options hash allowed for adding contacts to customer in chargebee
|
59
|
+
# - +options+ -> the options hash allowed for adding contacts to customer in chargebee
|
58
60
|
# For more details on the options hash, refer the input parameters for
|
59
61
|
# https://apidocs.chargebee.com/docs/api/customers?lang=ruby#add_contacts_to_a_customer
|
60
62
|
# * *Returns* :
|
@@ -71,7 +73,7 @@ module ChargebeeRails
|
|
71
73
|
# subscription owner
|
72
74
|
# * *Args* :
|
73
75
|
# - +customer+ -> the subscription owner
|
74
|
-
# - +options+ -> the options hash allowed for updating customer contacts in chargebee
|
76
|
+
# - +options+ -> the options hash allowed for updating customer contacts in chargebee
|
75
77
|
# For more details on the options hash, refer the input parameters for
|
76
78
|
# https://apidocs.chargebee.com/docs/api/customers?lang=ruby#update_contacts_for_a_customer
|
77
79
|
# * *Returns* :
|
@@ -22,7 +22,10 @@ module ChargebeeRails
|
|
22
22
|
# from the subscription details got from chargebee's hosted page
|
23
23
|
def update
|
24
24
|
@subscription = @customer.subscription
|
25
|
-
|
25
|
+
|
26
|
+
#if we're just updating the payment method, then the subscription can be empty
|
27
|
+
@subscription.update(subscription_attrs) if hosted_subscription.present?
|
28
|
+
|
26
29
|
manage_payment_method if hosted_payment_method.present?
|
27
30
|
@subscription
|
28
31
|
end
|
@@ -51,6 +51,21 @@ module ChargebeeRails
|
|
51
51
|
update(subscription_attrs(chargebee_subscription, self.plan))
|
52
52
|
end
|
53
53
|
|
54
|
+
#
|
55
|
+
# Remove scheduled changes to the subscription
|
56
|
+
#
|
57
|
+
def remove_scheduled_changes
|
58
|
+
begin
|
59
|
+
chargebee_subscription = ChargeBee::Subscription.remove_scheduled_changes(
|
60
|
+
chargebee_id
|
61
|
+
).subscription
|
62
|
+
update(subscription_attrs(chargebee_subscription, self.plan))
|
63
|
+
rescue ChargeBee::InvalidRequestError
|
64
|
+
Rails.logger.warn("No changes are scheduled for this subscription")
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
54
69
|
#
|
55
70
|
# Add or remove addons for the subscription
|
56
71
|
# * *Args* :
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module ChargebeeRails
|
2
|
+
class SyncPlans
|
3
|
+
attr_accessor :messages
|
4
|
+
|
5
|
+
def self.sync(create:true, update:true, delete:false)
|
6
|
+
syncer = SyncPlans.new
|
7
|
+
return syncer.do_sync(create:create, update:update, delete:delete)
|
8
|
+
end
|
9
|
+
|
10
|
+
def do_sync(create:true, update:true, delete:false)
|
11
|
+
self.get_plans
|
12
|
+
self.sync_plans(create:create, update:update, delete:delete)
|
13
|
+
|
14
|
+
return messages
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def output(message)
|
20
|
+
puts(message)
|
21
|
+
self.messages ||= []
|
22
|
+
self.messages << message
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_plans
|
26
|
+
loop do
|
27
|
+
plan_list = retrieve_plan_list
|
28
|
+
@offset = plan_list.next_offset
|
29
|
+
cb_plans << plan_list.flat_map(&:plan)
|
30
|
+
break unless @offset.present?
|
31
|
+
end
|
32
|
+
@cb_plans = cb_plans.flatten
|
33
|
+
end
|
34
|
+
|
35
|
+
def cb_plans
|
36
|
+
@cb_plans ||= []
|
37
|
+
end
|
38
|
+
|
39
|
+
def sync_plans(create:true, update:true, delete:false)
|
40
|
+
output "Removed #{remove_plans.count} plan(s)" if (delete)
|
41
|
+
output "Created #{create_new_plans.count} plan(s)" if (create)
|
42
|
+
output "Updated all #{update_all_plans.count} plan(s)" if (update)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Retrieve the plan list from chargebee
|
46
|
+
def retrieve_plan_list
|
47
|
+
options = { limit: 100 }
|
48
|
+
options[:offset] = @offset if @offset.present?
|
49
|
+
ChargeBee::Plan.list(options)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Remove plans from application that do not exist in chargebee
|
53
|
+
def remove_plans
|
54
|
+
cb_plan_ids = cb_plans.flat_map(&:id)
|
55
|
+
Plan.all.reject { |plan| cb_plan_ids.include?(plan.plan_id) }
|
56
|
+
.each { |plan| output "Deleting Plan - #{plan.plan_id}"; plan.destroy }
|
57
|
+
end
|
58
|
+
|
59
|
+
# Create new plans that are not present in app but are available in chargebee
|
60
|
+
def create_new_plans
|
61
|
+
plan_ids = Plan.all.map(&:plan_id)
|
62
|
+
cb_plans.reject { |cb_plan| plan_ids.include?(cb_plan.id) }
|
63
|
+
.each { |new_plan| output "Creating Plan - #{new_plan.id}"; Plan.create(plan_params(new_plan)) }
|
64
|
+
end
|
65
|
+
|
66
|
+
# Update all existing plans in the application
|
67
|
+
def update_all_plans
|
68
|
+
cb_plans.map do |cb_plan|
|
69
|
+
Plan.find_by(plan_id: cb_plan.id).update(plan_params(cb_plan))
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Build the plan params to be created or updated in the application
|
74
|
+
def plan_params plan
|
75
|
+
{
|
76
|
+
name: plan.name,
|
77
|
+
plan_id: plan.id,
|
78
|
+
status: plan.status,
|
79
|
+
chargebee_data: {
|
80
|
+
price: plan.price,
|
81
|
+
period: plan.period,
|
82
|
+
period_unit: plan.period_unit,
|
83
|
+
trial_period: plan.trial_period,
|
84
|
+
trial_period_unit: plan.trial_period_unit,
|
85
|
+
charge_model: plan.charge_model,
|
86
|
+
free_quantity: plan.free_quantity
|
87
|
+
}
|
88
|
+
}
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
@@ -5,8 +5,16 @@ module ChargebeeRails
|
|
5
5
|
# Handle the ChargeBee event retrieved from webhook and call the
|
6
6
|
# corresponding event type handler for the event
|
7
7
|
def handle(chargebee_event)
|
8
|
-
|
9
|
-
|
8
|
+
@chargebee_event = chargebee_event
|
9
|
+
if sync_events_list.include?(event.event_type)
|
10
|
+
sync_events
|
11
|
+
end
|
12
|
+
|
13
|
+
if self.respond_to?(event.event_type)
|
14
|
+
send(event.event_type)
|
15
|
+
else
|
16
|
+
Rails.logger.warn("Unrecognised chargebee event type #{event.event_type}")
|
17
|
+
end
|
10
18
|
end
|
11
19
|
|
12
20
|
# Set event as ChargeBee event
|
@@ -14,7 +22,37 @@ module ChargebeeRails
|
|
14
22
|
@event ||= @chargebee_event
|
15
23
|
end
|
16
24
|
|
17
|
-
# All the event types in ChargeBee
|
25
|
+
# All the event types in ChargeBee as of API V2
|
26
|
+
|
27
|
+
def plan_created; end
|
28
|
+
|
29
|
+
def plan_updated; end
|
30
|
+
|
31
|
+
def plan_deleted; end
|
32
|
+
|
33
|
+
def addon_created; end
|
34
|
+
|
35
|
+
def addon_updated; end
|
36
|
+
|
37
|
+
def addon_deleted; end
|
38
|
+
|
39
|
+
def coupon_created; end
|
40
|
+
|
41
|
+
def coupon_updated; end
|
42
|
+
|
43
|
+
def coupon_deleted; end
|
44
|
+
|
45
|
+
def coupon_set_created; end
|
46
|
+
|
47
|
+
def coupon_set_updated; end
|
48
|
+
|
49
|
+
def coupon_set_deleted; end
|
50
|
+
|
51
|
+
def coupon_codes_added; end
|
52
|
+
|
53
|
+
def coupon_codes_deleted; end
|
54
|
+
|
55
|
+
def coupon_codes_updated; end
|
18
56
|
|
19
57
|
def customer_created; end
|
20
58
|
|
@@ -22,6 +60,10 @@ module ChargebeeRails
|
|
22
60
|
|
23
61
|
def customer_deleted; end
|
24
62
|
|
63
|
+
def customer_moved_out; end
|
64
|
+
|
65
|
+
def customer_moved_in; end
|
66
|
+
|
25
67
|
def subscription_created; end
|
26
68
|
|
27
69
|
def subscription_started; end
|
@@ -44,6 +86,10 @@ module ChargebeeRails
|
|
44
86
|
|
45
87
|
def subscription_scheduled_cancellation_removed; end
|
46
88
|
|
89
|
+
def subscription_changes_scheduled; end
|
90
|
+
|
91
|
+
def subscription_scheduled_changes_removed; end
|
92
|
+
|
47
93
|
def subscription_shipping_address_updated; end
|
48
94
|
|
49
95
|
def subscription_deleted; end
|
@@ -61,7 +107,7 @@ module ChargebeeRails
|
|
61
107
|
def credit_note_created; end
|
62
108
|
|
63
109
|
def credit_note_updated; end
|
64
|
-
|
110
|
+
|
65
111
|
def credit_note_deleted; end
|
66
112
|
|
67
113
|
def subscription_renewal_reminder; end
|
@@ -79,9 +125,11 @@ module ChargebeeRails
|
|
79
125
|
def payment_refunded; end
|
80
126
|
|
81
127
|
def payment_initiated; end
|
82
|
-
|
128
|
+
|
83
129
|
def refund_initiated; end
|
84
|
-
|
130
|
+
|
131
|
+
def netd_payment_due_reminder; end
|
132
|
+
|
85
133
|
def card_added; end
|
86
134
|
|
87
135
|
def card_updated; end
|
@@ -92,10 +140,25 @@ module ChargebeeRails
|
|
92
140
|
|
93
141
|
def card_deleted; end
|
94
142
|
|
143
|
+
def payment_source_added; end
|
144
|
+
|
145
|
+
def payment_source_updated; end
|
146
|
+
|
147
|
+
def payment_source_deleted; end
|
148
|
+
|
149
|
+
def unbilled_charges_created; end
|
150
|
+
|
151
|
+
def unbilled_charges_voided; end
|
152
|
+
|
153
|
+
def unbilled_charges_deleted; end
|
154
|
+
|
155
|
+
def unbilled_charges_invoiced; end
|
156
|
+
|
157
|
+
|
95
158
|
private
|
96
159
|
|
97
160
|
def sync_events_list
|
98
|
-
%w(
|
161
|
+
%w(
|
99
162
|
card_expired
|
100
163
|
card_updated
|
101
164
|
card_expiry_reminder
|
@@ -141,7 +204,7 @@ module ChargebeeRails
|
|
141
204
|
plan_id: ::Plan.find_by(plan_id: subscription.plan_id).id,
|
142
205
|
plan_quantity: subscription.plan_quantity,
|
143
206
|
status: subscription.status,
|
144
|
-
event_last_modified_at: event.occurred_at,
|
207
|
+
event_last_modified_at: Time.at(event.occurred_at),
|
145
208
|
updated_at: Time.now,
|
146
209
|
chargebee_data: chargebee_subscription_data(subscription)
|
147
210
|
}
|
@@ -153,7 +216,8 @@ module ChargebeeRails
|
|
153
216
|
next_renewal_at: subscription.current_term_end,
|
154
217
|
cancelled_at: subscription.cancelled_at,
|
155
218
|
is_scheduled_for_cancel: (subscription.status == 'non-renewing' ? true : false),
|
156
|
-
has_scheduled_changes: subscription.has_scheduled_changes
|
219
|
+
has_scheduled_changes: subscription.has_scheduled_changes,
|
220
|
+
addons: subscription.addons
|
157
221
|
}
|
158
222
|
end
|
159
223
|
|
@@ -163,10 +227,10 @@ module ChargebeeRails
|
|
163
227
|
auto_collection: customer.auto_collection,
|
164
228
|
payment_type: customer.payment_method.type,
|
165
229
|
reference_id: customer.payment_method.reference_id,
|
166
|
-
card_last4: card.last4,
|
167
|
-
card_type: card.card_type,
|
230
|
+
card_last4: card && card.last4,
|
231
|
+
card_type: card && card.card_type,
|
168
232
|
status: customer.payment_method.status,
|
169
|
-
event_last_modified_at: event.occurred_at,
|
233
|
+
event_last_modified_at: Time.at(event.occurred_at),
|
170
234
|
updated_at: Time.now
|
171
235
|
}
|
172
236
|
end
|
data/lib/tasks/sync_plans.rake
CHANGED
@@ -1,82 +1,11 @@
|
|
1
|
+
require 'chargebee_rails/sync_plans'
|
2
|
+
|
1
3
|
namespace :chargebee_rails do
|
4
|
+
#include ChargebeeRails#
|
2
5
|
|
3
6
|
desc "chargebee plans sync with application"
|
4
7
|
task :sync_plans => :environment do
|
5
|
-
|
6
|
-
begin
|
7
|
-
STDOUT.puts "\n This will sync plans in your application with chargebee, do you want to continue ? [y/n]"
|
8
|
-
input = STDIN.gets.strip.downcase
|
9
|
-
end until %w(y n).include?(input)
|
10
|
-
if input == "y"
|
11
|
-
# Keep collecting the plans from chargebee until an offset is not provided
|
12
|
-
loop do
|
13
|
-
plan_list = retrieve_plan_list
|
14
|
-
@offset = plan_list.next_offset
|
15
|
-
cb_plans << plan_list.flat_map(&:plan)
|
16
|
-
break unless @offset.present?
|
17
|
-
end
|
18
|
-
@cb_plans = cb_plans.flatten
|
19
|
-
sync_plans
|
20
|
-
else
|
21
|
-
STDOUT.puts "Plan sync aborted!"
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
def cb_plans
|
28
|
-
@cb_plans ||= []
|
29
|
-
end
|
30
|
-
|
31
|
-
def sync_plans
|
32
|
-
# puts "Removed #{remove_plans.count} plan(s)"
|
33
|
-
puts "Created #{create_new_plans.count} plan(s)"
|
34
|
-
# puts "Updated all #{update_all_plans.count} plan(s)"
|
35
|
-
end
|
36
|
-
|
37
|
-
# Retrieve the plan list from chargebee
|
38
|
-
def retrieve_plan_list
|
39
|
-
options = { limit: 100 }
|
40
|
-
options[:offset] = @offset if @offset.present?
|
41
|
-
ChargeBee::Plan.list(options)
|
8
|
+
ChargebeeRails::SyncPlans.sync
|
42
9
|
end
|
43
10
|
|
44
|
-
# Remove plans from application that do not exist in chargebee
|
45
|
-
def remove_plans
|
46
|
-
cb_plan_ids = cb_plans.flat_map(&:id)
|
47
|
-
Plan.all.reject { |plan| cb_plan_ids.include?(plan.plan_id) }
|
48
|
-
.each { |plan| puts "Deleting Plan - #{plan.plan_id}"; plan.destroy }
|
49
|
-
end
|
50
|
-
|
51
|
-
# Create new plans that are not present in app but are available in chargebee
|
52
|
-
def create_new_plans
|
53
|
-
plan_ids = Plan.all.map(&:plan_id)
|
54
|
-
cb_plans.reject { |cb_plan| plan_ids.include?(cb_plan.id) }
|
55
|
-
.each { |new_plan| puts "Creating Plan - #{new_plan.id}"; Plan.create(plan_params(new_plan)) }
|
56
|
-
end
|
57
|
-
|
58
|
-
# Update all existing plans in the application
|
59
|
-
def update_all_plans
|
60
|
-
cb_plans.map do |cb_plan|
|
61
|
-
Plan.find_by(plan_id: cb_plan.id).update(plan_params(cb_plan))
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
# Build the plan params to be created or updated in the application
|
66
|
-
def plan_params plan
|
67
|
-
{
|
68
|
-
name: plan.name,
|
69
|
-
plan_id: plan.id,
|
70
|
-
status: plan.status,
|
71
|
-
chargebee_data: {
|
72
|
-
price: plan.price,
|
73
|
-
period: plan.period,
|
74
|
-
period_unit: plan.period_unit,
|
75
|
-
trial_period: plan.trial_period,
|
76
|
-
trial_period_unit: plan.trial_period_unit,
|
77
|
-
charge_model: plan.charge_model,
|
78
|
-
free_quantity: plan.free_quantity
|
79
|
-
}
|
80
|
-
}
|
81
|
-
end
|
82
11
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chargebee_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chargebee
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2019-01-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: chargebee
|
@@ -166,6 +166,7 @@ files:
|
|
166
166
|
- lib/chargebee_rails/metered_billing.rb
|
167
167
|
- lib/chargebee_rails/subscription.rb
|
168
168
|
- lib/chargebee_rails/subscription_builder.rb
|
169
|
+
- lib/chargebee_rails/sync_plans.rb
|
169
170
|
- lib/chargebee_rails/version.rb
|
170
171
|
- lib/chargebee_rails/webhook_handler.rb
|
171
172
|
- lib/generators/chargebee_rails/install_generator.rb
|
@@ -203,4 +204,3 @@ signing_key:
|
|
203
204
|
specification_version: 4
|
204
205
|
summary: A subscription management gem for Rails with ChargeBee.
|
205
206
|
test_files: []
|
206
|
-
has_rdoc:
|