protected void Page_Load(object sender, EventArgs e)
        {
            CommonHelper.SetResponseNoCache(Response);

            if (!Page.IsPostBack)
            {
                byte[] param = Request.BinaryRead(Request.ContentLength);
                string strRequest = Encoding.ASCII.GetString(param);
                Dictionary<string, string> values;

                PayPalSewbiePaymentProcessor processor = new PayPalSewbiePaymentProcessor();
                if (processor.VerifyIPN(strRequest, out values))
                {
                    #region values
                    decimal total = decimal.Zero;
                    try
                    {
                        total = decimal.Parse(values["mc_gross"], new CultureInfo("en-US"));
                    }
                    catch { }

                    string payer_status = string.Empty;
                    values.TryGetValue("payer_status", out payer_status);
                    string payment_status = string.Empty;
                    values.TryGetValue("payment_status", out payment_status);
                    string pending_reason = string.Empty;
                    values.TryGetValue("pending_reason", out pending_reason);
                    string mc_currency = string.Empty;
                    values.TryGetValue("mc_currency", out mc_currency);
                    string txn_id = string.Empty;
                    values.TryGetValue("txn_id", out txn_id);
                    string txn_type = string.Empty;
                    values.TryGetValue("txn_type", out txn_type);
                    string rp_invoice_id = string.Empty;
                    values.TryGetValue("rp_invoice_id", out rp_invoice_id);
                    string payment_type = string.Empty;
                    values.TryGetValue("payment_type", out payment_type);
                    string payer_id = string.Empty;
                    values.TryGetValue("payer_id", out payer_id);
                    string receiver_id = string.Empty;
                    values.TryGetValue("receiver_id", out receiver_id);
                    string invoice = string.Empty;
                    values.TryGetValue("invoice", out invoice);
                    string payment_fee = string.Empty;
                    values.TryGetValue("payment_fee", out payment_fee);

                    #endregion

                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("Paypal IPN:");
                    foreach (KeyValuePair<string, string> kvp in values)
                    {
                        sb.AppendLine(kvp.Key + ": " + kvp.Value);
                    }

                    PaymentStatusEnum newPaymentStatus = PaypalHelper.GetPaymentStatus(payment_status, pending_reason);
                    sb.AppendLine("New payment status: " + newPaymentStatus.GetPaymentStatusName());

                    switch (txn_type)
                    {
                        case "recurring_payment_profile_created":
                            //do nothing here
                            break;
                        case "recurring_payment":
                            #region Recurring payment
                            {
                                Guid orderNumberGuid = Guid.Empty;
                                try
                                {
                                    orderNumberGuid = new Guid(rp_invoice_id);
                                }
                                catch
                                {
                                }

                                Order initialOrder = this.OrderService.GetOrderByGuid(orderNumberGuid);
                                if (initialOrder != null)
                                {
                                    var recurringPayments = this.OrderService.SearchRecurringPayments(0, initialOrder.OrderId, null);
                                    foreach (var rp in recurringPayments)
                                    {
                                        switch (newPaymentStatus)
                                        {
                                            case PaymentStatusEnum.Authorized:
                                            case PaymentStatusEnum.Paid:
                                                {
                                                    var recurringPaymentHistory = rp.RecurringPaymentHistory;
                                                    if (recurringPaymentHistory.Count == 0)
                                                    {
                                                        //first payment
                                                        var rph = new RecurringPaymentHistory()
                                                        {
                                                            RecurringPaymentId = rp.RecurringPaymentId,
                                                            OrderId = initialOrder.OrderId,
                                                            CreatedOn = DateTime.UtcNow
                                                        };
                                                        this.OrderService.InsertRecurringPaymentHistory(rph);
                                                    }
                                                    else
                                                    {
                                                        //next payments
                                                        this.OrderService.ProcessNextRecurringPayment(rp.RecurringPaymentId);
                                                        //UNDONE change new order status according to newPaymentStatus
                                                        //UNDONE refund/void is not supported
                                                    }
                                                }
                                                break;
                                        }
                                    }

                                    //this.OrderService.InsertOrderNote(newOrder.OrderId, sb.ToString(), DateTime.UtcNow);
                                    this.LogService.InsertLog(LogTypeEnum.Unknown, "PayPal IPN. Recurring info", new NopException(sb.ToString()));
                                }
                                else
                                {
                                    this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN. Order is not found", new NopException(sb.ToString()));
                                }
                            }
                            #endregion
                            break;
                        default:
                            #region Standard payment
                            {
                                string orderNumber = string.Empty;
                                values.TryGetValue("custom", out orderNumber);
                                Guid orderNumberGuid = Guid.Empty;
                                try
                                {
                                    orderNumberGuid = new Guid(orderNumber);
                                }
                                catch
                                {
                                }

                                Order order = this.OrderService.GetOrderByGuid(orderNumberGuid);
                                if (order != null)
                                {
                                    this.OrderService.InsertOrderNote(order.OrderId, sb.ToString(), false, DateTime.UtcNow);
                                    switch (newPaymentStatus)
                                    {
                                        case PaymentStatusEnum.Pending:
                                            {
                                                this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN. Order Pending", new NopException(sb.ToString()));
                                            }
                                            break;
                                        case PaymentStatusEnum.Authorized:
                                            {
                                                this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN. Order " + order.OrderId.ToString() + " is Authorized", new NopException(sb.ToString()));
                                                if (this.OrderService.CanMarkOrderAsAuthorized(order))
                                                {
                                                    this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN. Order " + order.OrderId.ToString() + " can be Authorized", new NopException(sb.ToString()));
                                                    this.OrderService.MarkAsAuthorized(order.OrderId);
                                                }
                                            }
                                            break;
                                        case PaymentStatusEnum.Paid:
                                            {
                                                this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN. Order " + order.OrderId.ToString() + " is Paid", new NopException(sb.ToString()));
                                                if (this.OrderService.CanMarkOrderAsPaid(order))
                                                {
                                                    this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN. Order " + order.OrderId.ToString() + " can be Paid", new NopException(sb.ToString()));
                                                    this.OrderService.MarkOrderAsPaid(order.OrderId);
                                                }
                                            }
                                            break;
                                        case PaymentStatusEnum.Refunded:
                                            {
                                                this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN. Order " + order.OrderId.ToString() + " is Refunded", new NopException(sb.ToString()));
                                                if (this.OrderService.CanRefundOffline(order))
                                                {
                                                    this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN. Order " + order.OrderId.ToString() + " has been refunded offline.", new NopException(sb.ToString()));
                                                    this.OrderService.RefundOffline(order.OrderId);
                                                }
                                            }
                                            break;
                                        case PaymentStatusEnum.Voided:
                                            {
                                                this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN. Order " + order.OrderId.ToString() + " is void", new NopException(sb.ToString()));
                                                if (this.OrderService.CanVoidOffline(order))
                                                {
                                                    this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN. Order " + order.OrderId.ToString() + " has been voided offline.", new NopException(sb.ToString()));
                                                    this.OrderService.VoidOffline(order.OrderId);
                                                }
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                                else
                                {
                                    this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN. Order is not found", new NopException(sb.ToString()));
                                }
                            }
                            #endregion
                            break;
                    }
                }
                else
                {
                    this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN failed.", strRequest);
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            CommonHelper.SetResponseNoCache(Response);

            if (!Page.IsPostBack)
            {
                byte[] param = Request.BinaryRead(Request.ContentLength);
                string strRequest = Encoding.ASCII.GetString(param);
                Dictionary<string, string> values;

                PayPalSewbiePaymentProcessor processor = new PayPalSewbiePaymentProcessor();
                if (processor.VerifyIPN(strRequest, out values))
                {
                    #region values
                    decimal total = decimal.Zero;
                    try
                    {
                        total = decimal.Parse(values[Server.UrlEncode("transaction[0].amount")].Split("+".ToCharArray())[1], new CultureInfo("en-US"));
                    }
                    catch { }//value could not be converted to an decimal.

                    string transaction_type = string.Empty;
                    values.TryGetValue(Server.UrlEncode("transaction_type"), out transaction_type);
                    string status = string.Empty;
                    values.TryGetValue(Server.UrlEncode("status"), out status);
                    string sender_email = string.Empty;
                    values.TryGetValue(Server.UrlEncode("sender_email"), out sender_email);
                    string action_type = string.Empty;
                    values.TryGetValue(Server.UrlEncode("action_type"), out action_type);
                    string payment_request_date = string.Empty;
                    values.TryGetValue(Server.UrlEncode("payment_request_date"), out payment_request_date);
                    string reverse_all_parallel_payments_on_error = string.Empty;
                    values.TryGetValue(Server.UrlEncode("reverse_all_parallel_payments_on_error"), out reverse_all_parallel_payments_on_error);

                    //transaction level info.
                    string transaction_0_id = string.Empty;
                    values.TryGetValue(Server.UrlEncode("transaction[0].id"), out transaction_0_id);
                    string transaction_0_status = string.Empty;
                    values.TryGetValue(Server.UrlEncode("transaction[0].status"), out transaction_0_status);
                    string transaction_0_id_for_sender = string.Empty;
                    values.TryGetValue(Server.UrlEncode("transaction[0].id_for_sender"), out transaction_0_id_for_sender);
                    string transaction_0_status_for_sender_txn = string.Empty;
                    values.TryGetValue(Server.UrlEncode("transaction[0].status_for_sender_txn"), out transaction_0_status_for_sender_txn);
                    string transaction_0_receiver = string.Empty;
                    values.TryGetValue(Server.UrlEncode("transaction[0].receiver"), out transaction_0_receiver);
                    string transaction_0_invoiceId = string.Empty;
                    values.TryGetValue(Server.UrlEncode("transaction[0].invoiceId"), out transaction_0_invoiceId);
                    string transaction_0_amount = string.Empty;
                    values.TryGetValue(Server.UrlEncode("transaction[0].amount"), out transaction_0_amount);
                    string transaction_0_is_primary_receiver = string.Empty;
                    values.TryGetValue(Server.UrlEncode("transaction[0].is_primary_receiver"), out transaction_0_is_primary_receiver);

                    string return_url = string.Empty;
                    values.TryGetValue(Server.UrlEncode("return_url"), out return_url);
                    string cancel_url = string.Empty;
                    values.TryGetValue(Server.UrlEncode("cancel_url"), out cancel_url);
                    string ipn_notification_url = string.Empty;
                    values.TryGetValue(Server.UrlEncode("ipn_notification_url"), out ipn_notification_url);
                    string pay_key = string.Empty;
                    values.TryGetValue(Server.UrlEncode("pay_key"), out pay_key);
                    string memo = string.Empty;
                    values.TryGetValue(Server.UrlEncode("memo"), out memo);
                    string fees_payer = string.Empty;
                    values.TryGetValue(Server.UrlEncode("fees_payer"), out fees_payer);
                    string tracking_id = string.Empty;
                    values.TryGetValue(Server.UrlEncode("tracking_id"), out tracking_id);
                    string preapproval_key = string.Empty;
                    values.TryGetValue(Server.UrlEncode("preapproval_key"), out preapproval_key);
                    string reason_code = string.Empty;
                    values.TryGetValue(Server.UrlEncode("reason_code"), out reason_code);

                    #endregion

                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("Paypal IPN:");
                    foreach (KeyValuePair<string, string> kvp in values)
                    {
                        sb.AppendLine(kvp.Key + ": " + kvp.Value);
                    }

                    Guid orderNumberGuid = Guid.Empty;
                    Order order;
                    PaymentStatusEnum newPaymentStatus = PaypalAPHelper.GetPaymentStatus(status);
                    sb.AppendLine("New payment status: " + newPaymentStatus.GetPaymentStatusName());

                    switch (Server.UrlDecode(transaction_type))
                    {
                        case "Adaptive Payment PAY":
                            //should be the only case.
                            #region Adaptive Pay
                                try
                                {
                                    orderNumberGuid = new Guid(tracking_id);
                                }
                                catch
                                {
                                }

                                order = this.OrderService.GetOrderByGuid(orderNumberGuid);
                                if (order != null)
                                {
                                    this.OrderService.InsertOrderNote(order.OrderId, sb.ToString(), false, DateTime.UtcNow);

                                    switch (newPaymentStatus)
                                    {
                                        case PaymentStatusEnum.Pending:
                                            {
                                            }
                                            break;
                                        case PaymentStatusEnum.Authorized:
                                            {
                                                if (this.OrderService.CanMarkOrderAsAuthorized(order))
                                                {
                                                    this.OrderService.MarkAsAuthorized(order.OrderId);
                                                }
                                            }
                                            break;
                                        case PaymentStatusEnum.Paid:
                                            {
                                                if (this.OrderService.CanMarkOrderAsPaid(order))
                                                {
                                                    this.OrderService.MarkOrderAsPaid(order.OrderId);
                                                }
                                            }
                                            break;
                                        case PaymentStatusEnum.Refunded:
                                            {
                                                if (this.OrderService.CanRefundOffline(order))
                                                {
                                                    this.OrderService.RefundOffline(order.OrderId);
                                                }
                                            }
                                            break;
                                        case PaymentStatusEnum.Voided:
                                            {
                                                if (this.OrderService.CanVoidOffline(order))
                                                {
                                                    this.OrderService.VoidOffline(order.OrderId);
                                                }
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                                else
                                {
                                    this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN. Order is not found", new NopException(sb.ToString()));
                                }
                            break;
                            #endregion
                        case "Adjustment":
                            //Do nothing for now.
                            //switch (Server.UrlDecode(reason_code).ToLowerInvariant())
                            //{
                            //    case "chargeback settlement":
                            //        {
                            //            if (this.OrderService.CanRefundOffline(order))
                            //            {
                            //                this.OrderService.RefundOffline(order.OrderId);
                            //            }
                            //        }
                            //        break;
                            //    case "refund":
                            //        {
                            //            if (this.OrderService.CanRefundOffline(order))
                            //            {
                            //                this.OrderService.RefundOffline(order.OrderId);
                            //            }
                            //        }
                            //        break;
                            //    case "admin reversal":
                            //        {
                            //            if (this.OrderService.CanRefundOffline(order))
                            //            {
                            //                this.OrderService.RefundOffline(order.OrderId);
                            //            }
                            //        }
                            //        break;
                            //}
                            break;
                    }
                }
                else
                {
                    this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN failed.", strRequest);
                }
            }
        }