asp.net微信支付打通发货通知代码

时间:2024-12-06 13:07:32
上次遇到微信支付,发货接口的时候,官方的demo也没有提供相应的代码 ,因本人技术有限,百度 google 很久都没有asp.net 版本的,最后只好硬着头皮自己搞,没想到官方文档也是错的。

  我这一步是在支付完成之后做的回调

首先是获取access_token

http 请求方式: GET
地址:
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret
=APPSECRET
这里我分享一个获取的方法 //AccessToken
public string GetToken()
{
if (AccessToken != "")
return AccessToken; string res = "";
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + System.Configuration.ConfigurationManager.AppSettings["appid"].ToString() + "&secret=" + System.Configuration.ConfigurationManager.AppSettings["secret"].ToString());
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd(); // List<ACCESSTOKEN> myACCESSTOKEN = FluorineFx.Json.JSONStringToList<ACCESSTOKEN>(content);
JavaScriptSerializer jser = new JavaScriptSerializer();
ACCESSTOKEN myACCESSTOKEN = jser.Deserialize<ACCESSTOKEN>(content); //反序列化 res = myACCESSTOKEN.access_token; }
AccessToken = res;
return res;
} [Serializable]
public class ACCESSTOKEN
{
public string access_token;
public string expires_in;
} 有了accesstoken 就可以post PostData数据到微信服务器了, 这里我先将postData数据排列到json里面,坑坑的,微信官方文档json参数的顺序是错的,需要按照a-z的顺序,这里我就没有sort数据直接排序了。 String Timestamp = TenpayUtil.getTimestamp(); string str = "appid=" + System.Configuration.ConfigurationManager.AppSettings["appid"].ToString() + "&appkey=" + System.Configuration.ConfigurationManager.AppSettings["Appkey"].ToString() + "&deliver_msg=ok&deliver_status=1&deliver_timestamp=" + Timestamp + "&openid=openid&out_trade_no=out_trade_no&transid=transid";
Response.Write(str);
AppSignature = getSha1(str).ToLower();
Response.Write("AppSignature:"+AppSignature);
WriteLog("AppSignature:" + AppSignature); string postData = "{\"appid\": \"" + System.Configuration.ConfigurationManager.AppSettings["appid"].ToString() + "\",\"openid\": \"openid\",\"transid\": \"transid\",\"out_trade_no\": \"out_trade_no\",\"deliver_timestamp\": \"" + Timestamp + "\", \"deliver_status\": \"1\",\"deliver_msg\": \"ok\",\"app_signature\":\"" + AppSignature + "\",\"sign_method\":\"sha1\"}";
WriteLog("PostDate:" + postData); //sha1加密是用微信官方的 public static String getSha1(String str)
{
//建立SHA1对象
SHA1 sha = new SHA1CryptoServiceProvider();
//将mystr转换成byte[]
ASCIIEncoding enc = new ASCIIEncoding();
byte[] dataToHash = enc.GetBytes(str);
//Hash运算
byte[] dataHashed = sha.ComputeHash(dataToHash);
//将运算结果转换成string
string hash = BitConverter.ToString(dataHashed).Replace("-", "");
return hash;
} 这里最后用了一个GetPage 后台Post数据包GetPost 给微信服务器就可以了 public string GetPage(string posturl, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(postData);
// 准备请求...
try
{
// 设置参数
request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
outstream = request.GetRequestStream();
outstream.Write(data, , data.Length);
outstream.Close();
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
string err = string.Empty;
WriteLog("content:" + content);
return content;
}
catch (Exception ex)
{
string err = ex.Message;
Response.Write(err); WriteLog("err:" + err);
return string.Empty;
}
} 最后直接在你的backurl(回调页面发送请求就可以了) /// <summary>
/// 回调发货通知
/// </summary>
public void RollBack()
{
String Timestamp = TenpayUtil.getTimestamp(); string str = "appid=" + System.Configuration.ConfigurationManager.AppSettings["appid"].ToString() + "&appkey=" + System.Configuration.ConfigurationManager.AppSettings["Appkey"].ToString() + "&deliver_msg=ok&deliver_status=1&deliver_timestamp=" + Timestamp + "&openid=oF2a_t8-&out_trade_no=out_trade_no&transid=transid";
Response.Write(str);
AppSignature = getSha1(str).ToLower();
Response.Write("AppSignature:"+AppSignature);
WriteLog("AppSignature:" + AppSignature); string postData = "{\"appid\": \"" + System.Configuration.ConfigurationManager.AppSettings["appid"].ToString() + "\",\"openid\": \"oF2a_t8-\",\"transid\": \"transid\",\"out_trade_no\": \"out_trade_no\",\"deliver_timestamp\": \"" + Timestamp + "\", \"deliver_status\": \"1\",\"deliver_msg\": \"ok\",\"app_signature\":\"" + AppSignature + "\",\"sign_method\":\"sha1\"}";
WriteLog("PostDate:" + postData); string access_token = GetToken();
Response.Write("access_token" + access_token);
string i = GetPage("https://api.weixin.qq.com/pay/delivernotify?access_token=" + access_token, postData); Response.Write("postdate"+postData); } 有需要的也可以留言交流.