asp.Netcore微信APP支払い(スキャン支払い、H 5支払い、公衆番号支払い、app支払い)の4

17311 ワード

微信app支払いには以下のパラメータが必要であり、クラスパッケージは以下の通りである.
public class WxPayModel
    {
        /// 
        ///   ID
        /// 
        public string appid { set; get; } = "";
        /// 
        ///    
        /// 
        public string partnerid{ set; get; } = "";
        /// 
        ///        ID
        /// 
        public string prepayid { set; get; } = "";
        /// 
        ///     
        /// 
        public string package { set; get; } = "Sign=WXPay";
        /// 
        ///      
        /// 
        public string noncestr { set; get; } = "";
        /// 
        ///    
        /// 
        public string timestamp { set; get; } = "";

        /// 
        ///   
        /// 
        public string sign { set; get; } = "";
    }

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

/// 
        ///     APP       
        /// 
        ///    
        ///   
        ///   
        ///   IP
        /// 
        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 /// /// /// /// /// 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支払いに必要なデータを正しく返すことができる.