2

首先,我的问题类似于“ASP.NET MVC3 和 Google Checkout 入门:Take 2”,但我觉得这个问题没有得到完全回答,而且我的问题与 MVC3 无关。

好的,我已经成功地使用他们的沙盒服务器将 Google Checkout / Wallet 集成到 ASP.NET 应用程序中。现在我们已经准备好上线了,我所做的只是更改了一些参数 - 操作 url、商家 ID 和商家密钥 - 它不再有效。

我已经设置了一个简单的网络项目来尝试找到问题的根源,但我有点不知所措 - 一切似乎都在正常工作。我收到 400 错误请求错误 - 这是我使用 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>

这是 C# 代码 - 它与我在顶部提到的问题中的代码几乎相同:

#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;

好的,这里更新。我不认为上面有什么问题。登录谷歌卖家账号,查看集成控制台,发现成功收到如下:

<?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>

但是,Google 发送了一条错误消息,内容为“购物车必须包含至少一项”。有一个指向谷歌文档的链接说如果购物车中没有任何商品或数量设置为零,则会发生此错误。这些都不适用于这里,所以我仍然不知道为什么这个请求被拒绝。

4

0 回答 0