Phone verification using Twilio in Rails 3 - ruby-on-rails

I have written code for Phone verification code but it's not working.
Main problem is, that user can put any number for verification. That means verification_number don't work.
def phone_verification
u = User.find(params[:user_id])
if params[:verification_code]
if u.verification_number.to_s == params[:verification_code].to_s
u.phone_verified = true
u.save
respond_to do |format|
format.js { render :status => :ok }
end
end
else
u.update_attributes(phone: params[:phone], country_code_id: params[:prefix], verification_number: rand(10000))
u.save
# Phone verification
#full_phone = "#{CountryCode.find(params[:prefix]).calling_code}#{params[:phone]}"
number_to_send_to = Rails.env.production? || Rails.env.staging? ? #full_phone : TEST_MOBILE
Rails.logger.debug "-- Verification code: #{u.verification_number}"
#twilio_client = Twilio::REST::Client.new(Mekomy::Application.config.twilio_account_sid, Mekomy::Application.config.twilio_auth_token)
begin
#twilio_client.account.sms.messages.create(
:from => Mekomy::Application.config.twilio_phone_number,
:to => number_to_send_to,
:body => "Please add this verification code when prompt: #{u.verification_number}"
)
respond_to do |format|
format.js { render :status => :ok }
end
rescue
Rails.logger.debug "-- Some error"
end
end
end
Can someone help me to find, where I was wrong?
Thank you all!

Related

Updating Braintree Default Payment

In the seemingly never-ending quest to understand Braintree's obfuscated documentation, I ended up creating my own update UI with a list of BTPaymentMethod
The Braintree docs say to update a payment method with a token (which I see in my online sandbox vault). How do I retrieve an indivdual customers card token to update?
I'm using -[BTClient fetchPaymentMethodsWithSuccess:failure:], which returns BTPaymentMethodwhich only has thepayment_nonce property. Trying to update with this property return an error CVV is required.
def update_payment_default
#customer = Customer.where(id: params[:id]).first
if #customer.present?
#string = "customer_" + #customer.id.to_s
result = Braintree::Customer.update(
"my_customer_id"
:credit_card => {
:payment_method_nonce => params[:payment_method_nonce],
:options => {
:make_default => true
}
}
)
if result.success?
respond_to do |format|
msg = {:status => "SUCCESS", :message => "Updated payment payment method"}
format.json { render :json => msg } # don't do msg.to_json
end
else
respond_to do |format|
msg = {:status => "FAILED", :message => result.errors}
format.json { render :json => msg } # don't do msg.to_json
end
end
end

Can't implement before_save filter (when checking data that doesn't go to db)

