Tuesday, April 10, 2012

Authorize.net in asp.net



Below is the code to implement authorize.net with asp.net. But for this first please create a account on www.authorize.net. After that they provide you the LoginID and Transaction key which are used in the below code. Also for testing : set your account in test mode.  
Below is the function for Authorize.net payment with credit card.
//if you have to use “https://test.authorize.net/gateway/transact.dll
//” for testing then uncomment the x_test_request and if use "https://secure.authorize.net/gateway/transact.dll" then no need to use x_test_request variable.




private void MakePaymentWithAuthorizeDotNet()
    {

        String post_url = Convert.ToString(“https://secure.authorize.net/gateway/transact.dll”);

        Dictionary<string, string> post_values = new Dictionary<string, string>();

        #region Replace Parameters
        //the API Login ID and Transaction Key must be replaced with valid values
//if we have to use “https://test.authorize.net/gateway/transact.dll
//” then uncomment the x_test_request.
// post_values.Add("x_test_request", "TRUE".ToString());
        post_values.Add("x_login","Auth_loginID");
        post_values.Add("x_tran_key", "Auth_trankey");
        post_values.Add("x_delim_data", "TRUE");
        post_values.Add("x_delim_char", "|");
        post_values.Add("x_relay_response", "TRUE");
      
        post_values.Add("x_type", "AUTH_CAPTURE");
        post_values.Add("x_method", "CC");
        post_values.Add("x_card_num”,”Credit Card Number”);
        post_values.Add("x_exp_date",“MMYY”);
        post_values.Add("x_card_code", “CVVID code”);
        post_values.Add("x_Email", “Email ID”);
         post_values.Add("x_amount", "Price Amount"]));
        post_values.Add("x_description", "Shopping Cart");
        post_values.Add("x_ship_to_first_name", txtName.Text.Trim());

        post_values.Add("x_ship_to_address", txtShippingAddress.Text.Trim());
        if (txtPhoneNumber.Text != string.Empty)
        {
            post_values.Add("x_phone", txtPhoneNumber.Text.Trim());
        }
       
            post_values.Add("x_first_name", ConfigurationManager.AppSettings["firstname"]);
            post_values.Add("x_last_name", ConfigurationManager.AppSettings["lastname"]);
            post_values.Add("x_address", ConfigurationManager.AppSettings["address"]);
            post_values.Add("x_state", ConfigurationManager.AppSettings["state"]);
            post_values.Add("x_city", ConfigurationManager.AppSettings["city"]);
            post_values.Add("x_zip", ConfigurationManager.AppSettings["zipcode"]);
      

        #endregion

        String post_string = "";

        foreach (KeyValuePair<string, string> post_value in post_values)
        {
            post_string += post_value.Key + "=" + HttpUtility.UrlEncode(post_value.Value) + "&";
        }
        post_string = post_string.TrimEnd('&');

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

        // post data is sent as a stream
        StreamWriter myWriter = null;
        myWriter = new StreamWriter(objRequest.GetRequestStream());
        myWriter.Write(post_string);
        myWriter.Close();
        String post_response;
        HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
        using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
        {
            post_response = responseStream.ReadToEnd();
            responseStream.Close();
        }
        Array response_array = post_response.Split('|');
        string chk = response_array.GetValue(5).ToString();
        if (chk == null || chk == string.Empty)
        {
            Session["chk"] = null;
        }
        else
        {
            Session["chk"] = chk;
        }
        ViewState["status"] = response_array.GetValue(5).ToString();
        string trid = response_array.GetValue(6).ToString();
        string response_code = Convert.ToString(response_array.GetValue(0));
        if (response_code == "1")
        {
            if (trid != "0")
            {
                ViewState["TransID"] = response_array.GetValue(6).ToString();
            }
        }
        else
        {
            ViewState["TransID"] = null;
        }
        ViewState["CreditType"] = response_array.GetValue(51).ToString();
        ViewState["CreditCrd"] = response_array.GetValue(50).ToString();
    }

No comments:

Post a Comment