扫码支付asp(C#)实现步骤

扫码⽀付asp(C#)实现步骤
⽀付提交页⾯:
[HttpPost]
public ActionResult index(decimal amount)
{
//⽣成订单10位序列号,此处⽤时间和随机数⽣成,商户根据⾃⼰调整,保证唯⼀
string order_no = DateTime.Now.ToString("yyyyMMddHHmmss") + TenpayUtil.BuildRandomStr(4);
//这⾥是数据操作,代码已删除
ViewData["weixin_pay_qr_code"] = string.Format("/get_qrcode?product_id={0}", order_no);
return View(); }
输出⼆维码:
public void get_qrcode(string product_id)
二维码支付{
WxPayHelper helper = new WxPayHelper();
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("appid", config_util.mp_weixin_appid);
dic.Add("mch_id", config_util.weixin_mch_id);
dic.Add("nonce_str", Noncestr()); dic.Add("product_id", product_id); dic.Add("time_stamp", Timestamp());
dic.Add("sign", helper.GetSign(dic)); string url = WxPayHelper.FormatBizQueryParaMap(dic, false);//这⾥不要url编码 string code =
"weixin://wxpay/bizpayurl?" + url; var qrc = Create_ImgCode(code, 6); System.IO.MemoryStream ms = new System.IO.MemoryStream(); qrc.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); byte[] bytes = ms.GetBuffer(); //byte[] bytes= ms.ToArray(); 这两句都可以,⾄于区别么,下⾯有解释
ms.Close(); Response.BinaryWrite(bytes); return; }
原⽣拉取⽀付代码:
public ContentResult index()
{
if (Request.RequestType == "POST")
{
try
{
WxPayHelper helper = new WxPayHelper();
StreamReader reader = new StreamReader(Request.InputStream);
String xmlData = reader.ReadToEnd(); helper.ReceivePostXmlData(xmlData); common_util.WriteLog
("接收post来的xmlData=" + xmlData); if (helper.CheckSign()) { common_util.WriteLog("签名验证通过"); string product_id = helper.GetProductId(); common_util.WriteLog("产品id=" + product_id); string order_no = product_id;if (产品ID存在) { #region 业务处理 helper.SetParameter("body", "⽤户充值,⽤户号:" + item.user_id);
helper.SetParameter("out_trade_no", order_no); helper.SetParameter("total_fee", (item.amount * 100).ToString("#"));//这⾥单位是分
helper.SetParameter("notify_url", "http//www.openweixin/notify"); helper.SetParameter("trade_type", "NATIVE"); string prepay_id =
helper.GetPrepayId(); common_util.WriteLog("prepay_id=" + prepay_id); if (!string.IsNullOrEmpty(prepay_id)) {
helper.SetReturnParameter("return_code", "SUCCESS"); helper.SetReturnParameter("result_code", "SUCCESS");
helper.SetReturnParameter("prepay_id", prepay_id); helper.SetReturnParameter("appid", helper.GetAppId); helper.SetReturnParameter("mch_id", helper.GetMch_Id); helper.SetReturnParam
eter("nonce_str", Noncestr()); } else { helper.SetReturnParameter("return_code", "SUCCESS");//返回状态码 helper.SetReturnParameter("result_code", "FAIL");//业务结果 helper.SetReturnParameter("err_code_des", "预订单⽣产失败"); } #endregion } else { helper.SetReturnParameter("return_code", "SUCCESS");//返回状态码 helper.SetReturnParameter("result_code", "FAIL");//业务结果helper.SetReturnParameter("err_code_des", "此商品⽆效");//业务结果  } } else { helper.SetReturnParameter("return_code", "FAIL");
helper.SetReturnParameter("return_msg", "签名失败"); common_util.WriteLog("签名验证没有通过"); } string xmlStr = helper.GetReturnXml();
common_util.WriteLog("返回xml=" + xmlStr); Response.ContentType = "text/xml"; Response.Clear(); Response.Write(xmlStr); Response.End(); } catch (Exception ex) { common_util.WriteLog("异常了" + ex); } } return Content("OK"); }
⽀付成功通知页⾯:
if (Request.RequestType == "POST")
{
try
{
WxPayHelper helper = new WxPayHelper();
StreamReader reader = new StreamReader(Request.InputStream);
String xmlData = reader.ReadToEnd();
helper.ReceivePostXmlData(xmlData);
common_util.WriteLog("Notify_接收post来的xmlData=" + xmlData); if (helper.CheckSign()) { Dictionary<string, string> dicBack = helper.GetParameter();//获取所有参数 if (dicBack != null && dicBack.Keys.Contains("return_code")) { if (dicBack["return_code"] == "SUCCESS") { common_util.WriteLog("return_code=SUCCESS"); if (dicBack["result_code"] == "SUCCESS") { common_util.WriteLog("result_code=SUCCESS"); string out_trade_no = dicBack["out_trade_no"];//商户订单号 common_util.WriteLog("out_trade_no=" + out_trade_no); //1.验证商户订单号是否被处理 //2.处理过直接返回成功,否则返回 //此处根据out_trade_no 处理业务数据 //处理业务数据结束  common_util.WriteLog("Notify_验证签名成功");
helper.SetReturnParameter("return_code", "SUCCESS"); helper.SetReturnParameter("return_msg", ""); } } if (dicBack["return_code"] == "FAIL") { common_util.WriteLog("Notify_验证签名成功"); helper.SetReturnParameter("return_code", "SUCCESS"); helper.SetReturnParameter("return_msg", dicBack["return_msg"]); } } } else { common_util.WriteLog("Notify_验证签名失败"); helper.SetReturnParameter("return_code", "FAIL");
helper.SetReturnParameter("return_msg", "签名失败"); } string xmlStr = helper.GetReturnXml(); common_util.WriteLog("Notify_返回xml=" + xmlStr); Response.ContentType = "text/xml"; Response.Clear(); Response.Write(xmlStr); Response.End(); } catch (Exception ex) {
common_util.WriteLog("Notify_异常了" + ex); } } return Content("OK");
以上代码全部经过实体⽹站测试成功运⾏。

本文发布于:2024-09-22 14:20:11,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/1/358588.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:代码   处理   验证   签名
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议