How to read Post Parameters from Payment Gateway API After Successful Transaction - c#

I am using a Payment Gateway Which will send the response to the Success page. Their documentation indicates they are sending response as POST parameters. I tried to Read these parameters but i am not able to get the parameters. I had sent a mail to support and they said they will send the data from their server to our server prior redirection to success page. I implemented my code in success page. Where I should Implement and how to save these values in my code for further use.
My code is Here
protected void Page_Load(object sender, EventArgs e)
{
/// store all the posted form variables in an object to use later
response notifyresponse = new response();
notifyresponse.CreditVouchersTransactionId = Request["CreditVouchersTransactionId"];
notifyresponse.MerchantName = GetFormVariableOrNull(Request["MerchantName"]);
notifyresponse.AmountToPay = GetFormVariableOrNull(Request["AmountToPay"]);
notifyresponse.PaymentOKURL = GetFormVariableOrNull(Request["PaymentOKURL"]);
notifyresponse.OrderId = GetFormVariableOrNull(Request["OrderId"]);
notifyresponse.AmountCurrency = GetFormVariableOrNull(Request["AmountCurrency"]);
notifyresponse.PaymentType = GetFormVariableOrNull(Request["AmountType"]);
notifyresponse.PaymentStatus = GetFormVariableOrNull(Request["PaymentStatus"]);
string[] keys = Request.Form.AllKeys;
for (int i = 0; i < keys.Length; i++)
{
Session["amountpay"]=keys[i] ;
}
}
protected string GetFormVariableOrNull(object formvariable)
{
if (formvariable != null)
{
try
{
return formvariable.ToString();
}
catch (Exception ex)
{
/// log the exception in file or DB
Console.WriteLine(ex.Message);/// just for an example
return null;
}
}
else
return null;
}
Thanks

Use Fiddler, it will show you all the traffic going back forth and provide a more accurate picture as to whats going on between those transactions
https://www.telerik.com/download/fiddler
Edit
A HTTP message can be composed of two part, first is the header, the other is the body.
When making a GET request to a server, it won't contain a body. A POST request on the other hand, will. There are line breaks in between the headers and the body... I've demonstrated a POST request below

Related

Twilio Transcribe voice in ASP.NET MVC

