Search code examples
asp.netgoogle-checkout

Google Checkout HTTP Post with ASP.net


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.


Solution

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

    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