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 = "";
public static string _key = "192006250b4c09247ec02edce69f6a2d"; //模拟wx统一下单 openid(前台获取)
public string getda(string openid)
{
return Getprepay_id(_appid, "shanghaifendian", "monixiaofei", _mch_id, GetRandomString(), "http://www.weixin.qq.com/wxpay/pay.php", openid, getRandomTime(), ); } //微信统一下单获取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自己写的一个类
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 < )
return string.Empty; Random rnd = new Random();
byte[] buffer = new byte[]; ulong bit = ;
ulong result = ;
int index = ;
StringBuilder sb = new StringBuilder((length / + ) * ); while (sb.Length < length)
{
rnd.NextBytes(buffer); buffer[] = buffer[] = buffer[] = 0x00;
result = BitConverter.ToUInt64(buffer, ); while (result > && sb.Length < length)
{
index = (int)(bit & result);
sb.Append(key[index]);
result = result >> ;
}
}
return sb.ToString();
} /// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
private static long getTime()
{
TimeSpan cha = (DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , )));
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 = ; 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)
{ 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(urlArgs); 如果微信签名中有中文会签名失败
      byte[] postData = System.Text.Encoding.UTF8.GetBytes(urlArgs);     
byte[] responseData = wCient.UploadData(URL, "POST", postData);
        
string returnStr = System.Text.Encoding.UTF8.GetString(responseData);//返回接受的数据
return returnStr;
} /// <summary>
/// 生成订单号
/// </summary>
/// <returns></returns>
private static string getRandomTime()
{
Random rd = new Random();//用于生成随机数
string DateStr = DateTime.Now.ToString("yyyyMMddHHmmssMM");//日期
string str = DateStr + rd.Next().ToString().PadLeft(, '');//带日期的随机数
return str;
} }
}

使用的是MVC .NET Framework4

微信小程序支付 小程序端源码

MVC项目发布前的配置

微信小程序支付C#后端源码的更多相关文章

  1. 基于olami开放语义平台的微信小程序遥知之源码实现

    概述 实现一个智能生活信息查询的小秘书功能,支持查天气.新闻.日历.汇率.笑话.故事.百科.诗词.邮编.区号.菜谱.股票.节目预告,还支持闲聊.算24点.数学计算.单位换算.购物.搜索等功能. 使用方 ...

  2. 微信小程序支付+php后端

    最近在做自有项目后端用的是thinkphp5.1框架,闲话不说直接上代码 小程序代码 wxpay: function(e){ let thisid = e.currentTarget.dataset. ...

  3. 微信小程序支付源码,后台服务端代码

    作者:尹华南,来自原文地址 微信小程序支付绕坑指南 步骤 A:小程序向服务端发送商品详情.金额.openid B:服务端向微信统一下单 C:服务器收到返回信息二次签名发回给小程序 D:小程序发起支付 ...

  4. Java 后端微信小程序支付demo (网上说的坑里面基本上都有)

    Java 后端微信小程序支付 一.遇到的问题 1. 商户号该产品权限未开通,请前往商户平台>产品中心检查后重试 2.签名错误 3.已经调起微信统一下单接口,可以拿到预支付ID,但是前端支付的时候 ...

  5. 通俗易懂,C#如何安全、高效地玩转任何种类的内存之Span的脾气秉性(二)。 异步委托 微信小程序支付证书及SSL证书使用 SqlServer无备份下误删数据恢复 把list集合的内容写入到Xml中,通过XmlDocument方式写入Xml文件中 通过XDocument方式把List写入Xml文件

    通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的脾气秉性(二).   前言 读完上篇<通俗易懂,C#如何安全.高效地玩转任何种类的内存之Span的本质(一).>,相信大家对sp ...

  6. .Net后台实现微信小程序支付

    最近一直再研究微信支付和支付宝支付,官方支付文档中一直在讲与第三方支付打交道的原理,却没有介绍我们自己项目中的APP与后台该怎么交互(哈哈,人家也没必要介绍这一块).拜读了官方文档和前辈们的佳作,自己 ...

  7. 记录一次用宝塔部署微信小程序Node.js后端接口代码的详细过程

    一直忙着写毕设,上一次写博客还是元旦,大半年过去了.... 后面会不断分享各种新项目的源码与技术.欢迎关注一起学习哈! 记录一次部署微信小程序Node.js后端接口代码的详细过程,使用宝塔来部署. 我 ...

  8. 微信小程序支付及退款流程详解

    微信小程序的支付和退款流程 近期在做微信小程序时,涉及到了小程序的支付和退款流程,所以也大概的将这方面的东西看了一个遍,就在这篇博客里总结一下. 首先说明一下,微信小程序支付的主要逻辑集中在后端,前端 ...

  9. SpringBoot2.0微信小程序支付多次回调问题

    SpringBoot2.0微信小程序支付多次回调问题 WxJava - 微信开发 Java SDK(开发工具包); 支持包括微信支付.开放平台.公众号.企业微信/企业号.小程序等微信功能的后端开发. ...

随机推荐

  1. TZOJ 2754 Watering Hole(最小生成树Kruskal)

    描述 Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are convenie ...

  2. 【C基础】之联合体

    1.联合体 联合体(union)与结构体(struct)有一些相似之处.但两者有本质上的不同.在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和.而在联合体中,各成员共享一段内 ...

  3. IntentService----意图服务

    意图服务是异步进行的  执行完操作后就会自己消毁(onDestroy方法) 本例为点击按钮下载三张图片相当于连续执行三次意图服务中的onStartcommand方法 import android.ap ...

  4. 如何移除 input type="number" 时浏览器自带的上下箭头?

    Chrome 下 input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: no ...

  5. [精彩] 关于DB2的内存分配

    这两天在看DB2的内存管理的内容,看的很是模糊,有以下问题不明白,请教 是不是数据库管理器的共享内存就是DB2能够使用的最大内容呢,然后数据库全局内存从管理器内存那里获得分配的内存,然后应用程序全局内 ...

  6. C++11新特性——The C++ standard library, 2nd Edition 笔记(一)

    前言 这是我阅读<The C++ standard library, 2nd Edition>所做读书笔记的第一篇.这个系列基本上会以一章一篇的节奏来写,少数以C++03为主的章节会和其它 ...

  7. 如何使用css来让图片居中不变形 微信小程序和web端适用

    图片变形很多人祭奠出了妖魔鬼怪般的各种大法,比如使用jq来写,或者使用css表达式来写.今天我总结的是使用css3来写,唯一最大缺点就是对一些浏览器版本不够兼容.下面就是关于如何使用css来让图片居中 ...

  8. 2018.09.15 秘密的牛奶管道SECRET(次小生成树)

    描述 约翰叔叔希望能够廉价连接他的供水系统,但是他不希望他的竞争对手知道他选择的路线.一般这样的问题需要选择最便宜的方式,所以他决定避免这种情况而采用第二便宜的方式. 现在有W(3 <= W & ...

  9. 2018.07.20 bzoj2152: 聪聪可可(点分治)

    传送门 本蒟蒻AC的第二道点分治,调了30min" role="presentation" style="position: relative;"&g ...

  10. Nginx安装SSL安全证书

    1. 在Nginx的安装目录下的config目录下创建cert目录,并且将下载的证书全部文件拷贝到cert目录中.如果申请证书时是自己创建的CSR文件,请将对应的私钥文件放到cert目录下并且命名为2 ...