98 lines
3.0 KiB
Ruby
98 lines
3.0 KiB
Ruby
require '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}"
|
|
params[:jam_tracks].each do |jam_track_id|
|
|
jam_track = JamTrack.where("id=?", jam_track_id).first
|
|
if jam_track
|
|
@client.place_order(current_user, jam_track)
|
|
else
|
|
error="JamTrack not found for '#{jam_track_id}'"
|
|
break
|
|
end
|
|
end
|
|
|
|
if error
|
|
render json: { errors: {message:error}}, :status => 404
|
|
else
|
|
render :json=>{}, :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 |