Integrating PayPal payments with Ruby on Rails
In Agile Web Development with Rails the Pragmatic Programmers develop the Depot shopping application. The application manages a book catalog, displays the catalog to prospective buyers, manages a shopping cart and checkout. What they do not develop is payment processing after the checkout.
I recently created a PayPal account to make selling stuff I do not need any more on ebay easier to handle. What does it take to enhance the Depot application to do payment processing with PayPal?
The following code is an example used to understand how PayPal works with Ruby on Rails. For real world applications please take a look at the Active Merchant library first. Of course we do not want to reeinvent the wheel!
Getting a PayPal test environment / account
On the PayPal Developer Network you can singn in to Developer Central. Here you can find among other things How-To articles, documentation and a Sandbox test environment.
Standard Checkout
On Integration Center under Additional Payment Options you find a description of the Standard Checkout procedure. It works with HTML-Form code which POSTs a transaction to the PayPal site. The PayPal HTML-Form provides variables for specifying the items to check out.
An other integration option is the Website Payments Pro SOAP API. But for the time beeing we will take a look how Website Payments Standard Simple Checkout can be used to check out the Depot application's shopping cart. The SAOP API may be a topic of an upcoming blog posts.
Task: Payment processing with PayPal
Iteration 1: Website Payments Standard
The goal of the iteration is to enhance the checkout page with PayPal payment processing.
I will use the code of Depot application as at the end of Chapter 10 "Task E: Checkout!". At the time of this writing I have Agile Web Development with Rails 2nd Ed. available as a beta book version B1.12.
Checkout View
To create the form code for the PayPal button we need to iterate over the cart items and add appropriate form fields.
checkout.rhtml:
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<%= hidden_field_tag "cmd", "_cart" %>
<%= hidden_field_tag "upload", "1" %>
<%= hidden_field_tag "business", "test@test.com" %>
<%= render(:partial => "paypal_item", :collection => @cart.items) %>
<%= hidden_field_tag "return", "http://localhost:3000/store" %>
<%= submit_tag "Buy with PayPal", :class => "submit" %>
<% end_form_tag %>
_paypal_item.rhtml:
<%= hidden_field_tag sprintf("item_name_%d", paypal_item_counter + 1), h(paypal_item.title) %>
<%= hidden_field_tag sprintf("amount_%d", paypal_item_counter + 1), paypal_item.price %>
To-Do (future Iterations)
- On success of the PayPal payment we need to empty the cart.
- Use Instant Payment Notification (form field notify_url) to get PayPal payment information and store this information to the Depot database.
- If the user cancels the PayPal payment we want him to proceed shoping at our site. With the
cancel_return form field you can setthe URL. PayPal redirects the user to this URL.
What does it have to do with Salesforce?
Nothing in particular, but what about capturing a successful web-sale as an opportunity in salesforce.com? By the way adding buyer information as a lead or contact?
What does the information flow on your e-commerce sites look like? How would you like e-commerce sites integrate with salesforce.com?
ion flow on your e-commerce site look like? How would you like e-commerce sites integrate with salesforce.com?technorati tags:ruby, rubyonrails, paypal
6 Comments:
Would love to hear more about this!
Hi,
I was thinking that writing about PayPal may be a bit off topic. Because I am interested in this topic you could expect to hear more about this.
Regards
Oliver
Hey this is great stuff, excatly what i am trying to do, really interested to hear how you get on.
Generally, I like PayPal. It is a convenient tool anyway. But there was a sucky situation some time ago. I just got an e-mail from the PayPal 2 weeks ago. They wrote my account is suspended, because "I'm a hater". Actually they didn't like my political views and they says I'm a hater or something. They talked about my OsCommerce store too, about "hater items" too, although I didn't sell anything, and there were nothing hater in my OsCommerce store. Then we had different difficult talks. I really don't care who is hater and who isn't, the people has their rights, the PayPal should respect the Constitution.
So, my story is not about the money, but about the free speech, the PayPal not just stealing, but breaking your rights too!
Hi! Thanks for your post. It was helpful. But, I believe the end_form_tag line needs to have the equals sign in it (< % = ) to make it have output.
This drove me a bit crazy as all my other buttons on my screen were taking me to paypal... Yay for Firebug!
Also, I think I had to add the code to my checkout action in my store controller to add all the items to the cart. But I did go pretty far off script when building my shopping cart app, so maybe that was the problem :)
Cyber Coin fulfills a growing need for consumers to purchase lower-priced and impulse items on the Internet-especially digital goods and services that can be instantaneously downloaded to your computer, such as software, articles, research, games, and music, said Bill Melton, user of Cyber Cash. "Internet merchants must offer consumers the ability to make spontaneous, small denomination payments on the Internet to take electronic commerce to the next level. When a consumer wants to purchase an item on the Web, he or she simply clicks on the Coin icon next to the goods. The entire process takes only seconds. http://www.infysolutions.com
Post a Comment
Links to this post:
Create a Link