jam-cloud/web/lib/recurly_client.rb

149 lines
4.7 KiB
Ruby

require 'recurly'
module JamRuby
class RecurlyClient
def initialize()
end
def create_account(current_user, billing_info=nil)
options = account_hash(current_user, billing_info)
account = nil
begin
account = Recurly::Account.create(options)
raise RecurlyClientError.new(account.errors) if account.errors.any?
rescue Recurly::Error, NoMethodError => x
raise RecurlyClientError, x.to_s
else
if account
current_user.update_attribute(:recurly_code, account.account_code)
end
end
account
end
def delete_account(current_user)
account = get_account(current_user)
if (account)
begin
account.destroy
rescue Recurly::Error, NoMethodError => x
raise RecurlyClientError, x.to_s
end
else
raise RecurlyClientError, "Could not find account to delete."
end
account
end
def get_account(current_user)
(current_user && current_user.recurly_code) ? Recurly::Account.find(current_user.recurly_code) : nil
end
def update_account(current_user, billing_info=nil)
account = get_account(current_user)
if(account.present?)
options = account_hash(current_user, billing_info)
begin
account.update_attributes(options)
rescue Recurly::Error, NoMethodError => x
raise RecurlyClientError, x.to_s
end
end
account
end
def update_billing_info(current_user, billing_info=nil)
account = get_account(current_user)
if (account.present?)
begin
account.billing_info=billing_info
account.billing_info.save
rescue Recurly::Error, NoMethodError => x
raise RecurlyClientError, x.to_s
end
raise RecurlyClientError.new(account.errors) if account.errors.any?
else
raise RecurlyClientError, "Could not find account to update billing info."
end
account
end
def place_order(current_user, jam_track)
account = get_account(current_user)
if (account.present?)
begin
subscription = Recurly::Subscription.create(:account=>account, :plan_code=>jam_track.plan_code)
raise RecurlyClientError.new(subscription.errors) if subscription.errors.any?
# Reload and make sure it went through:
account = get_account(current_user)
paid_subscription = account.subscriptions.last
raise RecurlyClientError, "Subscription not found" if paid_subscription.nil?
raise RecurlyClientError, "Plan code '#{paid_subscription.plan_code}' doesn't match jam track: '#{jam_track.plan_code}'" if paid_subscription.plan_code != jam_track.plan_code
jam_track_right=JamRuby::JamTrackRight.find_or_create_by_user_id_and_jam_track_id(current_user.id, jam_track.id)
raise RecurlyClientError.new("Error creating jam_track_right for jam_track: #{jam_track.id}") if jam_track_right.nil?
raise RecurlyClientError.new(jam_track_right.errors) if jam_track_right.errors.any?
rescue Recurly::Error, NoMethodError => x
raise RecurlyClientError, x.to_s
end
raise RecurlyClientError.new(account.errors) if account.errors.any?
else
raise RecurlyClientError, "Could not find account to place order."
end
account
end
def find_or_create_account(current_user, billing_info=nil)
account = get_account(current_user)
if(account.nil?)
account = create_account(current_user, billing_info)
else
update_billing_info(current_user, billing_info)
end
account
end
private
def account_hash(current_user, billing_info)
options = {
account_code: current_user.id,
email: current_user.email,
first_name: current_user.first_name,
last_name: current_user.last_name,
address: {
city: current_user.city,
state: current_user.state,
country: current_user.country
}
}
options[:billing_info] = billing_info if billing_info
options
end
end # class
class RecurlyClientError < Exception
attr_accessor :errors
def initialize(data)
if data.respond_to?('has_key?')
self.errors = data
else
self.errors = {:message=>data.to_s}
end
end # initialize
def to_s
s=super
s << ", errors: #{errors.inspect}" if self.errors.any?
s
end
end # RecurlyClientError
end # module