简体   繁体   English

无法使Paypal和Rails应用程序正常工作。 通知Paypal无法正常运作

[英]Can't get Paypal and Rails app to work. Notification Paypal not working

I am building a functionality to send three MMS (via Twilio) to the phone number entered by the user in a form after receiving a payment of $1. 我正在构建一个功能,可以在收到1美元的付款后,将三个MMS(通过Twilio)发送到用户以表格形式输入的电话号码。

Story: 1) User insert phone number and name in form. 故事:1)用户在表格中输入电话号码和姓名。 Then, click on Place order (via paypal) 2) The user go through the process on paypal-side, then is returned to my site and I receive notification of payment from paypal (the problem starts here). 然后,单击“通过Paypal下订单”。2)用户在paypal端执行该过程,然后返回到我的网站,我收到了来自paypal的付款通知(问题从这里开始)。 3) Once I receive the completed :payment_status , I send the three MMS to the phone number. 3)一旦收到完整的:payment_status ,我:payment_status三个彩信发送到电话号码。

My question: I spent hours on this and I have no idea how to fix this. 我的问题:我花了几个小时,不知道如何解决这个问题。 I want my send_text_message method to fire as soon as I receive the payment notification from paypal. 我希望我的send_text_message方法在收到来自send_text_message宝的付款通知后立即启动。 I tried many things but nothing work and I start to worder if my setup is simply wrong. 我尝试了很多事情,但是没有任何效果,如果我的设置完全错误,我会开始写信。 I need some help please. 我需要一些帮助。

I have one controller (Home) and two models (Home and Message). 我有一个控制器(家庭)和两个模型(家庭和消息)。

# 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 -> Message 型号->留言

class Message < ActiveRecord::Base
belongs_to :home

end

MODEL -> Home 型号->主页

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: "merchant@sumbio.com",
          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: Home 控制器:主页

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

------------ Routes ------------ 路线

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

EDIT 编辑

The payment with paypal works. 使用Paypal付款。 When the user click on the place order button, it redirect him to paypal and proceed to payment. 当用户单击下订单按钮时​​,它会将他重定向到Paypal并继续付款。 The issue is that the send_text_message method is undefined and also do not get send. 问题是send_text_message方法未定义,也不会发送。 I can also see that the payment was made in the sandbox payment account 我还可以看到付款是在沙盒付款帐户中完成的

I think the problem is because the Home record is not found. 我认为问题是因为找不到Home记录。 I said I think because the OP specified that the issue is that the send_text_message is undefined not ActiveRecord::RecordNotFound . 我说我认为是因为OP指定问题是未定义send_text_message而不是ActiveRecord::RecordNotFound

To understand why this might be the bug, it's important to look at the Home#paypal_url method, as the invoice key is being set as id + 1000 . 要了解为什么这可能是错误,请务必查看Home#paypal_url方法,因为invoice密钥已设置为id + 1000

Now, in the HomeController#hook action, @home is being set to Home.find(params[:invoice]) , which for the first record in the db would mean Home.find(1001) . 现在,在HomeController#hook操作中,将@home设置为Home.find(params[:invoice]) ,对于db中的第一条记录,其含义是Home.find(1001) I doubt this is the required behaviour. 我怀疑这是必需的行为。

Changing the invoice key to id should fix this problem. invoice密钥更改为id应该可以解决此问题。

if you tried to make the payment directly on the phone, maybe the current problem from paypal, is the issue. 如果您尝试直接通过电话付款,则可能是Paypal当前的问题。

Look here: PayPal: Client proceed to payment, clicked pay and got back to success page, but was not charged 在此处查看: PayPal:客户继续付款,点击付款并返回成功页面,但未付款

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM