asp.net core 微信APP支付(扫码支付,H5支付,公众号支付,app支付)之4

  微信app支付需要以下参数,类封装如下

public class WxPayModel
    {
        /// <summary>
        /// 应用ID
        /// </summary>
        public string appid { set; get; } = "";
        /// <summary>
        /// 商户号
        /// </summary>
        public string partnerid{ set; get; } = "";
        /// <summary>
        /// 预支付交易会话ID
        /// </summary>
        public string prepayid { set; get; } = "";
        /// <summary>
        /// 扩展字段
        /// </summary>
        public string package { set; get; } = "Sign=WXPay";
        /// <summary>
        /// 随机字符串
        /// </summary>
        public string noncestr { set; get; } = "";
        /// <summary>
        /// 时间戳
        /// </summary>
        public string timestamp { set; get; } = "";

        /// <summary>
        /// 签名
        /// </summary>
        public string sign { set; get; } = "";
    }

  支付业务逻辑类如下,只需要传递订单号out_trade_no,描述body,价格total_fee,客户端ip,返回一个WxPayModel供app端调用。

/// <summary>
        /// 获取微信APP支付需要的信息
        /// </summary>
        /// <param name="out_trade_no">订单号</param>
        /// <param name="body">描述</param>
        /// <param name="total_fee">总价</param>
        /// <param name="ip">客户IP</param>
        /// <returns></returns>
        public WxPayModel GetWxAppPayInfo(string out_trade_no, string body, string total_fee, string ip)
        {
            //构造返回内容,缺少prepayid和sign需要去微信服务器获取
            WxPayModel wxPayModel = new WxPayModel()
            {
                appid = wxPayConfig.appid,
                partnerid = wxPayConfig.mchid,
                timestamp = Senparc.Weixin.Helpers.DateTimeHelper.GetWeixinDateTime(DateTime.Now) + "",
                noncestr = TenPayV3Util.GetNoncestr(),
            };

            //构造请求参数
            Senparc.Weixin.MP.TenPayLibV3.RequestHandler packageReqHandler = new Senparc.Weixin.MP.TenPayLibV3.RequestHandler();

            #region 构造请求参数
            packageReqHandler.SetParameter("appid", wxPayConfig.appid);//APPID
            packageReqHandler.SetParameter("mch_id", wxPayConfig.mchid);//商户号
            packageReqHandler.SetParameter("nonce_str", Senparc.Weixin.MP.TenPayLibV3.TenPayV3Util.GetNoncestr());//随机串
            packageReqHandler.SetParameter("body", body);
            packageReqHandler.SetParameter("out_trade_no", out_trade_no);//订单号
            packageReqHandler.SetParameter("total_fee", (int)(Convert.ToDecimal(total_fee) * 100) + ""); //金额,以分为单位
            packageReqHandler.SetParameter("spbill_create_ip", ip);//IP
            packageReqHandler.SetParameter("notify_url", wxPayConfig.notify_url); //回调地址
            packageReqHandler.SetParameter("trade_type", "APP");//APP支付

            packageReqHandler.SetParameter("sign", packageReqHandler.CreateMd5Sign("key", wxPayConfig.key));//商户API密钥(签名) 
            #endregion

            //将参数转为xml字符串
            string data = packageReqHandler.ParseXML();
            //发起post异步请求,获取返回的内容
            var result = PostWithStringFile(wxPayConfig.api_url, data);

            Log.Info("【GetWxAppPayInfo】订单:" + out_trade_no + ",请求得到的xml:" + result, "微信支付");

            var res = System.Xml.Linq.XDocument.Parse(result);//解析xml,为了填充prepay_id参数和_paySign参数。
            try
            {
                //填充prepay_id参数
                wxPayModel.prepayid = res.Element("xml").Element("prepay_id").Value;

                SortedDictionary<string, object> sParams = new SortedDictionary<string, object>();

          sParams.Add("appid", wxPayModel.appid);
          sParams.Add("noncestr", wxPayModel.noncestr);
          sParams.Add("package", "Sign=WXPay");
          sParams.Add("partnerid", wxPayModel.partnerid);
          sParams.Add("prepayid", wxPayModel.prepayid);
          sParams.Add("timestamp", wxPayModel.timestamp);

//填充sign参数 
                wxPayModel.sign = GetSign(sParams, wxPayConfig.key);
            }
            catch (Exception ex)
            {
                Log.Info($"【GetWxAppPayInfo】订单:{out_trade_no },异常:{ ex.ToString()}", "微信支付");
            }
            return wxPayModel;
        }

  其中获取微信签名方法如下

#region 获取微信签名
        /// <summary>
        /// 获取微信签名
        /// </summary>
        /// <param name="sParams"></param>
        /// <returns></returns>
        public string GetSign(SortedDictionary<string, object> sParams, string key)
        {
            string sign = string.Empty;
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<string, object> temp in sParams)
            {
                if (string.IsNullOrEmpty(temp.Value +"") || temp.Key.ToLower() == "sign")
                {
                    continue;
                }
                sb.Append(temp.Key.Trim() + "=" + temp.Value.ToString() + "&");
            }
            sb.Append("key=" + key.Trim() + "");
            string signkey = sb.ToString();
            sign = Senparc.Weixin.Helpers.EncryptHelper.GetMD5(signkey);
            return sign;
        }

        #endregion

  这样只要应用开通了微信app支付功能,就能正确返回app sdk支付所需要的数据。

 

posted @ 2018-06-22 15:59  jomz  阅读(2565)  评论(5编辑  收藏  举报