jam-cloud/web/app/controllers/api_recurly_controller.rb

114 lines
3.6 KiB
Ruby

require 'jam_ruby/recurly_client'
class ApiRecurlyController < ApiController
before_filter :api_signed_in_user
before_filter :create_client
respond_to :json
# create Recurly account
def create_account
@account = @client.find_or_create_account(current_user, params[:billing_info])
render :json=>account_json(@account)
rescue RecurlyClientError => x
render json: { :message => x.inspect, errors: x.errors }, :status => 404
end
def delete_account
@client.delete_account(current_user)
render json: {}, status: 200
rescue RecurlyClientError => x
render json: { :message => x.inspect, errors: x.errors}, :status => 404
end
# get Recurly account
def get_account
@account=@client.get_account(current_user)
render :json=>account_json(@account)
rescue RecurlyClientError => e
render json: { message: x.inspect, errors: x.errors}, :status => 404
end
# update Recurly account
def update_account
@account=@client.update_account(current_user, params[:billing_info])
render :json=>account_json(@account)
rescue RecurlyClientError => x
render json: { message: x.inspect, errors: x.errors}, :status => 404
end
# get Billing Information
def billing_info
@account = @client.get_account(current_user)
# @billing = @account.billing_info
# @billing ||= @account
render :json=> account_json(@account)
rescue RecurlyClientError => x
render json: { message: x.inspect, errors: x.errors}, :status => 404
end
# update Billing Information
def update_billing_info
@account=@client.update_billing_info(current_user, params[:billing_info])
render :json=> account_json(@account)
rescue RecurlyClientError => x
render json: { message: x.inspect, errors: x.errors}, :status => 404
end
def place_order
error=nil
puts "PLACING ORDER #{params.inspect}"
response = {jam_tracks:[]}
# 1st confirm that all specified JamTracks exist
jam_tracks = []
params[:jam_tracks].each do |jam_track_id|
jam_track = JamTrack.where("id=?", jam_track_id).first
if jam_track
jam_tracks << jam_track
else
error="JamTrack not found for '#{jam_track_id}'"
break
end
end
# then buy each
unless error
jam_tracks.each do |jam_track|
jam_track_right = @client.place_order(current_user, jam_track)
# build up the response object with JamTracks that were purchased.
# if this gets more complicated, we should switch to RABL
response[:jam_tracks] << {name: jam_track.name, id: jam_track.id, jam_track_right_id: jam_track_right.id, version: jam_track.version}
end
end
if error
render json: { errors: {message:error}}, :status => 404
else
render :json=>response, :status=>200
end
rescue RecurlyClientError => x
render json: { message: x.inspect, errors: x.errors}, :status => 404
end
private
def create_client
@client = RecurlyClient.new
end
def account_json(account)
{
:first_name => account.first_name,
:last_name => account.last_name,
:email => account.email,
:address1 => account.billing_info ? account.billing_info.address1 : nil,
:address2 => account.billing_info ? account.billing_info.address2 : nil,
:city => account.billing_info ? account.billing_info.city : nil,
:state => account.billing_info ? account.billing_info.state : nil,
:zip => account.billing_info ? account.billing_info.zip : nil,
:country => account.billing_info ? account.billing_info.country : nil
}
end
end # class