182 lines
5.7 KiB
Ruby
182 lines
5.7 KiB
Ruby
require 'jam_ruby/recurly_client'
|
|
class ApiRecurlyController < ApiController
|
|
before_filter :api_signed_in_user, :except => [:create_account]
|
|
before_filter :create_client
|
|
before_filter :ip_blacklist, :only => [:place_order]
|
|
before_filter :user_blacklist, :only => [:place_order]
|
|
|
|
|
|
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 billing_info && Rails.application.config.remove_whitespace_credit_card
|
|
number = billing_info[:number]
|
|
billing_info[:number] = number.gsub(/\s+/, "") if number
|
|
end
|
|
|
|
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 = {
|
|
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,
|
|
location: {:country => billing_info[:country], :state => billing_info[:state], :city => billing_info[:city]},
|
|
reuse_card: reuse_card_next_time,
|
|
affiliate_referral_id: cookies[:affiliate_visitor]
|
|
}
|
|
|
|
options = User.musician_defaults(request.remote_ip, ApplicationHelper.base_uri(request) + "/confirm", any_user, options)
|
|
|
|
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
|
|
|
|
# get Recurly payment history
|
|
def payment_history
|
|
@payments=@client.payment_history(current_user)
|
|
render :json => {payments: @payments}
|
|
rescue RecurlyClientError => x
|
|
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: []}
|
|
|
|
if Sale.is_mixed(current_user.shopping_carts)
|
|
msg = "has free and non-free items. Try removing non-free items."
|
|
render json: {message: "Cart " + msg, errors: {cart: [msg]}}, :status => 404
|
|
return
|
|
end
|
|
|
|
sales = Sale.place_order(current_user, current_user.shopping_carts)
|
|
|
|
|
|
sales.each do |sale|
|
|
if sale.is_jam_track_sale?
|
|
sale.sale_line_items.each do |line_item|
|
|
jam_track = line_item.product
|
|
jam_track_right = jam_track.right_for_user(current_user)
|
|
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
|
|
end
|
|
|
|
if error
|
|
render json: {errors: {message: error}}, :status => 404
|
|
else
|
|
set_purchased_jamtrack_cookie
|
|
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 |