【第三方支付】之微信支付

  微信支付里面,有两种方式。自己网站里面使用的是第二种方式。

 

需要注意的一点是,在开发的过程中,由于使用的是MVC的架构,回调地址以/xx/xx 结束。导致一直无法回调成功。后来把回调页面改为 xx.aspx 就可以了。

生成二维码的代码,参考微信支付帮助文档中资源代码。

步骤是:      

1.首先根据订单信息生成相应价格的二维码
2.微信支付,并调用回调页面函数

 

 public partial class WeiXinBack : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Log.Debug("回调地址", "开始");
            ResultNotify resultNotify = new ResultNotify(this);
            resultNotify.ProcessNotify();
            Log.Debug("回调地址", "结束");
        }
    }


  /// <summary>
    /// 支付结果通知回调处理类
    /// 负责接收微信支付后台发送的支付结果并对订单有效性进行验证,将验证结果反馈给微信支付后台
    /// </summary>
    public class ResultNotify : Notify
    {
        public ResultNotify(Page page)
            : base(page)
        {
        }
        // public static SqlMapper _SqlMap = (SqlMapper)Mapper.Instance();
        public override void ProcessNotify()
        {
            WxPayData notifyData = GetNotifyData();
            //Log.AddInterfaceLog("WeiXinBack.aspx", "微信回调接口", "true", notifyData.ToXml());
            //检查支付结果中transaction_id是否存在
            if (!notifyData.IsSet("transaction_id"))
            {
                //若transaction_id不存在,则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "支付结果中微信订单号不存在");
                Log.Error(this.GetType().ToString(), "The Pay result is error : " + res.ToXml());
                page.Response.Write(res.ToXml());
                page.Response.End();
            }

            string transaction_id = notifyData.GetValue("transaction_id").ToString();

            //查询订单,判断订单真实性
            if (!QueryOrder(transaction_id))
            {
                //若订单查询失败,则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", "订单查询失败");
                Log.Error(this.GetType().ToString(), "Order query failure : " + res.ToXml());
                page.Response.Write(res.ToXml());
                page.Response.End();
            }
            //查询订单成功
            else
            {
                string attach = notifyData.GetValue("attach").ToString();

                Log.Debug("执行方法attach:", attach);

                 Order _Order = GetModel( attach );

                try
                {
                     //修改订单状态逻辑处理,以及收银
                }
                catch (Exception ex)
                {
                    //_SqlMap.RollBackTransaction();
                    Log.Info("微信回调", "微信回调异常:" + ex.ToString());
                }
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "SUCCESS");
                res.SetValue("return_msg", "OK");
                Log.Info(this.GetType().ToString(), "order query success : " + res.ToXml());
                page.Response.Write(res.ToXml());
                page.Response.End();
            }
        }

        //查询订单
        private bool QueryOrder(string transaction_id)
        {
            WxPayData req = new WxPayData();
            req.SetValue("transaction_id", transaction_id);
            WxPayData res = WxPayApi.OrderQuery(req);
            if (res.GetValue("return_code").ToString() == "SUCCESS" &&
                res.GetValue("result_code").ToString() == "SUCCESS")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

 

posted @ 2015-07-14 18:00  Vincent_void  阅读(767)  评论(0编辑  收藏  举报