Google Checkout HTTP Post with ASP.net

拈花ヽ惹草 提交于 2019-12-08 09:18:05

问题


I have 2 pages I created in ASP.net(C#). The first one(called shoppingcart.asp) has a buy it now button. The second one(called processpay.asp) just waits for google checkout to send an HTTP request to it to process the payment. What I would like to do send a post statement to google checkout with a couple of variables that I want passed back to processpay.asp(ie clientid=3&itemid=10), but I don't know how to format the POST HTTP statement or what settings I have to change in google checkout to make it work.

Any ideas would be greatly appreciated.


回答1:


Google Checkout has sample code and a tutorial on how to integrate it with any .NET application:

  • Google Checkout API - Google Checkout Sample Code for .NET

Make sure to check the section titled: "Integrating the Sample Code into your Web Application".


However, if you prefer to use a server-side POST, you may want to check the following method which submits an HTTP post and returns the response as a string:

using System.Net;

string HttpPost (string parameters)
{ 
   WebRequest webRequest = WebRequest.Create("http://checkout.google.com/buttons/checkout.gif?merchant_id=1234567890");
   webRequest.ContentType = "application/x-www-form-urlencoded";
   webRequest.Method = "POST";

   byte[] bytes = Encoding.ASCII.GetBytes(parameters);

   Stream os = null;

   try
   { 
      webRequest.ContentLength = bytes.Length;
      os = webRequest.GetRequestStream();
      os.Write(bytes, 0, bytes.Length);      
   }
   catch (WebException e)
   {
      // handle e.Message
   }
   finally
   {
      if (os != null)
      {
         os.Close();
      }
   }

   try
   { 
      // get the response

      WebResponse webResponse = webRequest.GetResponse();

      if (webResponse == null) 
      { 
          return null; 
      }

      StreamReader sr = new StreamReader(webResponse.GetResponseStream());

      return sr.ReadToEnd().Trim();
   }
   catch (WebException e)
   {
      // handle e.Message
   }

   return null;
} 

Parameters need to be passed in the form: name1=value1&name2=value2




回答2:


The code will likely end up looking something like this:

GCheckout.Checkout.CheckoutShoppingCartRequest oneCheckoutShoppingCartRequest =
  GCheckoutButton1.CreateRequest();

oneCheckoutShoppingCartRequest.MerchantPrivateData = "clientid=3";

GCheckout.Checkout.ShoppingCartItem oneShoppingCartItem =
  new GCheckout.Checkout.ShoppingCartItem();
oneShoppingCartItem.Name = "YourProductDisplayName";
oneShoppingCartItem.MerchantItemID = "10";

oneCheckoutShoppingCartRequest.AddItem(oneShoppingCartItem);



回答3:


Yesterday I used http://www.codeproject.com/KB/aspnet/ASP_NETRedirectAndPost.aspx to send the post data and it works fine



来源:https://stackoverflow.com/questions/1983408/google-checkout-http-post-with-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!