服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - 微信小程序支付C#后端源码

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

2022-02-28 13:30oneall C#

这篇文章主要为大家详细介绍了微信小程序支付C#后端源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了微信小程序支付C#后端源码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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 + "&notify_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 = "<xml>";
      formData += "<appid>" + appid + "</appid>";//appid
      formData += "<attach>" + attach + "</attach>"; //附加数据(描述)
      formData += "<body>" + body + "</body>";//商品描述
      formData += "<mch_id>" + mch_id + "</mch_id>";//商户号
      formData += "<nonce_str>" + nonce_str + "</nonce_str>";//随机字符串,不长于32位。
      formData += "<notify_url>" + notify_url + "</notify_url>";//通知地址
      formData += "<openid>" + openid + "</openid>";//openid
      formData += "<out_trade_no>" + bookingNo + "</out_trade_no>";//商户订单号  --待
      formData += "<spbill_create_ip>61.50.221.43</spbill_create_ip>";//终端IP --用户ip
      formData += "<total_fee>" + total_fee + "</total_fee>";//支付金额单位为(分)
      formData += "<trade_type>JSAPI</trade_type>";//交易类型(JSAPI--公众号支付)
      formData += "<sign>" + strMD5 + "</sign>"; //签名
      formData += "</xml>";
 
 
 
      //请求数据
      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);
    }
 
    /// <summary>
    /// 生成随机串 
    /// </summary>
    /// <param name="length">字符串长度</param>
    /// <returns></returns>
    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();
    }
 
    /// <summary>
    /// 获取时间戳
    /// </summary>
    /// <returns></returns>
    private static long getTime()
    {
      TimeSpan cha = (DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)));
      long t = (long)cha.TotalSeconds;
      return t;
    }
 
 
    /// <summary>
    /// MD5签名方法
    /// </summary>
    /// <param name="inputText">加密参数</param>
    /// <returns></returns>
    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;
    }
    /// <summary>
    /// HMAC-SHA256签名方式
    /// </summary>
    /// <param name="message"></param>
    /// <param name="secret"></param>
    /// <returns></returns>
    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);
      }
    }
 
    /// <summary>
    /// wx统一下单请求数据
    /// </summary>
    /// <param name="URL">请求地址</param>
    /// <param name="urlArgs">参数</param>
    /// <returns></returns>
    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     }
 
    /// <summary>
    /// 生成订单号
    /// </summary>
    /// <returns></returns>
    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

微信小程序支付前端源码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/oneall/p/9548722.html

延伸 · 阅读

精彩推荐
  • C#C#设计模式之Visitor访问者模式解决长隆欢乐世界问题实例

    C#设计模式之Visitor访问者模式解决长隆欢乐世界问题实例

    这篇文章主要介绍了C#设计模式之Visitor访问者模式解决长隆欢乐世界问题,简单描述了访问者模式的定义并结合具体实例形式分析了C#使用访问者模式解决长...

    GhostRider9502022-01-21
  • C#C#通过KD树进行距离最近点的查找

    C#通过KD树进行距离最近点的查找

    这篇文章主要为大家详细介绍了C#通过KD树进行距离最近点的查找,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    帆帆帆6112022-01-22
  • C#C#裁剪,缩放,清晰度,水印处理操作示例

    C#裁剪,缩放,清晰度,水印处理操作示例

    这篇文章主要为大家详细介绍了C#裁剪,缩放,清晰度,水印处理操作示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    吴 剑8332021-12-08
  • C#深入解析C#中的交错数组与隐式类型的数组

    深入解析C#中的交错数组与隐式类型的数组

    这篇文章主要介绍了深入解析C#中的交错数组与隐式类型的数组,隐式类型的数组通常与匿名类型以及对象初始值设定项和集合初始值设定项一起使用,需要的...

    C#教程网6172021-11-09
  • C#C# 实现对PPT文档加密、解密及重置密码的操作方法

    C# 实现对PPT文档加密、解密及重置密码的操作方法

    这篇文章主要介绍了C# 实现对PPT文档加密、解密及重置密码的操作方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    E-iceblue5012022-02-12
  • C#C#实现XML文件读取

    C#实现XML文件读取

    这篇文章主要为大家详细介绍了C#实现XML文件读取的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Just_for_Myself6702022-02-22
  • C#Unity3D实现虚拟按钮控制人物移动效果

    Unity3D实现虚拟按钮控制人物移动效果

    这篇文章主要为大家详细介绍了Unity3D实现虚拟按钮控制人物移动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一...

    shenqingyu060520232410972022-03-11
  • C#WPF 自定义雷达图开发实例教程

    WPF 自定义雷达图开发实例教程

    这篇文章主要介绍了WPF 自定义雷达图开发实例教程,本文介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下...

    WinterFish13112021-12-06