Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Web.Script.Serialization;
- using System.Windows.Forms;
- using System.Threading;
- namespace WindowsFormsApp1
- {
- public partial class Form1 : Form
- {
- private static ManualResetEvent allDone = new ManualResetEvent(false);
- static byte[] byteArray = null;
- static string transactioninfo = string.Empty;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- Transaction t = new Transaction();
- t.branch = "PagoEnLinea";
- t.description = "Test";
- t.entity = "GBCSAprovechamiento";
- t.purchaseid = DateTime.Now.ToString("ddMMyyyyHHmmss");
- t.total = "112345";
- t.version = "2";
- // Ejemplo IOnicial String res=Pagar(t);
- String res = Llamar_Pagar(t);
- }
- public class Transaction
- {
- public string purchaseid { get; set; }
- public string total { get; set; }
- public string version { get; set; }
- public string entity { get; set; }
- public string branch { get; set; }
- public string description { get; set; }
- public Transaction()
- {
- purchaseid = "";
- total = "";
- version = "";
- entity = "";
- branch = "";
- description = "";
- }
- }
- public class nResponse
- {
- public int error { get; set; }
- public string errorDesc { get; set; }
- public string timeIni { get; set; }
- public DateTime _tIni { get; set; }
- public string timeEnd { get; set; }
- public DateTime _tEnd { get; set; }
- public string nData { get; set; }
- public nResponse()
- {
- error = 0;
- errorDesc = "";
- timeIni = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
- timeEnd = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
- nData = "";
- }
- }
- public string Llamar_Pagar(Transaction obj)
- {
- string strRegresa = string.Empty;
- // Declaración de Variables
- string respuesta = "";
- string url = "https://pcservicebsd.azurewebsites.net/frm/pago";
- string username = "CompraC&A";
- string password = "FgtyuO98Dca!=";
- //Preparacion de Datos a Enviar.
- JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
- string datos = String.Concat("transactioninfo:", jsSerializer.Serialize(obj));
- transactioninfo = datos;
- byteArray = Encoding.UTF8.GetBytes(transactioninfo);
- //Preparacion de Encabezado de solicitud
- CredentialCache mycache = new CredentialCache();
- mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
- //Preparacion de Acceso a Servicio de Pago
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- request.UserAgent = "Apache-HttpClient/4.2.1";
- request.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
- //1 request.Headers.Add("Signature", CanonicalReq);
- request.Headers.Add("SignedHeaders", "host");
- request.ProtocolVersion = HttpVersion.Version11;
- //0 request.Credentials = mycache;
- request.Headers.Add("Authorization", "Basic Q29tcHJhQyZBOkZndHl1Tzk4RGNhIT0="); // "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username + ":" + password)));
- request.ContentType = "application/x-www-form-urlencoded";
- request.Method = "POST";
- //0 request.ContentLength = transactioninfo.Length;
- //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
- allDone.WaitOne();
- return strRegresa;
- }
- private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
- {
- HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
- // End the operation
- Stream postStream = request.EndGetRequestStream(asynchronousResult);
- /*
- Console.WriteLine("Please enter the input data to be posted:");
- string postData = Console.ReadLine();
- // Convert the string into a byte array.
- byte[] byteArray = Encoding.UTF8.GetBytes(postData);
- // Write to the request stream.
- postStream.Write(byteArray, 0, postData.Length);
- */
- postStream.Write(byteArray, 0, transactioninfo.Length);
- postStream.Close();
- // Start the asynchronous operation to get the response
- request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
- }
- private static void GetResponseCallback(IAsyncResult asynchronousResult)
- {
- HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
- // End the operation
- HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
- Stream streamResponse = response.GetResponseStream();
- StreamReader streamRead = new StreamReader(streamResponse);
- string responseString = streamRead.ReadToEnd();
- Console.WriteLine(responseString);
- // Close the stream object
- streamResponse.Close();
- streamRead.Close();
- // Release the HttpWebResponse
- response.Close();
- allDone.Set();
- }
- public string Pagar(Transaction obj)
- {
- JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
- string respuesta = "";
- string url = "https://pcservicebsd.azurewebsites.net/frm/pago";
- //url = "http://localhost:64987/frm/pago";
- string username = "CompraC&A";
- string password = "FgtyuO98Dca!=";
- CredentialCache mycache = new CredentialCache();
- try
- {
- string datos = String.Concat("transactioninfo:", jsSerializer.Serialize(obj));
- mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
- WebRequest request = WebRequest.Create(url);
- request.Credentials = mycache;
- request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username + ":" + password)));
- request.Method = "POST";
- string transactioninfo = datos;
- byte[] byteArray = Encoding.UTF8.GetBytes(transactioninfo);
- request.ContentType = "application/x-www-form-urlencoded";
- request.ContentLength = byteArray.Length;
- Stream dataStream = request.GetRequestStream();
- dataStream.Write(byteArray, 0, byteArray.Length);
- dataStream.Close();
- WebResponse response = request.GetResponse();
- Console.WriteLine(((HttpWebResponse)response).StatusDescription);
- using (dataStream = response.GetResponseStream())
- {
- StreamReader reader = new StreamReader(dataStream);
- respuesta = reader.ReadToEnd();
- }
- nResponse respuestaT = jsSerializer.Deserialize<nResponse>(respuesta);
- response.Close();
- //
- }
- catch (Exception msgError)
- {
- respuesta = msgError.Message;
- }
- return respuesta;
- }
- }
- }
Add Comment
Please, Sign In to add comment