ttinggggg
ttinggggg

Reputation: 157

Rails app: Allow people to make a purchase without logging in

Im trying to remove the signing up / logging in requirement on my rails app for users to make a booking but failed to do so. I'm pretty new and I'm not sure what other changes I'm suppose to make but missed out, any help will be appreciated, thanks!

Working code (with the signing up / logging in requirement)

    class CourseController < ApplicationController
      before_filter { @account_menu = :courses }
      before_filter :authenticate_user!, :except => ['show', 'find', 'messages', 'chat_threads', 'new']

      before_filter lambda {
        if current_profile.try(:learner?)
          flash[:alert] = "You cannot access these pages"
          redirect_to '/'
        end
      }, :only => [:new, :edit, :cancel, :start, :complete]

      before_filter lambda {
        if current_profile.teacher?
          flash[:alert] = "You cannot access these pages"
          redirect_to '/'
        end
      }, :only => [:book, :wish_list, :wish, :unwish, :cancel_booking, :pending]

    layout 'account'

    def book
        @title = "Book a Course"
        @course = Course.by_uid(params[:uid])

        if current_profile.learner?
          if request.post?
            price = params[:price].try(:to_f) || 0
            current_profile.update_attributes({ :contact_email => params[:contact_email], :contact_phone => params[:contact_phone] })
            params['payment-option'] = 'learnlist' if price == 0
            case params['payment-option']
              when 'learnlist' then
                if current_user.balance >= price
                  current_user.transaction do
                    br = BookingRequest.create!({
                                                    :course_id => @course.id,
                                                    :deposited_price => price,
                                                    :hourly_price => params[:hourly_price].try(&:to_f),
                                                    :lessons => params[:lessons] || [],
                                                    :end_of_base_period => params[:end_of_base_period],
                                                    :learner_id => current_profile.id,
                                                    :source => 'learnlist',
                                                    :comments_by_learner => params[:comments],
                                                    :place_to_come_by_learner => params[:place_to_come],
                                                    :attendance => params[:attendance],
                                                    :learner_username => params[:username],
                                                    :learner_video_chat_platform => params[:video_chat_platform],
                                                    :cancellation_commission => @course.cancellation_commission
                                                })
                    flash[:notice] = "Your booking request has been successfully sent to the teacher for confirmation"
                    Notification.add(@course.teacher.user, 'booking_request', br)
                    redirect_to course_path(@course.uid)
                  end
                else
                  flash.now[:alert] = "You don't have enough funds in your account to book this course. You'll have to pay with PayPal to book the class"
                end
              when 'paypal' then
                if !current_profile.paypal_set_up? || params[:paypal_email] != current_profile.paypal_email
                  result = Financials.paypal_preapproval(current_profile, params[:paypal_email], { :course => @course.uid, :price => price, :lessons => params[:lessons] || [], :end_of_base_period => params[:end_of_base_period], :hourly_price => params[:hourly_price].try(&:to_f) })
                  if result
                    current_profile.update_attributes!({
                                                           :paypal_preapproval_id => result[:preapproval_id],
                                                           :paypal_preapproval_confirmed_at => nil,
                                                           :paypal_email => params[:paypal_email]
                                                       })
                    redirect_to result[:redirect_url]
                  else
                    flash.now[:alert] = "Could not setup PayPal payments. Payments preapproval could not be requested"
                  end
                else
                  br = BookingRequest.create!({
                                                  :course_id => @course.id,
                                                  :deposited_price => price,
                                                  :hourly_price => params[:hourly_price].try(&:to_f),
                                                  :lessons => params[:lessons] || [],
                                                  :end_of_base_period => params[:end_of_base_period],
                                                  :learner_id => current_profile.id,
                                                  :source => 'paypal',
                                                  :comments_by_learner => params[:comments],
                                                  :place_to_come_by_learner => params[:place_to_come],
                                                  :attendance => params[:attendance],
                                                  :learner_username => params[:username],
                                                  :learner_video_chat_platform => params[:video_chat_platform],
                                                  :cancellation_commission => @course.cancellation_commission,
                                                  :learnlist_partial_funding => params[:learnlist_partial].try(:to_i) == 1
                                              })
                  Notification.add(@course.teacher.user, 'booking_request', br)
                  flash[:notice] = "Booking successfully submitted"
                  redirect_to course_path(@course.uid)
                end
              when 'braintree' then
                if params[:payment_method_nonce].blank?
                  flash.now[:alert] = 'You did not configure your payment method. Please click Configure and set it up to proceed'
                else
                  br = BookingRequest.create!({
                                                  :course_id => @course.id,
                                                  :deposited_price => price,
                                                  :hourly_price => params[:hourly_price].try(&:to_f),
                                                  :lessons => params[:lessons] || [],
                                                  :end_of_base_period => params[:end_of_base_period],
                                                  :learner_id => current_profile.id,
                                                  :source => 'braintree',
                                                  :comments_by_learner => params[:comments],
                                                  :place_to_come_by_learner => params[:place_to_come],
                                                  :attendance => params[:attendance],
                                                  :learner_username => params[:username],
                                                  :learner_video_chat_platform => params[:video_chat_platform],
                                                  :braintree_payment_method_nonce => params[:payment_method_nonce],
                                                  :cancellation_commission => @course.cancellation_commission,
                                                  :learnlist_partial_funding => params[:learnlist_partial].try(:to_i) == 1
                                              })
                  Notification.add(@course.teacher.user, 'booking_request', br)
                  flash[:notice] = "Booking successfully submitted"
                  redirect_to course_path(@course.uid)
                end
            end
          end
        else
          flash[:alert] = "You cannot access this view"
          redirect_to '/'
        end
      end

