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;

                PayPalStandardPaymentProcessor processor = new PayPalStandardPaymentProcessor();
                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:
                            {
                            }
                            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()));
                        }
                    }
                        #endregion
                        break;
                    }
                }
                else
                {
                    this.LogService.InsertLog(LogTypeEnum.OrderError, "PayPal IPN failed.", strRequest);
                }
            }
        }
Exemple #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            CommonHelper.SetResponseNoCache(Response);

            if (!Page.IsPostBack)
            {
                //item_id_1 or vendor_order_id
                string nopOrderIdStr = HttpContext.Current.Request.Form["item_id_1"];
                int    nopOrderId    = 0;
                int.TryParse(nopOrderIdStr, out nopOrderId);
                Order order = this.OrderService.GetOrderById(nopOrderId);
                if (order != null)
                {
                    //debug info
                    StringBuilder sbDebug = new StringBuilder();
                    sbDebug.AppendLine("2Checkout IPN:");
                    foreach (string key in HttpContext.Current.Request.Form.AllKeys)
                    {
                        string value = HttpContext.Current.Request.Form[key];
                        sbDebug.AppendLine(key + ": " + value);
                    }
                    this.OrderService.InsertOrderNote(order.OrderId, sbDebug.ToString(), false, DateTime.UtcNow);


                    bool useSandbox = this.SettingManager.GetSettingValueBoolean("PaymentMethod.TwoCheckout.UseSandbox");

                    //sale id
                    string sale_id = string.Empty;
                    if (useSandbox)
                    {
                        sale_id = "1";
                    }
                    else
                    {
                        sale_id = HttpContext.Current.Request.Form["sale_id"];
                    }
                    if (sale_id == null)
                    {
                        sale_id = string.Empty;
                    }

                    //invoice id
                    string invoice_id = HttpContext.Current.Request.Form["invoice_id"];
                    if (invoice_id == null)
                    {
                        invoice_id = string.Empty;
                    }

                    if (this.SettingManager.GetSettingValueBoolean("PaymentMethod.TwoCheckout.UseMD5Hashing"))
                    {
                        string vendorId   = this.SettingManager.GetSettingValue("PaymentMethod.TwoCheckout.VendorId");
                        string secretWord = this.SettingManager.GetSettingValue("PaymentMethod.TwoCheckout.SecretWord");

                        string compareHash1 = TwoCheckoutPaymentProcessor.CalculateMD5hash(sale_id + vendorId + invoice_id + secretWord);
                        if (String.IsNullOrEmpty(compareHash1))
                        {
                            throw new NopException("2Checkout empty hash string");
                        }
                        string compareHash2 = HttpContext.Current.Request.Form["md5_hash"];
                        if (compareHash2 == null)
                        {
                            compareHash2 = string.Empty;
                        }

                        if (compareHash1.ToUpperInvariant() != compareHash2.ToUpperInvariant())
                        {
                            this.OrderService.InsertOrderNote(order.OrderId, "Hash validation failed", false, DateTime.UtcNow);
                            Response.Redirect(CommonHelper.GetStoreLocation());
                        }
                    }

                    string message_type = HttpContext.Current.Request.Form["message_type"];
                    if (message_type == null)
                    {
                        message_type = string.Empty;
                    }
                    string invoice_status = HttpContext.Current.Request.Form["invoice_status"];
                    if (invoice_status == null)
                    {
                        invoice_status = string.Empty;
                    }
                    string fraud_status = HttpContext.Current.Request.Form["fraud_status"];
                    if (fraud_status == null)
                    {
                        fraud_status = string.Empty;
                    }
                    string payment_type = HttpContext.Current.Request.Form["payment_type"];
                    if (payment_type == null)
                    {
                        payment_type = string.Empty;
                    }

                    PaymentStatusEnum newPaymentStatus = TwoCheckoutPaymentProcessor.GetPaymentStatus(message_type, invoice_status, fraud_status, payment_type);

                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("2Checkout IPN:");
                    sb.AppendLine("sale_id: " + sale_id);
                    sb.AppendLine("invoice_id: " + invoice_id);
                    sb.AppendLine("message_type: " + message_type);
                    sb.AppendLine("invoice_status: " + invoice_status);
                    sb.AppendLine("fraud_status: " + fraud_status);
                    sb.AppendLine("payment_type: " + payment_type);
                    sb.AppendLine("New payment status: " + newPaymentStatus.GetPaymentStatusName());
                    this.OrderService.InsertOrderNote(order.OrderId, sb.ToString(), false, DateTime.UtcNow);

                    //new payment status
                    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:
                    {
                        //TODO add partial refund support
                        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
                {
                    Response.Redirect(CommonHelper.GetStoreLocation());
                }
            }
        }