http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2334|回复: 0

[支付] 微信支付-异步回调处理

[复制链接]
发表于 2021-6-16 16:27:04 | 显示全部楼层 |阅读模式


在做支付的时候, 经常会碰见异步回调, 主要是用在支付完成后, 微信异步调取我们的页面, 进行支付成功后的订单操作
例如: 支付完成, 修改订单状态, 发送消息, 修改订单内容等

异步回调代码:
[C#] 纯文本查看 复制代码
WxPayData notifyData = GetNotifyData();

            var status = notifyData.GetValue("result_code").ToString();
            if (status == "SUCCESS")
            {
                var transaction_id = notifyData.GetValue("transaction_id").ToString();
                string out_trade_no = notifyData.GetValue("out_trade_no").ToString();//传入的订单号
                int transactionFee = int.Parse(notifyData.GetValue("total_fee").ToString());//订单金额

                // 获取支付订单信息
                user_topayBLL topaybll = new user_topayBLL();
                user_topay topay = topaybll.FindListOne($"out_trade_no='{out_trade_no}'");
                if (topay != null && topay.id > 0 && topay.paymoney * 100 == transactionFee)
                {
                    var result = topaybll.ExecuteScalar(System.Data.CommandType.StoredProcedure, "User_PayFinish", UtilDAL.CreateParameter("@comid", topay.comid),
                        UtilDAL.CreateParameter("@userid", topay.userid),
                        UtilDAL.CreateParameter("@openduid", 0),
                        UtilDAL.CreateParameter("@ip", IPHelper.GetTrueIP()),
                        UtilDAL.CreateParameter("@orderid", topay.orderid),
                        UtilDAL.CreateParameter("@out_trade_no", topay.out_trade_no),
                        UtilDAL.CreateParameter("@trade_no", transaction_id),
                                UtilDAL.CreateParameter("@paytype", topay.paytype));


                    //验证是否成功

                    WxPayData res = new WxPayData();
                    res.SetValue("return_code", "SUCCESS");
                    res.SetValue("return_msg", "OK");

                    Response.Clear();
                    Response.Write(res.ToXml());
                    Response.End();
                }
                else
                {
                    //输出对象
                    var resultjson = new JsonResultHelper<dynamic>();

                    resultjson.msgCode = -1;
                    resultjson.msg = "获取支付订单失败, 请核验";

                    resultjson.Send(Response);
                    return;
                }
            }


[C#] 纯文本查看 复制代码
        public WxPayData 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();

            // Log.Info(this.GetType().ToString(), "Receive data from WeChat : " + builder.ToString());

            //转换数据格式并验证签名
            WxPayData data = new WxPayData();
            try
            {
                data.FromXml(builder.ToString());
            }
            catch (Exception ex)
            {
                //若签名错误,则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", ex.Message);
                LogHelper.WriteLog("Sign check error : " + res.ToXml());
            }
            return data;
        }


在此处一定要记的处理完成后给微信的回复, 即如下代码:
[C#] 纯文本查看 复制代码
WxPayData res = new WxPayData();
res.SetValue("return_code", "SUCCESS");
res.SetValue("return_msg", "OK");

Response.Clear();
Response.Write(res.ToXml());
Response.End();


如果不做回复, 微信会在一定时间段内, 不停的回调我们的接口,一定谨记






1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-28 20:08

© 2014-2021

快速回复 返回顶部 返回列表