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.

0 comments:

Post a Comment