public IpnResult Ipn(HttpRequestBase request) { var result = new IpnResult(); string encoded = request.Params["encoded"]; string checksum = request.Params["checksum"]; if (checksum == calculateHmac(encoded)) { string response = ""; string data = Encoding.UTF8.GetString(Convert.FromBase64String(encoded)); foreach (string line in data.Split('\n')) { Match match = Regex.Match(line, @"/^INVOICE=(\d+):STATUS=(PAID|DENIED|EXPIRED)(:PAY_TIME=(\d+):STAN=(\d+):BCODE=([0-9a-zA-Z]+))?$/"); if (match.Success) { string invoice = match.Groups[1].Value; string status = match.Groups[2].Value; paymentHelper.LogPayment(Convert.ToInt32(invoice), this.GetType().Name, status == "PAID" ? PaymentStatus.Completed : PaymentStatus.Failed, null, line); response += "INVOICE=" + invoice + ":STATUS=OK\n"; } } result.Response = response; } else { result.Response = "ERR=Not valid CHECKSUM\n"; } return(result); }
public IpnResult Ipn(HttpRequestBase request) { var result = new IpnResult(); string postUrl = settings.TestMode ? "https://www.sandbox.paypal.com/cgi-bin/webscr" : "https://www.paypal.com/cgi-bin/webscr"; //Post back to either sandbox or live var req = (HttpWebRequest) WebRequest.Create(postUrl); //Set values for the request back req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; byte[] param = request.BinaryRead(request.ContentLength); string strRequest = Encoding.ASCII.GetString(param); string strResponse_copy = strRequest; //Save a copy of the initial info sent by PayPal strRequest += "&cmd=_notify-validate"; req.ContentLength = strRequest.Length; //for proxy //WebProxy proxy = new WebProxy(new Uri("http://url:port#")); //req.Proxy = proxy; //Send the request to PayPal and get the response var streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII); streamOut.Write(strRequest); streamOut.Close(); var streamIn = new StreamReader(req.GetResponse().GetResponseStream()); string strResponse = streamIn.ReadToEnd(); streamIn.Close(); if (strResponse == "VERIFIED") { //check the payment_status is Completed //check that txn_id has not been previously processed //check that receiver_email is your Primary PayPal email //check that payment_amount/payment_currency are correct //process payment // pull the values passed on the initial message from PayPal NameValueCollection args = HttpUtility.ParseQueryString(strResponse_copy); var orderId = Convert.ToInt32(args["custom"]); var amount = Convert.ToDecimal(args["mc_gross"]); PaymentStatus status; string paymentNotes; if (args["receiver_email"] != settings.Email) { paymentNotes = string.Format("PayPal recipient expected was {0} but received {1}", settings.Email, args["receiver_email"]); status = PaymentStatus.ManualReview; } else if (args["test_ipn"] == "1" && !settings.TestMode) { paymentNotes = string.Format("PayPal IPN was in test mode but site is not"); status = PaymentStatus.Failed; } else if (args["mc_currency"] != settingService.Get<string>(SettingField.CurrencyCode)) { paymentNotes = string.Format("Expected currency {0} but received {1}", settingService.Get<string>(SettingField.CurrencyCode), args["mc_currency"]); status = PaymentStatus.Failed; } else { paymentNotes = strResponse_copy; switch (args["payment_status"]) { case "Canceled_Reversal": case "Completed": case "Processed": status = PaymentStatus.Completed; break; case "Denied": case "Voided": case "Expired": case "Failed": status = PaymentStatus.Failed; break; case "Refunded": case "Reversed": status = PaymentStatus.Refunded; break; case "Pending": status = PaymentStatus.ManualReview; break; default: status = PaymentStatus.Failed; break; } } paymentHelper.LogPayment(orderId, GetType().Name, status, amount, paymentNotes); } else if (strResponse == "INVALID") { //log for manual investigation ErrorLog.GetDefault(HttpContext.Current).Log( new Error(new Exception("PayPal INVALID: " + strResponse_copy))); } else { //log response/ipn data for manual investigation ErrorLog.GetDefault(HttpContext.Current).Log( new Error(new Exception("PayPal " + strResponse + ": " + strResponse_copy))); } return result; }
public IpnResult Ipn(HttpRequestBase request) { var result = new IpnResult(); string postUrl = settings.TestMode ? "https://www.sandbox.paypal.com/cgi-bin/webscr" : "https://www.paypal.com/cgi-bin/webscr"; //Post back to either sandbox or live var req = (HttpWebRequest)WebRequest.Create(postUrl); //Set values for the request back req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; byte[] param = request.BinaryRead(request.ContentLength); string strRequest = Encoding.ASCII.GetString(param); string strResponse_copy = strRequest; //Save a copy of the initial info sent by PayPal strRequest += "&cmd=_notify-validate"; req.ContentLength = strRequest.Length; //for proxy //WebProxy proxy = new WebProxy(new Uri("http://url:port#")); //req.Proxy = proxy; //Send the request to PayPal and get the response var streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII); streamOut.Write(strRequest); streamOut.Close(); var streamIn = new StreamReader(req.GetResponse().GetResponseStream()); string strResponse = streamIn.ReadToEnd(); streamIn.Close(); if (strResponse == "VERIFIED") { //check the payment_status is Completed //check that txn_id has not been previously processed //check that receiver_email is your Primary PayPal email //check that payment_amount/payment_currency are correct //process payment // pull the values passed on the initial message from PayPal NameValueCollection args = HttpUtility.ParseQueryString(strResponse_copy); var orderId = Convert.ToInt32(args["custom"]); var amount = Convert.ToDecimal(args["mc_gross"]); PaymentStatus status; string paymentNotes; if (args["receiver_email"] != settings.Email) { paymentNotes = string.Format("PayPal recipient expected was {0} but received {1}", settings.Email, args["receiver_email"]); status = PaymentStatus.ManualReview; } else if (args["test_ipn"] == "1" && !settings.TestMode) { paymentNotes = string.Format("PayPal IPN was in test mode but site is not"); status = PaymentStatus.Failed; } else if (args["mc_currency"] != settingService.Get <string>(SettingField.CurrencyCode)) { paymentNotes = string.Format("Expected currency {0} but received {1}", settingService.Get <string>(SettingField.CurrencyCode), args["mc_currency"]); status = PaymentStatus.Failed; } else { paymentNotes = strResponse_copy; switch (args["payment_status"]) { case "Canceled_Reversal": case "Completed": case "Processed": status = PaymentStatus.Completed; break; case "Denied": case "Voided": case "Expired": case "Failed": status = PaymentStatus.Failed; break; case "Refunded": case "Reversed": status = PaymentStatus.Refunded; break; case "Pending": status = PaymentStatus.ManualReview; break; default: status = PaymentStatus.Failed; break; } } paymentHelper.LogPayment(orderId, GetType().Name, status, amount, paymentNotes); } else if (strResponse == "INVALID") { //log for manual investigation ErrorLog.GetDefault(HttpContext.Current).Log( new Error(new Exception("PayPal INVALID: " + strResponse_copy))); } else { //log response/ipn data for manual investigation ErrorLog.GetDefault(HttpContext.Current).Log( new Error(new Exception("PayPal " + strResponse + ": " + strResponse_copy))); } return(result); }