require 'jam_ruby/recurly_client' class ApiRecurlyController < ApiController before_filter :api_signed_in_user, :except => [:create_account] before_filter :create_client respond_to :json # create Recurly account def create_account billing_info = params[:billing_info] shipping_info = params[:shipping_info] # should we let the user reuse this card next time? reuse_card_next_time = params[:reuse_card_next_time] == "true" # should we update the card info, or use what's on file this time? reuse_card_this_time = params[:reuse_card_this_time] == "true" # terms of service accepted? terms_of_service = params[:terms_of_service] == "true" if current_user # keep reuse card up-to-date User.where(id: current_user.id).update_all(reuse_card: params[:reuse_card_next_time]) else options = { remote_ip: request.remote_ip, first_name: billing_info[:first_name], last_name: billing_info[:last_name], email: params[:email], password: params[:password], password_confirmation: params[:password], terms_of_service: terms_of_service, instruments: [{ :instrument_id => 'other', :proficiency_level => 1, :priority => 1 }], birth_date: nil, location: { :country => billing_info[:country], :state => billing_info[:state], :city => billing_info[:city]}, musician: true, skip_recaptcha: true, invited_user: nil, fb_signup: nil, signup_confirm_url: ApplicationHelper.base_uri(request) + "/confirm", any_user: any_user, reuse_card: reuse_card_next_time } user = UserManager.new.signup(options) if user.errors.any? # render any @user.errors on error respond_with_model(user) return else sign_in user end end begin billing_info[:ip_address] = request.remote_ip if billing_info if reuse_card_this_time # do not attempt to update any billing/shipping info unless the user re-inputs their info! @account = @client.get_account(current_user) else @account = @client.find_or_create_account(current_user, billing_info) end render :json=>account_json(@account) rescue RecurlyClientError => x render json: { :message => x.inspect, errors: x.errors }, :status => 404 end 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) if @account render :json=> account_json(@account) else render :json=> {}, :status => 404 end 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 response = {jam_tracks:[]} current_user.shopping_carts.each do |shopping_cart| jam_track = shopping_cart.cart_product # if shopping_cart has any marked_for_redeem, then we zero out the price by passing in 'free' # NOTE: shopping_carts have the idea of quantity, but you should only be able to buy at most one JamTrack. So anything > 0 is considered free for a JamTrack jam_track_right = @client.place_order(current_user, jam_track, shopping_cart) # 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 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) billing_info = account.billing_info.nil? ? nil : { :first_name => account.billing_info.first_name, :last_name => account.billing_info.last_name, :address1 => account.billing_info.address1, :address2 => account.billing_info.address2, :city => account.billing_info.city, :state => account.billing_info.state, :zip => account.billing_info.zip, :country => account.billing_info.country, :last_four => account.billing_info.last_four } { billing_info: billing_info } end end # class