First my question is similar to "Getting Started With ASP.NET MVC3 & Google Checkout: Take 2" but I don't feel this was completely answered and my question has nothing to do with MVC3.
Okay, I've managed to successfully integrate Google Checkout / Wallet into an ASP.NET application using their sandbox server. Now we are ready to go live and all I have done is change some of the paramaters - the action url, merchant id and merchant key - and it no longer works.
I've setup a simple web project to try and find the source of the problem but I'm at a bit of a loss - everything seems to be working as it should. I'm receiving a 400 bad request erorr - here is what is being sent which I captured using fiddler:
POST https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/XXXXXXXXXXXXXXX HTTP/1.1
Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Content-Type: application/xml;charset=UTF-8
Host: checkout.google.com
Content-Length: 679
Expect: 100-continue
Connection: Keep-Alive
<?xml version="1.0" encoding="UTF-8"?>
<checkout-shopping-cart xmlns="http://checkout.google.com/schema/2">
<shopping-cart>
<merchant-private-data>2390</merchant-private-data>
<items>
<item>
<item-name>Business Cards 107 - White 350gsm Silk</item-name>
<unit-price currency="GBP">20.40</unit-price>
<quantity>1</quantity>
<item-description>25 x1 One sided - Standard Business Cards</item-description>
<merchant-item-id>2956</merchant-item-id>
</item>
</items>
</shopping-cart>
<checkout-flow-support>
<merchant-checkout-flow-support />
</checkout-flow-support>
</checkout-shopping-cart>
And here's the C# code - it's pretty much the same as that in the question I refer to at the top:
#define LIVE
string url = string.Format(SandboxMerchantCheckoutUrl, SandboxMerchantId);
#if LIVE
url = string.Format(ProductionMerchantCheckoutUrl, ProductionMerchantId);
#endif
string path = HttpContext.Current.Server.MapPath("~/SampleXML/") + "order.xml";
string xml = File.ReadAllText(path);
byte[] bytes = Encoding.UTF8.GetBytes(xml);
string xmlstr = string.Empty;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
string authInfo = SandboxMerchantId + ":" + SandboxMerchantKey;
#if LIVE
authInfo = ProductionMerchantId + ":" + ProductionMerchantKey;
#endif
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Headers["Authorization"] = "Basic " + authInfo;
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
request.ContentType = "application/xml;charset=UTF-8";
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
xmlstr = reader.ReadToEnd();
}
else
{
string message = string.Format("POST failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}
}
XmlDocument xmldoc = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
nsmgr.AddNamespace("g", "http://checkout.google.com/schema/2");
xmldoc.LoadXml(xmlstr);
string serialNumber = xmldoc.SelectSingleNode("/g:checkout-redirect", nsmgr).Attributes["serial-number"].Value;
string redirectUrl = xmldoc.SelectSingleNode("/g:checkout-redirect/g:redirect-url", nsmgr).InnerText;
Okay, update here. I don't think there's anything wrong with the above. Logged into the Google seller account and checked the integration console and found the following was received successfully:
<?xml version="1.0" encoding="UTF-8"?>
<checkout-shopping-cart xmlns="http://checkout.google.com/schema/2">
<shopping-cart>
<merchant-private-data>2390</merchant-private-data>
<items>
<item>
<item-name>Business Cards 107 - White 350gsm Silk</item-name>
<unit-price currency="GBP">20.40</unit-price>
<quantity>1</quantity>
<item-description>25 x1 One sided - Standard Business Cards</item-description>
<merchant-item-id>2956</merchant-item-id>
</item>
</items>
</shopping-cart>
<checkout-flow-support>
<merchant-checkout-flow-support />
</checkout-flow-support>
</checkout-shopping-cart>
However an error is sent from Google which reads "Carts must contain at least one item." There is a link to the Google documentation which says this error occurs if there aren't any items in the shopping cart or if the quantity is set to zero. None of these apply here so I still have no idea why this request is being rejected.