public WxPayData GetNotifyData()
        {
            LogHelper.SaveLog(TxtLogType.WeiXin, TxtLogContentType.Common, TxtLogFileType.Time, "GetNotifyData");

            //接收从微信后台POST过来的数据
            System.IO.Stream s = Request.InputStream;
            int count          = 0;

            byte[]        buffer  = new byte[1024];
            StringBuilder builder = new StringBuilder();

            while ((count = s.Read(buffer, 0, 1024)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            s.Dispose();

            //转换数据格式并验证签名
            WxPayData data = new WxPayData();

            try
            {
                data.FromXml(builder.ToString());
                if (data.CheckSign())
                {
                    string result_code = data.GetValue("result_code").ToString();
                    if (result_code == "SUCCESS")
                    {
                        string out_trade_no = data.GetValue("out_trade_no").ToString();
                        string trade_no     = data.GetValue("transaction_id").ToString();
                        if (MPOrderBusiness.UpdateOrderForPaySuccess(out_trade_no, trade_no))
                        {
                            WxPayData res = new WxPayData();
                            res.SetValue("return_code", "SUCCESS");
                            res.SetValue("return_msg", "OK");
                            LogHelper.SaveLog(TxtLogType.WeiXin, TxtLogContentType.Common, TxtLogFileType.Time, res.ToXml());
                            Response.Write(res.ToXml());
                            Response.End();
                        }
                        LogHelper.SaveLog(TxtLogType.WeiXin, TxtLogContentType.Common, TxtLogFileType.Time, "Response.End()");
                    }
                }
                else
                {
                }
            }
            catch (WxPayException ex)
            {
                //若签名错误,则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", ex.Message);
                Response.Write(res.ToXml());
                Response.End();
            }
            return(data);
        }
Esempio n. 2
0
        }                                               //H5调起JS API参数
        protected void Page_Load(object sender, EventArgs e)
        {
            MPOrderBusiness.UpdateOrderForPaySuccess("2017112323575210001000000009", "1234324");

            if (!IsPostBack)
            {
                string openid    = "oNWocwVUdOOajF2CpSmnyD8uN3Nw";
                string total_fee = "5";
                //检测是否给当前页面传递了相关参数
                if (string.IsNullOrEmpty(openid) || string.IsNullOrEmpty(total_fee))
                {
                    Response.Write("<span style='color:#FF0000;font-size:20px'>" + "oNWocwVUdOOajF2CpSmnyD8uN3Nw" + "</span>");
                    Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面传参出错,请返回重试" + "</span>");
                    submit.Visible = false;
                    return;
                }

                //若传递了相关参数,则调统一下单接口,获得后续相关接口的入口参数
                JsApiPay jsApiPay = new JsApiPay(this);
                jsApiPay.openid       = openid;
                jsApiPay.total_fee    = int.Parse(total_fee);
                jsApiPay.body         = "ceshi";
                jsApiPay.out_trade_no = System.DateTime.Now.ToString("yyyyMMddHHmmss");
                jsApiPay.device_info  = "100010";


                //JSAPI支付预处理
                try
                {
                    WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();
                    wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数

                    //在页面上显示订单信息
                    Response.Write("<span style='color:#00CD00;font-size:20px'>订单详情:</span><br/>");
                    Response.Write("<span style='color:#00CD00;font-size:20px'>" + unifiedOrderResult.ToPrintStr() + "</span>");
                }
                catch (Exception ex)
                {
                    Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败,请返回重试" + "</span>");
                    submit.Visible = false;
                }
            }
        }
Esempio n. 3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            SortedDictionary <string, string> sPara = GetRequestPost();

            if (sPara.Count > 0)//判断是否有带返回参数
            {
                //PayLogHelper.WritePayLog(Request.Form.ToString());
                //Notify aliNotify = new Notify();
                Notify aliNotify    = new Notify(AliPayConfig.charset, AliPayConfig.sign_type, AliPayConfig.pid, AliPayConfig.mapiUrl, AliPayConfig.alipay_miniapp_public_key);
                bool   verifyResult = aliNotify.Verify(sPara, Request.Form["notify_id"], Request.Form["sign"]);

                //商户订单号
                string     out_trade_no = Request.Form["out_trade_no"];
                Data_Order order        = null;

                if (verifyResult && CheckParams(out order))//验证成功
                {
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    //请在这里加上商户的业务逻辑程序代码


                    //——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
                    //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表

                    //支付宝交易号
                    string trade_no = Request.Form["trade_no"];

                    //交易状态
                    //在支付宝的业务通知中,只有交易通知状态为TRADE_SUCCESS或TRADE_FINISHED时,才是买家付款成功。
                    string trade_status = Request.Form["trade_status"];

                    //交易状态
                    if (trade_status == "TRADE_SUCCESS" || trade_status == "TRADE_FINISHED")
                    {
                        try
                        {
                            if (MPOrderBusiness.UpdateOrderForPaySuccess(out_trade_no, trade_no))
                            {
                                LogHelper.SaveLog(TxtLogType.AliPay, TxtLogContentType.Debug, TxtLogFileType.Day, "应用:莘拍档 订单号:" + out_trade_no + " 支付成功!");

                                //支付宝买家用户id
                                string buyer_id = Request.Form["buyer_id"];
                                //支付时间
                                string gmt_payment = HttpUtility.UrlDecode(Request.Form["gmt_payment"]);

                                string aliId = string.Empty;
                                string msg   = string.Empty;
                                if (!MobileTokenBusiness.GetAliId(order.Mobile, out aliId, out msg))
                                {
                                    bool ret = MobileTokenBusiness.UpdateAliBuyerId(order.Mobile, buyer_id);
                                }

                                IAopClient client = new DefaultAopClient(AliPayConfig.serverUrl, AliPayConfig.miniAppId, AliPayConfig.merchant_miniapp_private_key, "json", "1.0", "RSA2", AliPayConfig.alipay_miniapp_public_key, AliPayConfig.charset, false);
                                AlipayOpenAppMiniTemplatemessageSendRequest request = new AlipayOpenAppMiniTemplatemessageSendRequest();
                                request.BizContent = "{" +
                                                     "\"to_user_id\":\"" + buyer_id + "\"," +
                                                     "\"form_id\":\"" + trade_no + "\"," +
                                                     "\"user_template_id\":\"" + AliPayConfig.MiniAppTemplateId + "\"," +
                                                     "\"page\":\"pages/login/login\"," +
                                                     "\"data\":\"{\\\"keyword1\\\":{\\\"value\\\":\\\"" + out_trade_no + "\\\"},\\\"keyword2\\\":{\\\"value\\\":\\\"" + order.Descript + "\\\"},\\\"keyword3\\\":{\\\"value\\\":\\\"" + order.Price.ToString("0.00") + "\\\"},\\\"keyword4\\\":{\\\"value\\\":\\\"" + order.CreateTime.ToString("yyyy-MM-dd HH:mm:ss") + "\\\"},\\\"keyword5\\\":{\\\"value\\\":\\\"" + gmt_payment + "\\\"}}\"" +
                                                     "}";
                                AlipayOpenAppMiniTemplatemessageSendResponse response = client.Execute(request);
                            }
                            else
                            {
                                LogHelper.SaveLog(TxtLogType.AliPay, TxtLogContentType.Debug, TxtLogFileType.Day, "应用:莘拍档 订单号:" + out_trade_no + " 已支付订单更新失败!!!");
                            }
                        }
                        catch (Exception ex)
                        {
                            //PayLogHelper.WriteError(ex);
                            LogHelper.SaveLog(TxtLogType.AliPay, ex.InnerException != null ? ex.InnerException.Message : ex.Message);
                        }

                        //判断是否在商户网站中已经做过了这次通知返回的处理
                        //如果没有做过处理,那么执行商户的业务程序
                        //如果有做过处理,那么不执行商户的业务程序
                        Response.Write("success");  //请不要修改或删除
                    }
                    else
                    {
                        Response.Write("fail");
                    }

                    //——请根据您的业务逻辑来编写程序(以上代码仅作参考)——

                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                }
                else//验证失败
                {
                    LogHelper.SaveLog(TxtLogType.AliPay, TxtLogContentType.Debug, TxtLogFileType.Day, "应用:莘拍档 订单号:" + out_trade_no + " 警告:支付回调验证失败!!!");
                    Response.Write("fail");
                }
            }
            else
            {
                Response.Write("无通知参数");
            }
        }