Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does WeChat Mini Programs use C# to realize the payment function

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of WeChat Mini Programs how to use C # to achieve the payment function, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe that after reading this WeChat Mini Programs article on how to use C # to achieve payment functions, we will gain something. Let's take a look.

The specific code is as follows

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 () } / / required value public static string _ appid = "wxd930ea5d5a258f4f"; public static string _ mch_id = "10000100"; public static string _ key = "192006250b4c09247ec02edce69f6a2d" / / simulate wx to issue a unified order openid (front desk acquisition) 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) } / / Wechat issued an order to get prepay_id & return data 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";" by signing again. / / Wechat Unified order request address 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 set for merchant platform key (fake) string strMD5 = MD5 (strk). ToUpper (); / / MD5 signature / / string strHash=HmacSHA256 ("sha256", strmd5). ToUpper (); / / only one signature method is required (MD5 or HmacSHA256 [pay documents to read carefully]) / / signature var formData = "; formData + =" + appid + " / / appid formData + = "" + attach + ""; / / additional data (description) formData + = "" + body + ""; / / Product description formData + = "" + mch_id + ""; / / merchant number formData + = "" + nonce_str + ""; / / Random string, no longer than 32 bits. FormData + = "" + notify_url + ""; / / Notification address formData + = "" + openid + ""; / / openid formData + = "" + bookingNo + ""; / / Merchant order number-pending formData + = "61.50.221.43"; / / Terminal IP-user ip formData + = "+ total_fee +"; / / payment amount unit is (cent) formData + = "JSAPI" / / transaction type (JSAPI-- official account payment) formData + = "" + strMD5 + "; / signature formData + ="; / / request data var getdata = sendPost (url, formData); / / get xml data XmlDocument doc = new XmlDocument (); doc.LoadXml (getdata); / / xml format to json string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode (doc) JObject jo = (JObject) JsonConvert.DeserializeObject (json); string prepay_id = jo ["xml"] ["prepay_id"] ["# cdata-section"] .ToString (); / / timestamp string _ time = getTime () .ToString () / / return data to Mini Program 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"; / / send json data return JsonConvert.SerializeObject (w) to Mini Program;} / generate random string / string length / 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 signature method / 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) Byte2String +) {secret + = targetdata [I] .ToString ("x2");} return byte2String;} / HMAC-SHA256 signature method / 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 Unified order request data / request address / / Parameter / 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 accepted data return returnStr / / generate order number / private static string getRandomTime () {Random rd = new Random (); / / used to generate random number string DateStr = DateTime.Now.ToString ("yyyyMMddHHmmssMM"); / / date string str = DateStr + rd.Next (10000). ToString (). PadLeft (4,'0'); / / Random number return str with date }}}

Using MVC .NET Framework4

This is the end of the article on "how WeChat Mini Programs uses C# to achieve payment function". Thank you for reading! I believe that everyone has a certain understanding of "WeChat Mini Programs how to use C # to achieve payment function" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report