Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cha ching! #20

Merged
merged 1 commit into from Jul 25, 2011
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 45 additions & 37 deletions lib/chargify_api_ares.rb
Expand Up @@ -8,7 +8,7 @@
require 'active_resource'
rescue LoadError
abort <<-ERROR
The 'activeresource' library could not be loaded. If you have RubyGems
The 'activeresource' library could not be loaded. If you have RubyGems
installed you can install ActiveResource by doing "gem install activeresource".
ERROR
end
Expand Down Expand Up @@ -48,17 +48,17 @@ def save


module Chargify

class << self
attr_accessor :subdomain, :api_key, :site, :format, :timeout

def configure
yield self

Base.user = api_key
Base.password = 'X'
Base.timeout = timeout unless (timeout.blank?)

self.site ||= "https://#{subdomain}.chargify.com"

Base.site = site
Expand All @@ -67,29 +67,29 @@ def configure
Subscription::Transaction.site = site + "/subscriptions/:subscription_id"
end
end

class Base < ActiveResource::Base
def self.element_name
name.split(/::/).last.underscore
end

def to_xml(options = {})
options.merge!(:dasherize => false)
super
end
end

class Site < Base
def self.clear_data!(params = {})
post(:clear_data, params)
end
end

class Customer < Base
def self.find_by_reference(reference)
Customer.new get(:lookup, :reference => reference)
end

def subscriptions(params = {})
params.merge!({:customer_id => self.id})
Subscription.find(:all, :params => params)
Expand All @@ -100,61 +100,61 @@ def payment_profiles(params = {})
PaymentProfile.find(:all, :params => params)
end
end

class Subscription < Base
def self.find_by_customer_reference(reference)
customer = Customer.find_by_reference(reference)
find(:first, :params => {:customer_id => customer.id})
find(:first, :params => {:customer_id => customer.id})
end

# Strip off nested attributes of associations before saving, or type-mismatch errors will occur
def save
self.attributes.delete('customer')
self.attributes.delete('product')
self.attributes.delete('credit_card')
super
end

def cancel
destroy
end

def component(id)
Component.find(id, :params => {:subscription_id => self.id})
end

def components(params = {})
params.merge!({:subscription_id => self.id})
Component.find(:all, :params => params)
end

def payment_profile
credit_card
end

# Perform a one-time charge on an existing subscription.
# For more information, please see the one-time charge API docs available
# For more information, please see the one-time charge API docs available
# at: http://support.chargify.com/faqs/api/api-charges
def charge(attrs = {})
post :charges, {}, attrs.to_xml(:root => :charge)
end

def credit(attrs = {})
post :credits, {}, attrs.to_xml(:root => :credit)
end

def refund(attrs = {})
post :refunds, {}, attrs.to_xml(:root => :refund)
end

def reactivate(params = {})
put :reactivate, params
end

def reset_balance
put :reset_balance
end

def migrate(attrs = {})
post :migrations, :migration => attrs
end
Expand All @@ -164,24 +164,32 @@ def statement(id)
raise ActiveResource::ResourceNotFound.new(nil) if (statement.subscription_id != self.id)
statement
end

def statements(params = {})
params.merge!(:subscription_id => self.id)
Statement.find(:all, :params => params)
end

def transactions(params = {})
params.merge!(:subscription_id => self.id)
Transaction.find(:all, :params => params)
end


def add_coupon(code)
post :add_coupon, :code => code
end

def remove_coupon
delete :remove_coupon
end

class Component < Base
# All Subscription Components are considered already existing records, but the id isn't used
def id
self.component_id
end
end

class Statement < Base
end

Expand All @@ -206,12 +214,12 @@ class Product < Base
def self.find_by_handle(handle)
Product.new get(:lookup, :handle => handle)
end

protected

# Products are created in the scope of a ProductFamily, i.e. /product_families/nnn/products
#
# This alters the collection path such that it uses the product_family_id that is set on the
# This alters the collection path such that it uses the product_family_id that is set on the
# attributes.
def create
pfid = begin
Expand All @@ -225,22 +233,22 @@ def create
end
end
end

class ProductFamily < Base
def self.find_by_handle(handle, attributes = {})
ProductFamily.find(:one, :from => :lookup, :handle => handle)
end
end

class Usage < Base
def subscription_id=(i)
self.prefix_options[:subscription_id] = i
end
def component_id=(i)
self.prefix_options[:component_id] = i
end
end
end

class Component < Base
end

Expand All @@ -262,8 +270,8 @@ def refund(attrs = {})
Subscription.find(self.subscription_id).refund(attrs)
end
end

class PaymentProfile < Base
end

end