user3100151 user3100151 - 1 year ago 74
Ruby Question

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) User insert phone number and name in form. 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).
3) Once I receive the completed

, I send the three MMS to the phone number.

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


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

random_messages.each do |message|
:from => sender_number,
:to =>,
:body => message[:body],
:media_url => message[:media_url]

serialize :notification_params, Hash

def paypal_url(return_path)
values = {
business: "",
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


def random_messages


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

def show

def new
@home =

def create
@home =

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

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:
format.html { render :new }
format.json { render json: @home.errors, status: :unprocessable_entity }

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

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


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


The payment with paypal works. When the user click on the place order button, it redirect him to paypal and proceed to payment. The issue is that the send_text_message method is undefined and also do not get send. I can also see that the payment was made in the sandbox payment account

Answer Source

I think the problem is because the Home record is not found. I said I think because the OP specified that the issue is that the send_text_message is undefined not 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.

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). I doubt this is the required behaviour.

Changing the invoice key to id should fix this problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download