public static bool STKPush(MpesaModel p, string bearer) { bool flag = false; //serialize the LipaMpesa class object string json = JsonConvert.SerializeObject(p); //StringContent stringcontent = new StringContent(json, Encoding.UTF8, "application/json"); var url = new Uri("https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"); try { //send post request using bytes HttpWebRequest httpWreq = (HttpWebRequest)WebRequest.Create(url); //HTTPAuthorization Header string Format string authorize = string.Format("{0} {1}", "Bearer", bearer); //Since i'll be working byte[] data = Encoding.UTF8.GetBytes(json); httpWreq.Method = "POST"; httpWreq.ContentType = "application/json"; httpWreq.Headers[HttpRequestHeader.Authorization] = authorize; using (var stream = httpWreq.GetRequestStream()) { // stream.Write(data, 0, data.Length); // // HttpWebResponse response = (HttpWebResponse)httpWreq.GetResponse(); string s = response.ToString(); StreamReader reader = new StreamReader(response.GetResponseStream()); string temp; string jsonresponse = "b"; while ((temp = reader.ReadLine()) != null) { jsonresponse += temp; } int b = 10; } return(true); } catch (WebException e) { using (WebResponse response = e.Response) { HttpWebResponse httpResponse = (HttpWebResponse)response; Console.WriteLine("Error code: {0}", httpResponse.StatusCode); using (Stream data = response.GetResponseStream()) using (var reader = new StreamReader(data)) { string text = reader.ReadToEnd(); Console.WriteLine(text); } } return(flag); } }
private static void NewMethod(MpesaModel b) { b.BusinessShortCode = "174379"; b.PartyA = "254703567954"; b.PartyB = "174379"; b.TransactionType = "CustomerPayBillOnline"; b.Amount = "70"; b.AccountReference = "ref"; b.TransactionDesc = "Kupiga sherehe"; b.CallBackURL = "http://ed2542f8.ngrok.io/confirm"; b.Timestamp = DateTime.UtcNow.ToString("yyyyMMddHHmmss"); b.Password = LipaMpesaPassword.Password("bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", "174379", b.Timestamp); b.PhoneNumber = "254703567954"; }
static void Main(string[] args) { Console.WriteLine("DR. Stickman Enterprise...!"); HTTP_REQUEST requests = new HTTP_REQUEST(); string basic = Authentication.Auth(); Console.WriteLine("we on"); string bearertoken = requests.SendRequest(basic); string certificate = @"C:\Users\derob\Desktop\cert.cer"; MpesaModel b = new MpesaModel(); RegisterURL url = new RegisterURL(); NewMethod(b); //call the mpesa api Lipa online no need for Validation Url since money to be credited is hard coded. LipaMpesa.STKPush(b, bearertoken); //breakpoint halt var date = DateTime.Now.ToString("yyyyMMddHHmmss"); //var h = Security.encryptInitiatorPassword(certificate, "der"); }