Skip to content

Commit

Permalink
modify booking
Browse files Browse the repository at this point in the history
  • Loading branch information
CH1006 committed Sep 11, 2019
1 parent 0364437 commit a1780d1
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 15 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class ApplicationController < ActionController::Base
protect_from_forgery
add_flash_types :success, :danger, :warning, :info
devise_group :user, contains: %i[admin member]
before_action :configure_permitted_parameters, if: :devise_controller?
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/bookings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ def valid_date?

def save_booking
@booking.status = 0
if @booking.voucher_id.present?
@booking.total_price = (@booking.room.price.cost - ((@booking.room.price.cost * @booking.voucher.sale) / 100)) + @booking.room.price.cleaning_fee
else
@booking.total_price = (@booking.room.price.cost + @booking.room.price.cleaning_fee)
end
if @booking.save
redirect_to thank_payment_path
else
Expand Down
48 changes: 45 additions & 3 deletions app/controllers/mailpayment_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
# frozen_string_literal: true

class MailpaymentController < ApplicationController
before_action :load_booking, only: :edit
before_action :check_token, only: :edit
before_action :check_expire_time, only: :edit

def create
@booking = Booking.includes(:room).find(params[:id])
PaymentMailer.payment_booking(@booking).deliver_now
flash[:notice] = t "messages.notice.mailpayment.sent_mail", email: @booking.email_booking.to_s
@booking = Booking.includes(:room).find_by(id: params[:id])
if @booking
@booking.create_booking_digest
PaymentMailer.payment_booking(@booking).deliver_now
flash[:notice] = t "messages.notice.mailpayment.sent_mail", email: @booking.email_booking.to_s
else
flash[:danger] = t "messages.failed.mailpayment.sent_mail"
end
redirect_to manager_bookings_path
end

def edit
if @booking.Pending?
redirect_to @booking.paypal_url(payment_booking_path(@booking))
else
flash[:danger] = t "messages.failed.mailpayment.paymented"
redirect_to root_path
end
end

private

def check_token
return if @booking.booking_digest == params[:id]

flash[:danger] = t "messages.failed.mailpayment.token"
redirect_to root_path
end

def load_booking
return if @booking = Booking.includes(:room).find_by(id: params[:booking])

flash[:danger] = t "messages.failed.mailpayment.sent_mail"
redirect_to root_path
end

def check_expire_time
return unless @booking.booking_expired?

@booking.destroy
flash[:danger] = t "messages.failed.mailpayment.expire"
redirect_to root_path
end
end
1 change: 0 additions & 1 deletion app/controllers/manager/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Manager
class BaseController < ApplicationController
layout "manager"
protect_from_forgery with: :exception
before_action :authenticate_admin!
end
end
2 changes: 1 addition & 1 deletion app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def index

def show
@items = @location + @area + @room
render json: @items
render json: @items.sort
end

private
Expand Down
33 changes: 31 additions & 2 deletions app/models/booking.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Booking < ApplicationRecord
before_save :downcase_email, :add_total_price

belongs_to :room
belongs_to :voucher, optional: true

Expand All @@ -18,7 +20,7 @@ def country_name
country.present? ? country.translations[I18n.locale.to_s] || country.name : country_code
end

