2016-06-26 17 views
1

我正在構建一項功能,可在收到$ 1付款後,將三條彩信(通過Twilio)發送至用戶在表單中輸入的電話號碼。無法使Paypal和Rails應用程序正常工作。通知Paypal不起作用

故事: 1)用戶在表單中插入電話號碼和姓名。然後,點擊下訂單(通過PayPal) 2)用戶通過PayPal側的過程,然後返回到我的網站,我收到貝寶支付通知(問題從這裏開始)。 3)一旦我收到完成的:payment_status,我將三條彩信發送到電話號碼。

我的問題: 我花了幾個小時,我不知道如何解決這個問題。我希望我的send_text_message方法在我從PayPal收到付款通知後立即觸發。我嘗試了很多東西,但沒有任何工作,如果我的設置完全錯誤,我開始擔心。我需要一些幫助。我有一個控制器(家庭)和兩個模型(家庭和消息)。

# Table name: homes 
# 
# id     :integer   not null, primary key 
# phone    :string 
# sender_name   :string 
# created_at   :datetime   not null 
# updated_at   :datetime   not null 

# Table name: messages 
# 
# id   :integer   not null, primary key 
# body  :string 
# media_url :string 
# created_at :datetime   not null 
# updated_at :datetime   not null 
# 

MODEL - >消息

class Message < ActiveRecord::Base 
belongs_to :home 

end 

MODEL - >首頁

class Home < ActiveRecord::Base 
    require 'twilio-ruby' 
    has_many :messages 
    validates :phone, presence: true 
    validates :sender_name, presence: true 
def send_text_message 
    twilio_sid = ENV["TWILIO_ACCOUNT_SID"] 
    twilio_token = ENV["TWILIO_AUTH_TOKEN"] 
    twilio_phone_number = ENV["TWILIO_PHONE_NUMBER"] 
    sender_number = twilio_phone_number.sample 

    @twilio_client = Twilio::REST::Client.new(twilio_sid, twilio_token) 

    random_messages.each do |message| 
     twilio_credentials.account.messages.create(
     :from => sender_number, 
     :to => self.phone, 
     :body => message[:body], 
     :media_url => message[:media_url] 
     ) 
    end 
    } 

serialize :notification_params, Hash 

    def paypal_url(return_path) 
     values = { 
      business: "[email protected]", 
      cmd: "_xclick", 
      upload: 1, 
      return: "#{Rails.application.secrets.app_host}#{return_path}", 
      invoice: id + 1000, 
      amount: '1.89', 
      item_name: "Game of Thrones - Funny Prank", 
      item_number: '1', 
      quantity: '1', 
      notify_url: "#{Rails.application.secrets.app_host}/hook" 
     } 
     "#{Rails.application.secrets.paypal_host}/cgi-bin/webscr?" + values.to_query 
    end 

private 

def random_messages 
    Message.order("RANDOM()").limit(3) 
end 

CONTROLLER:首頁

class HomesController < ApplicationController 
    protect_from_forgery except: [:hook] 
    before_action :set_home, only: [:show, :destroy] 

    def show 
    end 

    def new 
    @home = Home.new 
    end 

    def create 
    @home = Home.new(home_params) 

    respond_to do |format| 
     if @home.save 
     format.html { redirect_to @home.paypal_url(home_path(@home)) } 
     #@home.send_text_message 
     #redirect_to @home, notice: 'Your Attack Has Officially Launched!' 
     #format.json { render :show, status: :created, location: @home } 
     else 
     format.html { render :new } 
     format.json { render json: @home.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def hook 
    params.permit! # Permit all Paypal input params 
    status = params[:payment_status] 
    if status == "Completed" 
     @home = Home.find params[:invoice] 
     @home.update_attributes notification_params: params, status: status, transaction_id: params[:txn_id], purchased_at: Time.now 
     @home.send_text_message 
    end 
     format.html { render :new } 
     format.json { render json: @home.errors, status: :unprocessable_entity } 
    end 

    private 
    def set_home 
     @home = Home.find(params[:id]) 
    end 

    def home_params 
     params.require(:home).permit(:phone, :sender_name, :plan, :notification_params, :status, :transaction_id ,:purchased_at) 
    end 
end 

------------ 路線

resources :homes 
post "/homes/:id" => "homes#show" 
post "/hook" => "homes#hook" 
root 'homes#new' 

編輯

與貝寶的作品付款。當用戶點擊地方訂單按鈕時​​,它將他重定向到貝寶並繼續付款。問題是send_text_message方法未定義,也不會發送。我還可以看到,如果您嘗試直接在電話上進行付款,可能是貝寶當前存在的問題是問題,那麼支付是在沙盒支付賬戶

+0

在控制檯中運行Rails.application.secrets.app_host'的價值是什麼? – oreoluwa

+0

這是我的ngrok網址 – user3100151

+0

您是否收到IPN通知? – oreoluwa

回答

2

我認爲這個問題是因爲Home記錄沒有找到。我說我認爲是因爲OP指定的問題是send_text_message未定義,而不是ActiveRecord::RecordNotFound

要理解爲什麼這可能是該錯誤,重要的是查看Home#paypal_url方法,因爲invoice密鑰設置爲id + 1000

現在,在HomeController#hook行動,@home被設置爲Home.find(params[:invoice]),這對於在DB的第一條記錄將意味着Home.find(1001)。我懷疑這是需要的行爲。

invoice鍵更改爲id應解決此問題。

+0

我改變了它,但現在我得到這個錯誤。 完成500內部服務器錯誤在163ms(ActiveRecord的:129.8ms) 的ActiveRecord :: UnknownAttributeError(未知屬性的 '狀態' 家用): 應用程序/控制器/ homes_controller.rb:49:在'掛鉤」 – user3100151

+0

呀,根據上面的家庭表定義,你沒有以下colums:'status,transaction_id,purchased_at',所以這些仍然會引發錯誤。您應該運行遷移以添加這些遷移。 – oreoluwa

+0

對不起,我可能不知道那是什麼錯誤。但是我會說你應該檢查'Home.first.status',以防遷移沒有運行! – oreoluwa

相關問題