user3100151 user3100151 - 5 months ago 19
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.

Story:
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

:payment_status
, 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

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

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.

Comments