Exemple #1
0
        public ActionResult PxPay(int id)
        {
            string paidFlag = Request["paid"];

            if (paidFlag == null)
            {
                StallApplication.SysError("[MSG]pxpay callback without paid parameter");
                return(View("Error"));
            }

            StallApplication.SysInfoFormat("[MSG]PxPay call back [{0}]:{1}", paidFlag, Request.Url.ToString());
            bool isSuccess = "SUCCESS".Equals(paidFlag.ToUpper());

            string payResultId = Request["result"];

            if (string.IsNullOrEmpty(payResultId))
            {
                StallApplication.SysError("[MSG]pxpay callback without result id");
                return(View("Error"));
            }

            int paymentId = 0;

            if (!Accountant.VerifyPxPayPayment(payResultId, isSuccess, out paymentId))
            {
                StallApplication.BizErrorFormat("[MSG]PxPay not verified, result id={0}", payResultId);
                return(View("Error"));
            }

            if (paymentId != id)
            {
                StallApplication.BizErrorFormat("[MSG]transaction not matched, px {0} <> url {1}", paymentId, id);
                return(View("Error"));
            }

            if (isSuccess)
            {
                if (StallApplication.IsPaymentOperating(paymentId))
                {
                    StallApplication.BizErrorFormat("[MSG]payment {0} is operating", paymentId);
                    return(Redirect("/customer/orders"));
                }

                //set order as paid
                var orders = Models.Order.FindByPaymentId(paymentId, _db);
                foreach (var order in orders)
                {
                    if (!order.HasPaid)
                    {
                        try
                        {
                            order.HasPaid = true;
                            _db.SaveChanges();

                            //notify
                            var openIds = new List <string>();
                            //owner
                            var owner   = UserManager.FindById(order.Stall.UserId);
                            var ownerId = owner?.SnsInfos[WeChatClaimTypes.OpenId].InfoValue;
                            openIds.Add(ownerId);

                            //delivery man
                            var deliveryMen = Models.User.GetByRole(_db, "DeliveryMan");
                            foreach (var d in deliveryMen)
                            {
                                var dId = d.SnsInfos.FirstOrDefault(x => WeChatClaimTypes.OpenId.Equals(x.InfoKey))?.InfoValue;
                                if (!string.IsNullOrEmpty(dId))
                                {
                                    openIds.Add(dId);
                                }
                            }

                            //await order.Notify(_db, openId);
                            HostingEnvironment.QueueBackgroundWorkItem(x => order.Notify(_db, openIds));
                        }
                        catch (Exception ex)
                        {
                            StallApplication.SysError($"[MSG]failed to save order {order.Id}", ex);
                        }
                    }
                }

                StallApplication.RemoveOperatingPayment(paymentId);
                return(Redirect("/customer/orders?act=paid"));
            }
            else
            {
                return(Redirect("/errorpage/payfailed"));
            }
        }