Building eCommerce Applications
Developers from DevZone
Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo
Building eCommerce Applications by Developers from DevZone Copyright © 2011 O’Reilly Media. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribookson line.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or
[email protected].
Editor: Mary Treseler Production Editor: Kristen Borg Proofreader: Rachel Monaghan October 2011:
Cover Designer: Mark Paglietti Interior Designer: David Futato Illustrator: Robert Romano
First Edition.
Revision History for the First Edition: See http://oreilly.com/catalog/errata.csp?isbn=9781449316891 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Building eCommerce Applications and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
ISBN: 978-1-449-31689-1 [LSI] 1317663918
Table of Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. Creating a Mobile Enabled Shopping Cart with HTML5, jQuery, and TaffyDB, Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Step 1: Set Up the Tutorial Directory on Your Web Server Step 2: Provide the Entire Product Catalog in JSON Format from the eCommerce System Step 3: Set Up the Catalog HTML Page Step 4: Load the Product Catalog in JSON Format from the eCommerce System Step 5: Display the Store View Step 6: Display the Category View Step 7: Display the Product View Step 8: Style the Links and Buttons That’s It
1 2 4 5 6 8 9 11 12
2. Creating a Mobile Enabled Shopping Cart with HTML5, jQuery, and TaffyDB, Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Step 1: Bind the Add to Cart Button to the addToCart() Function Step 2: Initialize the HTML5 Cart Database Step 3: Store the Product Information in the cartItems Database Step 4: Add the HTML for the Cart View and Bind the View Cart Button to the demo.viewCart() Function Step 5: Display the Cart Step 6: Format the Cart View Step 7: Stringify the Cart to Send to the Server Step 8: Clear the Cart Contents That’s It
20 20 22 25 26 30 30 31 31
iii
3. Analyzing Open Data for Fun (and Profit?), Part 1 . . . . . . . . . . . . . . . . . . . . 33 Getting the World Bank Data Loading the Data into MySQL Downloading Google Visualization Tools Configuring a Visualization Web App with Tomcat Implementing and Querying a SQL Data Source Next Steps...
34 34 36 37 38 40
4. Analyzing Open Data for Fun (and Profit?), Part 2 . . . . . . . . . . . . . . . . . . . . 41 Implementing Table and Line Chart Visualizations Creating an Event-Driven Line Chart Application Next Steps Until Next Time
41 45 50 50
5. Analyzing Open Data for Fun (and Profit?), Part 3 . . . . . . . . . . . . . . . . . . . . 51 PayPal X: Your Global Payment Platform Dollars and Sense Adaptive Payments Configuration V-Rupees for Graphs Generating a Pay Key with the AdaptivePayments API Kickstart the Embedded Payments Flow Handling Payment Responses Wrapping Up
51 52 54 55 57 60 64 66
6. Build an MLM Engine with Neo4j and MassPay, Part 1 . . . . . . . . . . . . . . . . 67 What Is Multi-Level Marketing? MLMs Are Graphs The Design and Implementation of an MLM Engine Traversing the Graph to Compute Commissions Wrapping Things Up
67 68 69 76 80
7. Build an MLM Engine with Neo4j and MassPay, Part 2 . . . . . . . . . . . . . . . . 81 Introduction Defining the Class Authentication and API Resolution Making the Call Putting It All Together
81 82 83 85 87
8. Getting Started with PayPal on Django . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Introduction Requirements
iv | Table of Contents
89 89
Paypal Accounts Django Sample Application Running the Sample Application Configuration Database Web Server A Basic Payment Workflow The Model URL Mappings Step 1—List Content Available for Purchase Step 2—Attempt to Access Content Step 3—Purchase Content Step 4—Verify Payment Step 5—Successful Purchase Further Enhancements Conclusion Further Information and Other Resources
89 90 90 90 90 90 91 91 91 91 92 92 93 94 96 97 97 97
9. Accelerate Your Development Using the Apigee API Console . . . . . . . . . . . 99 What Is the Apigee PayPal API Console? APIs Available to You in the Console Using the Console Linking Calls Together Other Apigee Consoles You Might Want to Check Out Accelerate Your Own Development About the Author
99 100 101 103 104 105 105
10. Interview with iConcessionStand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 About the Author
115
11. Magento, Part 1: How to Install Magento . . . . . . . . . . . . . . . . . . . . . . . . . 117 Comparing the Editions Server Requirements Installing Magento, Step 1: Download, Extract, and Upload Installing Magento, Step 2: Set Up Permissions Installing Magento, Step 3: Creating the Database Installing Magento, Step 4: Run the Setup Wizard Installing Magento, Step 5: Verify the Installation Next Steps About the Author
117 118 118 119 119 120 121 121 122
Table of Contents | v
12. Magento, Part 2: How to Set Up Your Magento Store . . . . . . . . . . . . . . . . 123 Creating Categories Adding a Product in your Magento Store Wrapping Up About the Author
123 125 134 134
13. Magento, Part 3: Accepting PayPal Payments . . . . . . . . . . . . . . . . . . . . . . 137 How to Accept Payments with PayPal in Magento Wrapping Up About the Author
138 144 145
14. Magento, Part 4: Integrating Magento and WordPress Using the FishPig Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Integrating Magento and WordPress with FishPig Wrapping Up About the Author
148 158 159
15. The Convergence of Local, Social, and Mobile, Part 1: Local . . . . . . . . . . 161 Breaking Silos Defining Local The Evolution of Local Phase 1: Pre–personal transportation and technology Phase 2: Introduction of personal transportation Phase 3: The information “superhighway” Share Your Thoughts About the Author
162 162 163 163 164 165 166 167
16. The Convergence of Local, Social, and Mobile, Part 2: Social . . . . . . . . . . 169 Briefly Defining Social The Evolution of Social Phase 1: “Can I borrow your goat?” Phase 2: Pen pals, trade, and travel Phase 3: The rise of instant communication A Final Note on Social Media Share Your Thoughts About the Author
170 170 170 171 172 173 173 173
17. The Convergence of Local, Social, and Mobile, Part 3: Mobile . . . . . . . . . 175 The Evolution of Mobile Phase 1: Can you hear me now? Phase 2: The “G” wars
vi | Table of Contents
176 176 177
Phase 3: The phone gets a brain and learns to surf Stay Tuned Share Your Thoughts About the Author
178 180 180 180
18. The Convergence of Local, Social, and Mobile, Part 4: The Convergence . . 181 First, A Look at the Data What Does This Mean for the Future of Local, Social, and Mobile? Share Your Thoughts About the Author
182 183 185 185
19. To Catch a (Quantum) Thief . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Data Security in the Quantum Age The Downside Bad News Hacking Accelerated Game Changer 5 to 10
187 188 189 189 190 190
20. 10 Best Practices for Employing QR Codes . . . . . . . . . . . . . . . . . . . . . . . . . 193 Size Matters (the Bigger, the Better) Short Is Sweet Location Matters Know Why You’re Using It Make It Count A Little Goes a Long Way Tell People How to Use It Provide an Alternative Respect the Border Bring the Wow About the Author
194 194 194 195 195 195 196 196 196 196 196
21. Integrating Payments into WordPress, Part 1: Features and Getting Started . 197 What Is WordPress? WordPress Documentation Installing WordPress First Steps After Installation Coming Next Time About the Author
198 199 200 201 202 202
Table of Contents | vii
22. Integrating Payments into WordPress, Part 2: PayPal Plugins . . . . . . . . 203 Extending WordPress with Plugins Plugin Recommendations PayPal Plugins Plugin Learnings and Where They’ll Take Us About the Author
203 204 206 210 211
23. Integrating Payments into WordPress, Part 3: Build Your Own PayPal Plugin . 213 A Note About PHP Understanding “The Loop” The Anatomy of a WordPress Plugin PayPal Functionality in Our Plugin Deploying and Testing the Plugin Things to Learn from This WordPress Series About the Author
213 214 214 218 218 220 220
24. Crowd Marketing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Viral Isn’t a Plan—It Is a Consequence Making a Good Fire What Happened? Life Ahead Is eBay Without Having to Ship Stock from the Attic Let Them Buy Wholesale and Distribute Fans, Friends, Launchpads, and New Markets About the Author
221 222 222 223 223 224 224
25. Bitcoin: Money Without Governments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 What Is Bitcoin? Acquiring and Storing Bitcoins Using and Accepting Bitcoins Threats and Opportunities About the Author
viii | Table of Contents
225 227 230 230 232
Introduction
This collection of articles and blog entries is representative of the full spectrum of commerce-related content we’ve published on PayPal’s Developer Network over the past year. You will find tutorials and quick reference pieces for developers. With the creation of x.commerce, we’ve expanded our coverage to address the needs of eBay and Magento developers, and you can expect to see more content focused on helping both the developer and merchant communities in the coming year. Our team has covered a wide variety of topics: building mobile shopping carts, QR codes, working with various PayPal APIs, including how to integrate PayPal with other technologies such as WordPress. Three main themes have emerged in the commerce world today: Mobile, Social, and Local. Expect to see more coverage of these in the coming months. If you haven’t already visited, we hope you will join at DevZone: https://www .x.com/developers/paypal/devzone Feel free to drop me a line if there are topics you would like us to cover or if you would like to write for us. —Mary Treseler Editorial Strategist, O’Reilly Media Inc.
[email protected] ix
CHAPTER 1
Creating a Mobile Enabled Shopping Cart with HTML5, jQuery, and TaffyDB, Part 1
Humberto Roa The changing mobile landscape, and newer web technologies, means that working with mobile commerce requires a new approach to an old problem. This tutorial will take you through the steps to create a simple mobile commerce site that provides an interactive shopping experience over low-bandwidth connections. Part one of this tutorial will walk you through using PHP5, jQuery, and Taffy DB to create an interactive product catalog. Part two of this tutorial will walk you through using an HTML5 database to store a shopping cart and show you how to submit the shopping cart to a server at the time of checkout. If you’d like to follow along, you can download the files for the project template here. The final source files that you’ll have created by the end of this tutorial are here.
Step 1: Set Up the Tutorial Directory on Your Web Server You will be able to complete this tutorial using a standard web server that has PHP5 installed. To get started, create a new directory on your web server named tutorial. Inside the tutorial directory, create two directories: css and js. Under the js directory, create a directory named lib.
1
When you are done, the directory structure should look like this: tutorial/css/ tutorial/js/ tutorial/js/lib/ Next, download the following source libraries: • • • •
html5resetcss - http://code.google.com/p/html5resetcss/downloads/list jQuery 1.4.2 - http://jquery.com Taffy DB 1.7.2 - http://taffydb.com JSON2 - http://www.json.org
Place the file html5resetcss.css in the tutorial/css/ directory, and place the jQuery, Taffy DB, and JSON javascript files in the tutorial/js/lib/ directory. Finally, create the following four empty files. tutorial/catalog.php tutorial/index.html tutorial/js/site.js tutorial/css/site.css We’ll will be working with these four files in this tutorial.
Step 2: Provide the Entire Product Catalog in JSON Format from the eCommerce System In order to minimize http requests between the mobile device and the server while a customer is browsing the product catalog, we’re going to download the entire catalog from the ecommerce system when the catalog is viewed in a browser. Note that if you are working with a very large catalog, you may want to download categories or subcategories as needed instead of the full catalog. We’ll start with defining the category and product information with arrays. In practice, these arrays will contain the results of database queries from your PHP5-based ecommerce system. For this tutorial, open the file catalog.php, and add the following category and product information: