Getting paid: A quick payment integration

An online store is of little use if you can't sell products to customers. We will thoroughly explore the topic of payment processors in Chapter 4, Building Payment Processors. For now, however, to succeed in our promise to build a fully functional web store in 30 minutes, we will make use of a quick payment-integration with the Google Checkout API.

First, you will need to sign-up for a Google Checkout account. For the examples in this book, it is recommended that you use the Checkout sandbox, which is a test bed, non-live version of the Google Checkout system. You can get a seller account in the sandbox at the following URL: http://sandbox.google.com/checkout/sell.

Once you have an account, you can access the API document from this URL: http://code.google.com/apis/checkout/developer/.

We will create a very simple Buy It Now button using the Checkout API. After you create a sandbox seller account and log in for the first time, you can access several automatic checkout tools via the Tools tab at the top of the page. From here, select the Buy It Now button and you can generate HTML for an initial button. We will build on top of this HTML in our templates.

The buy it now HTML will looks like this (note that the actual merchant ID numbers, even though they're in a sandbox account, have been removed):

<form    action="https://sandbox.google.com/checkout/api/checkout/v2/checkoutForm/Merchant/xxxxx" id="BB_BuyButtonForm" method="post" name="BB_BuyButtonForm">
   <input name="item_name_1" 
         type="hidden" value="Cranberry Preserves"/>
   <input name="item_description_1" 
         type="hidden" value="Tasty jam made from cranberries."/>
   <input name="item_quantity_1" type="hidden" value="1"/>
   <input name="item_price_1" type="hidden" value="0.5"/>
   <input name="item_currency_1" type="hidden" value="USD"/>
   <input name="_charset_" type="hidden" value="utf-8"/>
    <input alt="" src="https://sandbox.google.com/checkout/buttons/buy.gif?merchant_id=xxxxx&amp;w=117&amp;h=48&amp;style=white&amp;variant=text&amp;loc=en_US" type="image"/>
</form>

Now we can copy and paste this HTML code into our product detail template. We will then modify the form code to use our Django template variables for the appropriate HTML form fields. Ultimately the form will look like this:

<form action="https://sandbox.google.com/checkout/api/checkout/v2/checkoutForm/Merchant/xxxxx" id="BB_BuyButtonForm" method="post" name="BB_BuyButtonForm">
    <input name="item_name_1" type="hidden" value="{{ object.name }}"/>
    <input name="item_description_1" 
        type="hidden" value="{{ object.description }}"/>
    <input name="item_quantity_1" type="hidden" value="1"/>
    <input name="item_price_1" 
        type="hidden" value="{{ object.price_in_dollars }}"/>
    <input name="item_currency_1" type="hidden" value="USD"/>
    <input name="_charset_" type="hidden" value="utf-8"/>
    <input alt="" src="https://sandbox.google.com/checkout/buttons/buy.gif?merchant_id=xxxxx&amp;w=117&amp;h=48&amp;style=white&amp;variant=text&amp;loc=en_US" type="image" />
</form>
Getting paid: A quick payment integration

Our simple product catalog that started out as a couple of Django models in the beginning of this chapter is now fully capable of processing transactions and selling products. If these Buy It Now buttons were activated through the real Google Checkout web service and not the sandbox, they would be totally live and able to sell any item in our product database.

Unfortunately, the Buy It Now button only supports selling a single item at a time. The method we've covered above will work for some very simple applications, but more than likely we will need something more sophisticated for a larger site. The typical method of supporting complicated transactions is to implement a "shopping cart" tool, a pattern that can be seen on many of the largest e-commerce sites.

The buy it now button has other problems as well. You cannot specify a choice of sizes, for example: small, medium, or large. These may be stored on our Product model, but could not be specified in a single Buy It Now button without additional work. It is also a requirement of the "buy it now" tool that the customer checkout on Google's site with a minimal amount of custom branding. If we want to provide a full-fledged checkout experience, including our own branding and HTML design, we must look to the full Checkout API. In Chapter 4 we will cover these topics and more.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset