Esempio n. 1
0
        protected void processPayPalData()
        {
            // refreshing cart (should be empty as IPN should have fired)
            RefreshCart();
            authToken = Configuration.PaypalPDTToken;

            //read in txn token from querystring
            txToken = Request.QueryString.Get("tx");


            query = string.Format("cmd=_notify-synch&tx={0}&at={1}", txToken, authToken);

            // Create the request back
            string         url = Configuration.PaypalEnv;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            // Set values for the request back
            req.Method        = "POST";
            req.ContentType   = "application/x-www-form-urlencoded";
            req.ContentLength = query.Length;

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

            stOut.Write(query);
            stOut.Close();

            // Do the request to PayPal and get the response
            StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());

            strResponse = stIn.ReadToEnd();
            stIn.Close();

            // sanity check
            lblPDT.Text = strResponse;

            // If response was SUCCESS, parse response string and output details
            if (strResponse.StartsWith("SUCCESS"))
            {
                PDTData data = PDTData.Parse(strResponse, false);
                lblResult.Text = "<b>" + Resources.Lang.PayPalThankyouLabel + "</b> <br/><br/>" +
                                 Resources.Lang.SaveTransactionID + "<b>" + data.TransactionId + "</b>";
            }
            else
            {
                lblResult.Text = Resources.Lang.ErrorInPayPalLabel;
            }
        }
Esempio n. 2
0
        protected void processRequest()
        {
            try
            {
                string         postUrl = Configuration.PaypalEnv;
                HttpWebRequest 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(HttpContext.Current.Request.ContentLength);
                string strRequest = Encoding.ASCII.GetString(param);
                string ipnPost    = strRequest;
                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());
                string       strResponse = streamIn.ReadToEnd();
                streamIn.Close();

                if (strResponse == "VERIFIED")
                {
                    strRequest = HttpUtility.UrlDecode(strRequest);
                    strRequest = strRequest.Replace('&', '\n');
                    PDTData pdt = PDTData.Parse(strRequest, true);
                    //check that receiver_email is your Primary PayPal email
                    if (pdt.ReceiverEmail != Configuration.PaypalSellerEmail)
                    {
                        BasePage.Log(null, "PayPal receiver email different from default:" + pdt.ReceiverEmail + " - txnID: " + pdt.TransactionId, "", "IPN Handler");
                        return;
                    }

                    //check that payment_amount/payment_currency are correct
                    if (pdt.Currency != Configuration.PaypalCurrency)
                    {
                        BasePage.Log(null, "PayPal currency different from default:" + pdt.ReceiverEmail + " - txnID: " + pdt.TransactionId, "", "IPN Handler");
                        return;
                    }

                    //check the payment_status is Completed

                    //check that txn_id has not been previously processed
                    PAYPAL_PAYMENT       pp    = ApplicationContext.Current.Payments.GetByTxnId(pdt.TransactionId);
                    List <SHOPPING_CART> carts = ApplicationContext.Current.Carts.GetShoppingCartItems(pdt.Invoice);

                    if (pdt.PaymentStatus.ToUpper() == "COMPLETED" || pdt.PaymentStatus.ToUpper() == "PENDING" || pdt.PaymentStatus.ToUpper() == "PROCESSED")
                    {
                        // first time proccessing for this payment
                        if (pp == null && carts != null && carts.Count > 0)
                        {
                            savePayPalOrder(pdt, carts, strRequest);
                        }
                        else if (pp == null)
                        {
                            BasePage.Log(null, "PayPal " + pdt.PaymentStatus + " TxnId: " + pdt.TransactionId + " but CART EXPIRED \n Response: " + strRequest, "", "IPN Handler");
                            BasePage.sendMailToAdmins("PayPal payment " + pdt.PaymentStatus + " but cart expired. PLACE REFUND!", "A " + pdt.PaymentStatus + " IPN was notified for cart " + pdt.Invoice +
                                                      " from " + pdt.PayerEmail + " for the amount " + pdt.GrossTotal + " " + pdt.Currency + " but the cart expired. \nProceed with REFUND!");
                        }
                    }
                    else
                    {
                        //In the case of a refund, reversal, or canceled reversal
                        if (pp == null && !String.IsNullOrWhiteSpace(pdt.ParentTransactionId))
                        {
                            pp = ApplicationContext.Current.Payments.GetByTxnId(pdt.ParentTransactionId);
                        }
                    }
                    // update of existing paypal transaction
                    if (pp != null)
                    {
                        updatePayPalData(pdt, pp, strRequest);
                    }
                }
                else if (strResponse == "INVALID")
                {
                    BasePage.Log(null, "Invalid IPN request" + strRequest, "", "IPN Handler");
                }
                else
                {
                    BasePage.Log(null, "IPN request" + strRequest, "", "IPN Handler");
                }
            }
            catch (Exception ex)
            {
                BasePage.Log(ex, ex.Message, ex.StackTrace, "paypalSuccess.Save");
            }
        }