Esempio n. 1
0
        /// <summary>
        /// 支付返回信息 Check
        /// Called before [show pay callback].
        /// </summary>
        /// <param name="payTypeSysNo">The pay type system no.</param>
        /// <param name="collection">The collection.</param>
        /// <exception cref="ECommerce.Utility.BusinessException">
        /// 订单不存在!
        /// or
        /// 订单实际支付金额与订单需要支付的金额不相等!
        /// or
        /// 支付请求签名验证失败!
        /// </exception>
        public void OnlineShowPayCallbackPreCheck(int payTypeSysNo, NameValueCollection collection)
        {
            var context = new CallbackContext();

            try
            {
                context.PaymentModeId = payTypeSysNo;
                context.ResponseForm  = collection;

                Charges charge = Charges.GetInstance(context);
                context.SOSysNo = charge.GetSOSysNo(context);
                context.SOInfo  = ShoppingOrderDA.PayGetOrderInfoBySOSysNo(context.SOSysNo);

                if (charge.VerifySign(context))
                {
                    //验签成功
                    #region 检查返回的订单实际支付金额与订单需要支付的金额是否相等
                    if (context.SOInfo == null)
                    {
                        throw new BusinessException("订单不存在!");
                    }
                    if (!charge.GetPayAmount(context).ToString("F2").Equals(context.SOInfo.RealPayAmt.ToString("F2")))
                    {
                        throw new BusinessException("订单实际支付金额与订单需要支付的金额不相等!");
                    }
                    #endregion

                    if (!charge.GetPayResult(context))
                    {
                        throw new BusinessException("支付失败,請完成支付!");
                    }
                }
                else
                {
                    //验签失败
                    throw new BusinessException("支付请求签名验证失败!");
                }
            }
            catch (Exception ex)
            {
                ECommerce.Utility.Logger.WriteLog(string.Format("系统异常,异常信息:{0}!", ex.ToString()), "OnlineShowPayCallbackPreCheck", "OnlineShowPayCallbackPreCheckFailure");

                if (ex is BusinessException)
                {
                    throw ex;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 支付后台回调
        /// </summary>
        /// <param name="payTypeSysNo">支付方式系统编号</param>
        /// <param name="collection">POST数据集合</param>
        /// <returns></returns>
        public string PaymentCallback(int payTypeSysNo, NameValueCollection collection, out CallbackContext context)
        {
            context = new CallbackContext();
            try
            {
                #region [ 写系统Log ]

                ApplicationEventLog log = new ApplicationEventLog()
                {
                    Source           = "B2C site pay",
                    EventType        = 8,
                    HostName         = "B2C",
                    EventTitle       = "Pay callback",
                    EventMessage     = Charges.BuildStringFromNameValueCollection(collection),
                    LanguageCode     = ConstValue.LanguageCode,
                    CompanyCode      = ConstValue.CompanyCode,
                    StoreCompanyCode = ConstValue.StoreCompanyCode
                };
                CommonDA.CreateApplicationEventLog(log);

                #endregion

                context.PaymentModeId = payTypeSysNo;
                context.ResponseForm  = collection;

                Charges charge = Charges.GetInstance(context);
                context.SOSysNo = charge.GetSOSysNo(context);
                context.SOInfo  = ShoppingOrderDA.PayGetOrderInfoBySOSysNo(context.SOSysNo);
                charge.UpdateCallbackPayment(context);

                //如果为泰隆网关支付,写入积分消费记录
                //if (payTypeSysNo == 201)
                //{
                //    CreateUserBankPointRecord(context);
                //}


                if (charge.VerifySign(context))
                {
                    #region [ 检查返回的订单实际支付金额与订单需要支付的金额是否相等,否则不予处理 ]

                    if (context.SOInfo == null)
                    {
                        return(BuildPaymentCallbackReturnResult(payTypeSysNo, false));
                    }
                    if (!charge.GetPayAmount(context).ToString("F2").Equals(context.SOInfo.RealPayAmt.ToString("F2")))
                    {
                        ECommerce.Utility.Logger.WriteLog("订单实际支付金额与订单需要支付的金额不相等!", "PayCallback", "CheckPayAmount");
                        return(BuildPaymentCallbackReturnResult(payTypeSysNo, false));
                    }

                    #endregion

                    #region [ 检查NetPay是否存在并且状态为>=0,是则已支付过 ]

                    NetpayInfo netPayInfo = ShoppingOrderDA.GetCenterDBNetpayBySOSysNo(context.SOSysNo);
                    if (netPayInfo != null && netPayInfo.Status > (int)NetPayStatusType.Origin)
                    {
                        ECommerce.Utility.Logger.WriteLog("订单已经支付!", "PayCallback", "CheckNetPay");
                        return(BuildPaymentCallbackReturnResult(payTypeSysNo, true));
                    }
                    #endregion

                    if (charge.GetPayResult(context))
                    {
                        //支付成功
                        using (ITransaction scope = TransactionManager.Create())
                        {
                            #region 1.写Netpay
                            netPayInfo = new NetpayInfo()
                            {
                                SOSysNo          = context.SOSysNo,
                                PayTypeSysNo     = payTypeSysNo,
                                PayAmount        = context.SOInfo.RealPayAmt,
                                Source           = 0,
                                Status           = NetPayStatusType.Verified,
                                CompanyCode      = context.SOInfo.CompanyCode,
                                LanguageCode     = context.SOInfo.LanguageCode,
                                CurrencySysNo    = context.SOInfo.CurrencySysNo,
                                StoreCompanyCode = context.SOInfo.StoreCompanyCode,
                                OrderAmt         = (context.SOInfo.SoAmt
                                                    - Math.Abs((context.SOInfo.Amount.PointPay * 1.00m) / decimal.Parse(ConstValue.PointExhangeRate))
                                                    - Math.Abs(context.SOInfo.PromotionAmt)
                                                    + Math.Abs(context.SOInfo.Amount.PayPrice)
                                                    + Math.Abs(context.SOInfo.Amount.ShipPrice)
                                                    + Math.Abs(context.SOInfo.Amount.PremiumAmt)
                                                    - Math.Abs(context.SOInfo.Amount.DiscountAmt)
                                                    + Math.Abs(context.SOInfo.TariffAmt)
                                                    ),
                                PrePayAmt      = context.SOInfo.Amount.PrepayAmt,
                                PointPayAmt    = context.SOInfo.PointPay * 1.00m / decimal.Parse(ConstValue.PointExhangeRate),
                                GiftCardPayAmt = context.SOInfo.Amount.GiftCardPay,
                                SerialNumber   = charge.GetSerialNumber(context),
                                PayProcessTime = charge.GetPayProcessTime(context)
                            };
                            bool isTrue = ShoppingOrderDA.CreateNetpay(netPayInfo);
                            #endregion

                            if (!isTrue)
                            {
                                //解决前后台多次并发处理重复数据
                                ECommerce.Utility.Logger.WriteLog("订单已经支付!", "PayCallback", "CreateNetPay");
                                return(BuildPaymentCallbackReturnResult(payTypeSysNo, false));
                            }
                            if (ConstValue.PaymentInventory)
                            {
                                List <OrderItem> subOrderItemList = new List <OrderItem>();
                                context.SOInfo.SOItemList.ForEach(Item =>
                                {
                                    OrderProductItem opitem = new OrderProductItem();
                                    opitem["Quantity"]      = Item.Quantity;
                                    opitem.ProductSysNo     = int.Parse(Item.ProductID);
                                    opitem.ProductName      = Item.ProductName;
                                    opitem.WarehouseNumber  = int.Parse(Item.WarehouseNumber);
                                    subOrderItemList.Add(opitem);
                                });
                                subOrderItemList.ForEach(Item =>
                                {
                                    //限时促销
                                    if (PipelineDA.CheckCountDownByProductSysNo(Item.ProductSysNo))
                                    {
                                        //扣减订单商品库存
                                        int inventoryType = PipelineDA.GetInventoryType(Item);
                                        var rowCount      = PipelineDA.UpdateInventory(Item);
                                        if (rowCount != 1 && inventoryType != 1 && inventoryType != 3 && inventoryType != 5)
                                        {
                                            ECommerce.Utility.Logger.WriteLog("inventory: qty is not enough", "SOPipeline.CreateSOBasicPersister");
                                            throw new BusinessException(string.Format("商品【{0}】库存不足", Item.ProductName));
                                        }
                                        rowCount = PipelineDA.UpdateInventoryStock(Item);
                                        if (rowCount != 1 && inventoryType != 1 && inventoryType != 3 && inventoryType != 5)
                                        {
                                            ECommerce.Utility.Logger.WriteLog("inventory_stock: qty is not enough", "SOPipeline.CreateSOBasicPersister");
                                            throw new BusinessException(string.Format("商品【{0}】库存不足", Item.ProductName));
                                        }
                                    }
                                });
                            }


                            #region 2.发送支付成功邮件
                            //确认不需要发邮件
                            //CustomerInfo customer = CustomerDA.GetCustomerInfo(context.SOInfo.CustomerSysNo);
                            //if (!string.IsNullOrWhiteSpace(customer.Email))
                            //{
                            //    AsyncEmail email = new AsyncEmail();
                            //    email.MailAddress = customer.Email;
                            //    email.CustomerID = customer.CustomerID;
                            //    email.Status = (int)EmailStatus.NotSend;
                            //    string token = Guid.NewGuid().ToString("N");
                            //    email.ImgBaseUrl = ConfigurationManager.AppSettings["CDNWebDomain"].ToString();
                            //    email.SetNewTokenUrl = "/EmailVerifySucceed?token=" + token + "&sysno=" + customer.SysNo.ToString() + "&email=" + System.Web.HttpUtility.HtmlEncode(customer.Email);

                            //    string subject = string.Empty;
                            //    email.MailBody = MailHelper.GetMailTemplateBody("SalesOrderPaySuccessful", out subject);
                            //    email.MailSubject = subject.Replace("[SOSysNo]", context.SOInfo.SoSysNo.ToString());
                            //    //订单内容
                            //    email.MailBody = email.MailBody.Replace("[CustomerName]", customer.CustomerName);
                            //    email.MailBody = email.MailBody.Replace("[SOSysNo]", context.SOInfo.SoSysNo.ToString());
                            //    email.MailBody = email.MailBody.Replace("[PayAmount]", context.SOInfo.RealPayAmt.ToString("F2"));
                            //    email.MailBody = email.MailBody.Replace("[PayTypeName]", context.SOInfo.Payment.PayTypeName);
                            //    email.MailBody = email.MailBody.Replace("[OrderDate]", context.SOInfo.OrderDate.ToString());
                            //    email.MailBody = email.MailBody.Replace("[ReceiveName]", context.SOInfo.ReceiveName);
                            //    email.MailBody = email.MailBody.Replace("[SOMemo]", context.SOInfo.Memo);
                            //    email.MailBody = email.MailBody.Replace("[NowDate]", DateTime.Now.ToShortDateString());
                            //    EmailDA.SendEmail(email);
                            //}
                            #endregion

                            scope.Complete();
                        }

                        return(BuildPaymentCallbackReturnResult(payTypeSysNo, true));
                    }
                    else
                    {
                        ECommerce.Utility.Logger.WriteLog("返回支付失败", "PayCallback", "CheckPayResult");
                        return(BuildPaymentCallbackReturnResult(payTypeSysNo, false));
                    }
                }
                else
                {
                    //验签失败
                    return(BuildPaymentCallbackReturnResult(payTypeSysNo, false));
                }
            }
            catch (Exception ex)
            {
                //系统异常,写日志
                ECommerce.Utility.Logger.WriteLog(string.Format("系统异常,异常信息:{0}!", ex.ToString()), "PayCallback", "OnlinePayBgCallbackFailure");
                return(BuildPaymentCallbackReturnResult(payTypeSysNo, false));
            }
        }