My attempts to remove the signing up / logging in requirement are as follow. I failed to do it as clicking the book button brings me back to the homepage instead of the book view.

    class CourseController < ApplicationController
  before_filter { @account_menu = :courses }
  before_filter :authenticate_user!, :except => ['show', 'find', 'messages', 'chat_threads', 'new', 'book']

  before_filter lambda {
    if current_profile.try(:learner?)
      flash[:alert] = "You cannot access these pages"
      redirect_to '/'
    end
  }, :only => [:new, :edit, :cancel, :start, :complete]

  before_filter lambda {
    if current_profile.try(:teacher?)
      flash[:alert] = "You cannot access these pages"
      redirect_to '/'
    end
  }, :only => [:book, :wish_list, :wish, :unwish, :cancel_booking, :pending]

  layout 'account'

def book
    @title = "Book a Course"
    @course = Course.by_uid(params[:uid])

      if request.post?
        price = params[:price].try(:to_f) || 0
        current_profile.update_attributes({ :contact_email => params[:contact_email], :contact_phone => params[:contact_phone] })
        params['payment-option'] = 'learnlist' if price == 0
        case params['payment-option']
          when 'learnlist' then
            if current_user.balance >= price
              current_user.transaction do
                br = BookingRequest.create!({
                                                :course_id => @course.id,
                                                :deposited_price => price,
                                                :hourly_price => params[:hourly_price].try(&:to_f),
                                                :lessons => params[:lessons] || [],
                                                :end_of_base_period => params[:end_of_base_period],
                                                :learner_id => current_profile.id,
                                                :source => 'learnlist',
                                                :comments_by_learner => params[:comments],
                                                :place_to_come_by_learner => params[:place_to_come],
                                                :attendance => params[:attendance],
                                                :learner_username => params[:username],
                                                :learner_video_chat_platform => params[:video_chat_platform],
                                                :cancellation_commission => @course.cancellation_commission
                                            })
                flash[:notice] = "Your booking request has been successfully sent to the teacher for confirmation"
                Notification.add(@course.teacher.user, 'booking_request', br)
                redirect_to course_path(@course.uid)
              end
            else
              flash.now[:alert] = "You don't have enough funds in your account to book this course. You'll have to pay with PayPal to book the class"
            end
          when 'paypal' then
            if !current_profile.paypal_set_up? || params[:paypal_email] != current_profile.paypal_email
              result = Financials.paypal_preapproval(current_profile, params[:paypal_email], { :course => @course.uid, :price => price, :lessons => params[:lessons] || [], :end_of_base_period => params[:end_of_base_period], :hourly_price => params[:hourly_price].try(&:to_f) })
              if result
                current_profile.update_attributes!({
                                                       :paypal_preapproval_id => result[:preapproval_id],
                                                       :paypal_preapproval_confirmed_at => nil,
                                                       :paypal_email => params[:paypal_email]
                                                   })
                redirect_to result[:redirect_url]
              else
                flash.now[:alert] = "Could not setup PayPal payments. Payments preapproval could not be requested"
              end
            else
              br = BookingRequest.create!({
                                              :course_id => @course.id,
                                              :deposited_price => price,
                                              :hourly_price => params[:hourly_price].try(&:to_f),
                                              :lessons => params[:lessons] || [],
                                              :end_of_base_period => params[:end_of_base_period],
                                              :learner_id => current_profile.id,
                                              :source => 'paypal',
                                              :comments_by_learner => params[:comments],
                                              :place_to_come_by_learner => params[:place_to_come],
                                              :attendance => params[:attendance],
                                              :learner_username => params[:username],
                                              :learner_video_chat_platform => params[:video_chat_platform],
                                              :cancellation_commission => @course.cancellation_commission,
                                              :learnlist_partial_funding => params[:learnlist_partial].try(:to_i) == 1
                                          })
              Notification.add(@course.teacher.user, 'booking_request', br)
              flash[:notice] = "Booking successfully submitted"
              redirect_to course_path(@course.uid)
            end
          when 'braintree' then
            if params[:payment_method_nonce].blank?
              flash.now[:alert] = 'You did not configure your payment method. Please click Configure and set it up to proceed'
            else
              br = BookingRequest.create!({
                                              :course_id => @course.id,
                                              :deposited_price => price,
                                              :hourly_price => params[:hourly_price].try(&:to_f),
                                              :lessons => params[:lessons] || [],
                                              :end_of_base_period => params[:end_of_base_period],
                                              :learner_id => current_profile.id,
                                              :source => 'braintree',
                                              :comments_by_learner => params[:comments],
                                              :place_to_come_by_learner => params[:place_to_come],
                                              :attendance => params[:attendance],
                                              :learner_username => params[:username],
                                              :learner_video_chat_platform => params[:video_chat_platform],
                                              :braintree_payment_method_nonce => params[:payment_method_nonce],
                                              :cancellation_commission => @course.cancellation_commission,
                                              :learnlist_partial_funding => params[:learnlist_partial].try(:to_i) == 1
                                          })
              Notification.add(@course.teacher.user, 'booking_request', br)
              flash[:notice] = "Booking successfully submitted"
              redirect_to course_path(@course.uid)
            end
        end
      end
    else
      flash[:alert] = "You cannot access this view"
      redirect_to '/'
    end

Sorry for the long block of code, if there's a need for any more information, I'll be happy to refurnish.

Upvotes: 0

Views: 57

Answers (1)

Max Williams
Max Williams

Reputation: 32933

The standard way of dealing with this is to create a user account for the user, without any personal details, but keeping them "logged in" to this user account. This way they can have persistence across pages, can fill their basket, come back later on the same computer etc.

Later, when you actually need their personal details you can say "Before we go to the next step you need to register". Then, you can add the personal details to that account you created for them earlier, and do email verification or whatever you want to do.

With this approach, you will end up with lots of "incomplete" user accounts, where the person never bothered to register, and you could have a scheduled task to delete all the ones that are more than a week old, for example.

Upvotes: 1

Related Questions