Пример #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //Tolog("进入支付回调");
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(Request.InputStream);
                xmlDoc.Save(string.Format("C:\\WXPay\\SmsRechargeNotify{0}.xml", DateTime.Now.ToString("yyyyMMddHHmmssfff")));//写入日志

                //全部参数
                Dictionary <string, string> parametersAll = new Dictionary <string, string>();
                foreach (XmlElement item in xmlDoc.DocumentElement.ChildNodes)
                {
                    string key   = item.Name;
                    string value = item.InnerText;
                    if ((!string.IsNullOrEmpty(key)) && (!string.IsNullOrEmpty(value)))
                    {
                        parametersAll.Add(key, value);
                    }
                }
                parametersAll = (from entry in parametersAll
                                 orderby entry.Key ascending
                                 select entry).ToDictionary(pair => pair.Key, pair => pair.Value); //全部参数排序
                PayConfig payConfig = bllPay.GetPayConfig();
                if (!bllPay.VerifySignatureWx(parametersAll, payConfig.WXPartnerKey))              //验证签名
                {
                    Tolog("验证签名出错");
                    Response.Write(failXml);
                    return;
                }
                OrderPay orderPay = bllOrder.GetOrderPay(parametersAll["out_trade_no"]);
                if (orderPay == null)
                {
                    Tolog("订单未找到");
                    Response.Write(failXml);
                    return;
                }
                if (orderPay.Status.Equals(1))
                {
                    Tolog("已支付");
                    Response.Write(successXml);
                    return;
                }
                //更新订单状态
                if (parametersAll["return_code"].Equals("SUCCESS") && parametersAll["result_code"].Equals("SUCCESS"))//交易成功
                {
                    //orderPay.Trade_No = parametersAll["transaction_id"];

                    string msg       = "";
                    bool   isSuccess = bllSms.SmsRecharge(orderPay.UserId, orderPay.OrderId, orderPay.BuySmsTotalCount, out msg);
                    if (isSuccess)
                    {
                        orderPay.Status = 1;
                        orderPay.Ex2    = msg;
                        if (bllOrder.Update(orderPay))
                        {
                            Response.Write(successXml);
                            return;
                        }
                        else
                        {
                            Response.Write(failXml);
                            return;
                        }
                    }
                    else
                    {
                        Tolog("短信充值失败:" + msg);
                        Response.Write(failXml);
                        return;
                    }
                }

                Response.Write(failXml);
            }
            catch (Exception ex)
            {
                Tolog("出错了:" + ex.ToString());
                Response.Write(failXml);
            }
        }