c# 微信小程序添加卡券签名_微信小程序支付C#后端源码

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Web;6 usingSystem.Web.Mvc;7 usingSystem.IO;8 usingSystem.Security.Cryptography;9 usingSystem.Text;10 usingSystem.Xml;11 usingNewtonsoft.Json;12 usingNewtonsoft.Json.Linq;13 namespaceMvc_vue.Controllers14 {15 public classwxController : Controller16 {17 //

18 //GET: /wx/

19

20 publicActionResult Index()21 {22 returnView();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(stringopenid)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, inttotal_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 = "";50 formData += "" + appid + "";//appid

51 formData += "" + attach + ""; //附加数据(描述)

52 formData += "

" + body + "";//商品描述

53 formData += "" + mch_id + "";//商户号

54 formData += "" + nonce_str + "";//随机字符串,不长于32位。

55 formData += "" + notify_url + "";//通知地址

56 formData += "" + openid + "";//openid

57 formData += "" + bookingNo + "";//商户订单号 --待

58 formData += "61.50.221.43";//终端IP --用户ip

59 formData += "" + total_fee + "";//支付金额单位为(分)

60 formData += "JSAPI";//交易类型(JSAPI--公众号支付)

61 formData += "" + strMD5 + ""; //签名

62 formData += "";63

64

65

66 //请求数据

67 var getdata =sendPost(url, formData);68

69 //获取xml数据

70 XmlDocument doc = newXmlDocument();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自己写的一个类

87 wx w = newwx();88 w.timeStamp =_time;89 w.nonceStr =nonce_str;90 w.package = "prepay_id=" +prepay_id;91 w.paySign =MD5(strB).ToUpper(); ;92 w.signType = "MD5";93

94 //向小程序返回json数据

95 returnJsonConvert.SerializeObject(w);96 }97

98 ///

99 ///生成随机串100 ///

101 /// 字符串长度

102 ///

103 private static string GetRandomString(intlength)104 {105 const string key = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";106 if (length < 1)107 return string.Empty;108

109 Random rnd = newRandom();110 byte[] buffer = new byte[8];111

112 ulong bit = 31;113 ulong result = 0;114 int index = 0;115 StringBuilder sb = new StringBuilder((length / 5 + 1) * 5);116

117 while (sb.Length

121 buffer[5] = buffer[6] = buffer[7] = 0x00;122 result = BitConverter.ToUInt64(buffer, 0);123

124 while (result > 0 && sb.Length > 5;129 }130 }131 returnsb.ToString();132 }133

134 ///

135 ///获取时间戳136 ///

137 ///

138 private static longgetTime()139 {140 TimeSpan cha = (DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)));141 long t = (long)cha.TotalSeconds;142 returnt;143 }144

145

146 ///

147 ///MD5签名方法148 ///

149 /// 加密参数

150 ///

151 private static string MD5(stringinputText)152 {153 MD5 md5 = newMD5CryptoServiceProvider();154 byte[] fromData =System.Text.Encoding.UTF8.GetBytes(inputText);155 byte[] targetData =md5.ComputeHash(fromData);156 string byte2String = null;157

158 for (int i = 0; i < targetData.Length; i++)159 {160 byte2String += targetData[i].ToString("x2");161 }162

163 returnbyte2String;164 }165 ///

166 ///HMAC-SHA256签名方式167 ///

168 ///

169 ///

170 ///

171 private static string HmacSHA256(string message, stringsecret)172 {173 secret = secret ?? "";174 var encoding = newSystem.Text.UTF8Encoding();175 byte[] keyByte =encoding.GetBytes(secret);176 byte[] messageBytes =encoding.GetBytes(message);177 using (var hmacsha256 = newHMACSHA256(keyByte))178 {179 byte[] hashmessage =hmacsha256.ComputeHash(messageBytes);180 returnConvert.ToBase64String(hashmessage);181 }182 }183

184 ///

185 ///wx统一下单请求数据186 ///

187 /// 请求地址

188 /// 参数

189 ///

190 private static string sendPost(string URL, stringurlArgs)191 {192

193 System.Net.WebClient wCient = newSystem.Net.WebClient();194 wCient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");195 //byte[] postData = System.Text.Encoding.ASCII.GetBytes(urlArgs); 如果微信签名中有中文会签名失败

196       byte[] postData = System.Text.Encoding.UTF8.GetBytes(urlArgs);197 byte[] responseData = wCient.UploadData(URL, "POST", postData);198

199 string returnStr = System.Text.Encoding.UTF8.GetString(responseData);//返回接受的数据

200 returnreturnStr;201 }202

203 ///

204 ///生成订单号205 ///

206 ///

207 private static stringgetRandomTime()208 {209 Random rd = new Random();//用于生成随机数

210 string DateStr = DateTime.Now.ToString("yyyyMMddHHmmssMM");//日期

211 string str = DateStr + rd.Next(10000).ToString().PadLeft(4, '0');//带日期的随机数

212 returnstr;213 }214

215 }216 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值