How to add Authorized .net payment integration in asp.net



protected void btnnext_Click(object sender, EventArgs e)
{
    string[] objRetVals;
        string str = readHtmlPage("https://secure.authorize.net/gateway/transact.dll");
        objRetVals = Convert.ToString(str).Split('|');
        if (objRetVals[0].ToString() == "1")  //--------------------- 1 means successfull payment done
        {
          string transactionid = objRetVals[6].ToString();
          // code for save order in database
        }
}




private String readHtmlPage(string url)
    {
        String result = "";
        String strPost = "x_login=authorized.net_login_name&x_tran_key=TransactionKey&x_method=CC&x_type=AUTH_CAPTURE&x_amount=" + hfototal.Value + "&x_delim_data=TRUE&x_delim_char=|&x_relay_response=FALSE&x_card_num="+ txtcnumber.Text+"&x_exp_date="+ddlm.SelectedValue+""+ddly.SelectedValue +"&x_version=3.1";
        //---------Order Information
        strPost += "&x_description=Pressurecookerparts";
        //---------Customer Information
        strPost += "&x_first_name=" + lblbfname.Text + "&x_last_name=" + lblblname.Text + "&x_address="+ lblbaddress.Text +"&x_city="+ lblbcity.Text+"&x_state="+ lblbstate +"&x_zip="+ lblbzip.Text +"&x_country="+ lblbcountry.Text+"&x_phone="+ lblbph.Text+" &x_email="+ lblbemail.Text +"";
        //---------Shipping Information
        strPost += "&x_ship_to_first_name=" + lblsfname.Text + "&x_ship_to_last_name=" + lblslname.Text + "&x_ship_to_address=" + lblsaddress.Text + "&x_ship_to_city=" + lblscity.Text + "&x_ship_to_state=" + lblsstate.Text + "&x_ship_to_zip=" + lblszip.Text + "&x_ship_to_country=" + lblscountry.Text + "";


        //---------Itemized Order Information
        foreach (GridViewRow gr in dgvCart.Rows)
        {
            Label lbl = (Label)gr.Cells[1].FindControl("lblpname");
            string pname=lbl.Text;
            if(lbl.Text.Length>30){pname=lbl.Text.Substring(0,30);}          


            strPost += "&x_line_item=" + pname + "<|>" + Convert.ToString(gr.Cells[0].Text) + "<|><|>" + Convert.ToString(gr.Cells[3].Text) + "<|>" + Convert.ToString(gr.Cells[2].Text).Replace("$","") + "<|>N";      
        }
        //---------Shipping Information
        strPost += "&x_tax=" + lbltax.Text.Replace("$", "") + "&x_freight=Freight<|><|>" + lblshipping.Text.Replace("$", "") + "&";
       
        StreamWriter myWriter = null;


        HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
        objRequest.Method = "POST";
        objRequest.ContentLength = strPost.Length;
        objRequest.ContentType = "application/x-www-form-urlencoded";


        try
        {
            myWriter = new StreamWriter(objRequest.GetRequestStream());
            myWriter.Write(strPost);
        }
        catch (Exception e)
        {
            return e.Message;
        }
        finally
        {
            myWriter.Close();
        }


        HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
        using (StreamReader sr =
        new StreamReader(objResponse.GetResponseStream()))
        {
            result = sr.ReadToEnd();


            // Close and clean up the StreamReader
            sr.Close();
        }
        return result;
    }

Comments

Popular posts from this blog

GROUP BY, CUBE, ROLLUP and SQL SERVER 2005

How to get content of Ckeditor & Fckeditor using Javascript

How to Fix Error- Sys.WebForms.PageRequestManagerTimeoutException - The server request timed out