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

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

【民泊5.1】【Windows】予約確認電子メール

「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.fullname %><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
96行目に「ReservationMailer.send_email_to_guest(reservation.user, room, reservation).deliver_later」の記述追加

class ReservationsController < ApplicationController
    before_action :authenticate_user!
    before_action :set_reservation, only: [:approve, :decline]
  
    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 payment_method_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
  
          if @reservation.Waiting!
            if room.Request?
              flash[:notice] = "予約承認申請を送信しました。予約が承認されるまでしばらくお待ち下さい。"
            else
              charge(room, @reservation)
            end
          else
            flash[:alert] = "ご予約できません!"
          end
        end
      end
      redirect_to room
    end

    # 宿泊者用予約確認
    def your_trips
      @trips = current_user.reservations.order(start_date: :asc)
    end

    #ホスト用予約確認
    def your_reservations
      @rooms = current_user.rooms
    end

    def approve
      charge(@reservation.room, @reservation)
      redirect_to your_reservations_path
    end
  
    def decline
      @reservation.Declined!
      redirect_to your_reservations_path
    end
    
    private

    def send_sms(room, reservation)
      @client = Twilio::REST::Client.new
      @client.messages.create(
        from: '+12056565281',
        to: room.user.phone_number,
        body: "#{reservation.user.fullname}様、#{room.listing_name}を予約しました。"
      )
    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.Approved!
          send_sms(room, reservation)
          ReservationMailer.send_email_to_guest(reservation.user, room, reservation).deliver_later
          flash[:notice] = "お支払い手続きが完了し、ご予約されました。お越しをお待ちしております!"
        else
          reservation.Declined!
          flash[:notice] = "お支払い手続きができません。予約ができませんでした。"
        end
      end
    rescue Stripe::CardError => e
      reservation.declined!
      flash[:alert] = e.message
    end

    def set_reservation
      @reservation = Reservation.find(params[:id])
    end

    def reservation_params
      params.require(:reservation).permit(:start_date, :end_date)
    end

  end
  



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