我正在構建一項功能,可在收到$ 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方法未定義,也不會發送。我還可以看到,如果您嘗試直接在電話上進行付款,可能是貝寶當前存在的問題是問題,那麼支付是在沙盒支付賬戶
在控制檯中運行Rails.application.secrets.app_host'的價值是什麼? – oreoluwa
這是我的ngrok網址 – user3100151
您是否收到IPN通知? – oreoluwa