当前位置: 移动技术网 > IT编程>开发语言>.net > 微信小程序支付C#后端源码

微信小程序支付C#后端源码

2018年08月29日  | 移动技术网IT编程  | 我要评论

台服痞子狼,潘霜霜三围,6221轴承

  1 using system;
  2 using system.collections.generic;
  3 using system.linq;
  4 using system.text;
  5 using system.web;
  6 using system.web.mvc;
  7 using system.io;
  8 using system.security.cryptography;
  9 using system.text;
 10 using system.xml;
 11 using newtonsoft.json;
 12 using newtonsoft.json.linq;
 13 namespace mvc_vue.controllers
 14 {
 15     public class wxcontroller : controller
 16     {
 17         //
 18         // get: /wx/
 19 
 20         public actionresult index()
 21         {
 22             return view();
 23         }
 24         //所需值
 25         public static string _appid = "wxd930ea5d5a258f4f";
 26         public static string _mch_id = "10000100";
 27         public static string _key = "192006250b4c09247ec02edce69f6a2d";
 28 
 29         //模拟wx统一下单 openid(前台获取)
 30         public string getda(string openid)
 31         {
 32             return getprepay_id(_appid, "shanghaifendian", "monixiaofei", _mch_id, getrandomstring(30), "http://www.weixin.qq.com/wxpay/pay.php", openid, getrandomtime(), 1);
 33 
 34         }
 35 
 36         
 37 
 38         //微信统一下单获取prepay_id & 再次签名返回数据
 39         private static string getprepay_id(string appid, string attach, string body, string mch_id, string nonce_str, string notify_url, string openid, string bookingno, int total_fee)
 40         {
 41             var url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信统一下单请求地址
 42             string stra = "appid=" + appid + "&attach=" + attach + "&body=" + body + "&mch_id=" + mch_id + "&nonce_str=" + nonce_str + "&notify_url=" + notify_url + "&openid=" + openid + "&out_trade_no=" + bookingno + "&spbill_create_ip=61.50.221.43&total_fee=" + total_fee + "&trade_type=jsapi";
 43             string strk = stra + "&key="+_key;  //key为商户平台设置的密钥key(假)
 44             string strmd5 = md5(strk).toupper();//md5签名
 45 
 46             //string strhash=hmacsha256("sha256",strmd5).toupper();   //签名方式只需一种(md5 或 hmacsha256     【支付文档需仔细看】)
 47 
 48             //签名
 49             var formdata = "<xml>";
 50             formdata += "<appid>" + appid + "</appid>";//appid  
 51             formdata += "<attach>" + attach + "</attach>"; //附加数据(描述)
 52             formdata += "<body>" + body + "</body>";//商品描述
 53             formdata += "<mch_id>" + mch_id + "</mch_id>";//商户号  
 54             formdata += "<nonce_str>" + nonce_str + "</nonce_str>";//随机字符串,不长于32位。  
 55             formdata += "<notify_url>" + notify_url + "</notify_url>";//通知地址
 56             formdata += "<openid>" + openid + "</openid>";//openid
 57             formdata += "<out_trade_no>" + bookingno + "</out_trade_no>";//商户订单号    --待
 58             formdata += "<spbill_create_ip>61.50.221.43</spbill_create_ip>";//终端ip  --用户ip
 59             formdata += "<total_fee>" + total_fee + "</total_fee>";//支付金额单位为(分)
 60             formdata += "<trade_type>jsapi</trade_type>";//交易类型(jsapi--公众号支付)
 61             formdata += "<sign>" + strmd5 + "</sign>"; //签名
 62             formdata += "</xml>";
 63 
 64 
 65 
 66             //请求数据
 67             var getdata = sendpost(url, formdata);
 68 
 69             //获取xml数据
 70             xmldocument doc = new xmldocument();
 71             doc.loadxml(getdata);
 72             //xml格式转json
 73             string json = newtonsoft.json.jsonconvert.serializexmlnode(doc);
 74 
 75 
 76 
 77             jobject jo = (jobject)jsonconvert.deserializeobject(json);
 78             string prepay_id = jo["xml"]["prepay_id"]["#cdata-section"].tostring();
 79 
 80             //时间戳
 81             string _time = gettime().tostring();
 82 
 83             //再次签名返回数据至小程序
 84             string strb = "appid=" + appid + "&noncestr=" + nonce_str + "&package=prepay_id=" + prepay_id + "&signtype=md5&timestamp=" + _time + "&key="_key;
 85 
 86             wx w = new wx();
 87             w.timestamp = _time;
 88             w.noncestr = nonce_str;
 89             w.package = "prepay_id=" + prepay_id;
 90             w.paysign = md5(strb).toupper(); ;
 91             w.signtype = "md5";
 92 
 93             //向小程序发送json数据
 94              return jsonconvert.serializeobject(w);
 95         }
 96 
 97         /// <summary>
 98         /// 生成随机串    
 99         /// </summary>
100         /// <param name="length">字符串长度</param>
101         /// <returns></returns>
102         private static string getrandomstring(int length)
103         {
104             const string key = "abcdefghjklmnpqrstuvwxyz23456789";
105             if (length < 1)
106                 return string.empty;
107 
108             random rnd = new random();
109             byte[] buffer = new byte[8];
110 
111             ulong bit = 31;
112             ulong result = 0;
113             int index = 0;
114             stringbuilder sb = new stringbuilder((length / 5 + 1) * 5);
115 
116             while (sb.length < length)
117             {
118                 rnd.nextbytes(buffer);
119 
120                 buffer[5] = buffer[6] = buffer[7] = 0x00;
121                 result = bitconverter.touint64(buffer, 0);
122 
123                 while (result > 0 && sb.length < length)
124                 {
125                     index = (int)(bit & result);
126                     sb.append(key[index]);
127                     result = result >> 5;
128                 }
129             }
130             return sb.tostring();
131         }
132 
133         /// <summary>
134         /// 获取时间戳
135         /// </summary>
136         /// <returns></returns>
137         private static long gettime()
138         {
139             timespan cha = (datetime.now - timezone.currenttimezone.tolocaltime(new system.datetime(1970, 1, 1)));
140             long t = (long)cha.totalseconds;
141             return t;
142         }
143 
144 
145         /// <summary>
146         /// md5签名方法  
147         /// </summary>  
148         /// <param name="inputtext">加密参数</param>  
149         /// <returns></returns>  
150         private static string md5(string inputtext)
151         {
152             md5 md5 = new md5cryptoserviceprovider();
153             byte[] fromdata = system.text.encoding.utf8.getbytes(inputtext);
154             byte[] targetdata = md5.computehash(fromdata);
155             string byte2string = null;
156 
157             for (int i = 0; i < targetdata.length; i++)
158             {
159                 byte2string += targetdata[i].tostring("x2");
160             }
161 
162             return byte2string;
163         }
164         /// <summary>
165         /// hmac-sha256签名方式
166         /// </summary>
167         /// <param name="message"></param>
168         /// <param name="secret"></param>
169         /// <returns></returns>
170         private static string hmacsha256(string message, string secret)
171         {
172             secret = secret ?? "";
173             var encoding = new system.text.utf8encoding();
174             byte[] keybyte = encoding.getbytes(secret);
175             byte[] messagebytes = encoding.getbytes(message);
176             using (var hmacsha256 = new hmacsha256(keybyte))
177             {
178                 byte[] hashmessage = hmacsha256.computehash(messagebytes);
179                 return convert.tobase64string(hashmessage);
180             }
181         }
182 
183         /// <summary>
184         /// wx统一下单请求数据
185         /// </summary>
186         /// <param name="url">请求地址</param>
187         /// <param name="urlargs">参数</param>
188         /// <returns></returns>
189         private static string sendpost(string url, string urlargs)
190         {
191             
192             system.net.webclient wcient = new system.net.webclient();
193             wcient.headers.add("content-type", "application/x-www-form-urlencoded");
194             byte[] postdata = system.text.encoding.ascii.getbytes("body=" + urlargs);
195 
196             byte[] responsedata = wcient.uploaddata(url, "post", postdata);
197 
198             string returnstr = system.text.encoding.utf8.getstring(responsedata);//返回接受的数据 
199             return returnstr;202         }
203 
204         /// <summary>
205         /// 生成订单号
206         /// </summary>
207         /// <returns></returns>
208         private static string getrandomtime()
209         {
210             random rd = new random();//用于生成随机数
211             string datestr = datetime.now.tostring("yyyymmddhhmmssmm");//日期
212             string str = datestr + rd.next(10000).tostring().padleft(4, '0');//带日期的随机数
213             return str;
214         }
215 
216     }
217 }

使用的是mvc .net framework4

mvc项目发布前的配置

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网