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

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

【民泊5.1】【Windows】予約確認SMS

「app\controllers\reservations_controller.rb」ファイルを編集します。


1.記述追加 app\controllers\reservations_controller.rb(66行目)
「from」の部分にはTwilioで取得したご自分のアメリカの電話番号を入力してください。

    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



2.記述追加 app\controllers\reservations_controller.rb(95行目)

send_sms(room, reservation)



app\controllers\reservations_controller.rb

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)
          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
  
  



これで予約されると宿泊者(ゲスト)の電話番号に確認のショートメールが送られるようになりました。