Paypal customer checkout with rails adaptive payment - ruby-on-rails

Has any successfully set the header image in the displayOptions.headerImageUrl with adaptive payments in Ruby? I'm banging my head against the wall trying to figure this out.
From my understanding, I have one action set_pay_chained that gets the paykey and calls the pay function, but need to create an entirely different function to just tell PayPal the image url. Is this correct? Does anyone have an example of how to do this in Ruby/Rails?
Currently my code basically looks like this.
def set_pay_chained
#host=request.host.to_s
#port=request.port.to_s
#cancelURL= params[:returnurl_paypal]
##returnURL="http://#{#host}:#{#port}/websamples/ap/setpaychained/pay_details"
#returnURL= params[:returnurl_paypal]
##ep["SERVICE"]="/AdaptivePayments/Pay"
#caller = PayPalSDKCallers::Caller.new(false)
req={
"requestEnvelope.errorLanguage" => "en_US",
"clientDetails.ipAddress"=>##clientDetails["ipAddress"],
"clientDetails.deviceId" =>##clientDetails["deviceId"],
"clientDetails.applicationId" => ##clientDetails["applicationId"],
"feesPayer"=> "PRIMARYRECEIVER",
"receiverList.receiver[0].email"=>params[:receiveremail_0],
"receiverList.receiver[1].email"=> params[:receiveremail_1],
"receiverList.receiver[0].amount"=>params[:amount_0],
"receiverList.receiver[1].amount"=>params[:amount_1],
"receiverList.receiver[0].primary[0]"=> "true",
"receiverList.receiver[1].primary[1]"=> "false",
"currencyCode"=> "USD",
"actionType"=>"PAY",
"returnUrl" => #returnURL,
"cancelUrl"=>"#{#cancelURL}&paykey=#{#paykey}",
}
#transaction = #caller.call(req)
if (#transaction.success?)
session[:setpaychained_response]=#transaction.response
#response = session[:setpaychained_response]
#paykey = #response["payKey"][0]
#paymentExecStatus=#response["paymentExecStatus"]
if (#paymentExecStatus.to_s=="COMPLETED")
redirect_to :controller => 'setpaychained',:action => 'pay_details'
else
redirect_to "https://www.sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=#{#paykey}"
end
else
session[:paypal_error]=#transaction.response
redirect_to :controller => 'calls', :action => 'error'
end
rescue Errno::ENOENT => exception
flash[:error] = exception
redirect_to :controller => 'calls', :action => 'exception'
end
== Update ==
On Paypal's developer forms, I've been given a variety of advice. I've moved the discussion here to see if I could get a more "rails" way of handling this. So far, SweatCoder has provided what I think is the closest solution I'm looking for. Unfortunately, the code is in PHP (which I have only a very, very, basic understanding) so I'm now trying to translate this code (https://www.x.com/message/211714#211714) into ruby. My understanding is that I need to make two separate calls. I can't tell if I'm supposed to make these calls simultaneously and if not, which call should come first.

Related

Ruby on Rails redirect_to missing parameter

I've been doing some the last part of my assingment in Ruby on Rails and I'm stuck on redirect_to for an hour.
I want to achieve this kind of structure on the URI.
http://localhost:3000/movies?ratings[G]&ratings[R]
My current code on this:
redirect_to :controller => 'movies', :ratings => session[:ratings_uri]
#session[:ratings_uri] = "ratings[G]&ratings[R]"
And this is what I got after the redirection.
http://localhost:3000/movies?ratings=%26ratings[G]%26ratings[R]
My question is, Is there any way to make my desired URL? Do I need to delete some paramaters on my code.

dynamic email subject and body from database

In my admin site, I want editable
Dynamic subject in database text.
mailer = MailTemplate.find_by_template_name('friend_request')
# mailer.subject = "#{#user.username} wants to be friend on site.com"
I have to use code like :
mail(:to => friend.email, :subject => mailer.subject) do |format|
format.text { render :inline => nl2br(mailer.body) }
format.html { render :inline => nl2br(mailer.body) }
end
Now, I found body is working properly with dynamic code. But SUBJECT is not working with
mailer.subject for value like "#{#user.username} wants to be friend on SportsPundit.com"
Please suggest. I tried with eval(mailer.subject) as well.
Consider using a different templating language so you're not exposing ruby to the user. Handlebars may be a good choice.
In your MailTemplate object, subject would be
"{{ user/username }} wants to be friend on site.com"
Then you would render it using handlebars-rails or some other lib, passing #user as user in the handlebars template. This is better for the average user to figure out, so they aren't looking at ruby.

How do you call a model's show url from a Rail create controller?

I'm having trouble calling a model's show path from within a create controller.
I'm using the Koala gem in a Rails 3.2 app. I'm trying to publish to Facebook's open graph automatically when a User creates a particular record type.
I have a page set up with all the required FB meta tags.
I can run the Koala methods from the console and everything works fine.
But if I try to run this from the controller I get an error.
My controller looks like this:
def create
#fb_model = current_user.fb_models.build(params[:fb_model])
if #fb_model.save
Koala::Facebook::API.new(app_token).put_connections( current_user.fb_uid, "namespace:action", :object => fb_model_url(#fb_model) )
respond_to do |format|
format.html { redirect_to(#fb_model, :notice => 'Successfully created.') }
format.xml { render :xml => #fb_model, :status => :created, :location => #fb_model }
end
else
respond_to do |format|
format.html { render :action => "new" }
format.xml { render :xml => #fb_model.errors, :status => :unprocessable_entity }
end
end
end
When I create a record, my logs show:
Koala::Facebook::APIError (HTTP 500: Response body: {"error":{"type":"Exception","message":"Could not retrieve data from URL."}}):
If I edit the controller to use a static url for testing, everything works fine.
...
if #fb_model.save
Koala::Facebook::API.new(app_token).put_connections( current_user.fb_uid, "namespace:action", :object => "http://myapp.com/fb_model/2" )
...
Why can't I pass the record's url to FB from within the create controller using fb_model_url(#fb_model)?
I eventually got to the bottom of this. It's actually a really frustrating issue, as there is no indication that this is the problem in any logs or elsewhere.
The problem was that I was deploying/ testing on Heroku, and only had 1 web dyno running. My app was unable to handle both the Facebook request and the post/ get simultaneously, causing the error.
This has been addressed in another question Facebook Open Graph from Rails Heroku. It's really not what I was expecting, and I didn't come across this question in any of my earlier searching. Hopefully this can help someone else.
I solved the issue by switching from thin to unicorn.
build is finalised when the parent model is saved, and you dont seem to be operating on a parent.
I think you actually want this:
#fb_model = current_user.fb_models.new(params[:fb_model])
Also you seem to be calling #fb_model.save twice which is wrong.
Thanks for posting your findings - I've been dealing with this issue for the past couple of days and wouldnt have figured that. So when you simply increased your dyno load, you no longer had this error? I was on the verge of just using the Javascript SDK even though my 'put_connections' callbacks work in the heroku console.

Basic Ruby on Rails AJAX Error

I'm working through Agile Web Development with Rails, Edition 4 with some tweaks (mostly just naming variations), and I've arrived at Iteration F2. In this iteration, you modify the index button with :remote => true, you add format.js to the respond_to section of the controller, and you generate a js.rjs file to execute the AJAX render. Or at least that's my interpretation of it. The goal of these steps is to have a cart (in this case, a team) in the sidebar update using AJAX when adding new line items (in this case, members)
In my case, I'm trying to add members to a team. Her's some code snippets I've added:
index.html.erb:
<%= button_to 'Add to Team', members_path(:player_id => player),
:remote => true %>
members_controller:
def create
#team = current_team
player = Player.find(params[:player_id])
#member = #team.add_player(player.id)
respond_to do |format|
if #member.save
format.html { redirect_to(nba_url) }
format.js
format.xml { render :xml => #member,
:status => :created, :location => #member }
else
format.html { render :action => "new" }
format.xml { render :xml => #member.errors,
:status => :unprocessable_entity }
end
end
end
create.js.rjs:
page.replace_html('team', render(#team))
The page is able to render, and I'm still able to click the button to add members to the team. However, the AJAX isn't working. When I reload, I can still see that the members have been added in the sidebar. All of the other team functionality remains, as I'm able to empty the team and add whichever members I wish. When I check the server log, I find the following error:
Error:
ActionView::Template::Error (undefined local variable or method `page' for #<
#<Class:0x413e1b8>:0x413cb20>):
1: page.replace_html('team', render(#team))
app/views/members/create.js.rjs:1:in `block in _app_views_members_create_js_rj
s___908569197_34199712_807066544'
app/views/members/create.js.rjs:1:in `_app_views_members_create_js_rjs___90856
9197_34199712_807066544'
app/controllers/members_controller.rb:47:in `create'
Based on this it seems like it has found the create.js.rjs but is having trouble interpreting it. I'm not sure what the weird symbols are in front of page.
Edit: I've also found that if I view the source code before and after clicking the button, the button is indeed refreshing the code and adding the desired items. The problem seems to be exclusively in trying to refresh the partial.
Any help is appreciated. Thanks!
It seems your rjs file has some invalid bits at the start. Maybe try to re-create the file?
What did you expect to do with render(#team) ?
I've taken a look at the action view's method "render" and didn't found how you were expecting it to function. Maybe there is another functionality that you are aware and I don't.
You can also use erb and not rjs, using it just like a view
Along the lines of Bert Goethal's answer, is your editor saving your text file as UTF-8 with BOM?
A BOM will add two unicode encoded characters to the beginning of the file, and that might be where those are coming from...

Rhomobile rhodes Rho AsyncHttp post

I am having problems with Rhomobile rhodes, plaese can someone tell me how to make http post, get, put, and delete using Rho::AsyncHttp?
I've been trying it to no success for hours.
Here's some sample code to place in your controller.rb file
Here's the initial call
def index
Rho::AsyncHttp.get(
:url => 'http://the.page.you.want.to.get',
:callback => (url_for :action => :httpget_callback),
:callback_param => "" )
render :action => :wait
end
the code above will initiate the httpget_callback method (below)
while that goes off and loads the url it'll change the screen and load the wait.erb file
def httpget_callback
if #params['status'] != 'ok'
##error_params = #params
WebView.navigate(url_for :action => :show_error )
else
#html = #params['body']
end
WebView.navigate ( url_for :action => :show_result )
end
Without getting too far into it - the body of the returned page is placed into #html variable
Hope that helps, if you need more help, let me know.
I have a sample of get an post
res = Rho::AsyncHttp.post(:url => 'http://192.168.1.64/WebServiceTest/Service.asmx/Sumar')
#msg= "Sync http call: #{res}"
http://wiki.rhomobile.com/index.php/RhodesConnectToWebServices
I'm often struggling with the nuances of AsyncHttp in Rhodes as well, so I can't claim mastery yet, but I really felt the need to chime in with a suggestion:
I find using the Firebug plugin of Firefox to be VERY helpful when debugging my Rhodes app. You can hook it up very easily! You can load your app with any browser by configuring the web server to run on a specific port. This setting is in rhoconfig.txt and it is called local_server_port.
This is specifically helpful because you can easily survey the HTML and raw data of requests/responses and use the console to run javascript commands and play with the DOM and web page in realtime.

Resources