Authorize.Net payment Integration using C# / 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")
        {//Save order to database
         }
   }



 private String readHtmlPage(string url)
    {
        String result = "";
        String strPost = "x_login=LOGIN&x_tran_key=TRANSKey&x_method=CC&x_type=AUTH_CAPTURE&x_amount=AMOUNT&x_delim_data=TRUE&x_delim_char=|&x_relay_response=FALSE&x_card_num=CARDNo&x_exp_date=CARDEXP&x_version=3.1";
        //---------Order Information
        strPost += "&x_description=Description";
        //---------Customer Information
        strPost += "&x_first_name=FNAME&x_last_name=LNAME&x_address=ADDRESS&x_city=CITY&x_state=STATE&x_zip=ZIP&x_country=COUNTRY&x_phone=PHONE&x_email=EMAIL";
        //---------Shipping Information
        strPost += "&x_ship_to_first_name=BFNAME&x_ship_to_last_name=BLNAME&x_ship_to_address=BADDRESS&x_ship_to_city=BCITY&x_ship_to_state=BSTATE&x_ship_to_zip=BZIP&x_ship_to_country=BCOUNTRY";


        //---------Itemized Order Information (for loop for adding all item of cart)
        foreach (GridViewRow gr in dgvCart.Rows)
        {
            strPost += "&x_line_item=ITEMNAME<|>ITEMCODE<|><|>ITEMQTY<|>ITEMPRICE<|>N";        
        }
        //---------Shipping Information
        strPost += "&x_tax=STAX&x_freight=Freight<|><|>SHIPPINGAMOUNT&";
        
        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