public ActionResult Webhook() { // The APIContext object can contain an optional override for the trusted certificate. var apiContext = PayPalConfiguration.GetAPIContext(); // Get the received request's headers var requestheaders = HttpContext.Request.Headers; // Get the received request's body var requestBody = string.Empty; using (var reader = new System.IO.StreamReader(HttpContext.Request.InputStream)) { requestBody = reader.ReadToEnd(); } //get event data from the request body dynamic jsonBody = JObject.Parse(requestBody); string webhookId = jsonBody.id; string paypalID = jsonBody.resource.id; decimal Totalamount = (decimal)jsonBody.resource.amount.total; DateTime paypalEventDate = (DateTime)jsonBody.create_time; DateTime paypalTransDate = (DateTime)jsonBody.resource.create_time; var ev = WebhookEvent.Get(apiContext, webhookId); // We have all the information the SDK needs, so perform the validation. // Note: at least on Sandbox environment this returns false. // var isValid = WebhookEvent.ValidateReceivedEvent(apiContext, ToNameValueCollection(requestheaders), requestBody, webhookId); //add record on the Notification table //Note: remove if events are successful. Notification table is used for sms notifications. DB.addTestNotification(1, "1"); //get jobid from CUSTOM object field //int jobId = (int)jsonBody.resource.custom ; // bookingid , //PPtrans.AddPaypalNotif("1", 1, paypalEventDate, paypalTransDate, ev.event_type, Totalamount); //get job id from invoice number //PPtrans.AddPaypalNotif(paypalID, jobId, paypalEventDate, paypalTransDate, ev.event_type, Totalamount); //jobId = (int)jsonBody.resource.invoice_number; // DB.addTestNotification(jobId, paypalID); //get job description JobMain jobOrder = db.JobMains.Find(1); string clientName = jobOrder.Description; EMailHandler mail = new EMailHandler(); string siteRedirect = "https://realwheelsdavao.com/reservation/"; /* Handle transaction request from paypal webhook events * switch (ev.event_type) * { * case "PAYMENT.CAPTURE.COMPLETED": * case "PAYMENT.SALE.COMPLETED": // Handle payment completed * //record payment * AddPaymentRecord(jobId, Totalamount); * * //send mail * mail.SendMail(jobId, "*****@*****.**", "PAYMENT-SUCCESS", clientName, siteRedirect); * //mail.SendMail(jobId, "*****@*****.**", "PAYMENT-SUCCESS", clientName, siteRedirect); * //mail.SendMail(jobId, "*****@*****.**", "PAYMENT-SUCCESS", clientName, siteRedirect); * * //add to log * PPtrans.AddPaypalNotif(paypalID, jobId, paypalEventDate, paypalTransDate, ev.event_type, Totalamount); * break; * case "PAYMENT.SALE.DENIED": * case "PAYMENT.CAPTURE.DENIED": // Handle payment denied * * //send mail * mail.SendMail(jobId, "*****@*****.**", "PAYMENT-DENIED", clientName, siteRedirect); * //mail.SendMail(jobId, "*****@*****.**", "PAYMENT-DENIED", clientName, siteRedirect); * //mail.SendMail(jobId, "*****@*****.**", "PAYMENT-DENIED", clientName, siteRedirect); * * //add to log * PPtrans.AddPaypalNotif(paypalID, jobId, paypalEventDate, paypalTransDate, ev.event_type, Totalamount); * * break; * // Handle other webhooks * default: // Handle payment denied * //send mail * mail.SendMail(jobId, "*****@*****.**", "PAYMENT-PENDING", clientName, siteRedirect); * //mail.SendMail2(jobId, "*****@*****.**", "PAYMENT-PENDING", clientName, siteRedirect, ev.event_type); * //mail.SendMail(jobId, "*****@*****.**", "PAYMENT-PENDING", clientName, siteRedirect); * //mail.SendMail(jobId, "*****@*****.**", "PAYMENT-PENDING", clientName, siteRedirect); * * //add to log * PPtrans.AddPaypalNotif(paypalID, jobId, paypalEventDate, paypalTransDate, ev.event_type, Totalamount); * * break; * } */ //AddPaymentRecord(jobId, Totalamount); //add to log //PPtrans.AddPaypalNotif(paypalID, jobId, paypalEventDate, paypalTransDate, ev.event_type, Totalamount); //send mail //mail.SendMail(1, "*****@*****.**", "PAYMENT-PENDING", clientName, siteRedirect); //mail.SendMail2(jobId, "*****@*****.**", "PAYMENT-PENDING", clientName, siteRedirect, ev.event_type); //mail.SendMail(jobId, "*****@*****.**", "PAYMENT-PENDING", clientName, siteRedirect); //mail.SendMail(jobId, "*****@*****.**", "PAYMENT-PENDING", clientName, siteRedirect); return(new HttpStatusCodeResult(200)); }
public ActionResult Webhook() { // The APIContext object can contain an optional override for the trusted certificate. var apiContext = PayPalConfiguration.GetAPIContext(); // Get the received request's headers var requestheaders = HttpContext.Request.Headers; // Get the received request's body var requestBody = string.Empty; using (var reader = new System.IO.StreamReader(HttpContext.Request.InputStream)) { requestBody = reader.ReadToEnd(); } dynamic jsonBody = JObject.Parse(requestBody); string webhookId = jsonBody.id; string paypalID = jsonBody.resource.id; decimal Totalamount = (decimal)jsonBody.resource.amount.total; int jobId = (int)jsonBody.resource.invoice_number; // bookingid var ev = WebhookEvent.Get(apiContext, webhookId); // We have all the information the SDK needs, so perform the validation. // Note: at least on Sandbox environment this returns false. // var isValid = WebhookEvent.ValidateReceivedEvent(apiContext, ToNameValueCollection(requestheaders), requestBody, webhookId); // DB.addTestNotification(jobId, paypalID); //get job description JobMain jobOrder = db.JobMains.Find(jobId); string clientName = jobOrder.Description; EMailHandler mail = new EMailHandler(); switch (ev.event_type) { case "PAYMENT.CAPTURE.COMPLETED": case "PAYMENT.SALE.COMPLETED": // Handle payment completed //record payment AddPaymentRecord(jobId, Totalamount); //send mail mail.SendMail(jobId, "*****@*****.**", "PAYMENT-SUCCESS", clientName); mail.SendMail(jobId, "*****@*****.**", "PAYMENT-SUCCESS", clientName); mail.SendMail(jobId, "*****@*****.**", "PAYMENT-SUCCESS", clientName); //add to log DB.addTestNotification(jobId, paypalID); break; case "PAYMENT.SALE.DENIED": case "PAYMENT.CAPTURE.DENIED": // Handle payment denied //send mail mail.SendMail(jobId, "*****@*****.**", "PAYMENT-DENIED", clientName); mail.SendMail(jobId, "*****@*****.**", "PAYMENT-DENIED", clientName); mail.SendMail(jobId, "*****@*****.**", "PAYMENT-DENIED", clientName); //add to log DB.addTestNotification(jobId, paypalID); break; // Handle other webhooks default: // Handle payment denied //send mail mail.SendMail(jobId, "*****@*****.**", "PAYMENT-PENDING", clientName); mail.SendMail(jobId, "*****@*****.**", "PAYMENT-PENDING", clientName); mail.SendMail(jobId, "*****@*****.**", "PAYMENT-PENDING", clientName); //add to log DB.addTestNotification(jobId, paypalID); break; } return(new HttpStatusCodeResult(200)); }