学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

Rails6.0 | 民泊サイトの構築(改良版)| 40 | 予約確認メール | for MacOSX



| 39 | クレジットカード決済  <<  [ホーム] >> | 41 | メッセージと会話


「app\mailers」フォルダに「reservation_mailer.rb」ファイルを新規作成してください。


app\mailers\reservation_mailer.rb(新規作成したファイル)

class ReservationMailer < ApplicationMailer
    def send_email_to_guest(guest, room, reservation)
      @recipient = guest
      @room = room
      @reservation = reservation
      mail(to: @recipient.email, subject: "ご予約ありがとうございます")
    end
end



「app\views」フォルダに「reservation_mailer」フォルダを新規作成してください。
作成した「reservation_mailer」フォルダに「send_email_to_guest.html.erb」ファイルを新規作成してください。



app\views\reservation_mailer\send_email_to_guest.html.erb(新規作成したファイル)

<%= @recipient.full_name %><br/>
<br/>
<br/>
お部屋名「"<%= @room.listing_name %>」のご予約、誠にありがとうございます。<br/>
ご予約が以下の内容で完了しましたのでご連絡いたします。<br/>
<br/>
<br/>
お部屋名:<%= @room.listing_name %><br/>
住所:<%= @room.address %><br/>
<br/>
<br/>
チェックイン日 :<%= I18n.l(@reservation.start_date, format: :full_date) %><br/>
チェックアウト日:<%= I18n.l(@reservation.end_date, format: :full_date) %><br/>
<br/>
<br/>
1泊の宿泊料(税込):<%= number_to_currency(@reservation.price) %><br/>
宿泊日数:<%= (@reservation.total/@reservation.price).to_i %><br/>
合計(税込):<%= number_to_currency(@reservation.total) %><br/>
<br/>
<br/>
よいご旅行になりますように<br/>



記述追加 app\controllers\reservations_controller.rb
77行目に以下の記述を追加します。

reservation.Waiting!
ReservationMailer.send_email_to_guest(reservation.user, room, reservation).deliver_later



記述追加「app\controllers\reservations_controller.rb」

class ReservationsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_reservation, only: [:approve]

  def create
    room = Room.find(params[:room_id])

    if current_user == room.user
      flash[:alert] = "オーナーが予約することはできません。"
    elsif current_user.stripe_id.blank?
      flash[:alert] = "予約する前にクレジットカードを登録する必要があります。"
      return redirect_to settings_payment_path
    else

        start_date = Date.parse(reservation_params[:start_date])
        end_date = Date.parse(reservation_params[:end_date])
        days = (end_date - start_date).to_i
        if days == 0
          flash[:alert] = "宿泊日数が1泊以上でなければ予約することはできません。"
        else

          @reservation = current_user.reservations.build(reservation_params)
          @reservation.room = room
          @reservation.price = room.price
          @reservation.total = room.price * days
          #@reservation.save

          charge(room, @reservation)
          flash[:notice] = "予約が完了しました。"
        end
      end
      redirect_to room
  end

  def your_trips
    @trips = current_user.reservations.order(created_at: :desc).page(params[:page]).per(5)
    
  end

  def your_reservations
    @rooms = current_user.rooms

    @reservations = Reservation.all

  end

  def approve
    @reservation.Approved!
    redirect_to your_reservations_path
  end

  private
    def reservation_params
      params.require(:reservation).permit(:start_date, :end_date)
    end
    
    def set_reservation
      @reservation = Reservation.find(params[:id])
    end

    def charge(room, reservation)
      host_amount = (reservation.total * 0.8).to_i # 売上の80%がホストに入る
      if !reservation.user.stripe_id.blank?
        customer = Stripe::Customer.retrieve(reservation.user.stripe_id)
        charge = Stripe::Charge.create(
          :customer => customer.id,
          :amount => reservation.total,
          :description => room.listing_name,
          :currency => "jpy",
          transfer_data: {
            amount: host_amount, 
            destination: room.user.merchant_id, # ホストのストライプID
          },
        )
  
        if charge
          reservation.Waiting!
          ReservationMailer.send_email_to_guest(reservation.user, room, reservation).deliver_later
          flash[:notice] = "お支払い手続きが完了し、ご予約されました。"
        else
          flash[:notice] = "お支払い手続きができません。予約ができませんでした。"
        end
      end
    rescue Stripe::CardError => e
      reservation.declined!
      flash[:alert] = e.message
    end


  end
  



これで予約完了時に宿泊者(ゲスト)へ電子メールが送信されるようになりました。

予約確認メール
予約確認メール



↓↓クリックして頂けると励みになります。


| 39 | クレジットカード決済  <<  [ホーム] >> | 41 | メッセージと会話