接口地址 :

​https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi​

需要引用

using System.Security.Cryptography;

发起请求、加密、签名代码如下

/// <summary>
///
/// </summary>
/// <param name="url">微信的接口地址</param>
/// <param name="postData">post请求的数据,json格式 </param>
/// <param name="privateKey">apiclient_key.pem中的内容,不要-----BEGIN PRIVATE KEY----- -----END PRIVATE KEY-----</param>
/// <param name="merchantId">发起请求的商户(包括直连商户、服务商或渠道商)的商户号 mchid</param>
/// <param name="serialNo">商户证书号</param>
/// <returns></returns>
public string postJson(string url, string postData, string privateKey, string merchantId, string serialNo)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
request.UserAgent = "faloo.com/WebRequest";
request.Accept = "application/json";

string Authorization = GetAuthorization(url, "POST", postData, privateKey, merchantId, serialNo);
request.Headers.Add("Authorization", Authorization);
byte[] paramJsonBytes;
paramJsonBytes = System.Text.Encoding.UTF8.GetBytes(postData);
request.ContentLength = paramJsonBytes.Length;
Stream writer;
try
{
writer = request.GetRequestStream();
}
catch (Exception)
{
writer = null;
Console.Write("连接服务器失败!");
}
writer.Write(paramJsonBytes, 0, paramJsonBytes.Length);
writer.Close();
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = ex.Response as HttpWebResponse;
}
Stream resStream = response.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
string text = reader.ReadToEnd();
return text;
}

//获取HTTP头的授权信息
/// <summary>
///
/// </summary>
/// <param name="url">微信的接口地址</param>
/// <param name="method">请求的方式GET,POST,PUT</param>
/// <param name="jsonParame">post请求的数据,json格式 ,get时传空</param>
/// <param name="privateKey">apiclient_key.pem中的内容,不要-----BEGIN PRIVATE KEY----- -----END PRIVATE KEY-----</param>
/// <param name="merchantId">发起请求的商户(包括直连商户、服务商或渠道商)的商户号 mchid</param>
/// <param name="serialNo">商户证书号</param>
/// <returns></returns>
protected string GetAuthorization(string url, string method, string jsonParame, string privateKey, string merchantId, string serialNo)
{
var uri = new Uri(url);
string urlPath = uri.PathAndQuery;
string nonce = Guid.NewGuid().ToString();
var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
//数据签名 HTTP请求方法\n接口地址的url\n请求时间戳\n请求随机串\n请求报文主体\n
method = string.IsNullOrEmpty(method) ? "" : method;
string message = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n", method, urlPath, timestamp, nonce, jsonParame);

调用接口代码

原文的方法不全,我这边在测试的时候,把不全的代码已经不齐了

string appid = ""; //公众号ID
string merchantId = ""; //商户号
string serialNo = ""; //证书编号
string privateKey = @""; // NOTE: 私钥不包括私钥文件起始的-----BEGIN PRIVATE KEY----- 亦不包括结尾的-----END PRIVATE KEY-----
int UserID = 10000;//本地数据库用户ID
string UserThirdId = "";//用户账号通过微信登陆授权后的 openid
string url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";


StringBuilder str = new StringBuilder();
str.Append("{");
str.AppendFormat(" \"mchid\": \"{0}\",", merchantId);
str.AppendFormat(" \"out_trade_no\": \"{0}_{1}\",", DateTime.Now.ToString("yyyyMMddHHmmss"), UserID);
str.Append(" \"appid\": \""+ appid + "\",");
str.Append(" \"description\": \"描述如某某充值\",");
str.Append(" \"notify_url\": \"http://www.qq.com/WeiXinPay/NotifyUrl\","); //支付成功后的通知地址
str.Append(" \"amount\": {");
str.AppendFormat(" \"total\": {0},", 1); //支付金额
str.Append(" \"currency\": \"CNY\"");
str.Append(" },");
str.Append(" \"payer\": {");
str.Append(" \"openid\": \"" + UserThirdId + "\"");
str.Append(" }");
str.Append("}");

string transactionsResponse = postJson(url, str.ToString(), privateKey, merchantId, serialNo);