chargebee_rails 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +470 -0
- data/Rakefile +10 -0
- data/app/controllers/chargebee_rails/webhooks_controller.rb +40 -0
- data/config/routes.rb +9 -0
- data/lib/chargebee_rails.rb +119 -0
- data/lib/chargebee_rails/configuration.rb +47 -0
- data/lib/chargebee_rails/customer.rb +85 -0
- data/lib/chargebee_rails/engine.rb +6 -0
- data/lib/chargebee_rails/errors.rb +26 -0
- data/lib/chargebee_rails/hosted_page_subscription_manager.rb +134 -0
- data/lib/chargebee_rails/metered_billing.rb +56 -0
- data/lib/chargebee_rails/subscription.rb +163 -0
- data/lib/chargebee_rails/subscription_builder.rb +162 -0
- data/lib/chargebee_rails/version.rb +4 -0
- data/lib/chargebee_rails/webhook_handler.rb +175 -0
- data/lib/generators/chargebee_rails/install_generator.rb +58 -0
- data/lib/generators/chargebee_rails/templates/app/models/payment_method.rb +3 -0
- data/lib/generators/chargebee_rails/templates/app/models/plan.rb +4 -0
- data/lib/generators/chargebee_rails/templates/app/models/subscription.rb +7 -0
- data/lib/generators/chargebee_rails/templates/config/initializers/chargebee_rails.rb +38 -0
- data/lib/generators/chargebee_rails/templates/event_sync_log_migration.rb +8 -0
- data/lib/generators/chargebee_rails/templates/new_subscription_migration.rb +16 -0
- data/lib/tasks/chargebee_rails_tasks.rake +7 -0
- data/lib/tasks/sync_failed_events.rake +72 -0
- data/lib/tasks/sync_plans.rake +82 -0
- metadata +206 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: afdc0e21ae0e3a2b9386ca490c0400edf38403b7
|
4
|
+
data.tar.gz: 4a0ed7ca48571072d5ea570765e080f97999d536
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 41ab32b0d4de201a54934e6976c5665372dcfae9ae5294d292fbf798b4e730bcc1779b34b5abc1b351ad02a2501bbc2c6abde9307cfc864a5f6f8471e7d6a160
|
7
|
+
data.tar.gz: cb59201ae3827a8f3cbf737825b3a682161ad4a20aa2afc11103396173d699324139b0e08da44584f233e5c80067eb86ecda09d59da392715177a27a738da06a
|
data/README.md
ADDED
@@ -0,0 +1,470 @@
|
|
1
|
+
# Chargebee Rails
|
2
|
+
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
|
+
|
4
|
+
[Introduction](#introduction)
|
5
|
+
|
6
|
+
[Prerequisite](#prerequisite)
|
7
|
+
|
8
|
+
[Installation](#installation)
|
9
|
+
|
10
|
+
[Customer](#customer)
|
11
|
+
|
12
|
+
[Subscription](#subscription)
|
13
|
+
|
14
|
+
[Metered Billing](#metered-billing)
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
# Introduction
|
26
|
+
|
27
|
+
This ruby gem provides you with a set of boilerplate classes to accelerate the implementation of a subscription billing module onto your rails application.
|
28
|
+
|
29
|
+
The gem can automatically handle:
|
30
|
+
|
31
|
+
* Setting up of relevant db models to store subscription data.
|
32
|
+
|
33
|
+
* Upgrade/downgrade of subscriptions.
|
34
|
+
|
35
|
+
* Coupons.
|
36
|
+
|
37
|
+
* Webhooks from Chargebee to ensure the data is in sync.
|
38
|
+
|
39
|
+
|
40
|
+
Apart from this, the gem also supports:
|
41
|
+
|
42
|
+
* Template webhook handling controllers that you can simply inherit and override.
|
43
|
+
|
44
|
+
* Pluggable tracking of metered billing usage (also customizable).
|
45
|
+
|
46
|
+
* APIs to handle various subscription billing scenarios.
|
47
|
+
|
48
|
+
|
49
|
+
# Prerequisite
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
|
53
|
+
ruby > 2.0.0
|
54
|
+
rails > 4.2.4
|
55
|
+
|
56
|
+
```
|
57
|
+
|
58
|
+
If the rest-client version is less than 1.8.0, update the latest version by running the command:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
|
62
|
+
bundle update rest-client
|
63
|
+
|
64
|
+
```
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
# Installation
|
69
|
+
|
70
|
+
##Step 1: Install the “chargebee_rails” gem to your application
|
71
|
+
|
72
|
+
Add the below line to your Gemfile:
|
73
|
+
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
|
77
|
+
gem 'chargebee_rails'
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
And, run
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
|
85
|
+
bundle
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
|
90
|
+
##Step 2: Add Subscription models to your app
|
91
|
+
|
92
|
+
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
|
+
|
94
|
+
The entity name has to be passed in _<subscription_owner_entity>_, so that the subscription models are setup with relation to this entity.
|
95
|
+
|
96
|
+
**Note**: *Presence of subscription owner model (For example user, customer, etc.) is required*
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
|
102
|
+
rails g chargebee_rails:install <subscription_owner_entity>
|
103
|
+
|
104
|
+
```
|
105
|
+
|
106
|
+
Allow migration to override templates
|
107
|
+
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
|
111
|
+
rake db:migrate
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
Now, you will have models and database tables set for subscriptions, plans, payment_methods and event_sync_logs.
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
##Step 3: Set up Chargebee
|
123
|
+
|
124
|
+
|
125
|
+
Configure your Chargebee site name and API key in the `config/initializers/chargebee_rails.rb` file.
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
|
129
|
+
#The API key can be found in your Chargebee site under Settings> API & WEBHOOKS > API Keys
|
130
|
+
|
131
|
+
config.chargebee_site = 'CHARGEBEE_SITE'
|
132
|
+
|
133
|
+
#The API key can be found in your Chargebee site under Settings> API & WEBHOOKS > API Keys
|
134
|
+
|
135
|
+
config.chargebee_api_key = 'CHARGEBEE_API_KEY
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
|
140
|
+
##Gateway credentials
|
141
|
+
|
142
|
+
Payment Gateway credentials have to be set up in Chargebee under *Settings> Site Settings> Gateway Settings*.
|
143
|
+
|
144
|
+
|
145
|
+
##Webhook notifications
|
146
|
+
|
147
|
+
You can set up basic authentication for your incoming webhook notifications in `config/initializers/chargebee_rails.rb` file.
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
|
151
|
+
config.secure_webhook_api = true
|
152
|
+
|
153
|
+
config.webhook_authentication = {user: username, secret: password}
|
154
|
+
|
155
|
+
```
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
##Set the controller name used to handle webhooks
|
160
|
+
|
161
|
+
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
|
+
|
163
|
+
```ruby
|
164
|
+
|
165
|
+
#The username and password should match the ones specified in your Chargebee site settings under Settings> API & WEBHOOKS> Webhook Settings
|
166
|
+
|
167
|
+
config.webhook_handler = 'webhook_overriding_controller_name'
|
168
|
+
|
169
|
+
```
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
For instance, if you have a controller *MyAppEventsController* in the `my_app_events_controller.rb` file, then set this as:
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
|
177
|
+
config.webhook_handler = 'my_app_events'
|
178
|
+
|
179
|
+
```
|
180
|
+
|
181
|
+
##Configure the webhook url in Chargebee
|
182
|
+
|
183
|
+
Configure the webhook url in Chargebee under *API & Webhooks> Webhook* Settings. The path can be specified as shown below:
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
|
187
|
+
config.webhook_api_path = 'chargebee_rails_event'
|
188
|
+
|
189
|
+
```
|
190
|
+
|
191
|
+
*chargebee_rails_event* is the path you can use to receive events from Chargebee to your application.
|
192
|
+
|
193
|
+
The webhook url for your site will be _http(s)://<your-domain>.com/chargebee_rails_event_.
|
194
|
+
|
195
|
+
|
196
|
+
##Sync plans
|
197
|
+
|
198
|
+
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
|
+
|
200
|
+
The plans can be synced to your application using the following command:
|
201
|
+
|
202
|
+
```ruby
|
203
|
+
|
204
|
+
rake chargebee_rails:sync_plans
|
205
|
+
|
206
|
+
```
|
207
|
+
|
208
|
+
|
209
|
+
**Note**: The archived plans will also be synced in this method.
|
210
|
+
|
211
|
+
|
212
|
+
##Sync failed events
|
213
|
+
|
214
|
+
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
|
+
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
|
219
|
+
rake chargebee_rails:sync_failed_events
|
220
|
+
|
221
|
+
```
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
##Sync events with your application##
|
226
|
+
|
227
|
+
The event types listed below are synced with the application by this gem
|
228
|
+
|
229
|
+
* subscription_started
|
230
|
+
|
231
|
+
* subscription_trial_end_reminder
|
232
|
+
|
233
|
+
* subscription_activated
|
234
|
+
|
235
|
+
* subscription_changed
|
236
|
+
|
237
|
+
* subscription_cancellation_scheduled
|
238
|
+
|
239
|
+
* subscription_cancellation_reminder
|
240
|
+
|
241
|
+
* subscription_cancelled
|
242
|
+
|
243
|
+
* subscription_reactivated
|
244
|
+
|
245
|
+
* subscription_renewed
|
246
|
+
|
247
|
+
* subscription_scheduled_cancellation_removed
|
248
|
+
|
249
|
+
* subscription_renewal_reminder
|
250
|
+
|
251
|
+
* card_expired
|
252
|
+
|
253
|
+
* card_updated
|
254
|
+
|
255
|
+
* card_expiry_reminder
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
##Configure your default plan Id##
|
260
|
+
|
261
|
+
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
|
+
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`.
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
|
266
|
+
config.default_plan_id = 'your_default_plan_id'
|
267
|
+
|
268
|
+
```
|
269
|
+
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
##Advanced settings##
|
274
|
+
|
275
|
+
If you would like to control the subscription upgrade/downgrade behaviour, you can specify this in:
|
276
|
+
|
277
|
+
```ruby
|
278
|
+
|
279
|
+
config/initializers/chargebee_rails.rb
|
280
|
+
|
281
|
+
```
|
282
|
+
|
283
|
+
The subscription's default [term end](https://apidocs.chargebee.com/docs/api/subscriptions#change_term_end) (the date when the subscription's term gets over) value can also be specified for subscription related changes like subscription update and cancellation.
|
284
|
+
|
285
|
+
```ruby
|
286
|
+
|
287
|
+
config.end_of_term = false
|
288
|
+
|
289
|
+
```
|
290
|
+
|
291
|
+
If the above parameter is set to true, subscription changes will be made at the end of term or during next renewal.
|
292
|
+
|
293
|
+
```ruby
|
294
|
+
|
295
|
+
config.proration = true
|
296
|
+
|
297
|
+
```
|
298
|
+
|
299
|
+
If the above parameter is set to true, prorated charges will be applied during subscription change.
|
300
|
+
|
301
|
+
If you’d like to include delayed charges during [update_subscription_estimate](https://apidocs.chargebee.com/docs/api/estimates#update_subscription_estimate), you can specify the *include_delayed_charges* parameter in `config/initializers/chargebee_rails.rb`.
|
302
|
+
|
303
|
+
```ruby
|
304
|
+
|
305
|
+
config.include_delayed_charges = {
|
306
|
+
changes_estimate: false,
|
307
|
+
renewal_estimate: true
|
308
|
+
}
|
309
|
+
|
310
|
+
```
|
311
|
+
|
312
|
+
##Customer
|
313
|
+
|
314
|
+
**Retrieve as Chargebee Customer**
|
315
|
+
|
316
|
+
```ruby
|
317
|
+
|
318
|
+
customer = Customer.first
|
319
|
+
|
320
|
+
customer.as_chargebee_customer
|
321
|
+
|
322
|
+
```
|
323
|
+
|
324
|
+
**Update a Customer**
|
325
|
+
|
326
|
+
```ruby
|
327
|
+
|
328
|
+
ChargebeeRails.update_customer(customer, {})
|
329
|
+
|
330
|
+
```
|
331
|
+
|
332
|
+
**Update billing info for a Customer**
|
333
|
+
|
334
|
+
```ruby
|
335
|
+
|
336
|
+
ChargebeeRails.update_billing_addr(customer, {})
|
337
|
+
|
338
|
+
```
|
339
|
+
|
340
|
+
**Update contacts for a customer**
|
341
|
+
|
342
|
+
```ruby
|
343
|
+
|
344
|
+
ChargebeeRails.add_customer_contacts(customer, {})
|
345
|
+
|
346
|
+
```
|
347
|
+
|
348
|
+
|
349
|
+
##Subscription
|
350
|
+
|
351
|
+
**Create a Subscription**
|
352
|
+
|
353
|
+
```ruby
|
354
|
+
|
355
|
+
customer = Customer.find(1)
|
356
|
+
|
357
|
+
customer.subscribe(customer: customer_params)
|
358
|
+
|
359
|
+
```
|
360
|
+
|
361
|
+
**Update a Subscription**
|
362
|
+
|
363
|
+
```ruby
|
364
|
+
|
365
|
+
customer.update_subscription(plan_id: params[:plan_id], coupon: params[:coupon_id])
|
366
|
+
|
367
|
+
```
|
368
|
+
|
369
|
+
**Retrieve a Subscription**
|
370
|
+
|
371
|
+
```ruby
|
372
|
+
|
373
|
+
subscription = customer.subscription
|
374
|
+
|
375
|
+
subscription.as_chargebee_subscription
|
376
|
+
|
377
|
+
```
|
378
|
+
|
379
|
+
**Update Plan for a Subscription**
|
380
|
+
|
381
|
+
```ruby
|
382
|
+
|
383
|
+
subscription.change_plan(plan_object, end_of_term=false) # end_of_term is optional
|
384
|
+
|
385
|
+
```
|
386
|
+
|
387
|
+
**Update Plan quantity for a Subscription**
|
388
|
+
|
389
|
+
```ruby
|
390
|
+
|
391
|
+
subscription.set_plan_quantity(quantity, end_of_term=false) # end_of_term is optional
|
392
|
+
|
393
|
+
```
|
394
|
+
|
395
|
+
**Add Or remove Addons for a Subscription**
|
396
|
+
|
397
|
+
```ruby
|
398
|
+
|
399
|
+
subscription.manage_addons(addon_id, quantity=1)
|
400
|
+
|
401
|
+
```
|
402
|
+
|
403
|
+
**Cancel a Subscription**
|
404
|
+
|
405
|
+
```ruby
|
406
|
+
|
407
|
+
subscription.cancel(params)
|
408
|
+
|
409
|
+
```
|
410
|
+
|
411
|
+
**Remove scheduled cancellation for a Subscription**
|
412
|
+
|
413
|
+
``` ruby
|
414
|
+
|
415
|
+
subscription.stop_cancellation
|
416
|
+
|
417
|
+
```
|
418
|
+
|
419
|
+
|
420
|
+
# Metered billing
|
421
|
+
|
422
|
+
If you’d like to charge your customers based on usage, you could enable the Metered Billing option. This option can be enabled by checking the *Notify and wait to close invoices* option under *Settings> Site Settings> Site* Info.
|
423
|
+
|
424
|
+
**Note:** The above mentioned webhook configuration is mandatory for Metered Billing.
|
425
|
+
|
426
|
+
The subscription’s usage charges have to tracked from your end. During renewal, a pending invoice will be created and this will be sent to you through a webhook. You would have to implement the *ChargebeeRails::MeteredBilling.close_invoice(invoice_id)* method where you will get the invoice object. Using the invoice object, you can add the subscription and its charges.
|
427
|
+
Use the below API method to add the line items to the pending invoice after you have calculated how much the customer needs to be charged
|
428
|
+
|
429
|
+
**Add charge to pending Invoice**
|
430
|
+
|
431
|
+
``` ruby
|
432
|
+
|
433
|
+
ChargebeeRails::MeteredBilling.add_charge(invoice_id, amount, description)
|
434
|
+
|
435
|
+
```
|
436
|
+
|
437
|
+
|
438
|
+
**Add Addon charge to pending Invoice**
|
439
|
+
|
440
|
+
``` ruby
|
441
|
+
|
442
|
+
ChargebeeRails::MeteredBilling.add_addon_charge(invoice_id, addon_id, addon_quantity)
|
443
|
+
|
444
|
+
```
|
445
|
+
|
446
|
+
|
447
|
+
**Close Invoice**
|
448
|
+
|
449
|
+
``` ruby
|
450
|
+
|
451
|
+
ChargebeeRails::MeteredBilling.close_invoice(invoice_id)
|
452
|
+
|
453
|
+
```
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
## Support and contribution
|
458
|
+
|
459
|
+
If you’d like us to guide you through the set up process or if you have any questions regarding the Ruby gem implementation, contact us at chargebee@spritle.com. For feature requests or feedback, submit [here](https://github.com/spritlesoftware/chargebee-rails-subscriptions/issues/new).
|
460
|
+
|
461
|
+
If you have questions regarding how Chargebee works, send an email to support@chargebee.com.
|
462
|
+
|
463
|
+
## Pull requests
|
464
|
+
|
465
|
+
If you’ve added new functionalities that you think might be helpful for all, do send us a pull request.
|
466
|
+
|
467
|
+
## License
|
468
|
+
|
469
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
470
|
+
|