Tuesday, February 7, 2012

Google checkout Integration .NET



1. Go to http://sandbox.google.com/checkout/sell/ to set up test accounts in the Google Checkout Sandbox service. The Sandbox is a development environment that is designed to help you test your Google Checkout implementation. The Sandbox offers the same functionality as the production Google Checkout system with the following exceptions: 
  • The Sandbox requires you to use test credit card numbers.
  • The Sandbox does not actually execute debits and credits.
  • The Sandbox user interface displays an overlay that indicates you are working in the Sandbox environment.

You need to create two test accounts in the Sandbox. One of these accounts will function as a buyer account and the other will function as your merchant account. Please note that Google Checkout will not let you use your merchant account to complete an order at your own store. (In other words, the same account can not function as both the customer and the merchant for the same transaction.) In addition, you need to provide different information to create these two accounts. 

For Example code show bellow
web.config
<appSettings>
    <add key="UseSSL" value="false" />
    <add key="SharedSSL" value="" />
    <add key="GoogleMerchantID" value="519645447749033" />
    <add key="GoogleMerchantKey" value="9gY5jUA3oZE-UJXwQRnVKQ" />
    <add key="GoogleEnvironment" value="Sandbox" />
    <add key="GoogleAuthenticateCallback" value="True" /
</appSettings>

ShoppingCart.aspx
<cc1:GCheckoutButton ID="GCheckoutButton1" runat="server" Height="19px" />

ShoppingCart.aspx.cs
using GCheckout.Checkout;
using GCheckout.Util;

CheckoutShoppingCartRequest Req = GCheckoutButton1.CreateRequest();
                    PayPal objpaypal = new PayPal();
                    DataView dvItem = new DataView();
                    dvItem = objpaypal.GetItem(OrderID);
                    for (int i = 0; i < dvItem.Table.Rows.Count; i++)
                    {
                        int ProductId = Convert.ToInt32(dvItem[i]["ProductID"].ToString());
                        DataView dv = new DataView();
                        dv = oProducts.GetProductByID(ProductId);
                        string Name=dvItem[i]["Title"].ToString();
                        decimal Unitprice = Convert.ToDecimal(dvItem[i]["UnitPrice"].ToString());
                        int quentity = Convert.ToInt32(dvItem[i]["Quantity"].ToString());
                        string description = dv[0]["Description"].ToString();
                        Req.AddItem(Name, OrderID + "-" + description, Unitprice, quentity);
                    }
                    GCheckoutResponse Resp = Req.Send();
                    if (Resp.IsGood)
                    {
                        Response.Redirect(Resp.RedirectUrl, true);
                    }
                    else
                    {
                        Response.Write("Resp.ResponseXml = " + Resp.ResponseXml + "<br>");
                        Response.Write("Resp.RedirectUrl = " + Resp.RedirectUrl + "<br>");
                        Response.Write("Resp.IsGood = " + Resp.IsGood + "<br>");
                        Response.Write("Resp.ErrorMessage = " + Resp.ErrorMessage + "<br>");
                    }


GoogleNotify.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GCheckout.Checkout;
using GCheckout.Util;
using System.Text;
using GCheckout.OrderProcessing;
using GCheckout.MerchantCalculation;
using System.Xml;

public partial class GoogleNotify : System.Web.UI.Page
{
    google obj = new google();

    Products OrderProduct = new Products();

    public string InsertSerialnumber { get; set; }

    public string orderstate { get; set; }

    public string oredrid { get; set; }

    public string StatusID { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

        string serial = Request["serial-number"];
        string googleOrderNumber = GetGoogleOrderNumber(serial);
        AddMerchantOrderNumberRequest AmoR = new AddMerchantOrderNumberRequest(serial, googleOrderNumber);
        NotificationHistoryRequest oneNotificationHistoryRequest = new NotificationHistoryRequest(new NotificationHistorySerialNumber(serial));
        NotificationHistoryResponse oneNotificationHistoryResponse = (NotificationHistoryResponse)oneNotificationHistoryRequest.Send();
        XmlDocument config = new XmlDocument();
        config.LoadXml(oneNotificationHistoryResponse.ResponseXml);
        XmlNodeList ObjectTypeID = config.GetElementsByTagName("financial-order-state");
        foreach (XmlNode n in ObjectTypeID)
        {
            XmlElement _ObjectTypeID = (XmlElement)n;
            orderstate = _ObjectTypeID.FirstChild.InnerText.ToString();
            if (orderstate == "REVIEWING")
            {
                StatusID = "2";
            }
        }
        XmlNodeList ObjectTypeID1 = config.GetElementsByTagName("item-description");
        foreach (XmlNode n in ObjectTypeID1)
        {
            XmlElement _ObjectTypeID = (XmlElement)n;
            oredrid = GetOrderId(_ObjectTypeID.FirstChild.InnerText.ToString());
        }

        OrderProduct.GetStatusUpdate(StatusID, googleOrderNumber, oredrid);
    }

    public static string GetGoogleOrderNumber(string serialNumber)
    {
        return serialNumber.Substring(0, serialNumber.IndexOf('-'));
    }
    public static string GetOrderId(string OrderNo)
    {
        return OrderNo.Substring(0, OrderNo.IndexOf('-'));
    }


}

No comments:

Post a Comment