1 private Dictionary<string, object> WeixinPayTest(string body, string attach, string out_trade_no, int total_fee, long product_id) 2 { 3 string url = "http://spdbapi.ulopay.com/pay/unifiedorder";//浦发银行测试获取支付二维码地址 4 Dictionary<string, object> dict = new Dictionary<string, object>(); 5 string userip = System.Web.HttpContext.Current.Request.UserHostAddress;//获取用户IP 6 if (userip == "::1") 7 { 8 userip = "127.0.0.1"; 9 } 10 11 Dictionary<string, string> dicts = new Dictionary<string, string>(); 12 dicts.Add("body", "测试购买商品");//说明 13 dicts.Add("mch_id", "26206409");//商户号 14 dicts.Add("nonce_str", "ibuaiVcKdpRxkhJA");//随机数,不能超过32位 15 dicts.Add("notify_url", "http://www.baidu.com/");//支付完返回地址 16 dicts.Add("out_trade_no", out_trade_no);//订单号 17 dicts.Add("spbill_create_ip", userip); 18 dicts.Add("total_fee", "1");//支付金额 19 dicts.Add("trade_type", "trade.weixin.native");//类型,这里是微信二维码扫描 20 //生成签名 21 StringBuilder str = new StringBuilder(); 22 str.Append("body=测试购买商品");//商品或支付单简要描述 23 str.Append("&mch_id=26206409");//商户号ID 24 str.Append("&nonce_str=ibuaiVcKdpRxkhJA");//随机数,不能超过32位 25 str.Append("¬ify_url=http://www.baidu.com/"); 26 str.Append("&out_trade_no=" + out_trade_no); 27 str.Append("&spbill_create_ip=" + userip); 28 str.Append("&total_fee=1"); 29 str.Append("&trade_type=trade.weixin.native"); 30 str.Append("&key=e5ffcbcede5216753749a976ccef4202");//key 31 string sign = BLL.Common.Utils.MD5(str.ToString()); 32 dicts.Add("sign", sign.ToUpper()); 33 //制作简单xml 34 StringBuilder sb = new StringBuilder(); 35 sb.Append("<xml>"); 36 foreach (var k in dicts) 37 { 38 sb.Append("<" + k.Key + ">" + k.Value + "</" + k.Key + ">"); 39 } 40 sb.Append("</xml>"); 41 string data = sb.ToString(); 42 WebRequestHelper req = new WebRequestHelper(url, data, Encoding.GetEncoding("utf-8")); 43 req.Accept = "application/xml"; 44 req.ContentType = "application/xml;charset=utf-8";//输出xml格式和utf-8 45 string res = req.PostRequest();//获取放回值 46 if (res == "") 47 return null; 48 NameValueCollection list = GetXmlFieldList(res);//把返回的xml转换为键值对形式 49 if (list["return_code"] == "SUCCESS" && list["result_code"] == "SUCCESS") 50 { 51 dict.Add("url", "/pay/GetPayQRCode.ashx?data=" + Web.UI.Fun.Utils.DESEncrypt(list["code_url"])); 52 dict.Add("code", 0); 53 } 54 55 return dict; 56 } 57 58 private NameValueCollection GetXmlFieldList(string res) 59 { 60 XmlDocument doc = new XmlDocument(); 61 doc.LoadXml(res); 62 XmlNode root = doc.SelectSingleNode("xml"); 63 if (root == null) return null; 64 65 NameValueCollection list = new NameValueCollection(); 66 foreach (XmlNode node in root.ChildNodes) 67 { 68 list.Add(node.Name, node.InnerText); 69 } 70 return list; 71 }
接着使用生成二维码展示出来,使用第三方插件QRCode
1 Web.UI.Request request = new UI.Request(true); 2 string data = request.QueryString("data"); 3 if (string.IsNullOrEmpty(data)) 4 { 5 return; 6 } 7 8 try 9 { 10 data = Utils.DESDecrypt(data); 11 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); 12 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; 13 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L; 14 qrCodeEncoder.QRCodeVersion = 4; 15 qrCodeEncoder.QRCodeScale = 6; 16 17 Image image = qrCodeEncoder.Encode(data, Encoding.Default); 18 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 19 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 20 context.Response.ClearContent(); 21 context.Response.ContentType = "image/Jpeg"; 22 context.Response.BinaryWrite(ms.ToArray()); 23 context.Response.End(); 24 } 25 catch (Exception ex) 26 { 27 28 }
原文地址:https://www.cnblogs.com/huodetiantang/p/9105760.html
时间: 2024-10-29 18:52:57