Im trying to transcribe vocal response of a caller and programmaticaly read out the users vocal response via twilio.
So, when user initially calls to the twilio number the call gets hooked to below action method (looked in to https://www.twilio.com/docs/voice/twiml/record?code-sample=code-record-a-voicemail&code-language=C%23&code-sdk-version=5.x) of the ASP.NET MVC Application.
[HttpPost]
public TwiMLResult Welcome()
{
var response = new VoiceResponse();
try
{
response.Say("Please say your user Id, example ABC123, \n and press star when done", Say.VoiceEnum.Alice, null, Say.LanguageEnum.EnGb);
// record and transcribe users voice
response.Record(
transcribe: true,
transcribeCallback: new Uri("https://35eb31e3.ngrok.io/Ivr/HandleTranscribedVrn"),
finishOnKey: "*");
response.Say("I did not receive a recording");
}
catch (Exception e)
{
ErrorLog.LogError(e, "Error within ivr/Welcome");
response = RejectCall();
}
return TwiML(response);
}
Note - https://35eb31e3.ngrok.io/Ivr/HandleTranscribedVrn is the ngRok tunneled public URL to call back method.
So, Im trying to record the users voice input after user says his/her user Id and then presses * key. So, after pressing * , I expect twilio to transcribe and respond to below callback action method (https://35eb31e3.ngrok.io/Ivr/HandleTranscribedVrn) with the transcription text and other transcribed information.
[HttpPost]
public TwiMLResult HandleTranscribedVrn()
{
var response = new VoiceResponse();
try
{
// get the transcribed result - https://www.twilio.com/docs/voice/twiml/record#transcribe
var result = new TranscribedResult
{
TranscriptionSid = Request.Params["TranscriptionSid"],
TranscriptionText = Request.Params["TranscriptionText"],
TranscriptionUrl = Request.Params["TranscriptionUrl"],
TranscriptionStatus = Request.Params["TranscriptionStatus"],
RecordingSid = Request.Params["RecordingSid"],
RecordingUrl = Request.Params["RecordingUrl"],
AccountSid = Request.Params["AccountSid"]
};
// reading the transcibed result
response.Say("You said,\n {0}", result.TranscriptionText);
// done
response.Say("Good Bye", Say.VoiceEnum.Alice, null, Say.LanguageEnum.EnGb);
}
catch (Exception e)
{
ErrorLog.LogError(e, "Error within ivr/HandleTranscribedVrn");
response.Say(ConversationHelper.NothingReceived, ConversationHelper.SpeakVoice, 1, ConversationHelper.SpeakLanguage);
}
return TwiML(response);
}
In brief, I want above callback action to grab the transcript to user voice input and read it out, like
You said, {Users Voice Transcript - example - abc123}, Good Bye
The Problem
When user calls to twilio number it executes Welcome() action controller, and says
"Please say your user Id, example ABC123, \n and press star when done"
The user says his/her user Id - EFG456 and presses * key as usual.
Then it again says (infinitely till user disconnects call), without going to transcribed call back action - HandleTranscribedVrn - "Please say your user Id, example ABC123, \n and press star when done"
Any help will be much appreciated.
With the help of the Twilio support we managed to find this solution. So, instead of <record> we have to use <gather> feature provided by Twilio. On gather we could either use speech, DTMF tones (keyboard inputs) or both.
The gather complete callback method will be executed when the speech transcription is ready. More information can be found on https://www.twilio.com/docs/voice/twiml/gather
Below is the sample code. Hope it would be helpful to anyone who faces a similar issue.
[HttpPost]
public ActionResult Welcome()
{
var response = new VoiceResponse();
try
{
var gatherOptionsList = new List<Gather.InputEnum>
{
Gather.InputEnum.Speech,
//Gather.InputEnum.Dtmf
};
var gather = new Gather(
input: gatherOptionsList,
timeout: 60,
finishOnKey:"*",
action: Url.ActionUri("OnGatherComplete", "Ivr")
);
gather.Say("Please say \n", Say.VoiceEnum.Alice, 1, Say.LanguageEnum.EnGb);
response.Append(gather);
}
catch (Exception e)
{
ErrorLog.LogError(e, "Error within ivr/Welcome");
}
return TwiML(response);
}
[HttpPost]
public TwiMLResult OnGatherComplete(string SpeechResult, double Confidence)
{
var response = new VoiceResponse();
try
{
var identifyingConfidence = Math.Round(Confidence * 100, 2);
var transcript = $"You said {SpeechResult} with Confidence {identifyingConfidence}.\n Good Bye";
var say = new Say(transcript);
response.Append(say);
}
catch (Exception e)
{
ErrorLog.LogError(e, "Error within ivr/OnGatherComplete");
}
return TwiML(response);
}

C# Connecting to Rest Service to retrieve information

I’m relatively new to c# and working with API’s. I’ve created a simple windows form in VS and I’m trying to connect t a rest service to retrieve information based on a search condition (e.g ID number) and display everything in a data grid. I’ve been looking for examples of what I’m trying to achieve with very little success. The idea is to enable a user to enter an ID number inside a text box and click a “search” button which will then connect to the rest service and retrieve all the information related to that specific ID number and display it all in a data grid with column names. Is this possible? Can anyone advise me on how to establish my connection to the rest service?
Try this. Path1 is your connection:
The Class ApiResult is just a class with a List of ArticleApiModel.
public static List<ArticleApiModel> GetArticles (int id)
{
try
{
var task = Task<List<ArticleApiModel>>.Run(async () =>
{
using (HttpClient client = new HttpClient())
{
var response = await client.GetAsync(path1 + "/api/articles/",id);
if (response != null)
{
var jsonString = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<ApiResult>(jsonString);
return result.Result;
}
}
return null;
});
task.Wait();
return task.Result;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return null;
}

c# get post form data sent from browser by socket sometimes socket closes.why?

I have webserver receive data by async sockets:
var e = new SocketAsyncEventArgs();
e.Completed += new EventHandler<SocketAsyncEventArgs>(e_Completed);
while (true)
{ allDone.Reset();
mySocket.AcceptAsync(e);
allDone.WaitOne();
}
and the other method:
public void e_Completed(object sender, SocketAsyncEventArgs e)
{
var socket = (Socket)sender;
ThreadPool.QueueUserWorkItem(handleTcpRequest, e.AcceptSocket);
e.AcceptSocket = null;
socket.AcceptAsync(e);
}
this is the handleTcpRequest method.in this part I receive data from socket and do operation:
public void handleTcpRequest(object state)
{
string sBuffer = "";
string BufferTotal = "";
byte[] secureMessage;
Byte[] bReceive = new Byte[1024];
var mySocket = (Socket)state;
do
{
try
{
firstBufferRead = mySocket.Receive(bReceive, bReceive.Length, 0);
}
catch (Exception ex)
{
Console.WriteLine("Error Occurred (:))) " + ex.Message);
}
sBuffer += Encoding.GetEncoding(1252).GetString(bReceive, 0, firstBufferRead);
BufferTotal += Encoding.UTF8.GetString(bReceive, 0, firstBufferRead);
} while (mySocket.Available != 0);
.
.
.
.
mySocket.Close();
}
whats wrong?
sometimes connection resets and closes. this happens when distance is far or post data not multipart. but in multipart happens rarely. more with forms not in multipart.
when and where should I close socket?
when I work with socket in handleTcpRequest method its local. isn't it correct? I can't find the origin of the problem
The only way to know that you've received everything in a HTTP request is to understand the HTTP request. And to understand a HTTP request you have to choices:
Use a complete HTTP server
Create a HTTP parser
The reason to why your code fails for multi-part data is probably because the other party sends one part at a time, which means that your code manages to do a mySocket.Available != 0 before the rest is sent.
If you want to do the latter you have to read the HTTP header (in format headerName: headervalue, do note that there are also white space rules that you have to consider). Search for a header named content-length, parse it's value as an integer. Then wait for two line feeds in a row (\r\n\r\n). Finally start count bytes until you have received the number of bytes that was specified in the content-length header.
ohh.. ony more thing.. pray to God that Transfer-Encoding: Chunkedis not used.
My advice is that you give up on using sockets directly as it's apparent that you don't understand how they work or how to do research on them.
If response has a header of Connection: Close, then the socket closes automatically.

Trying to check if disconnected using System.Net.WebRequest

I'm developing a website in asp.net and c# which needs to catch if the user isn't connected when they press a button. So basically, if the user is connected, it will load up the GetList function, and if not a message will appear.
Code so far is...
protected void btnAlphabetical_Click(object sender, EventArgs e)
{
Session["Online"] = 0;
CheckConnect();
if ((int)Session["Online"] == 1) { GetList(); }
if ((int)Session["Online"] == 0) { Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "alertMessage", "alert('You are currently offline')", true); }
}
protected void CheckConnect()
{
System.Uri Url = new System.Uri("http://www.mywebsite.com/apicture.jpg?" + DateTime.Now);
System.Net.WebRequest WebReq;
System.Net.WebResponse Resp;
WebReq = System.Net.WebRequest.Create(Url);
try
{
Resp = WebReq.GetResponse();
Resp.Close();
WebReq = null;
Session["Online"] = 1;
}
catch
{
WebReq = null;
Session["Online"] = 0;
}
}
Basically, we set our Online session value to 0 and call the CheckConnect function. This gets a jpg that's already on the website and, if it can be loaded, sets our Online variable to 1. If it can't find it, it sets it to 0. When control returns to the main function (a button), we progress depending on what Online is - 1 or 0.
Trouble is, and I'm unsure whether this is more to do with my system settings than anything:
when we're online and getting something that DOES exist it works fine and GetList is fired
when we're online and getting something that DOESN'T exist (an invalid URL) it works fine and our message appears (GetList isn't fired)
HOWEVER, when we're offline and fire it, my browser (IE8) just goes to the regular "diagnose connection settings" screen
Is this my code, or part of IE8 in general? I can't use another browser as it's the one my company uses.
Thanks.
EDIT - the general purpose of this is to be used on mobile devices. The user will load up the page and make changes, then use the button. If connection is lost between the page being loaded and the user pressing the button, I don't want the user to lose their changes.

Message Box popping on wrong page wp7

I have a page which is fetching data from a webservice using async call.
If i get the response from webservice control goes to catch where a message box is pooped.
The code is given below:
string uri = "http://free.worldweatheronline.com/feed/weather.ashx?key=b7d3b5ed25080109113008&q=Mumbai&num_of_days=5";
UriBuilder fullUri = new UriBuilder("http://free.worldweatheronline.com/feed/weather.ashx");
fullUri.Query = "key=b7d3b5ed25080109113008&q=Mumbai&num_of_days=5";
HttpWebRequest forecastRequest = (HttpWebRequest)WebRequest.Create(fullUri.Uri);
// set up the state object for the async request
ForecastUpdateState forecastState = new ForecastUpdateState();
forecastState.AsyncRequest = forecastRequest;
// start the asynchronous request
forecastRequest.BeginGetResponse(new AsyncCallback(HandleForecastResponse), forecastState);
This part is response
private void HandleForecastResponse(IAsyncResult asyncResult)
{
try
{
// get the state information
ForecastUpdateState forecastState = (ForecastUpdateState)asyncResult.AsyncState;
HttpWebRequest forecastRequest = (HttpWebRequest)forecastState.AsyncRequest;
// end the async request
forecastState.AsyncResponse = (HttpWebResponse)forecastRequest.EndGetResponse(asyncResult);
Stream streamResult;
string newCityName = "";
//int newHeight = 0;
// get the stream containing the response from the async call
streamResult = forecastState.AsyncResponse.GetResponseStream();
// load the XML
XElement xmlWeather = XElement.Load(streamResult);
}
catch (Exception ex)
{
MessageBox.Show("Connection Error");
}
}
Problem :
when the page is loaded it starts fetching data from webservice(consider the case when the web service is not responding and control goes to catch part).
In the mean time if we press the back button or navigate the page the message box popps on the new page.
How could i stop that.
Thanks and Regards
Haven't tested it, but it may work:
1/ Store the value of the NavigationService.CurrentSource property somewhere it can be retrieved (the best would be in the asyncState parameter, but a property may work as well
2/ In the HandleForecastResponse, compare the old and new value of the NavigationService.CurrentSource. This way, you should be able to deduce if the active page has changed or not.
ifixed that problem by add
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
});
try this
private void HandleForecastResponse(IAsyncResult asyncResult)
{
try
{
// get the state information
ForecastUpdateState forecastState = (ForecastUpdateState)asyncResult.AsyncState;
HttpWebRequest forecastRequest = (HttpWebRequest)forecastState.AsyncRequest;
// end the async request
forecastState.AsyncResponse = (HttpWebResponse)forecastRequest.EndGetResponse(asyncResult);
Stream streamResult;
string newCityName = "";
//int newHeight = 0;
// get the stream containing the response from the async call
streamResult = forecastState.AsyncResponse.GetResponseStream();
// load the XML
XElement xmlWeather = XElement.Load(streamResult);
}
catch (Exception ex)
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Connection Error");
});
}
}
Finally solved it.
catch (Exception x)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
var currentPage = ((App)Application.Current).RootFrame.Content as PhoneApplicationPage;
if ((currentPage.ToString()).Equals("MumbaiMarathon.Info.News"))
{
MessageBox.Show("Connection Error");
}
});
}
I just checked at the time of popping the message box the name of the Current UI application page. If its same as the page from which message box is initiated than it pops otherwise not.

Categories