Lukkor

BoughtDetailsController

Apr 11th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.01 KB | None | 0 0
  1. class Backend::BoughtDetailsController < BackendController
  2.     before_action :set_entry_type, only: [:index, :new, :create]
  3.  
  4.     # GET backend/bought_details
  5.     # GET backend/bought_details.json
  6.     def index
  7.         @bought_details = BoughtDetail.all
  8.         @last_bought = BoughtDetail.order('bought_data').last
  9.     end
  10.  
  11.     # GET backend/bought_details/new
  12.     def new
  13.         @bought_detail = BoughtDetail.new
  14.     end
  15.  
  16.     def create
  17.         @bought_detail = BoughtDetail.new(bought_detail_params)
  18.         @bought_detail.entry_type = @entry_type
  19.         @bought_detail.person = current_person
  20.         if @bought_detail.entry_type.kind == 'Karnet'
  21.             @bought_detail.cost = @bought_detail.entry_type.price * (:days / 30).floor
  22.         else
  23.             @bought_detail.cost = @bought_detail.entry_type.price
  24.         end
  25.         Stripe.api_key = ENV['STRIPE_API_KEY']
  26.         token = params[:stripeToken]
  27.  
  28.         begin
  29.             # customer = Stripe::Customer.create email: @bought_detail.person.email,
  30.             #                                    card: token
  31.             Stripe::Charge.create(
  32.                 # :customer => customer.id,
  33.                 amount: (@bought_detail.entry_type.price * 100).floor,
  34.                 currency: 'PLN',
  35.                 source: params[:stripeToken]
  36.             )
  37.             flash[:notice] = "Dziękujemy za zakup!"
  38.             @bought_detail.save
  39.             redirect_to backend_entry_type_bought_details_path(@entry_type), notice: flash[:notice]
  40.         rescue Stripe::CardError => e
  41.             flash[:danger] = e.message
  42.             render :new
  43.         end
  44.  
  45.     end
  46.  
  47.     private
  48.  
  49.     def set_entry_type
  50.         @entry_type = EntryType.find(params[:entry_type_id])
  51.     end
  52.  
  53.     # Never trust parameters from the scary internet, only allow the white list through.
  54.     def bought_detail_params
  55.         params.require(:bought_detail).permit(:bought_data, :start_on, :end_on, :entry_type_id, :person_id, :days, :credit_card, :card_code)
  56.     end
  57. end
Advertisement
Add Comment
Please, Sign In to add comment