c# 对接通联的微信小程序支付代码
时间: 2023-10-28 16:01:58 浏览: 67
以下是一个C#对接通联微信小程序支付的示例代码:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Xml;
namespace WeChatPay
{
class Program
{
static void Main(string[] args)
{
string strAppid = "你的小程序的appid";
string strMchId = "你的商户号";
string strKey = "你的API密钥";
string strNonce = Guid.NewGuid().ToString().Replace("-", "");
string strBody = "商品描述";
string strOutTradeNo = "商户订单号";
string strTotalFee = "订单总金额,单位为分";
string strSpbillCreateIp = "调用微信支付API的机器的IP地址";
string strNotifyUrl = "接收微信支付异步通知回调地址";
string strTradeType = "JSAPI";
string strOpenid = "用户在商户appid下的唯一标识";
string strSign = "";
//生成签名
SortedDictionary<string, string> dic = new SortedDictionary<string, string>();
dic.Add("appid", strAppid);
dic.Add("mch_id", strMchId);
dic.Add("nonce_str", strNonce);
dic.Add("body", strBody);
dic.Add("out_trade_no", strOutTradeNo);
dic.Add("total_fee", strTotalFee);
dic.Add("spbill_create_ip", strSpbillCreateIp);
dic.Add("notify_url", strNotifyUrl);
dic.Add("trade_type", strTradeType);
dic.Add("openid", strOpenid);
string strSignTemp = "";
foreach (KeyValuePair<string, string> kvp in dic)
{
if (!string.IsNullOrEmpty(kvp.Value))
{
strSignTemp += kvp.Key + "=" + kvp.Value + "&";
}
}
strSignTemp += "key=" + strKey;
strSign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSignTemp, "MD5").ToUpper();
//生成XML请求参数
string strXml = "<xml>"
+ "<appid>" + strAppid + "</appid>"
+ "<mch_id>" + strMchId + "</mch_id>"
+ "<nonce_str>" + strNonce + "</nonce_str>"
+ "<body>" + strBody + "</body>"
+ "<out_trade_no>" + strOutTradeNo + "</out_trade_no>"
+ "<total_fee>" + strTotalFee + "</total_fee>"
+ "<spbill_create_ip>" + strSpbillCreateIp + "</spbill_create_ip>"
+ "<notify_url>" + strNotifyUrl + "</notify_url>"
+ "<trade_type>" + strTradeType + "</trade_type>"
+ "<openid>" + strOpenid + "</openid>"
+ "<sign>" + strSign + "</sign>"
+ "</xml>";
//向微信支付接口发送请求
string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] data = Encoding.UTF8.GetBytes(strXml);
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
//获取微信支付接口返回的数据
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string strResult = reader.ReadToEnd();
XmlDocument xmlResult = new XmlDocument();
xmlResult.LoadXml(strResult);
//解析返回数据,获取prepay_id
XmlNode xmlNode = xmlResult.SelectSingleNode("xml/prepay_id");
string strPrepayId = xmlNode.InnerText;
//生成小程序支付需要的参数
SortedDictionary<string, string> dicPay = new SortedDictionary<string, string>();
dicPay.Add("appId", strAppid);
dicPay.Add("nonceStr", strNonce);
dicPay.Add("package", "prepay_id=" + strPrepayId);
dicPay.Add("signType", "MD5");
dicPay.Add("timeStamp", Convert.ToInt64(DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds).ToString());
string strSignTempPay = "";
foreach (KeyValuePair<string, string> kvp in dicPay)
{
if (!string.IsNullOrEmpty(kvp.Value))
{
strSignTempPay += kvp.Key + "=" + kvp.Value + "&";
}
}
strSignTempPay += "key=" + strKey;
string strPaySign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSignTempPay, "MD5").ToUpper();
//返回小程序支付所需参数
Console.WriteLine("appId:" + strAppid);
Console.WriteLine("nonceStr:" + strNonce);
Console.WriteLine("package:" + "prepay_id=" + strPrepayId);
Console.WriteLine("signType:MD5");
Console.WriteLine("timeStamp:" + dicPay["timeStamp"]);
Console.WriteLine("paySign:" + strPaySign);
}
}
}
```
请注意替换代码中的变量值为您自己的实际值。此代码仅供参考,具体实现方式可能因为不同的商户号、API密钥等因素而有所不同。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)