Calculating shipping charges

Handling shipping and its related charges is one of the most difficult aspects of an e-commerce platform. In many countries there are a wide variety of shipping companies, each with their own list of services and fees. The issue is further complicated by the fact that shipping costs must be computed before accepting payment from a payment service.

If you ship only a few products with very similar characteristics, it can be relatively easy to manage. You'll often know exactly how much to charge for shipping and can hardcode those values into your payment processor (in the Checkout shopping cart XML, for example). Often this is unfeasible, though, because you sell so many products in such different sizes and packaging.

There are no one-size-fits-all solutions to these problems, but tools such as Django and Python can simplify your life if you struggle with generating accurate shipping charges. The large shipping companies such as FedEx, DHL, UPS, and even the United States Postal Service, now offer web services APIs to perform shipping calculations. We'll discuss each of these services briefly, and then demonstrate another option for shipping calculations, which is builtin to Google Checkout.

Notably, the United States Postal Service offers some of the most useful and sophisticated web-based APIs. Not only can you calculate shipping charges, but you can standardize and verify addresses, look up ZIP codes, and even schedule carrier pickups. All of these services are free and use a very simple XML API, similar to the Checkout APIs we discussed earlier. The services are well documented and easily accessed by developers. It's clear that the USPS has tried to build a very web-friendly toolset. The only downside is that it isn't useful for international developers.

FedEx has implemented a set of web services using the Simple Object Access Protocol (SOAP) and Web Service Definition Language (WSDL) standards. Their documentation is good and they provide code samples in several languages. However, you will be required to sign-up for a developer account before accessing many of the resources. There is also an excellent third-party Python library called python-fedex that is a light wrapper around the FedEx tools. It supports shipment creation, cancellations, and tracking by tracking number. It is available on Google Code: http://code.google.com/p/python-fedex/.

UPS offers a similar set of services as the previous two, but there is a significant amount of work to get a developer key and browse the documentation. Their API can do typical activities such as package tracking, rate and service calculations, address validation, and so on. In my experience, however, the UPS tools are not the easiest to use. This includes using their website, which can be a daunting experience for new, non-technical users attempting their first shipment.

Integrating any of these API services into our e-commerce Django application could follow several patterns. One method is to attempt to retrieve shipping calculations from our checkout view. This is an acceptable solution for many applications, though you are relying on the shipper's web service to return relatively quickly, otherwise the rendering time on your checkout view will be extended (this can be mitigated somewhat with a short timeout and default values). Another alternative is to use AJAX to retrieve shipping values from the appropriate web service, and dynamically update the HTML checkout form.

Tracking information is decidedly easier. In the simplest case, we could attach a tracking field to our Order model. This would have to be populated manually, unless we went the full step of integrating our label making/shipment printing tool with our web service. This would be possible using FedEx or UPS, but is a particularly advanced feature that may require significant development time.

The main alternative to a custom implementation of shipping services is to let your payment processor handle it. Google Checkout does this very effectively. When we created our shopping cart XML in earlier chapters, we created as simple a document as possible. There are many additional features, however, one of which is carrier-calculated shipping.

The carrier-calculated shipping service in the Checkout API allows you to specify one or more shipping carriers that the customer can choose from. The checkout process queries the carrier's appropriate API automatically and retrieves shipping information. For this to work, however, it is required that you provide a weight for each item. This could be done using the ProductDetail and ProductAttribute models we built in Chapter 2.

This feature currently supports UPS, FedEx, and USPS only. There are several optional features, including adding fixed handling charges or automatically increasing/decreasing shipping calculations by a certain percentage.

To implement carrier-calculated shipping, we must add several tags to our shopping cart XML file from previous chapters. The most important thing is that each <item> block must now contain an <item-weight> tag whose attributes specify the item's actual weight. The following XML fragment adds a weight of 5.5 pounds to the listed item:

<item>
<item-name>Fresh Cranberries</item-name>
  ...
<item-weight unit="LB" value="5.5"/>
</item>

After adding weights to all our ordered items, we can create the carrier-calculated shipping rules. These are added to the <checkout-flow-support> tag of our XML document and look like the following:

<checkout-flow-support>
<merchant-checkout-flow-support>
<shipping-methods>
<carrier-calculated-shipping>
<carrier-calculated-shipping-options>
<carrier-calculated-shipping-option>
<price currency="USD">10.00</price>
<shipping-company>FedEx</shipping-company>
<carrier-pickup>REGULAR_PICKUP</carrier-pickup>
<shipping-type>Priority Overnight</shipping-type>
</carrier-calculated-shipping-option>
</carrier-calculated-shipping-options>

<shipping-packages>
<shipping-package>
<height unit="IN" value="6"/>
<length unit="IN" value="24"/>
<width unit="IN" value="15"/>
<ship-from id="BOS">
<city>Boston</city>
<region>MA</region>
<country-code>US</country-code>
<postal-code>02110</postal-code>
</ship-from>
</shipping-package>
</shipping-packages>
</carrier-calculated-shipping>
</shipping-methods>
      ...
</checkout-flow-support>

Note that the <price> tag in the <carrier-calculated-shipping-options> section is the default price for shipping using this method. This is required for cases where Google Checkout is unable to reach the shipper's web service or when another technical error prevents automatic calculation.

The <carrier-calculated-shipping> is composed of two parts: the shipping options and the shipping packages. The former is where you can define each vendor (FedEx, UPS, or USPS), their service and their pickup option. The shipping packages section is where you define the package size (optional) and the location you will be shipping from. These details are used by Google Checkout's system to present the customer with a total for shipping charges when they enter payment.

Checkout API also supports flat-rate shipping, which you can combine with the carrier-calculated results to provide customers with even more choices. This is done using a <flat-rate-shipping> tag added to the <shipping-methods> section of the checkout flow support. An example follows:

<flat-rate-shipping name="USPS Priority Mail>
<price currency="USD">4.00</price>
</flat-rate-shipping>
<flat-rate-shipping name="Free Shipping>
<price currency="USD">0.00</price>
</flat-rate-shipping>

You can learn more about carrier-calculated shipping at the following documentation URL:

http://code.google.com/apis/checkout/developer/Google_Checkout_XML_API_Carrier_Calculated_Shipping.html.

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

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