chargebee 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +24 -0
- data/README.rdoc +30 -0
- data/chargebee.gemspec +24 -0
- data/lib/chargebee.rb +33 -0
- data/lib/chargebee/api_error.rb +23 -0
- data/lib/chargebee/environment.rb +23 -0
- data/lib/chargebee/list_result.rb +25 -0
- data/lib/chargebee/models/card.rb +19 -0
- data/lib/chargebee/models/customer.rb +22 -0
- data/lib/chargebee/models/event.rb +25 -0
- data/lib/chargebee/models/hosted_page.rb +33 -0
- data/lib/chargebee/models/invoice.rb +31 -0
- data/lib/chargebee/models/model.rb +47 -0
- data/lib/chargebee/models/subscription.rb +40 -0
- data/lib/chargebee/models/transaction.rb +23 -0
- data/lib/chargebee/request.rb +12 -0
- data/lib/chargebee/rest.rb +80 -0
- data/lib/chargebee/result.rb +46 -0
- data/lib/chargebee/util.rb +43 -0
- data/lib/chargebee/version.rb +13 -0
- metadata +146 -0
data/LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2011-2012 Bubblepath Inc
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person
|
6
|
+
obtaining a copy of this software and associated documentation
|
7
|
+
files (the "Software"), to deal in the Software without
|
8
|
+
restriction, including without limitation the rights to use,
|
9
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the
|
11
|
+
Software is furnished to do so, subject to the following
|
12
|
+
conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be
|
15
|
+
included in all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
19
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
21
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
22
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
23
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
24
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
= ChargeBee Ruby Client Library
|
2
|
+
|
3
|
+
The ruby library for integrating with ChargeBee Recurring Billing and Subscription Management solution.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
Install the latest version of the gem with the following command...
|
8
|
+
|
9
|
+
$ sudo gem install ghargebee
|
10
|
+
|
11
|
+
== Documentation
|
12
|
+
|
13
|
+
For API reference see https://apidocs.chargebee.com/docs/api?lang=ruby
|
14
|
+
|
15
|
+
== Usage
|
16
|
+
|
17
|
+
To create a new subscription:
|
18
|
+
|
19
|
+
ChargeBee.configure({:api_key => "your_api_key"}, {:site => "your_site"})
|
20
|
+
result = ChargeBee::Subscription.create({
|
21
|
+
:id => "sub_KyVqDh__dev__NTn4VZZ1",
|
22
|
+
:plan_id => "basic",
|
23
|
+
})
|
24
|
+
subscription = result.subscription
|
25
|
+
puts "created subscription is #{subscription}"
|
26
|
+
|
27
|
+
== License
|
28
|
+
|
29
|
+
See the LICENSE file.
|
30
|
+
|
data/chargebee.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
2
|
+
require 'chargebee/version'
|
3
|
+
|
4
|
+
spec = Gem::Specification.new do |s|
|
5
|
+
s.name = 'chargebee'
|
6
|
+
s.version = ChargeBee::Version.to_s
|
7
|
+
s.summary = 'Ruby client for Chargebee API'
|
8
|
+
s.description = 'Subscription Billing - Simple. Secure. Affordable. More details at www.chargebee.com'
|
9
|
+
|
10
|
+
s.files = Dir.glob ["README.rdoc", "LICENSE", "{lib}/**/*.rb", "*.gemspec"]
|
11
|
+
|
12
|
+
s.authors = ['Rajaraman S', 'Thiyagarajan T']
|
13
|
+
s.email = ['rr@chargebee.com', 'thiyagu@chargebee.com']
|
14
|
+
s.homepage = 'https://apidocs.chargebee.com/api/docs'
|
15
|
+
|
16
|
+
s.homepage = 'http://github.com/chargebee/chargebee-ruby'
|
17
|
+
|
18
|
+
s.add_dependency('json_pure', '~> 1.5')
|
19
|
+
s.add_dependency('rest-client', '~> 1.4')
|
20
|
+
|
21
|
+
s.add_development_dependency('rpsec', '~> 2.9.0')
|
22
|
+
s.add_development_dependency('mocha')
|
23
|
+
|
24
|
+
end
|
data/lib/chargebee.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/chargebee/environment'
|
2
|
+
require File.dirname(__FILE__) + '/chargebee/rest'
|
3
|
+
require File.dirname(__FILE__) + '/chargebee/util'
|
4
|
+
require File.dirname(__FILE__) + '/chargebee/request'
|
5
|
+
require File.dirname(__FILE__) + '/chargebee/result'
|
6
|
+
require File.dirname(__FILE__) + '/chargebee/list_result'
|
7
|
+
|
8
|
+
require File.dirname(__FILE__) + '/chargebee/api_error'
|
9
|
+
|
10
|
+
require File.dirname(__FILE__) + '/chargebee/models/model'
|
11
|
+
require File.dirname(__FILE__) + '/chargebee/models/subscription'
|
12
|
+
require File.dirname(__FILE__) + '/chargebee/models/customer'
|
13
|
+
require File.dirname(__FILE__) + '/chargebee/models/card'
|
14
|
+
require File.dirname(__FILE__) + '/chargebee/models/transaction'
|
15
|
+
require File.dirname(__FILE__) + '/chargebee/models/invoice'
|
16
|
+
require File.dirname(__FILE__) + '/chargebee/models/hosted_page'
|
17
|
+
require File.dirname(__FILE__) + '/chargebee/models/event'
|
18
|
+
|
19
|
+
|
20
|
+
module ChargeBee
|
21
|
+
|
22
|
+
@@default_env = nil
|
23
|
+
|
24
|
+
def self.configure(options)
|
25
|
+
@@default_env = Environment.new(options)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.default_env
|
29
|
+
@@default_env
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ChargeBee
|
2
|
+
class APIError < StandardError
|
3
|
+
|
4
|
+
attr_reader :message, :http_code, :http_body, :json_obj, :error_code, :param
|
5
|
+
|
6
|
+
def initialize(message=nil, http_code=nil, http_body=nil, json_obj = nil)
|
7
|
+
@message = message
|
8
|
+
@http_code = http_code
|
9
|
+
@http_body = http_body
|
10
|
+
@json_obj = json_obj
|
11
|
+
if(json_obj != nil)
|
12
|
+
@error_code = json_obj[:error_code]
|
13
|
+
@param = json_obj[:param]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
hc = @http_code.nil? ? "" : "(Http Code #{@http_code}) "
|
19
|
+
"#{hc}#{@message}"
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ChargeBee
|
2
|
+
class Environment
|
3
|
+
|
4
|
+
attr_accessor :api_key, :site
|
5
|
+
attr_reader :api_endpoint
|
6
|
+
|
7
|
+
def initialize(options)
|
8
|
+
[:api_key, :site].each do |attr|
|
9
|
+
instance_variable_set "@#{attr}", options[attr]
|
10
|
+
end
|
11
|
+
if($CHARGEBEE_DOMAIN == nil)
|
12
|
+
@api_endpoint = "https://#{@site}.chargebee.com/api/v1"
|
13
|
+
else
|
14
|
+
@api_endpoint = "http://#{@site}.#{$CHARGEBEE_DOMAIN}:8080/api/v1"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def api_url(url)
|
19
|
+
url = @api_endpoint + url
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module ChargeBee
|
4
|
+
class ListResult
|
5
|
+
extend Forwardable
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
def_delegator :@list, :each, :each
|
9
|
+
def_delegator :@list, :length, :length
|
10
|
+
|
11
|
+
def initialize(response)
|
12
|
+
@response = response
|
13
|
+
@list = Array.new
|
14
|
+
initItems()
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def initItems()
|
19
|
+
@response.each do |item|
|
20
|
+
@list.push(Result.new(item))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ChargeBee
|
2
|
+
class Card < Model
|
3
|
+
|
4
|
+
attr_accessor :customer_id, :status, :gateway, :first_name, :last_name, :iin, :last4, :card_type,
|
5
|
+
:expiry_month, :expiry_year, :masked_number
|
6
|
+
|
7
|
+
# OPERATIONS
|
8
|
+
#-----------
|
9
|
+
|
10
|
+
def self.retrieve(id, env=nil)
|
11
|
+
Request.send('get', "/cards/#{id.to_s}", {}, env)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.update_card_for_customer(id, params, env=nil)
|
15
|
+
Request.send('post', "/customers/#{id.to_s}/credit_card", params, env)
|
16
|
+
end
|
17
|
+
|
18
|
+
end # ~Card
|
19
|
+
end # ~ChargeBee
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ChargeBee
|
2
|
+
class Customer < Model
|
3
|
+
|
4
|
+
attr_accessor :id, :first_name, :last_name, :email, :company, :created_at, :card_status
|
5
|
+
|
6
|
+
# OPERATIONS
|
7
|
+
#-----------
|
8
|
+
|
9
|
+
def self.list(params={}, env=nil)
|
10
|
+
Request.send('get', "/customers", params, env)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.retrieve(id, env=nil)
|
14
|
+
Request.send('get', "/customers/#{id.to_s}", {}, env)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.update(id, params={}, env=nil)
|
18
|
+
Request.send('post', "/customers/#{id.to_s}", params, env)
|
19
|
+
end
|
20
|
+
|
21
|
+
end # ~Customer
|
22
|
+
end # ~ChargeBee
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ChargeBee
|
2
|
+
class Event < Model
|
3
|
+
|
4
|
+
attr_accessor :id, :occurred_at, :webhook_status, :webhook_failure_reason, :event_type
|
5
|
+
|
6
|
+
class Content < Result
|
7
|
+
end
|
8
|
+
|
9
|
+
def content
|
10
|
+
Content.new(@values[:content])
|
11
|
+
end
|
12
|
+
|
13
|
+
# OPERATIONS
|
14
|
+
#-----------
|
15
|
+
|
16
|
+
def self.list(params={}, env=nil)
|
17
|
+
Request.send('get', "/events", params, env)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.retrieve(id, env=nil)
|
21
|
+
Request.send('get', "/events/#{id.to_s}", {}, env)
|
22
|
+
end
|
23
|
+
|
24
|
+
end # ~Event
|
25
|
+
end # ~ChargeBee
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module ChargeBee
|
2
|
+
class HostedPage < Model
|
3
|
+
|
4
|
+
attr_accessor :id, :type, :url, :state, :failure_reason, :pass_thru_content, :created_at, :expires_at
|
5
|
+
|
6
|
+
class Content < Result
|
7
|
+
end
|
8
|
+
|
9
|
+
def content
|
10
|
+
Content.new(@values[:content])
|
11
|
+
end
|
12
|
+
|
13
|
+
# OPERATIONS
|
14
|
+
#-----------
|
15
|
+
|
16
|
+
def self.checkout_new(params, env=nil)
|
17
|
+
Request.send('post', "/hosted_pages/checkout_new", params, env)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.checkout_existing(params, env=nil)
|
21
|
+
Request.send('post', "/hosted_pages/checkout_existing", params, env)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.update_card(params, env=nil)
|
25
|
+
Request.send('post', "/hosted_pages/update_card", params, env)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.retrieve(id, env=nil)
|
29
|
+
Request.send('get', "/hosted_pages/#{id.to_s}", {}, env)
|
30
|
+
end
|
31
|
+
|
32
|
+
end # ~HostedPage
|
33
|
+
end # ~ChargeBee
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ChargeBee
|
2
|
+
class Invoice < Model
|
3
|
+
|
4
|
+
class LineItem < Model
|
5
|
+
attr_accessor :date_from, :date_to, :unit_amount, :quantity
|
6
|
+
end
|
7
|
+
|
8
|
+
class Discount < Model
|
9
|
+
attr_accessor :amount, :description
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_accessor :id, :subscription_id, :status, :start_date, :end_date, :amount, :paid_on, :next_retry,
|
13
|
+
:sub_total, :line_items, :discounts
|
14
|
+
|
15
|
+
# OPERATIONS
|
16
|
+
#-----------
|
17
|
+
|
18
|
+
def self.list(params={}, env=nil)
|
19
|
+
Request.send('get', "/invoices", params, env)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.retrieve(id, env=nil)
|
23
|
+
Request.send('get', "/invoices/#{id.to_s}", {}, env)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.invoices_for_subscription(id, params={}, env=nil)
|
27
|
+
Request.send('get', "/subscriptions/#{id.to_s}/invoices", params, env)
|
28
|
+
end
|
29
|
+
|
30
|
+
end # ~Invoice
|
31
|
+
end # ~ChargeBee
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module ChargeBee
|
2
|
+
class Model
|
3
|
+
|
4
|
+
def initialize(values, sub_types={})
|
5
|
+
@values = values
|
6
|
+
@sub_types = sub_types
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_s(*args)
|
10
|
+
JSON.pretty_generate(@values)
|
11
|
+
end
|
12
|
+
|
13
|
+
def inspect()
|
14
|
+
"#<#{self.class}:0x#{self.object_id.to_s(16)} > JSON: " + JSON.pretty_generate(@values)
|
15
|
+
end
|
16
|
+
|
17
|
+
def load(values)
|
18
|
+
instance_eval do
|
19
|
+
values.each do |k, v|
|
20
|
+
set_val = nil
|
21
|
+
case v
|
22
|
+
when Hash
|
23
|
+
set_val = (@sub_types[k] != nil) ? @sub_types[k].construct(v) : v
|
24
|
+
when Array
|
25
|
+
if(@sub_types[k] != nil)
|
26
|
+
set_val = v.map { |item| @sub_types[k].construct(item)}
|
27
|
+
else
|
28
|
+
set_val = v
|
29
|
+
end
|
30
|
+
else
|
31
|
+
set_val = v
|
32
|
+
end
|
33
|
+
instance_variable_set("@#{k}", set_val)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.construct(values, sub_types = {})
|
39
|
+
if(values != nil)
|
40
|
+
obj = self.new(values, sub_types)
|
41
|
+
obj.load(values)
|
42
|
+
obj
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module ChargeBee
|
2
|
+
class Subscription < Model
|
3
|
+
|
4
|
+
class Addon < Model
|
5
|
+
attr_accessor :id, :quantity
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_accessor :id, :plan_id, :plan_quantity, :status, :trial_start, :trial_end, :current_term_start,
|
9
|
+
:current_term_end, :created_at, :activated_at, :cancelled_at, :due_invoices_count, :due_since,
|
10
|
+
:total_dues, :addons
|
11
|
+
|
12
|
+
# OPERATIONS
|
13
|
+
#-----------
|
14
|
+
|
15
|
+
def self.create(params, env=nil)
|
16
|
+
Request.send('post', "/subscriptions", params, env)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.list(params={}, env=nil)
|
20
|
+
Request.send('get', "/subscriptions", params, env)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.retrieve(id, env=nil)
|
24
|
+
Request.send('get', "/subscriptions/#{id.to_s}", {}, env)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.update(id, params={}, env=nil)
|
28
|
+
Request.send('post', "/subscriptions/#{id.to_s}", params, env)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.cancel(id, params={}, env=nil)
|
32
|
+
Request.send('post', "/subscriptions/#{id.to_s}/cancel", params, env)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.reactivate(id, env=nil)
|
36
|
+
Request.send('post', "/subscriptions/#{id.to_s}/reactivate", {}, env)
|
37
|
+
end
|
38
|
+
|
39
|
+
end # ~Subscription
|
40
|
+
end # ~ChargeBee
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ChargeBee
|
2
|
+
class Transaction < Model
|
3
|
+
|
4
|
+
attr_accessor :id, :subscription_id, :gateway, :invoice_id, :type, :date, :amount, :id_at_gateway,
|
5
|
+
:masked_card_number, :error_code, :error_text, :refunded_tx_id, :refund_memo, :voided_at, :status
|
6
|
+
|
7
|
+
# OPERATIONS
|
8
|
+
#-----------
|
9
|
+
|
10
|
+
def self.list(params={}, env=nil)
|
11
|
+
Request.send('get', "/transactions", params, env)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.retrieve(id, env=nil)
|
15
|
+
Request.send('get', "/transactions/#{id.to_s}", {}, env)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.transactions_for_subscription(id, params={}, env=nil)
|
19
|
+
Request.send('get', "/subscriptions/#{id.to_s}/transactions", params, env)
|
20
|
+
end
|
21
|
+
|
22
|
+
end # ~Transaction
|
23
|
+
end # ~ChargeBee
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module ChargeBee
|
2
|
+
class Request
|
3
|
+
|
4
|
+
def self.send(method, url, params={}, env=nil)
|
5
|
+
env ||= ChargeBee.default_env
|
6
|
+
ser_params = Util.serialize(params)
|
7
|
+
resp = Rest.request(method, url, env, ser_params||={})
|
8
|
+
resp.has_key?(:list) ? ListResult.new(resp[:list]) : Result.new(resp)
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'rest_client'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module ChargeBee
|
5
|
+
module Rest
|
6
|
+
|
7
|
+
def self.request(method, url, env, params=nil)
|
8
|
+
raise APIError.new('No environment configured.') unless env
|
9
|
+
api_key = env.api_key
|
10
|
+
headers = {}
|
11
|
+
ssl_opts = { :verify_ssl => ($API_DOMAIN==nil)?true:false }
|
12
|
+
|
13
|
+
case method.to_s.downcase.to_sym
|
14
|
+
when :get, :head, :delete
|
15
|
+
headers = { :params => params }.merge(headers)
|
16
|
+
payload = nil
|
17
|
+
else
|
18
|
+
payload = params
|
19
|
+
end
|
20
|
+
|
21
|
+
headers = {
|
22
|
+
"User-Agent" => "Chargebee-Ruby-Client",
|
23
|
+
:accept => :json
|
24
|
+
}.merge(headers)
|
25
|
+
|
26
|
+
opts = {
|
27
|
+
:method => method,
|
28
|
+
:url => env.api_url(url),
|
29
|
+
:user => api_key,
|
30
|
+
:headers => headers,
|
31
|
+
:payload => payload,
|
32
|
+
:open_timeout => 50,
|
33
|
+
:timeout => 100
|
34
|
+
}.merge(ssl_opts)
|
35
|
+
|
36
|
+
begin
|
37
|
+
response = RestClient::Request.execute(opts)
|
38
|
+
rescue Exception => e
|
39
|
+
case(e)
|
40
|
+
when SocketError
|
41
|
+
raise APIError.new("Error while connecting to chargebee. If you see this repeatedly, contact us at support@chargebee.com")
|
42
|
+
when RestClient::ExceptionWithResponse
|
43
|
+
if rcode = e.http_code and rbody = e.http_body
|
44
|
+
raise handle_for_error(e, rcode, rbody)
|
45
|
+
else
|
46
|
+
raise APIError.new(e.message)
|
47
|
+
end
|
48
|
+
when RestClient::Exception
|
49
|
+
raise APIError.new("Unexpected error received: #{e.message}", e.http_code, e.http_body)
|
50
|
+
else
|
51
|
+
raise APIError.new(e.message)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
rbody = response.body
|
55
|
+
rcode = response.code
|
56
|
+
begin
|
57
|
+
resp = JSON.parse(rbody)
|
58
|
+
rescue JSON::ParserError
|
59
|
+
raise APIError.new("Invalid response object from API", rcode, rbody)
|
60
|
+
end
|
61
|
+
|
62
|
+
resp = Util.symbolize_keys(resp)
|
63
|
+
resp
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.handle_for_error(e, rcode=nil, rbody=nil)
|
67
|
+
if(rcode == 204)
|
68
|
+
raise APIError.new("No response returned by the chargebee api", rcode)
|
69
|
+
end
|
70
|
+
begin
|
71
|
+
error_obj = JSON.parse(rbody)
|
72
|
+
error_obj = Util.symbolize_keys(error_obj)
|
73
|
+
rescue JSON::ParseError
|
74
|
+
raise APIError.new("Invalid JSON response #{rbody.inspect} received with HTTP response code #{rcode}", rcode, rbody)
|
75
|
+
end
|
76
|
+
raise APIError.new(error_obj[:error_msg], rcode, rbody, error_obj)
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ChargeBee
|
2
|
+
class Result
|
3
|
+
|
4
|
+
def initialize(response)
|
5
|
+
@response = response
|
6
|
+
end
|
7
|
+
|
8
|
+
def subscription()
|
9
|
+
get(:subscription, Subscription, {:addons => Subscription::Addon})
|
10
|
+
end
|
11
|
+
|
12
|
+
def customer()
|
13
|
+
get(:customer, Customer)
|
14
|
+
end
|
15
|
+
|
16
|
+
def card()
|
17
|
+
get(:card, Card)
|
18
|
+
end
|
19
|
+
|
20
|
+
def invoice()
|
21
|
+
get(:invoice, Invoice, {:line_items => Invoice::LineItem, :discounts => Invoice::Discount})
|
22
|
+
end
|
23
|
+
|
24
|
+
def transaction()
|
25
|
+
get(:transaction, Transaction)
|
26
|
+
end
|
27
|
+
|
28
|
+
def event()
|
29
|
+
get(:event, Event)
|
30
|
+
end
|
31
|
+
|
32
|
+
def hosted_page()
|
33
|
+
get(:hosted_page, HostedPage)
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s(*args)
|
37
|
+
JSON.pretty_generate(@response)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def get(type, klass, sub_types = {})
|
42
|
+
klass.construct(@response[type], sub_types)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module ChargeBee
|
2
|
+
module Util
|
3
|
+
|
4
|
+
def self.serialize(value, prefix = nil, idx = nil)
|
5
|
+
serialized = {}
|
6
|
+
case value
|
7
|
+
when Hash
|
8
|
+
value.each do |k, v|
|
9
|
+
if(v.instance_of? Hash or v.instance_of? Array)
|
10
|
+
serialized.merge!(serialize(v, k))
|
11
|
+
else
|
12
|
+
key = "#{(prefix!=nil) ? prefix:''}#{(idx != nil) ? '['+idx.to_s+']':''}#{(prefix!=nil) ? '['+k.to_s+']' : k}"
|
13
|
+
serialized.merge!({key => (v!=nil)?v:''})
|
14
|
+
end
|
15
|
+
end
|
16
|
+
when Array
|
17
|
+
value.each_with_index do |v, i|
|
18
|
+
serialized.merge!(serialize(v, prefix, i))
|
19
|
+
end
|
20
|
+
else
|
21
|
+
raise ArgumentError.new("only hash or arrays are allowed as value")
|
22
|
+
end
|
23
|
+
serialized
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.symbolize_keys(object)
|
27
|
+
case object
|
28
|
+
when Hash
|
29
|
+
new = {}
|
30
|
+
object.each do |key, value|
|
31
|
+
key = (key.to_sym rescue key) || key
|
32
|
+
new[key] = symbolize_keys(value)
|
33
|
+
end
|
34
|
+
new
|
35
|
+
when Array
|
36
|
+
object.map { |value| symbolize_keys(value) }
|
37
|
+
else
|
38
|
+
object
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chargebee
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Rajaraman S
|
14
|
+
- Thiyagarajan T
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2012-04-10 00:00:00 Z
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: json_pure
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 5
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 5
|
33
|
+
version: "1.5"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rest-client
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 7
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 4
|
48
|
+
version: "1.4"
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: rpsec
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 43
|
60
|
+
segments:
|
61
|
+
- 2
|
62
|
+
- 9
|
63
|
+
- 0
|
64
|
+
version: 2.9.0
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: mocha
|
69
|
+
prerelease: false
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
type: :development
|
80
|
+
version_requirements: *id004
|
81
|
+
description: Subscription Billing - Simple. Secure. Affordable. More details at www.chargebee.com
|
82
|
+
email:
|
83
|
+
- rr@chargebee.com
|
84
|
+
- thiyagu@chargebee.com
|
85
|
+
executables: []
|
86
|
+
|
87
|
+
extensions: []
|
88
|
+
|
89
|
+
extra_rdoc_files: []
|
90
|
+
|
91
|
+
files:
|
92
|
+
- README.rdoc
|
93
|
+
- LICENSE
|
94
|
+
- lib/chargebee/api_error.rb
|
95
|
+
- lib/chargebee/environment.rb
|
96
|
+
- lib/chargebee/list_result.rb
|
97
|
+
- lib/chargebee/models/card.rb
|
98
|
+
- lib/chargebee/models/customer.rb
|
99
|
+
- lib/chargebee/models/event.rb
|
100
|
+
- lib/chargebee/models/hosted_page.rb
|
101
|
+
- lib/chargebee/models/invoice.rb
|
102
|
+
- lib/chargebee/models/model.rb
|
103
|
+
- lib/chargebee/models/subscription.rb
|
104
|
+
- lib/chargebee/models/transaction.rb
|
105
|
+
- lib/chargebee/request.rb
|
106
|
+
- lib/chargebee/rest.rb
|
107
|
+
- lib/chargebee/result.rb
|
108
|
+
- lib/chargebee/util.rb
|
109
|
+
- lib/chargebee/version.rb
|
110
|
+
- lib/chargebee.rb
|
111
|
+
- chargebee.gemspec
|
112
|
+
homepage: http://github.com/chargebee/chargebee-ruby
|
113
|
+
licenses: []
|
114
|
+
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
hash: 3
|
126
|
+
segments:
|
127
|
+
- 0
|
128
|
+
version: "0"
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
hash: 3
|
135
|
+
segments:
|
136
|
+
- 0
|
137
|
+
version: "0"
|
138
|
+
requirements: []
|
139
|
+
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 1.8.15
|
142
|
+
signing_key:
|
143
|
+
specification_version: 3
|
144
|
+
summary: Ruby client for Chargebee API
|
145
|
+
test_files: []
|
146
|
+
|