Tuesday, December 22, 2009

Basic PayPal

RyanB's Railscasts provided the perfect example to adding PayPal functionality to your application, http://railscasts.com/episodes/141-paypal-basics.

But what if let say you don't wanna do a cart system but instead you want the site to take you directly to the payment section in one click. With that of course, you can't have the user selecting items into the cart but that would do if you are charging all users the same price for the same item.

In the view, we will need a link_to taking us to the payment site on PayPal. We can also use a form to have it sent as a POST method if we are concern about the details in the url.


Based on RyanB's tutorial, we will need a sandbox account to play with this kinda stuff before firing up the real deal.

models/user.rb

def paypal_url(return_url)
values = {
:business => 'sell_1261499871_biz@live.com',
:cmd => '_xclick',
:return => return_url,
:invoice => id
}
values.merge!({
"amount" => 500,
"item_name" => "Registration"
})
"https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query
end


So now when we go ahead and click the "Pay Up Now!" link, we will be redirected to the PayPal site where we are gonna pay for a registration fee of $500...

You may like to refer to the documentation and HTML variables for more info and features.

Tuesday, December 8, 2009

Fooling Around With Sessions

A very trivial try,


def session_string
if session[:storage].nil?
session[:storage] = "Hi! i reside in the session storage."
else
session[:storage]
end

This action checks whether the session[:storage] is nil? If it is, then just put a string into it. If it isn't then the action will just return the session.


puts session[:storage]

It will display the string stored in the session.

A lil part i picked, from the Agile Development Rails book,


def find_cart
unless(session[:cart])
session[:cart] = Cart.new
end
session[:cart]
end


This action will check if a session[:cart] is nil or contains a cart. If it doesn't then create a new Cart object and store it into the session. If there is, then it will just return the session[:cart] which contains a former Cart object in it.

Session is stored in a cookie by default in Rails. So each browser will have its own session storage and will interact differently between different clients.