Custom Table

Nadia Tahir asked on May 30, 2018 23:51

texthello kentico dev i create a new web part in kentico for form to pass data to authorize.net and it works but i want save the data in custom table too what is the methode thanks protected void Submit(object sender, EventArgs e) {

    //String post_url = "https://test.authorize.net/gateway/transact.dll";

    String post_url = "https://secure.authorize.net/gateway/transact.dll";


    Dictionary<string, string> post_values = new Dictionary<string, string>();
    //the API Login ID and Transaction Key must be replaced with valid values




    post_values.Add("x_login", AuthorizeNetLoginName);
    post_values.Add("x_tran_key", AuthorizeNetTransactionKey);



    post_values.Add("x_delim_data", "TRUE");
    post_values.Add("x_delim_char", "|");
    post_values.Add("x_relay_response", "FALSE");

    post_values.Add("x_type", "AUTH_CAPTURE");
    post_values.Add("x_method", "CC");

    //Credit Card Number
    post_values.Add("x_card_num", Credit.Text);


    //Expiration Date Card Number
    string expire = month.SelectedValue + "" + year.SelectedValue;
    post_values.Add("x_exp_date", expire);

    //Order Amount
    post_values.Add("x_amount", AmountText.Text);
    string SelectedEndPoint = currentRadioButtonList.SelectedItem.Value.ToString();
    string Description = SelectedEndPoint + " - " + comment.Text;
    post_values.Add("x_description", Description);
    // comment = Fund + comment 
    post_values.Add("x_first_name", First.Text);
    post_values.Add("x_last_name", Last.Text);
    post_values.Add("x_address", Address1.Text);
    post_values.Add("x_state", State.Text);
    post_values.Add("x_email", Email.Text);
    post_values.Add("x_Country", Country.SelectedValue);
    post_values.Add("x_Phone", Phone.Text);
    post_values.Add("x_city", City.Text);
    post_values.Add("x_zip", Postal.Text);
    Random r = new Random();
    int n = r.Next();
    string InvoiceNumber = n + " - " + DateTime.Now.ToString("dd MM yyyy").Replace(" ","");
    post_values.Add("x_invoice_num", InvoiceNumber);
    post_values.Add("x_card_type", Card.SelectedValue);
    post_values.Add("x_card_code", Security.Text);
    var id = First.Text + Last.Text + Postal.Text;
    post_values.Add("x_cust_id", id);
    // Additional fields can be added here as outlined in the AIM integration
    // guide at: http://developer.authorize.net

    // This section takes the input fields and converts them to the proper format
    // for an http post.  For example: "x_login=username&x_tran_key=a1B2c3D4"
    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('&');

    // The following section provides an example of how to add line item details to
    // the post string.  Because line items may consist of multiple values with the
    // same key/name, they cannot be simply added into the above array.
    //
    // This section is commented out by default.
    /*
    string[] line_items = {
        "item1<|>golf balls<|><|>2<|>18.95<|>Y",
        "item2<|>golf bag<|>Wilson golf carry bag, red<|>1<|>39.99<|>Y",
        "item3<|>book<|>Golf for Dummies<|>1<|>21.99<|>Y"};

    foreach( string value in line_items )
    {
        post_string += "&x_line_item=" + HttpUtility.UrlEncode(value);
    }
    */

    // create an HttpWebRequest object to communicate with Authorize.net
    const SslProtocols _Tls12 = (SslProtocols)0x00000C00;
    const SecurityProtocolType Tls12 = (SecurityProtocolType)_Tls12;
    ServicePointManager.SecurityProtocol = Tls12;
    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();

    // returned values are returned as a stream, then read into a string
    String post_response;
    HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
    using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
    {
        post_response = responseStream.ReadToEnd();
        responseStream.Close();
    }

    // the response string is broken into an array
    // The split character specified here must match the delimiting character specified above
    Array response_array = post_response.Split('|');

    // the results are output to the screen in the form of an html numbered list.
    var result = "<OL> \n";
    foreach (string value in response_array)
    {
        result = result + "<LI>" + value + " </LI> \n";
    }
    result = result + "</OL> \n";
    // individual elements of the array could be accessed to read certain response
    // fields.  For example, response_array[0] would return the Response Code,
    // response_array[2] would return the Response Reason Code.
    // for a list of response fields, please review the AIM Implementation Guide

} 

Correct Answer

Dragoljub Ilic answered on May 31, 2018 10:36

Hi Nadia,

First you need to create custom table and specify fields that you want to keep there. After that, with simple using of API, you can map and save them with this code:

string customTableClassName = "customtable.sampletable";

// Gets the custom table
DataClassInfo customTable = DataClassInfoProvider.GetDataClassInfo(customTableClassName);
if (customTable != null)
{
    // Creates a new custom table item
    CustomTableItem newCustomTableItem = CustomTableItem.New(customTableClassName);

    // Sets the values for the fields of the custom table (ItemText in this case)
    newCustomTableItem.SetValue("ItemText", "New text");

    // Save the new custom table record into the database
    newCustomTableItem.Insert();
}

I copied this for Kentico 11 documentation. You can find more on this link.

Best regards, Dragoljub

0 votesVote for this answer Unmark Correct answer

Recent Answers


Nadia Tahir answered on May 31, 2018 12:21

thank you Dragoljub Ilic for answer my question now how to ansert custom table code to my authorize methode you find code in my first question

0 votesVote for this answer Mark as a Correct answer

Dragoljub Ilic answered on May 31, 2018 12:42

Hi Nadia,

I will assume next:

  1. You are using Kentico custom tables feature
  2. Custom table with class name 'customtable.sampletable' already exist - You can modify it however suits you best
  3. Definition for field in that table with name 'ResultField' exist
  4. You want to save whole result output inside custom table

If you want to structure data (not to save whole result), you should create fields for that in custom table and handle them directly in the foreach loop.

var result = "<OL> \n";
foreach (string value in response_array)
{
    result = result + "<LI>" + value + " </LI> \n";
}
var customTableClassName = "customtable.sampletable";
var customTable = DataClassInfoProvider.GetDataClassInfo(customTableClassName);
if (customTable != null)
{
    var newCustomTableItem = CustomTableItem.New(customTableClassName);
    newCustomTableItem.SetValue("ResultField", result);
    newCustomTableItem.Insert();
}

Best regards, Dragoljub

0 votesVote for this answer Mark as a Correct answer

Nadia Tahir answered on May 31, 2018 13:38 (last edited on May 31, 2018 19:00)

Thank you Dragoljub

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.