asp.net WeChat Pay, sometimes he can pay successfully, but sometimes he will call the payment JSAPI and lack the parameter package,. Do you know why?
parameters are not wrong. Appid,server, merchant number and address are all correct, but they still report an error. I hope God will save me
.related codes
/ / Please paste the code text below (do not replace the code with pictures)
private void GetUserOpenId()
{
string code = Request.QueryString["code"];
if (string.IsNullOrEmpty(code))
{
string code_url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=lk-sharpwechat_redirect", "wx97199c27223d23df", "http%3A%2F%2Fhnsthm.cn%2FWap%2Fwxpay%2FUser_WeCha.aspx");
Response.Redirect(code_url);
}
else
{
LogUtil.WriteLog(" ============ =====================");
-sharpregion OpenID================
string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", PayConfig.AppId, PayConfig.AppSecret, code);
string returnStr = HttpUtil.Send("", url);
LogUtil.WriteLog("Send returnStr :" + returnStr);
var obj = JsonConvert.DeserializeObject<OpenModel>(returnStr);
/****************************************/
/****************************************/
url = string.Format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1}", PayConfig.AppId, obj.refresh_token);
returnStr = HttpUtil.Send("", url);
obj = JsonConvert.DeserializeObject<OpenModel>(returnStr);
LogUtil.WriteLog("Send access_token:" + obj.access_token);
LogUtil.WriteLog("Send openid=" + obj.openid);
/****************************************/
/****************************************/
url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}", obj.access_token, obj.openid);
returnStr = HttpUtil.Send("", url);
LogUtil.WriteLog("Send returnStr:" + returnStr);
//this.UserOpenId = obj.openid;
LogUtil.WriteLog(" ============ =====================");
/****************************************/
/****************************************/
/************************************ ******************************************/
LogUtil.WriteLog("============ ===============");
PayModel model = new PayModel();
model.OrderSN = txtOrderSN.Text;
/*model.TotalFee = int.Parse(rbtlPay.SelectedValue);*/
model.TotalFee = Convert.ToInt32(0.01);
//if (rbtlPay.SelectedValue == "0")
//{
// model.TotalFee = Convert.ToInt32(Send.Text);
//}
model.Body = txtBody.Text;
model.OpenId = obj.openid;
model.Attach = "test";
string OrderSN = model.OrderSN;
string Body = model.Body;
int aa = model.TotalFee * 100;
string TotalFee = aa.ToString();
string Attach = model.Attach;
string UserOpenId = obj.openid;
LogUtil.WriteLog(string.Format(":OrderSN={0}Body={1}TotalFee={2}Attach={3}UserOpenId={4}",
//this.OrderSN, this.Body, this.TotalFee, this.Attach, this.UserOpenId));
OrderSN, Body, TotalFee, Attach, UserOpenId));
-sharpendregion
-sharpregion ============================
-sharpregion ===========================
//
TimeStamp = TenpayUtil.getTimestamp();
//
NonceStr = TenpayUtil.getNoncestr();
//
var packageReqHandler = new RequestHandler(Context);
//
packageReqHandler.init();
//package pdf
packageReqHandler.setParameter("body", Body); // 127
packageReqHandler.setParameter("appid", PayConfig.AppId);
packageReqHandler.setParameter("mch_id", PayConfig.MchId);
packageReqHandler.setParameter("nonce_str", NonceStr.ToLower());
packageReqHandler.setParameter("notify_url", PayConfig.NotifyUrl);/***************************************************/
packageReqHandler.setParameter("openid", UserOpenId);
packageReqHandler.setParameter("out_trade_no", OrderSN); //
packageReqHandler.setParameter("spbill_create_ip", Page.Request.UserHostAddress); //ipIP
packageReqHandler.setParameter("total_fee", TotalFee.ToString()); //,(money * 100).ToString()
packageReqHandler.setParameter("trade_type", "JSAPI");
if (!string.IsNullOrEmpty(Attach))
packageReqHandler.setParameter("attach", Attach);// 127
-sharpendregion
-sharpregion sign===============================
Sign = packageReqHandler.CreateMd5Sign("key", PayConfig.AppKey);/***************************************************/
LogUtil.WriteLog("WeiPay sign:" + Sign);
-sharpendregion
-sharpregion package======================
packageReqHandler.setParameter("sign", Sign);
string data = packageReqHandler.parseXML();
LogUtil.WriteLog("WeiPay packageXML:" + data);
string prepayXml = HttpUtil.Send(data, "https://api.mch.weixin.qq.com/pay/unifiedorder");
LogUtil.WriteLog("WeiPay packageBack_XML:" + prepayXml);
//ID
var xdoc = new XmlDocument();
xdoc.LoadXml(prepayXml);
XmlNode xn = xdoc.SelectSingleNode("xml");
XmlNodeList xnl = xn.ChildNodes;
if (xnl.Count > 7)
{
PrepayId = xnl[7].InnerText;
Package = string.Format("prepay_id={0}", PrepayId);
LogUtil.WriteLog("WeiPay package:" + Package);
}
-sharpendregion
/*H5JS*/
-sharpregion ==============
var paySignReqHandler = new RequestHandler(Context);
paySignReqHandler.setParameter("appId", PayConfig.AppId);
paySignReqHandler.setParameter("timeStamp", TimeStamp);
paySignReqHandler.setParameter("nonceStr", NonceStr);
paySignReqHandler.setParameter("package", Package);
paySignReqHandler.setParameter("signType", "MD5");
PaySign = paySignReqHandler.CreateMd5Sign("key", PayConfig.AppKey);
LogUtil.WriteLog("WeiPay paySign:" + PaySign);
-sharpendregion
-sharpendregion
}