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 set the 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