chargify_api_ares 0.4.4 → 0.5.0
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.
- data/.gitignore +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +57 -0
- data/Guardfile +15 -0
- data/README.md +29 -37
- data/Rakefile +12 -5
- data/chargify_api_ares.gemspec +25 -63
- data/examples/coupons.rb +50 -0
- data/{samples → examples}/customers.rb +1 -1
- data/{samples → examples}/metered_components.rb +5 -2
- data/{samples → examples}/products.rb +1 -1
- data/{samples → examples}/subscriptions.rb +2 -2
- data/{samples → examples}/transactions.rb +2 -3
- data/lib/chargify_api_ares.rb +14 -314
- data/lib/chargify_api_ares/config.rb +16 -0
- data/lib/chargify_api_ares/resources/base.rb +14 -0
- data/lib/chargify_api_ares/resources/component.rb +4 -0
- data/lib/chargify_api_ares/resources/coupon.rb +19 -0
- data/lib/chargify_api_ares/resources/customer.rb +21 -0
- data/lib/chargify_api_ares/resources/payment_profile.rb +4 -0
- data/lib/chargify_api_ares/resources/product.rb +25 -0
- data/lib/chargify_api_ares/resources/product_family.rb +34 -0
- data/lib/chargify_api_ares/resources/site.rb +7 -0
- data/lib/chargify_api_ares/resources/statement.rb +4 -0
- data/lib/chargify_api_ares/resources/subscription.rb +123 -0
- data/lib/chargify_api_ares/resources/transaction.rb +17 -0
- data/lib/chargify_api_ares/resources/usage.rb +11 -0
- data/spec/factories.rb +28 -24
- data/spec/remote/remote.example.yml +6 -0
- data/spec/remote/remote_spec.rb +347 -452
- data/spec/remote/spec_helper.rb +11 -15
- data/spec/{base_spec.rb → resources/base_spec.rb} +1 -1
- data/spec/resources/coupon_spec.rb +35 -0
- data/spec/resources/customer_spec.rb +36 -0
- data/spec/resources/product_family_spec.rb +57 -0
- data/spec/resources/product_spec.rb +23 -0
- data/spec/{subscriptions_component_spec.rb → resources/subscription_component_spec.rb} +2 -2
- data/spec/{subscription_spec.rb → resources/subscription_spec.rb} +2 -2
- data/spec/resources/usage_spec.rb +44 -0
- data/spec/spec_helper.rb +6 -8
- data/spec/{mocks → support}/fake_resource.rb +0 -0
- metadata +148 -29
- data/VERSION +0 -1
- data/config/remote.example.yml +0 -7
- data/spec/components_spec.rb +0 -48
- data/spec/customer_spec.rb +0 -23
- data/spec/product_spec.rb +0 -23
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
chargify_api_ares (0.4.4)
|
5
|
+
activeresource (>= 2.3.5)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.1.1)
|
11
|
+
activesupport (= 3.1.1)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
i18n (~> 0.6)
|
14
|
+
activeresource (3.1.1)
|
15
|
+
activemodel (= 3.1.1)
|
16
|
+
activesupport (= 3.1.1)
|
17
|
+
activesupport (3.1.1)
|
18
|
+
multi_json (~> 1.0)
|
19
|
+
builder (3.0.0)
|
20
|
+
diff-lcs (1.1.3)
|
21
|
+
factory_girl (2.1.2)
|
22
|
+
activesupport
|
23
|
+
faker (1.0.1)
|
24
|
+
i18n (~> 0.4)
|
25
|
+
fakeweb (1.3.0)
|
26
|
+
growl (1.0.3)
|
27
|
+
guard (0.8.8)
|
28
|
+
thor (~> 0.14.6)
|
29
|
+
guard-rspec (0.5.0)
|
30
|
+
guard (>= 0.8.4)
|
31
|
+
i18n (0.6.0)
|
32
|
+
multi_json (1.0.3)
|
33
|
+
rake (0.9.2.2)
|
34
|
+
rb-fsevent (0.4.3.1)
|
35
|
+
rspec (2.7.0)
|
36
|
+
rspec-core (~> 2.7.0)
|
37
|
+
rspec-expectations (~> 2.7.0)
|
38
|
+
rspec-mocks (~> 2.7.0)
|
39
|
+
rspec-core (2.7.1)
|
40
|
+
rspec-expectations (2.7.0)
|
41
|
+
diff-lcs (~> 1.1.2)
|
42
|
+
rspec-mocks (2.7.0)
|
43
|
+
thor (0.14.6)
|
44
|
+
|
45
|
+
PLATFORMS
|
46
|
+
ruby
|
47
|
+
|
48
|
+
DEPENDENCIES
|
49
|
+
chargify_api_ares!
|
50
|
+
factory_girl (~> 2.1.0)
|
51
|
+
faker (~> 1.0.1)
|
52
|
+
fakeweb (~> 1.3.0)
|
53
|
+
growl (~> 1.0.3)
|
54
|
+
guard-rspec (~> 0.5.0)
|
55
|
+
rake (~> 0.9.2)
|
56
|
+
rb-fsevent (~> 0.4.2)
|
57
|
+
rspec (~> 2.7.0)
|
data/Guardfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# Run me with:
|
2
|
+
#
|
3
|
+
# $ bundle exec guard
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'rb-inotify'
|
7
|
+
require 'libnotify'
|
8
|
+
rescue LoadError
|
9
|
+
end
|
10
|
+
|
11
|
+
guard 'rspec', :all_on_start => false, :all_after_pass => false do
|
12
|
+
watch(%r{^spec/.+_spec\.rb})
|
13
|
+
watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
14
|
+
watch(%r{^spec/.+_spec\.rb})
|
15
|
+
end
|
data/README.md
CHANGED
@@ -1,31 +1,30 @@
|
|
1
1
|
Chargify API wrapper for Ruby (using ActiveResource) [](http://travis-ci.org/grasshopperlabs/chargify_api_ares)
|
2
2
|
====================================================
|
3
3
|
|
4
|
-
chargify_api_ares
|
5
|
-
-----------------
|
6
|
-
|
7
4
|
This is a Ruby wrapper for the [Chargify](http://chargify.com) API that leverages ActiveResource.
|
5
|
+
|
8
6
|
It allows you to interface with the Chargify API using simple ActiveRecord-like syntax, i.e.:
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
subscription.cancel
|
8
|
+
``` ruby
|
9
|
+
Chargify::Subscription.create(
|
10
|
+
:customer_reference => 'moklett',
|
11
|
+
:product_handle => 'chargify-api-ares-test',
|
12
|
+
:credit_card_attributes => {
|
13
|
+
:first_name => "Michael",
|
14
|
+
:last_name => "Klett",
|
15
|
+
:expiration_month => 1,
|
16
|
+
:expiration_year => 2010,
|
17
|
+
:full_number => "1234-1234-1234-1234"
|
18
|
+
}
|
19
|
+
)
|
20
|
+
|
21
|
+
subscription.credit_card_attributes = { :expiration_year => 2013 }
|
22
|
+
subscription.save
|
26
23
|
|
27
|
-
|
24
|
+
subscription.cancel
|
25
|
+
```
|
28
26
|
|
27
|
+
See the `examples` directory for more usage examples.
|
29
28
|
|
30
29
|
### Installation
|
31
30
|
|
@@ -35,31 +34,24 @@ You can install this library as a gem using the following command:
|
|
35
34
|
|
36
35
|
$ gem install chargify_api_ares
|
37
36
|
|
38
|
-
|
39
|
-
### Requirements
|
40
|
-
|
41
|
-
This library requires ActiveResource version 2.3.4 or greater.
|
42
|
-
|
43
|
-
$ gem install activeresource
|
44
|
-
|
45
|
-
|
46
37
|
### Usage
|
47
38
|
|
48
39
|
Simply require this library before you use it:
|
49
40
|
|
50
|
-
|
41
|
+
``` ruby
|
42
|
+
require 'chargify_api_ares'
|
43
|
+
```
|
51
44
|
|
52
|
-
|
53
45
|
If you're using Rails, you could include this gem in your configuration, i.e. in `environment.rb`
|
54
46
|
|
55
|
-
|
47
|
+
``` ruby
|
48
|
+
config.gem 'chargify_api_ares'
|
49
|
+
```
|
56
50
|
|
57
|
-
|
58
51
|
Now you'll have access to classes the interact with the Chargify API, such as:
|
59
52
|
|
60
|
-
`Chargify::Product`
|
61
|
-
`Chargify::Customer`
|
62
|
-
`
|
53
|
+
* `Chargify::Product`
|
54
|
+
* `Chargify::Customer`
|
55
|
+
* `Chargify::Subscription`
|
63
56
|
|
64
|
-
Check out the examples in the `
|
65
|
-
you may be interested in some [ActiveResource Documentation](http://apidock.com/rails/ActiveResource/Base)
|
57
|
+
Check out the examples in the `examples` directory. If you're not familiar with how ActiveResource works, you may be interested in some [ActiveResource Documentation](http://apidock.com/rails/ActiveResource/Base)
|
data/Rakefile
CHANGED
@@ -1,9 +1,16 @@
|
|
1
|
-
require '
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
2
3
|
require 'rspec/core/rake_task'
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
6
|
+
t.pattern = FileList['spec/**/*_spec.rb'].exclude('spec/remote/**/*')
|
7
|
+
end
|
8
|
+
|
9
|
+
namespace :spec do
|
10
|
+
desc "Run remote specs"
|
11
|
+
RSpec::Core::RakeTask.new(:remote) do |t|
|
12
|
+
t.pattern = FileList['spec/remote/**/*_spec.rb']
|
13
|
+
end
|
14
|
+
end
|
8
15
|
|
9
16
|
task :default => :spec
|
data/chargify_api_ares.gemspec
CHANGED
@@ -1,69 +1,31 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
|
-
s.
|
3
|
-
s.version = "0.4.4"
|
4
|
-
|
2
|
+
s.specification_version = 3 if s.respond_to? :specification_version=
|
5
3
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
4
|
+
s.rubygems_version = '1.3.7'
|
5
|
+
|
6
|
+
s.name = 'chargify_api_ares'
|
7
|
+
s.version = '0.5.0'
|
8
|
+
s.date = '2011-11-08'
|
9
|
+
s.summary = 'A Chargify API wrapper for Ruby using ActiveResource'
|
10
|
+
s.description = ''
|
6
11
|
s.authors = ["Michael Klett", "Nathan Verni", "Rodrigo Franco", "Shay Frendt"]
|
7
|
-
s.
|
8
|
-
s.
|
9
|
-
|
10
|
-
s.
|
11
|
-
"
|
12
|
-
|
13
|
-
]
|
14
|
-
s.files = [
|
15
|
-
".gitignore",
|
16
|
-
"LICENSE.txt",
|
17
|
-
"README.md",
|
18
|
-
"Rakefile",
|
19
|
-
"VERSION",
|
20
|
-
"chargify_api_ares.gemspec",
|
21
|
-
"config/remote.example.yml",
|
22
|
-
"lib/chargify_api_ares.rb",
|
23
|
-
"samples/customers.rb",
|
24
|
-
"samples/metered_components.rb",
|
25
|
-
"samples/products.rb",
|
26
|
-
"samples/subscriptions.rb",
|
27
|
-
"samples/transactions.rb",
|
28
|
-
"spec/base_spec.rb",
|
29
|
-
"spec/components_spec.rb",
|
30
|
-
"spec/customer_spec.rb",
|
31
|
-
"spec/factories.rb",
|
32
|
-
"spec/mocks/fake_resource.rb",
|
33
|
-
"spec/product_spec.rb",
|
34
|
-
"spec/remote/remote_spec.rb",
|
35
|
-
"spec/remote/spec_helper.rb",
|
36
|
-
"spec/spec.opts",
|
37
|
-
"spec/spec_helper.rb",
|
38
|
-
"spec/subscription_spec.rb",
|
39
|
-
"spec/subscriptions_component_spec.rb"
|
40
|
-
]
|
41
|
-
s.homepage = %q{http://github.com/grasshopperlabs/chargify_api_ares}
|
42
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
43
|
-
s.require_paths = ["lib"]
|
44
|
-
s.rubygems_version = %q{1.3.7}
|
45
|
-
s.summary = %q{A Chargify API wrapper for Ruby using ActiveResource}
|
46
|
-
s.test_files = [
|
47
|
-
"spec/base_spec.rb",
|
48
|
-
"spec/components_spec.rb",
|
49
|
-
"spec/customer_spec.rb",
|
50
|
-
"spec/factories.rb",
|
51
|
-
"spec/mocks/fake_resource.rb",
|
52
|
-
"spec/product_spec.rb",
|
53
|
-
"spec/remote/remote_spec.rb",
|
54
|
-
"spec/remote/spec_helper.rb",
|
55
|
-
"spec/spec_helper.rb",
|
56
|
-
"spec/subscription_spec.rb",
|
57
|
-
"spec/subscriptions_component_spec.rb"
|
58
|
-
]
|
12
|
+
s.email = 'support@chargify.com'
|
13
|
+
s.homepage = 'http://github.com/grasshopperlabs/chargify_api_ares'
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.require_paths = %w[lib]
|
59
18
|
|
60
|
-
|
61
|
-
|
62
|
-
s.specification_version = 3
|
19
|
+
# Runtime Dependencies
|
20
|
+
s.add_runtime_dependency('activeresource', '>= 2.3.5')
|
63
21
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
22
|
+
# Development Dependencies
|
23
|
+
s.add_development_dependency('rake', '~> 0.9.2')
|
24
|
+
s.add_development_dependency('rspec', '~> 2.7.0')
|
25
|
+
s.add_development_dependency('factory_girl', '~> 2.1.0')
|
26
|
+
s.add_development_dependency('fakeweb', '~> 1.3.0')
|
27
|
+
s.add_development_dependency('faker', '~> 1.0.1')
|
28
|
+
s.add_development_dependency('guard-rspec', '~> 0.5.0')
|
29
|
+
s.add_development_dependency('growl', '~> 1.0.3')
|
30
|
+
s.add_development_dependency('rb-fsevent', '~> 0.4.2')
|
69
31
|
end
|
data/examples/coupons.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
2
|
+
|
3
|
+
require 'chargify_api_ares'
|
4
|
+
|
5
|
+
# You could load your credentials from a file...
|
6
|
+
chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', 'chargify.yml'))
|
7
|
+
|
8
|
+
Chargify.configure do |c|
|
9
|
+
c.subdomain = chargify_config['subdomain']
|
10
|
+
c.api_key = chargify_config['api_key']
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
# Create a new coupon
|
15
|
+
coupon = Chargify::Coupon.create(
|
16
|
+
:name => "66% off",
|
17
|
+
:code => "66OFF",
|
18
|
+
:description => "66% OFF for life",
|
19
|
+
:percentage => "66",
|
20
|
+
:recurring => false,
|
21
|
+
:product_family_id => 2
|
22
|
+
)
|
23
|
+
|
24
|
+
# Lookup up existing coupon using the product_family_id and the coupon code
|
25
|
+
coupon = Chargify::Subscription::Coupon.find_by_product_family_id_and_code(2,"10OFF")
|
26
|
+
|
27
|
+
# Looking up all coupons for a product family
|
28
|
+
coupons = Chargify::Coupon.find_all_by_product_family_id(12345)
|
29
|
+
|
30
|
+
# Update coupon information
|
31
|
+
coupon = Chargify::Coupon.find_by_product_family_id_and_code(2,"10OFF")
|
32
|
+
coupon.code = "FOO"
|
33
|
+
coupon.save
|
34
|
+
|
35
|
+
# Archive coupon
|
36
|
+
coupon = Chargify::Coupon.find_by_product_family_id_and_code(2,"10OFF")
|
37
|
+
coupon.archive
|
38
|
+
|
39
|
+
# Inspect coupon usage
|
40
|
+
coupon = Chargify::Coupon.find_by_product_family_id_and_code(2,"10OFF")
|
41
|
+
coupon.usage
|
42
|
+
#>> [{"name"=>"Product 1", "savings"=>0.0, "id"=>8, "signups"=>5, "revenue"=>0.0}, {"name"=>"Product 2", "savings"=>0.0, "id"=>9, "signups"=>0, "revenue"=>0.0}]
|
43
|
+
|
44
|
+
# Apply coupon to subscription
|
45
|
+
subscription = Subscription.find_by_customer_reference('moklett')
|
46
|
+
subscription.add_coupon('5OFF')
|
47
|
+
|
48
|
+
# Remove coupon from subscription
|
49
|
+
subscription = Subscription.find_by_customer_reference('moklett')
|
50
|
+
subscription.remove_coupon
|
@@ -3,7 +3,7 @@ $: << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
|
3
3
|
require 'chargify_api_ares'
|
4
4
|
|
5
5
|
# You could load your credentials from a file...
|
6
|
-
chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', '
|
6
|
+
chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', 'chargify.yml'))
|
7
7
|
|
8
8
|
Chargify.configure do |c|
|
9
9
|
c.subdomain = chargify_config['subdomain']
|
@@ -3,7 +3,7 @@ $: << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
|
3
3
|
require 'chargify_api_ares'
|
4
4
|
|
5
5
|
# You could load your credentials from a file...
|
6
|
-
chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', '
|
6
|
+
chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', 'chargify.yml'))
|
7
7
|
|
8
8
|
Chargify.configure do |c|
|
9
9
|
c.subdomain = chargify_config['subdomain']
|
@@ -32,4 +32,7 @@ u.save
|
|
32
32
|
|
33
33
|
|
34
34
|
x = Chargify::Usage.find(:last, :params => {:subscription_id => subscription.id, :component_id => component.id})
|
35
|
-
puts x.memo == d
|
35
|
+
puts x.memo == d
|
36
|
+
|
37
|
+
# Call a specific Component by ID
|
38
|
+
component = Chargify::ProductFamily::Component.find(component_id, :params => {:product_family_id => product_family_id})
|
@@ -3,7 +3,7 @@ $: << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
|
3
3
|
require 'chargify_api_ares'
|
4
4
|
|
5
5
|
# You could load your credentials from a file...
|
6
|
-
chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', '
|
6
|
+
chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', 'chargify.yml'))
|
7
7
|
|
8
8
|
Chargify.configure do |c|
|
9
9
|
c.subdomain = chargify_config['subdomain']
|
@@ -3,7 +3,7 @@ $: << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
|
3
3
|
require 'chargify_api_ares'
|
4
4
|
|
5
5
|
# You could load your credentials from a file...
|
6
|
-
chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', '
|
6
|
+
chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', 'chargify.yml'))
|
7
7
|
|
8
8
|
Chargify.configure do |c|
|
9
9
|
c.subdomain = chargify_config['subdomain']
|
@@ -50,4 +50,4 @@ subscription.charge(:amount => 10.00, :memo => 'Extra service')
|
|
50
50
|
# Cancel a subscription
|
51
51
|
subscription.cancel
|
52
52
|
subscription.reload
|
53
|
-
# => #<Chargify::Subscription:0x1020ed4b0 @prefix_options={}, @attributes={"cancellation_message"=>nil, "activated_at"=>Tue Nov 17 16:00:17 UTC 2009, "expires_at"=>nil, "updated_at"=>Tue Nov 17 16:00:17 UTC 2009, "credit_card"=>#<Chargify::Subscription::CreditCard:0x10234f168 @prefix_options={}, @attributes={"card_type"=>"bogus", "expiration_year"=>2015, "masked_card_number"=>"XXXX-XXXX-XXXX-2", "first_name"=>"Michael", "expiration_month"=>1, "last_name"=>"Klett"}>, "product"=>#<Chargify::Product:0x10234f370 @prefix_options={}, @attributes={"price_in_cents"=>0, "name"=>"Chargify API Ares Test", "handle"=>"chargify-api-ares-test", "product_family"=>#<Chargify::Product::ProductFamily:0x102354708 @prefix_options={}, @attributes={"name"=>"Chargify API ARes Test", "handle"=>"chargify-api-ares-test", "id"=>79, "accounting_code"=>nil}>, "id"=>153, "accounting_code"=>nil, "interval_unit"=>"month", "interval"=>1}>, "credit_card_attributes"=>{:full_number=>"2", :expiration_year=>"2015"}, "trial_ended_at"=>nil, "id"=>331, "current_period_ends_at"=>Thu Dec 17 16:00:17 UTC 2009, "product_handle"=>"chargify-api-ares-test", "customer"=>#<Chargify::Customer:0x10234f730 @prefix_options={}, @attributes={"reference"=>"moklett", "updated_at"=>Tue Nov 17 15:51:02 UTC 2009, "id"=>331, "first_name"=>"Michael", "organization"=>"Chargify", "last_name"=>"Klett", "email"=>"moklett@example.com", "created_at"=>Tue Nov 17 15:51:02 UTC 2009}>, "trial_started_at"=>nil, "balance_in_cents"=>0, "current_period_started_at"=>Tue Nov 17 16:00:17 UTC 2009, "state"=>"canceled", "created_at"=>Tue Nov 17 16:00:17 UTC 2009, "customer_reference"=>"moklett"}>
|
53
|
+
# => #<Chargify::Subscription:0x1020ed4b0 @prefix_options={}, @attributes={"cancellation_message"=>nil, "activated_at"=>Tue Nov 17 16:00:17 UTC 2009, "expires_at"=>nil, "updated_at"=>Tue Nov 17 16:00:17 UTC 2009, "credit_card"=>#<Chargify::Subscription::CreditCard:0x10234f168 @prefix_options={}, @attributes={"card_type"=>"bogus", "expiration_year"=>2015, "masked_card_number"=>"XXXX-XXXX-XXXX-2", "first_name"=>"Michael", "expiration_month"=>1, "last_name"=>"Klett"}>, "product"=>#<Chargify::Product:0x10234f370 @prefix_options={}, @attributes={"price_in_cents"=>0, "name"=>"Chargify API Ares Test", "handle"=>"chargify-api-ares-test", "product_family"=>#<Chargify::Product::ProductFamily:0x102354708 @prefix_options={}, @attributes={"name"=>"Chargify API ARes Test", "handle"=>"chargify-api-ares-test", "id"=>79, "accounting_code"=>nil}>, "id"=>153, "accounting_code"=>nil, "interval_unit"=>"month", "interval"=>1}>, "credit_card_attributes"=>{:full_number=>"2", :expiration_year=>"2015"}, "trial_ended_at"=>nil, "id"=>331, "current_period_ends_at"=>Thu Dec 17 16:00:17 UTC 2009, "product_handle"=>"chargify-api-ares-test", "customer"=>#<Chargify::Customer:0x10234f730 @prefix_options={}, @attributes={"reference"=>"moklett", "updated_at"=>Tue Nov 17 15:51:02 UTC 2009, "id"=>331, "first_name"=>"Michael", "organization"=>"Chargify", "last_name"=>"Klett", "email"=>"moklett@example.com", "created_at"=>Tue Nov 17 15:51:02 UTC 2009}>, "trial_started_at"=>nil, "balance_in_cents"=>0, "current_period_started_at"=>Tue Nov 17 16:00:17 UTC 2009, "state"=>"canceled", "created_at"=>Tue Nov 17 16:00:17 UTC 2009, "customer_reference"=>"moklett"}>
|
@@ -3,15 +3,14 @@ $: << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
|
3
3
|
require 'chargify_api_ares'
|
4
4
|
|
5
5
|
# You could load your credentials from a file...
|
6
|
-
chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', '
|
6
|
+
chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', 'chargify.yml'))
|
7
7
|
|
8
8
|
Chargify.configure do |c|
|
9
9
|
c.subdomain = chargify_config['subdomain']
|
10
10
|
c.api_key = chargify_config['api_key']
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
13
|
# Retrieve a list of all your customers
|
15
14
|
subscription = Chargify::Subscription.find(:all).first
|
16
15
|
|
17
|
-
transactions = subscription.transactions
|
16
|
+
transactions = subscription.transactions
|
data/lib/chargify_api_ares.rb
CHANGED
@@ -1,314 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require '
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
ERROR
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
|
20
|
-
# Version check
|
21
|
-
module Chargify
|
22
|
-
MIN_VERSION = '2.3.4'
|
23
|
-
end
|
24
|
-
require 'active_resource/version'
|
25
|
-
unless Gem::Version.new(ActiveResource::VERSION::STRING) >= Gem::Version.new(Chargify::MIN_VERSION)
|
26
|
-
abort <<-ERROR
|
27
|
-
ActiveResource version #{Chargify::MIN_VERSION} or greater is required.
|
28
|
-
ERROR
|
29
|
-
end
|
30
|
-
|
31
|
-
# Patch ActiveResource version 2.3.4
|
32
|
-
if ActiveResource::VERSION::STRING == '2.3.4'
|
33
|
-
module ActiveResource
|
34
|
-
class Base
|
35
|
-
def save
|
36
|
-
save_without_validation
|
37
|
-
true
|
38
|
-
rescue ResourceInvalid => error
|
39
|
-
case error.response['Content-Type']
|
40
|
-
when /application\/xml/
|
41
|
-
errors.from_xml(error.response.body)
|
42
|
-
when /application\/json/
|
43
|
-
errors.from_json(error.response.body)
|
44
|
-
end
|
45
|
-
false
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
|
52
|
-
module Chargify
|
53
|
-
|
54
|
-
class << self
|
55
|
-
attr_accessor :subdomain, :api_key, :site, :format, :timeout
|
56
|
-
|
57
|
-
def configure
|
58
|
-
yield self
|
59
|
-
|
60
|
-
Base.user = api_key
|
61
|
-
Base.password = 'X'
|
62
|
-
Base.timeout = timeout unless (timeout.blank?)
|
63
|
-
|
64
|
-
self.site ||= "https://#{subdomain}.chargify.com"
|
65
|
-
|
66
|
-
Base.site = site
|
67
|
-
Subscription::Component.site = site + "/subscriptions/:subscription_id"
|
68
|
-
Subscription::Statement.site = site + "/subscriptions/:subscription_id"
|
69
|
-
Subscription::Transaction.site = site + "/subscriptions/:subscription_id"
|
70
|
-
Coupon.site = site + "/product_families/:product_family_id"
|
71
|
-
ProductFamily::Component.site = site + "/product_families/:product_family_id"
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
class Base < ActiveResource::Base
|
76
|
-
self.format = :xml
|
77
|
-
|
78
|
-
def self.element_name
|
79
|
-
name.split(/::/).last.underscore
|
80
|
-
end
|
81
|
-
|
82
|
-
def to_xml(options = {})
|
83
|
-
options.merge!(:dasherize => false)
|
84
|
-
super
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
class Site < Base
|
89
|
-
def self.clear_data!(params = {})
|
90
|
-
post(:clear_data, params)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
class Customer < Base
|
95
|
-
def self.find_by_reference(reference)
|
96
|
-
Customer.new get(:lookup, :reference => reference)
|
97
|
-
end
|
98
|
-
|
99
|
-
def subscriptions(params = {})
|
100
|
-
params.merge!({:customer_id => self.id})
|
101
|
-
Subscription.find(:all, :params => params)
|
102
|
-
end
|
103
|
-
|
104
|
-
def payment_profiles(params = {})
|
105
|
-
params.merge!({:customer_id => self.id})
|
106
|
-
PaymentProfile.find(:all, :params => params)
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
class Subscription < Base
|
111
|
-
def self.find_by_customer_reference(reference)
|
112
|
-
customer = Customer.find_by_reference(reference)
|
113
|
-
find(:first, :params => {:customer_id => customer.id})
|
114
|
-
end
|
115
|
-
|
116
|
-
# Strip off nested attributes of associations before saving, or type-mismatch errors will occur
|
117
|
-
def save
|
118
|
-
self.attributes.delete('customer')
|
119
|
-
self.attributes.delete('product')
|
120
|
-
self.attributes.delete('credit_card')
|
121
|
-
super
|
122
|
-
end
|
123
|
-
|
124
|
-
def cancel
|
125
|
-
destroy
|
126
|
-
end
|
127
|
-
|
128
|
-
def component(id)
|
129
|
-
Component.find(id, :params => {:subscription_id => self.id})
|
130
|
-
end
|
131
|
-
|
132
|
-
def components(params = {})
|
133
|
-
params.merge!({:subscription_id => self.id})
|
134
|
-
Component.find(:all, :params => params)
|
135
|
-
end
|
136
|
-
|
137
|
-
def payment_profile
|
138
|
-
self.respond_to?('credit_card') ? credit_card : nil
|
139
|
-
end
|
140
|
-
|
141
|
-
# Perform a one-time charge on an existing subscription.
|
142
|
-
# For more information, please see the one-time charge API docs available
|
143
|
-
# at: http://support.chargify.com/faqs/api/api-charges
|
144
|
-
def charge(attrs = {})
|
145
|
-
post :charges, {}, attrs.to_xml(:root => :charge)
|
146
|
-
end
|
147
|
-
|
148
|
-
def credit(attrs = {})
|
149
|
-
post :credits, {}, attrs.to_xml(:root => :credit)
|
150
|
-
end
|
151
|
-
|
152
|
-
def refund(attrs = {})
|
153
|
-
post :refunds, {}, attrs.to_xml(:root => :refund)
|
154
|
-
end
|
155
|
-
|
156
|
-
def reactivate(params = {})
|
157
|
-
put :reactivate, params
|
158
|
-
end
|
159
|
-
|
160
|
-
def reset_balance
|
161
|
-
put :reset_balance
|
162
|
-
end
|
163
|
-
|
164
|
-
def migrate(attrs = {})
|
165
|
-
post :migrations, :migration => attrs
|
166
|
-
end
|
167
|
-
|
168
|
-
def statement(id)
|
169
|
-
statement = Chargify::Statement.find(id)
|
170
|
-
raise ActiveResource::ResourceNotFound.new(nil) if (statement.subscription_id != self.id)
|
171
|
-
statement
|
172
|
-
end
|
173
|
-
|
174
|
-
def statements(params = {})
|
175
|
-
params.merge!(:subscription_id => self.id)
|
176
|
-
Statement.find(:all, :params => params)
|
177
|
-
end
|
178
|
-
|
179
|
-
def transactions(params = {})
|
180
|
-
params.merge!(:subscription_id => self.id)
|
181
|
-
Transaction.find(:all, :params => params)
|
182
|
-
end
|
183
|
-
|
184
|
-
def adjustment(attrs = {})
|
185
|
-
post :adjustments, {}, attrs.to_xml(:root => :adjustment)
|
186
|
-
end
|
187
|
-
|
188
|
-
def add_coupon(code)
|
189
|
-
post :add_coupon, :code => code
|
190
|
-
end
|
191
|
-
|
192
|
-
def remove_coupon(code)
|
193
|
-
delete :remove_coupon, :code => code
|
194
|
-
end
|
195
|
-
|
196
|
-
class Component < Base
|
197
|
-
# All Subscription Components are considered already existing records, but the id isn't used
|
198
|
-
def id
|
199
|
-
self.component_id
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
class Statement < Base
|
204
|
-
end
|
205
|
-
|
206
|
-
class Transaction < Base
|
207
|
-
def full_refund(attrs = {})
|
208
|
-
return false if self.transaction_type != 'payment'
|
209
|
-
|
210
|
-
attrs.merge!(:amount_in_cents => self.amount_in_cents)
|
211
|
-
self.refund(attrs)
|
212
|
-
end
|
213
|
-
|
214
|
-
def refund(attrs = {})
|
215
|
-
return false if self.transaction_type != 'payment'
|
216
|
-
|
217
|
-
attrs.merge!(:payment_id => self.id)
|
218
|
-
Subscription.find(self.prefix_options[:subscription_id]).refund(attrs)
|
219
|
-
end
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
class Product < Base
|
224
|
-
def self.find_by_handle(handle)
|
225
|
-
Product.new get(:lookup, :handle => handle)
|
226
|
-
end
|
227
|
-
|
228
|
-
protected
|
229
|
-
|
230
|
-
# Products are created in the scope of a ProductFamily, i.e. /product_families/nnn/products
|
231
|
-
#
|
232
|
-
# This alters the collection path such that it uses the product_family_id that is set on the
|
233
|
-
# attributes.
|
234
|
-
def create
|
235
|
-
pfid = begin
|
236
|
-
self.product_family_id
|
237
|
-
rescue NoMethodError
|
238
|
-
0
|
239
|
-
end
|
240
|
-
connection.post("/product_families/#{pfid}/products.#{self.class.format.extension}", encode, self.class.headers).tap do |response|
|
241
|
-
self.id = id_from_response(response)
|
242
|
-
load_attributes_from_response(response)
|
243
|
-
end
|
244
|
-
end
|
245
|
-
end
|
246
|
-
|
247
|
-
class ProductFamily < Base
|
248
|
-
def self.find_by_handle(handle, attributes = {})
|
249
|
-
ProductFamily.find(:one, :from => :lookup, :params => { :handle => handle })
|
250
|
-
end
|
251
|
-
|
252
|
-
class Component < Base
|
253
|
-
end
|
254
|
-
|
255
|
-
def components(params = {})
|
256
|
-
params.merge!({:product_family_id => self.id})
|
257
|
-
::Chargify::ProductFamily::Component.find(:all, :params => params)
|
258
|
-
end
|
259
|
-
|
260
|
-
end
|
261
|
-
|
262
|
-
class Usage < Base
|
263
|
-
def subscription_id=(i)
|
264
|
-
self.prefix_options[:subscription_id] = i
|
265
|
-
end
|
266
|
-
def component_id=(i)
|
267
|
-
self.prefix_options[:component_id] = i
|
268
|
-
end
|
269
|
-
end
|
270
|
-
|
271
|
-
class Component < Base
|
272
|
-
end
|
273
|
-
|
274
|
-
class Statement < Base
|
275
|
-
end
|
276
|
-
|
277
|
-
class Transaction < Base
|
278
|
-
def full_refund(attrs = {})
|
279
|
-
return false if self.transaction_type != 'payment'
|
280
|
-
|
281
|
-
attrs.merge!(:amount_in_cents => self.amount_in_cents)
|
282
|
-
self.refund(attrs)
|
283
|
-
end
|
284
|
-
|
285
|
-
def refund(attrs = {})
|
286
|
-
return false if self.transaction_type != 'payment'
|
287
|
-
|
288
|
-
attrs.merge!(:payment_id => self.id)
|
289
|
-
Subscription.find(self.subscription_id).refund(attrs)
|
290
|
-
end
|
291
|
-
end
|
292
|
-
|
293
|
-
class PaymentProfile < Base
|
294
|
-
end
|
295
|
-
|
296
|
-
class Coupon < Base
|
297
|
-
def self.find_all_by_product_family_id(product_family_id)
|
298
|
-
Coupon.find(:all, :params => { :product_family_id => product_family_id })
|
299
|
-
end
|
300
|
-
|
301
|
-
def self.find_by_product_family_id_and_code(product_family_id, code)
|
302
|
-
Coupon.new get(:lookup, :product_family_id => product_family_id, :code => code)
|
303
|
-
end
|
304
|
-
|
305
|
-
def usage
|
306
|
-
get :usage
|
307
|
-
end
|
308
|
-
|
309
|
-
def archive
|
310
|
-
self.destroy
|
311
|
-
end
|
312
|
-
end
|
313
|
-
|
314
|
-
end
|
1
|
+
require 'active_resource'
|
2
|
+
require 'chargify_api_ares/config'
|
3
|
+
require 'chargify_api_ares/resources/base'
|
4
|
+
require 'chargify_api_ares/resources/component'
|
5
|
+
require 'chargify_api_ares/resources/coupon'
|
6
|
+
require 'chargify_api_ares/resources/customer'
|
7
|
+
require 'chargify_api_ares/resources/payment_profile'
|
8
|
+
require 'chargify_api_ares/resources/product'
|
9
|
+
require 'chargify_api_ares/resources/product_family'
|
10
|
+
require 'chargify_api_ares/resources/site'
|
11
|
+
require 'chargify_api_ares/resources/statement'
|
12
|
+
require 'chargify_api_ares/resources/subscription'
|
13
|
+
require 'chargify_api_ares/resources/transaction'
|
14
|
+
require 'chargify_api_ares/resources/usage'
|