欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年08月29日 09:04:29   作者:oneall  
這篇文章主要為大家詳細(xì)介紹了微信小程序支付C#后端源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了微信小程序支付C#后端源碼,供大家參考,具體內(nèi)容如下

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統(tǒng)一下單 openid(前臺(tái)獲取)
    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);

    }

    

    //微信統(tǒng)一下單獲取prepay_id & 再次簽名返回?cái)?shù)據(jù)
    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";//微信統(tǒng)一下單請(qǐng)求地址
      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為商戶平臺(tái)設(shè)置的密鑰key(假)
      string strMD5 = MD5(strk).ToUpper();//MD5簽名

      //string strHash=HmacSHA256("sha256",strmd5).ToUpper();  //簽名方式只需一種(MD5 或 HmacSHA256   【支付文檔需仔細(xì)看】)

      //簽名
      var formData = "<xml>";
      formData += "<appid>" + appid + "</appid>";//appid 
      formData += "<attach>" + attach + "</attach>"; //附加數(shù)據(jù)(描述)
      formData += "<body>" + body + "</body>";//商品描述
      formData += "<mch_id>" + mch_id + "</mch_id>";//商戶號(hào) 
      formData += "<nonce_str>" + nonce_str + "</nonce_str>";//隨機(jī)字符串,不長于32位。 
      formData += "<notify_url>" + notify_url + "</notify_url>";//通知地址
      formData += "<openid>" + openid + "</openid>";//openid
      formData += "<out_trade_no>" + bookingNo + "</out_trade_no>";//商戶訂單號(hào)  --待
      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--公眾號(hào)支付)
      formData += "<sign>" + strMD5 + "</sign>"; //簽名
      formData += "</xml>";



      //請(qǐng)求數(shù)據(jù)
      var getdata = sendPost(url, formData);

      //獲取xml數(shù)據(jù)
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(getdata);
      //xml格式轉(zhuǎn)json
      string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);



      JObject jo = (JObject)JsonConvert.DeserializeObject(json);
      string prepay_id = jo["xml"]["prepay_id"]["#cdata-section"].ToString();

      //時(shí)間戳
      string _time = getTime().ToString();

      //再次簽名返回?cái)?shù)據(jù)至小程序
      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";

      //向小程序發(fā)送json數(shù)據(jù)
       return JsonConvert.SerializeObject(w);
    }

    /// <summary>
    /// 生成隨機(jī)串  
    /// </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>
    /// 獲取時(shí)間戳
    /// </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">加密參數(shù)</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統(tǒng)一下單請(qǐng)求數(shù)據(jù)
    /// </summary>
    /// <param name="URL">請(qǐng)求地址</param>
    /// <param name="urlArgs">參數(shù)</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);//返回接受的數(shù)據(jù) 
      return returnStr;202     }

    /// <summary>
    /// 生成訂單號(hào)
    /// </summary>
    /// <returns></returns>
    private static string getRandomTime()
    {
      Random rd = new Random();//用于生成隨機(jī)數(shù)
      string DateStr = DateTime.Now.ToString("yyyyMMddHHmmssMM");//日期
      string str = DateStr + rd.Next(10000).ToString().PadLeft(4, '0');//帶日期的隨機(jī)數(shù)
      return str;
    }

  }
}

使用的是MVC .NET Framework4

微信小程序支付前端源碼

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#異步編程Task的創(chuàng)建方式

    C#異步編程Task的創(chuàng)建方式

    這篇文章介紹了C#異步編程Task的創(chuàng)建方式,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • .net 隨機(jī)生成漢字

    .net 隨機(jī)生成漢字

    在c#中可以使用system.text來處理所有語言編碼,其中encodiong類是比較重要的漢字編碼類,接下來我們就圍繞c#隨機(jī)生成漢字代碼講起,有需要的朋友可以參考下
    2015-08-08
  • C#使用泛型方法實(shí)現(xiàn)操作不同數(shù)據(jù)類型的數(shù)組

    C#使用泛型方法實(shí)現(xiàn)操作不同數(shù)據(jù)類型的數(shù)組

    這篇文章主要為大家詳細(xì)介紹了C#如何使用一個(gè)泛型方法對(duì)不同類型的數(shù)組進(jìn)行操作,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下
    2024-02-02
  • C#數(shù)字圖像處理之圖像縮放的方法

    C#數(shù)字圖像處理之圖像縮放的方法

    這篇文章主要介紹了C#數(shù)字圖像處理之圖像縮放的方法,涉及C#針對(duì)圖像的縮放技巧,需要的朋友可以參考下
    2015-04-04
  • 詳解C#中String.ToCharArray方法的使用

    詳解C#中String.ToCharArray方法的使用

    這篇文章主要為大家詳細(xì)介紹了C#中String.ToCharArray方法的使用的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • C#9新特性之增強(qiáng)的模式匹配

    C#9新特性之增強(qiáng)的模式匹配

    這篇文章主要介紹了C#9新特性之增強(qiáng)的模式匹配,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C#不可變類型深入解析

    C#不可變類型深入解析

    這篇文章主要介紹了C#不可變類型,對(duì)于C#程序員深入理解string數(shù)據(jù)類型有很大的幫助,需要的朋友可以參考下
    2014-08-08
  • 詳解C#中一維數(shù)組的插入

    詳解C#中一維數(shù)組的插入

    本文內(nèi)容給大家分享了在C#中進(jìn)行一維數(shù)組的插入的詳細(xì)實(shí)例代碼,大家可以測試下。
    2018-03-03
  • C#實(shí)現(xiàn)字體旋轉(zhuǎn)的方法

    C#實(shí)現(xiàn)字體旋轉(zhuǎn)的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)字體旋轉(zhuǎn)的方法,涉及C#通過Matrix實(shí)現(xiàn)字體旋轉(zhuǎn)效果的方法,需要的朋友可以參考下
    2015-06-06
  • C#實(shí)現(xiàn)掃描局域網(wǎng)內(nèi)的所有IP和端口

    C#實(shí)現(xiàn)掃描局域網(wǎng)內(nèi)的所有IP和端口

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)掃描局域網(wǎng)內(nèi)的所有IP和端口的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12

最新評(píng)論