public void PerformHandShake(HttpRequestBase Request)
        {
            //Read the PayPals' Instant Pay Notification (IPN) POST
            var strFormValues = Encoding.ASCII
                .GetString(Request.BinaryRead(Request.ContentLength));

            // Create the request back
            var req = PayPalServiceHelper
                .CreateRequest("https://www.paypal.com/cgi-bin/webscr");

            // Set values for the request back
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            var strNewValue = strFormValues + "&cmd=_notify-validate";
            req.ContentLength = strNewValue.Length;

            //// Write the request back IPN strings
            var stOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
            stOut.Write(strNewValue);
            stOut.Close();

            //send the request, read the response
            var strResponse = req.GetResponse();
            var IPNResponseStream = strResponse.GetResponseStream();
            var encode = System.Text.Encoding.GetEncoding("utf-8");
            var readStream = new StreamReader(IPNResponseStream, encode);

            var read = new char[256];
            var count = readStream.Read(read, 0, 256);
            string IPNResponse = new string(read, 0, count);
            if (IPNResponse == "VERIFIED")
            {
                //IPN is valid
            }
            else
            {
                //IPN is INVALID
            }

            //tidy up, close streams
            if (readStream != null) readStream.Close();
            if (strResponse != null) strResponse.Close();
        }
Пример #2
0
        private static bool IsValidIPNRequest(HttpRequestBase requestBase)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(GlobalConfig.PayPalSubmitUrl);

            //Set values for the request back
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            byte[] param = requestBase.BinaryRead(requestBase.ContentLength);
            string strRequest = System.Text.Encoding.ASCII.GetString(param);
            strRequest += "&cmd=_notify-validate";
            req.ContentLength = strRequest.Length;

            //for proxy
            //WebProxy proxy = new WebProxy(new Uri("http://*****:*****@"E:\StagingSites\tfc.tv\App_Data\Paypal.txt";
                    TextWriter w = new StreamWriter(fileFullPath, true);
                    w.WriteLine(strResponse);
                    w.Close();
                    //Log errors
                }
                catch (Exception) { }
                return false;
            }
        }
Пример #3
0
        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;
        }
Пример #4
0
        /// <summary>
        /// Validate the paypal request via postbackurl
        /// </summary>
        /// <param name="paypalRequest">the request to validate</param>
        /// <returns>string telling if paypalrequest is valid</returns>
        private string ValidateIPNRequest(HttpRequestBase paypalRequest, PaymentConstants config)
        {
            string strResponse = null;

            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(config.PostbackUrl);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                byte[] param = paypalRequest.BinaryRead(HttpContext.Current.Request.ContentLength);
                string strRequest = Encoding.ASCII.GetString(param);
                strRequest += "&cmd=_notify-validate";
                req.ContentLength = strRequest.Length;

                //Send the request to PayPal and get the response
                StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
                streamOut.Write(strRequest);
                streamOut.Close();
                StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
                strResponse = streamIn.ReadToEnd();
                streamIn.Close();

            }
            catch (Exception ex)
            {
                _Logger.Error("ValidateIPNRequest", ex);
            }

            return strResponse;
        }
Пример #5
0
        public static string IPNHandler(HttpRequestBase requestBase)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(GlobalConfig.PayPalSubmitUrl);

            //Set values for the request back
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            byte[] param = requestBase.BinaryRead(requestBase.ContentLength);
            string strRequest = System.Text.Encoding.ASCII.GetString(param);
            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
            StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
            streamOut.Write(strRequest);
            streamOut.Close();
            StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
            string strResponse = streamIn.ReadToEnd();
            streamIn.Close();
            return strResponse;
        }