def paypal_url(_return_path)
def paypal_url(return_path)
values = {
business: Settings.email.to_s,
cmd: "_xclick",
Expand All @@ -33,11 +35,38 @@ def paypal_url(_return_path)
"#{Settings.paypal_host}/cgi-bin/webscr?" + values.to_query
end

private
# Returns a random token.
def self.new_token
SecureRandom.urlsafe_base64
end

def create_booking_digest
update_attributes(booking_digest: Booking.new_token,
booking_sent_at: Time.zone.now)
end

def booking_expired?
booking_sent_at < 1.hours.ago
end

def checkin_date_after_checkout_date
return if checkin.blank? || checkout.blank?

errors.add(:checkout, "must be after the start date") if checkout < checkin
end

private

def add_total_price
total_price = (self.room.price.cost * ((self.checkout - self.checkin) / 86400))
if self.voucher_id.present?
self.total_price = ( total_price - ((total_price * self.voucher.sale) / 100)) + self.room.price.cleaning_fee
else
self.total_price = total_price + self.room.price.cleaning_fee
end
end

def downcase_email
self.email_booking = email_booking.downcase
end
end
6 changes: 6 additions & 0 deletions app/views/payment_mailer/invoice_payment.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ table.body border="0" cellpadding="0" cellspacing="0" role="presentation"
= t ".sale"
.col-7
= number_to_percentage(@booking.voucher.sale, precision: 0)
.row
.col-2
.col-3 style="font-weight: bold;"
= t ".clean"
.col-7
= number_to_currency(@room.price.cleaning_fee)
.row
.col-2
.col-3 style="font-weight: bold;"
Expand Down
8 changes: 7 additions & 1 deletion app/views/payment_mailer/payment_booking.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ table.body border="0" cellpadding="0" cellspacing="0" role="presentation"
= t ".sale"
.col-7
= number_to_percentage(@booking.voucher.sale, precision: 0)
.row
.col-2
.col-3 style="font-weight: bold;"
= t ".clean"
.col-7
= number_to_currency(@room.price.cleaning_fee)
.row
.col-2
.col-3 style="font-weight: bold;"
Expand Down Expand Up @@ -75,7 +81,7 @@ table.body border="0" cellpadding="0" cellspacing="0" role="presentation"
p = t ".end_mail"
/! END MAIN CONTENT AREA
= link_to t(".payment_now"),
@booking.paypal_url(payment_booking_url(@booking)),
check_payment_url(id: @booking.booking_digest, booking: @booking.id),
class: "btn btn-primary", style: "text-decoration: none;"
/! END CENTERED WHITE CONTAINER
/! START FOOTER
Expand Down
2 changes: 1 addition & 1 deletion app/views/trends/_trend.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
h2.extra-bold.p--giant= link_to trend.name, trend_path(trend), class: "trend-title"
p = trend.description
.row
= render trend.rooms.limit Settings.trend_limit
= render trend.rooms.includes(:trend_rooms, :room_images, :location, :price, :likes, :favorite_space).limit Settings.trend_limit
8 changes: 8 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ en:
failed:
admins:
create: "Create failed!"
mailpayment:
sent_mail: "Not find booking!"
paymented: "Booking paymented!"
token: "Token not true!"
expire: "Payment expired"
notice:
mailpayment:
sent_mail: "Sent a mail to %{email}"

manager:
rooms:
create_room: "Thank you to create room"
Expand Down Expand Up @@ -167,6 +173,7 @@ en:
room_name: "Room Name:"
cost: "Cost:"
sale: "Sale:"
clean: "Clean fee:"
total_price: "Total price:"
checkin: "Check in:"
checkout: "Check out:"
Expand All @@ -184,6 +191,7 @@ en:
room_name: "Room name:"
price: "Price:"
sale: "Sale:"
clean: "Clean fee:"
total_price: "Total price:"
checkin: "Check in:"
checkout: "Check out:"
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
get "/favorite_spaces/:id", to: "home#show"
get "/autocomplete", to: "search#show"
post "/mailpayment/:id", to: "mailpayment#create", as: :mailpayment
get "/paypal/:id", to: "mailpayment#edit", as: :check_payment
post "/payment_update", to: "payment_booking#create", as: :payment_bookings
get "/payment_booking/:id", to: "payment_booking#show", as: :payment_booking
get "/payment_booking", to: "payment_booking#index", as: :thank_payment
Expand Down
2 changes: 1 addition & 1 deletion config/settings/development.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
paypal_host: https://www.sandbox.paypal.com
app_host: http://514e8bcd.ngrok.io
app_host: http://3fa1c2d5.ngrok.io
email: bookinghomestay@merchant.com
2 changes: 2 additions & 0 deletions db/migrate/20190903025307_create_bookings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def change
t.text :request
t.time :intend_time
t.decimal :total_price
t.string :booking_digest
t.datetime :booking_sent_at

t.timestamps
end
Expand Down
2 changes: 2 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
t.text "request"
t.time "intend_time"
t.decimal "total_price", precision: 10
t.string "booking_digest"
t.datetime "booking_sent_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "notification_params"
Expand Down

0 comments on commit a1780d1

Please sign in to comment.