解析微信支付的实现方法(.NET版)(2)

//创建支付应答对象 RequestHandler packageReqHandler = new RequestHandler(null); //初始化 packageReqHandler.Init(); //时间戳 string timeStamp = TenPayUtil.GetTimestamp(); //随机字符串 string nonceStr = TenPayUtil.GetNoncestr(); //设置package订单参数 生成prepayId预支付Id packageReqHandler.SetParameter("appid", account.AppId); //公众账号ID packageReqHandler.SetParameter("mch_id", account.PartnertId); //商户号 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("body", account.Body); packageReqHandler.SetParameter("out_trade_no", account.OrderSerialId); //商家订单号 packageReqHandler.SetParameter("total_fee", account.TotalAmount); //商品金额,以分为单位(money * 100).ToString() packageReqHandler.SetParameter("spbill_create_ip", account.RequestIp); //用户的公网ip,不是商户服务器IP packageReqHandler.SetParameter("notify_url", account.NotifyUrl); //接收财付通通知的URL packageReqHandler.SetParameter("trade_type", "JSAPI"); //交易类型 packageReqHandler.SetParameter("openid", account.OpenId); //用户的openId string sign = packageReqHandler.CreateMd5Sign("key", account.PaySignKey); packageReqHandler.SetParameter("sign", sign); //签名 string prepayId = string.Empty; try { string data = packageReqHandler.ParseXML(); var result = TenPayV3.Unifiedorder(data); MailHelp.SendMail("调用统一下单接口,下单结果:--"+result+"请求参数:"+data); var res = XDocument.Parse(result); prepayId = res.Element("xml").Element("prepay_id").Value; AppLog.Write("调用统一下单接口获取预支付prepayId成功", AppLog.LogMessageType.Debug); } catch (Exception ex) { AppLog.Write("获取到openid和access_tokenu异常", AppLog.LogMessageType.Debug, ex); MailHelp.SendMail("调用统一下单接口获取预支付prepayid异常:", ex); return null; }

4、组建jsapi微信支付请求参数,发起支付

我这里是首先组装好微信支付所需要的参数,然后再创建调用js脚本    

//生成JsAPI支付参数 RequestHandler paySignReqHandler = new RequestHandler(null); paySignReqHandler.SetParameter("appId", account.AppId); paySignReqHandler.SetParameter("timeStamp", timeStamp); paySignReqHandler.SetParameter("nonceStr", nonceStr); paySignReqHandler.SetParameter("package", string.Format("prepay_id={0}", prepayId)); paySignReqHandler.SetParameter("signType", "MD5"); string paySign = paySignReqHandler.CreateMd5Sign("key", account.PaySignKey); WeChatJsPayRequestModel resultModel = new WeChatJsPayRequestModel { AppId = account.AppId, NonceStr = nonceStr, TimeStamp = timeStamp, Package = string.Format("prepay_id={0}", prepayId), PaySign = paySign, SignType = "MD5" };

创建调用脚本

private string CreateWeixinJs(WeChatJsPayRequestModel model) { string js = @"<script type='text/javascript'> callpay(); function jsApiCall(){ WeixinJSBridge.invoke( 'getBrandWCPayRequest', { requestParam }, function (res) { if(res.err_msg == 'get_brand_wcpay_request:ok' ){ window.location.href = 'successUrl'; }else{ window.location.href = 'failUrl'; } } ); } function callpay() { if (typeof WeixinJSBridge == 'undefined'){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', jsApiCall); document.attachEvent('onWeixinJSBridgeReady', jsApiCall); } }else{ jsApiCall(); } } </script>"; string requestParam = string.Format(@"'appId': '{0}','timeStamp': '{1}','nonceStr': '{2}','package': '{3}','signType': '{4}','paySign': '{5}'", model.AppId, model.TimeStamp, model.NonceStr, model.Package, model.SignType, model.PaySign); js = js.Replace("requestParam", requestParam) .Replace("successUrl", model.JumpUrl + "&result=1") .Replace("failUrl", model.JumpUrl + "&result=0"); AppLog.Write("生成可执行脚本成功", AppLog.LogMessageType.Debug); return js; }

5、接收微信支付回调进行后续操作

回调的时候首先需要验证签名是否正确,保证安全性,签名验证通过之后再进行后续的操作,订单状态、通知啥的。 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjyzpp.html