微信小程序如何使用C#实现支付功能
发表于:2024-10-19 作者:千家信息网编辑
千家信息网最后更新 2024年10月19日,这篇文章主要介绍了微信小程序如何使用C#实现支付功能的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇微信小程序如何使用C#实现支付功能文章都会有所收获,下面我们一起来看看吧
千家信息网最后更新 2024年10月19日微信小程序如何使用C#实现支付功能
这篇文章主要介绍了微信小程序如何使用C#实现支付功能的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇微信小程序如何使用C#实现支付功能文章都会有所收获,下面我们一起来看看吧。
具体代码如下
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web;using System.Web.Mvc;using System.IO;using System.Security.Cryptography;using System.Text;using System.Xml;using Newtonsoft.Json;using Newtonsoft.Json.Linq;namespace Mvc_vue.Controllers{ public class wxController : Controller { // // GET: /wx/ public ActionResult Index() { return View(); } //所需值 public static string _appid = "wxd930ea5d5a258f4f"; public static string _mch_id = "10000100"; public static string _key = "192006250b4c09247ec02edce69f6a2d"; //模拟wx统一下单 openid(前台获取) public string getda(string openid) { return Getprepay_id(_appid, "shanghaifendian", "monixiaofei", _mch_id, GetRandomString(30), "http://www.weixin.qq.com/wxpay/pay.php", openid, getRandomTime(), 1); } //微信统一下单获取prepay_id & 再次签名返回数据 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) { var url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信统一下单请求地址 string strA = "appid=" + appid + "&attach=" + attach + "&body=" + body + "&mch_id=" + mch_id + "&nonce_str=" + nonce_str + "¬ify_url=" + notify_url + "&openid=" + openid + "&out_trade_no=" + bookingNo + "&spbill_create_ip=61.50.221.43&total_fee=" + total_fee + "&trade_type=JSAPI"; string strk = strA + "&key="+_key; //key为商户平台设置的密钥key(假) string strMD5 = MD5(strk).ToUpper();//MD5签名 //string strHash=HmacSHA256("sha256",strmd5).ToUpper(); //签名方式只需一种(MD5 或 HmacSHA256 【支付文档需仔细看】) //签名 var formData = ""; formData += " "; //请求数据 var getdata = sendPost(url, formData); //获取xml数据 XmlDocument doc = new XmlDocument(); doc.LoadXml(getdata); //xml格式转json string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc); JObject jo = (JObject)JsonConvert.DeserializeObject(json); string prepay_id = jo["xml"]["prepay_id"]["#cdata-section"].ToString(); //时间戳 string _time = getTime().ToString(); //再次签名返回数据至小程序 string strB = "appId=" + appid + "&nonceStr=" + nonce_str + "&package=prepay_id=" + prepay_id + "&signType=MD5&timeStamp=" + _time + "&key="_key; wx w = new wx(); w.timeStamp = _time; w.nonceStr = nonce_str; w.package = "prepay_id=" + prepay_id; w.paySign = MD5(strB).ToUpper(); ; w.signType = "MD5"; //向小程序发送json数据 return JsonConvert.SerializeObject(w); } ///" + appid + " ";//appid formData += "" + attach + " "; //附加数据(描述) formData += "" + body + "";//商品描述 formData += "" + mch_id + " ";//商户号 formData += "" + nonce_str + " ";//随机字符串,不长于32位。 formData += "" + notify_url + " ";//通知地址 formData += "" + openid + " ";//openid formData += "" + bookingNo + " ";//商户订单号 --待 formData += "61.50.221.43 ";//终端IP --用户ip formData += "" + total_fee + " ";//支付金额单位为(分) formData += "JSAPI ";//交易类型(JSAPI--公众号支付) formData += "" + strMD5 + " "; //签名 formData += "/// 生成随机串 /// /// 字符串长度 ///private static string GetRandomString(int length) { const string key = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; if (length < 1) return string.Empty; Random rnd = new Random(); byte[] buffer = new byte[8]; ulong bit = 31; ulong result = 0; int index = 0; StringBuilder sb = new StringBuilder((length / 5 + 1) * 5); while (sb.Length < length) { rnd.NextBytes(buffer); buffer[5] = buffer[6] = buffer[7] = 0x00; result = BitConverter.ToUInt64(buffer, 0); while (result > 0 && sb.Length < length) { index = (int)(bit & result); sb.Append(key[index]); result = result >> 5; } } return sb.ToString(); } /// /// 获取时间戳 /// ///private static long getTime() { TimeSpan cha = (DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1))); long t = (long)cha.TotalSeconds; return t; } /// /// MD5签名方法 /// /// 加密参数 ///private static string MD5(string inputText) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] fromData = System.Text.Encoding.UTF8.GetBytes(inputText); byte[] targetData = md5.ComputeHash(fromData); string byte2String = null; for (int i = 0; i < targetData.Length; i++) { byte2String += targetData[i].ToString("x2"); } return byte2String; } /// /// HMAC-SHA256签名方式 /// /// /// ///private static string HmacSHA256(string message, string secret) { secret = secret ?? ""; var encoding = new System.Text.UTF8Encoding(); byte[] keyByte = encoding.GetBytes(secret); byte[] messageBytes = encoding.GetBytes(message); using (var hmacsha256 = new HMACSHA256(keyByte)) { byte[] hashmessage = hmacsha256.ComputeHash(messageBytes); return Convert.ToBase64String(hashmessage); } } /// /// wx统一下单请求数据 /// /// 请求地址 /// 参数 ///private static string sendPost(string URL, string urlArgs) { //context.Request["args"] System.Net.WebClient wCient = new System.Net.WebClient(); wCient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); byte[] postData = System.Text.Encoding.ASCII.GetBytes("body=" + urlArgs); byte[] responseData = wCient.UploadData(URL, "POST", postData); string returnStr = System.Text.Encoding.UTF8.GetString(responseData);//返回接受的数据 return returnStr;202 } /// /// 生成订单号 /// ///private static string getRandomTime() { Random rd = new Random();//用于生成随机数 string DateStr = DateTime.Now.ToString("yyyyMMddHHmmssMM");//日期 string str = DateStr + rd.Next(10000).ToString().PadLeft(4, '0');//带日期的随机数 return str; } }}
使用的是MVC .NET Framework4
关于"微信小程序如何使用C#实现支付功能"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"微信小程序如何使用C#实现支付功能"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
数据
支付
程序
功能
C#
统一
商户
地址
知识
生成
内容
再次
参数
字符
字符串
方式
日期
时间
篇文章
订单
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
便宜服务器代理商
零点科技网络安全
黑龙江大学知网数据库怎么用
阜阳有没有软件开发的公司
河北项目软件开发报价
网络技术mac
山西正规软件开发过程检测中心
青羊区网络安全示范
漳州地区网络安全形势
蓝凌办公软件开发公司
为什么要构建网络安全共同体
绿色软件开发技术基础
浦发银行软件开发岗位
亚信安全代理服务器地址大全
山西省能源局网络安全建设
医院服务器外网端口
开展全面排查筑牢网络安全防线
一台2u服务器一般需要多少安电
河北学生网络技术服务优化价格
数据库创建表的字段类型
成都网络技术培训公司
dell730服务器磁盘阵列
刘峰中医药专家数据库
淘宝上货软件开发公司
崩坏学园什么服务器好
云服务器和虚机
南京一区云服务器
湖北统一软件开发过程检测中心
跨数据库修改数据
如何访问discord 服务器