Serialize Google Checkout object to generate Cart XML checkout-shopping-cart
|
|
|
|
|
Downloads
If you are seeing this section and do not see download links, this means that you are not logged into our site. If you already are a member, click on the login link
and login into site and come back to this page for downloading the control files. If you are not a member, click on registration link to
become a Winista member and download the control for free.
In my article Google Checkout XSD File To Generate .Net class I explained how you
can use xsd.exe tool to generate C# and VB.Net class from Google Checkout XSD file APIV@.xsd. now I will show you
how you will use the generated class in your project to generate XML file that defines showing cart.
If you will use the generated .Net class in your project, generation of cart xml structure is no more than couple of
lines of codes after you have filled required fields in apiv2 class members. For example if you have one item to
add to shopping cart, the process is as follows:
- Create new
CheckoutShoppingCart object.
- Then create
ShoppingCart object in CheckoutShoppingCart object.
- Create
Item object and fill the required fields itemname, itemdescription etc.
- Use XmlSerializer to serialize
CheckoutShoppingCart to generate xml cart xml structure.
Following code snippets are fom a component library that we are developing to make use of Google Checkout API little
easy to use.
protected void OnCheckOut(object sender, ImageClickEventArgs e)
{
GCheckOutFlow checkoutFlow = new GCheckOutFlow("mymerchantid", "mymerchantkey");
GCheckoutShoppingCart gCart = checkoutFlow.CreateShoppingCart();
Google.CheckoutShoppingCart obShoopingCart = gCart.ShoopingCart;
// Add items to the cart.
Google.Item[] collItems = new Google.Item[1];
Google.Item item = new Google.Item();
item.itemname = "My Test Item";
item.itemdescription = "My test item sold on google";
item.quantity = Int32.Parse(ctlQtyTextBox.Text);
Google.Money price = new Google.Money();
price.currency = "USD";
price.Value = 9.99M;
item.unitprice = price;
collItems[0] = item;
obShoopingCart.shoppingcart.items = collItems;
obShoopingCart.checkoutflowsupport.Item = new Google.MerchantCheckoutFlowSupport();
checkoutFlow.PlaceOrder(gCart);
}
public void PlaceOrder(GCheckoutShoppingCart oShoppingCart)
{
if (null == oShoppingCart)
{
throw new ArgumentNullException("GCheckoutShoppingCart");
}
CheckoutProtocol oProtocol = new CheckoutProtocol(oShoppingCart);
String strCartXml = oProtocol.SerializeCartToXml();
System.Diagnostics.Debug.WriteLine(strCartXml);
String strCartSigntaure = oProtocol.CreateCartSignature(strCartXml);
String strEncodedCart = oProtocol.EncodeCart(strCartXml);
}
internal String SerializeCartToXml()
{
StringBuilder strXml = new StringBuilder();
XmlSerializer serializer = new XmlSerializer(typeof(Google.CheckoutShoppingCart));
StringWriter sw = new StringWriter(strXml);
serializer.Serialize(sw, m_oCart);
sw.Close();
return CleanupCartXml(strXml.ToString());
}
SerializeCartToXml demonstrates how serialization converts the object to xml document defining the
xml structure as per XSD file.
|