I need to verify User data before_save. I'm saving only paypal_email, and don't save first and last name.
I added before_save filter in my model:
attr_accessible :paypal_email, :first_name, :last_name
attr_accessor :first_name
attr_accessor :last_name
before_save :verify
and added veify method:
protected
def verify
require 'httpclient'
require 'xmlsimple'
clnt = HTTPClient.new
header = {"X-PAYPAL-SECURITY-USERID" => "1111111111",
"X-PAYPAL-SECURITY-PASSWORD" => "111111",
"X-PAYPAL-SECURITY-SIGNATURE" => "11111",
"X-PAYPAL-REQUEST-DATA-FORMAT" => "NV",
"X-PAYPAL-RESPONSE-DATA-FORMAT" => "XML",
"X-PAYPAL-APPLICATION-ID" => "APP-2J632856DC989803F"
}
logger.info(#user.first_name)
data = {"emailAddress" => self.paypal_email,
"firstName"=> self.first_name,
"lastName" => self.last_name,
"matchCriteria" => "NAME",
"requestEnvelope.errorLanguage" => "en_US"}
uri = "https://svcs.paypal.com/AdaptiveAccounts/GetVerifiedStatus"
res = clnt.post(uri, data, header)
#xml = XmlSimple.xml_in(res.content)
if res.status == 200
if #xml['accountStatus']!=nil
account_status = #xml['accountStatus'][0]
if account_status == "VERIFIED"
redirect_to :back
flash[:success] = "Your account is verified"
else
redirect_to :back
flash[:error] = res.content
end
else
redirect_to :back
flash[:error] = res.content
end
else
flash[:error] = "Oops! Can't conntect to PayPal"
end
end
EDIT
def create
#user = User.new(params[:user])
if #user.valid?
#user.save
flash[:notice] = "success"
else
render :new
flash[:error] = "error"
end
what give me error:
undefined method `first_name' for nil:NilClass
Where is my error ?
Since you're in your model, replace #user.first_name with self.first_name or even first_name
Other issues
third party service calls should live in background jobs.
point flash is unknown in models, it belongs to controller, as well as redirect.
redirect_to :back, is not that a good practice: some browsers don't send the referrer

Rails 3: How to "redirect_to" in Ajax call?

The following attempt_login method is called using Ajax after a login form is submitted.
class AccessController < ApplicationController
[...]
def attempt_login
authorized_user = User.authenticate(params[:username], params[:password])
if authorized_user
session[:user_id] = authorized_user.id
session[:username] = authorized_user.username
flash[:notice] = "Hello #{authorized_user.name}."
redirect_to(:controller => 'jobs', :action => 'index')
else
[...]
end
end
end
The problem is that redirect_to doesn't work.
How would you solve this ?
Finally, I just replaced
redirect_to(:controller => 'jobs', :action => 'index')
with this:
render :js => "window.location = '/jobs/index'"
and it works fine!
There is very easy way to keep the flash for the next request. In your controller do something like
flash[:notice] = 'Your work was awesome! A unicorn is born!'
flash.keep(:notice)
render js: "window.location = '#{root_path}'"
The flash.keep will make sure the flash is kept for the next request.
So when the root_path is rendered, it will show the given flash message. Rails is awesome :)
I think this is slightly nicer:
render js: "window.location.pathname='#{jobs_path}'"
In one of my apps, i use JSON to carry on the redirect and flash message data. It would look something like this:
class AccessController < ApplicationController
...
def attempt_login
...
if authorized_user
if request.xhr?
render :json => {
:location => url_for(:controller => 'jobs', :action => 'index'),
:flash => {:notice => "Hello #{authorized_user.name}."}
}
else
redirect_to(:controller => 'jobs', :action => 'index')
end
else
# Render login screen with 422 error code
render :login, :status => :unprocessable_entity
end
end
end
And simple jQuery example would be:
$.ajax({
...
type: 'json',
success: functon(data) {
data = $.parseJSON(data);
if (data.location) {
window.location.href = data.location;
}
if (data.flash && data.flash.notice) {
// Maybe display flash message, etc.
}
},
error: function() {
// If login fails, sending 422 error code sends you here.
}
})
Combining the best of all answers:
...
if request.xhr?
flash[:notice] = "Hello #{authorized_user.name}."
flash.keep(:notice) # Keep flash notice around for the redirect.
render :js => "window.location = #{jobs_path.to_json}"
else
...
def redirect_to(options = {}, response_status = {})
super(options, response_status)
if request.xhr?
# empty to prevent render duplication exception
self.status = nil
self.response_body = nil
path = location
self.location = nil
render :js => "window.location = #{path.to_json}"
end
end
I didn't want to modify my controller actions so I came up with this hack:
class ApplicationController < ActionController::Base
def redirect_to options = {}, response_status = {}
super
if request.xhr?
self.status = 200
self.response_body = "<html><body><script>window.location.replace('#{location}')</script></body></html>"
end
end
end

How do I update all other records when one is marked as default?

I am trying to change all of the default_standing fields to FALSE for all other records when someone marks one as TRUE. That way I will only ever have one default record in the table. Here is what I am doing in both create and update in my controller, but it doesn't seem to be working:
def update
#standing = Standing.find(params[:id])
if #standing.default_standing
#standings = Standing.where(["default_standing = ? AND id != ?", true, params[:id]])
#standings.each do |s|
s.default_standing = false
s.save!
end
end
respond_to do |format|
if #standing.update_attributes(params[:standing])
format.html { redirect_to(#standing, :notice => 'Standing was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #standing.errors, :status => :unprocessable_entity }
end
end
end
I think the condition is wrong in shingara's update_all.
Should update all where id is not standing.id:
class Standing
def self.all_false_instead_of(standing)
return if standing.default_standing
Standing.update_all(["default_standing = ?", false], ['id <> ?', standing.id])
standing.update_attributes!(:default_standing, true)
end
end
class Standing
def self.all_false_instead_of(standing)
return if standing.default_standing
Standing.update_all("default_standing = false", {:id => standing.id})
standing.update_attributes!(:default_standing, true)
end
end
It's better in Model and something like that I suppose. Have you the unit test failing ?
In your controller
def update
#standing = Standing.find(params[:id])
Standing.all_false_instead_of(#standing)
end
In your code you never push default_standing to true in you #standing

Attachment_fu failing to validate on update

I have been using attachment_fu on a project for a long time and all was fine but now as I am trying to bring the project up to rails 2.3.3 I am running into a strange bug that is driving me nuts. The Attachment, a logo in this case, validates correctly on create but does not fail validation on update. I have debugged it and it fails the intial validate but does not seem to throw an exception or at least not one that is caught by my rescue in the controller. Seems like I have tried everything but can't figure this one out.
Controller:
# POST /tournaments
# POST /tournaments.xml
def create
# Build tournament
#tournament = Tournament.new(params[:tournament].merge(:user_id => current_user.id) )
# Save the uploaded attachments
params[:uploads].each do |upload|
#tournament.documents << Document.new({:uploaded_data => upload[:document]}.merge(:description => upload[:description]))
end unless params[:uploads].nil?
# if supplied save an event logo
#logo = Logo.new({:uploaded_data => params[:logo][:upload_data]}) unless params[:logo].nil? or params[:logo][:upload_data].blank?
#tournament.logo = #logo unless #logo.nil?
respond_to do |format|
begin
Tournament.transaction do
#tournament.logo.save! unless #tournament.logo.nil?
#tournament.save!
end
flash[:notice] = 'Tournament was successfully created.'
format.html { redirect_to tournament_url(#tournament) }
format.xml { head :created, :location => tournament_url(#tournament) }
rescue
flash[:notice] = 'Errors prevented your Tournament from being saved'
format.html { render :action => "new" }
format.xml { render :xml => #tournament.errors, :status => :unprocessable_entity }
end
end
end
# PUT /tournaments/1
# PUT /tournaments/1.xml
def update
#tournament = Tournament.find(params[:id])
#tournament.user_id = session[:orig_user_id]
respond_to do |format|
begin
Tournament.transaction do
# Update Logo if necessary
unless params[:logo][:upload_data].blank?
#tournament.logo.destroy unless #tournament.logo.nil?
#tournament.logo = Logo.new({:uploaded_data => params[:logo][:upload_data]}.merge(:user_id => current_user.id))
end
# Save any uploaded documents
params[:uploads].each do |upload|
#tournament.documents << Document.new({:uploaded_data => upload[:document]}.merge(:description => upload[:description]))
end unless params[:uploads].nil?
# Update Tournamnet Attributes
#tournament.attributes = params[:tournament]
# Save the Tournament
#tournament.save!
end
flash[:notice] = 'Tournament was successfully updated.'
format.html { redirect_to tournament_url(#tournament) }
format.xml { head :ok, :location => tournament_url(#tournament) }
rescue
flash[:notice] = 'Errors prevented your Tournament from being updated'
format.html { render :action => "edit" }
format.xml { render :xml => #tournament.errors, :status => :unprocessable_entity }
end
end
end
Logo Model:
class Logo < Asset
validate_on_create :attachment_valid?
has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 4.megabytes,
:resize_to => '810x150>',
:processor => :ImageScience,
:thumbnails => { :thumb => '270x50>' }
def attachment_valid?
content_type = attachment_options[:content_type]
unless content_type.nil? || content_type.include?(self.content_type)
errors.add(:upload_data, " * must be an image file (jpg, gif, or png)")
end
size = attachment_options[:size]
unless size.nil? || size.include?(self.size)
errors.add(:upload_data, "* image must be 4MB or less")
end
end
before_thumbnail_saved do |thumbnail|
record = thumbnail.parent
thumbnail.user_id = record.user_id
thumbnail.listing_id = record.listing_id
end
end
I am running the following
Rails 2.3.3
image_science 1.2.0
Thanks
--Tim
You could also use a :before_save callback to test the object. If it is invalid, raise an exception.
try adding:
validate_on_update :attachment_valid?

Resources