商家转账到零钱 c#
时间: 2023-11-24 15:52:43 浏览: 222
根据提供的引用内容,我们可以使用微信支付API实现商家转账到零钱。以下是C#代码示例:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace WeChatPayDemo
{
class Program
{
static void Main(string[] args)
{
//微信支付分配的商户号
string mchId = "your_mch_id";
//微信支付分配的APPID
string appId = "your_app_id";
//微信支付分配的密钥
string key = "your_key";
//随机字符串
string nonceStr = Guid.NewGuid().ToString().Replace("-", "");
//商户订单号
string partnerTradeNo = "your_partner_trade_no";
//用户openid
string openid = "your_openid";
//转账金额,单位为分
int amount = 100;
//转账描述
string description = "your_description";
//IP地址
string spbillCreateIp = "your_spbill_create_ip";
//构造参数
Dictionary<string, string> paras = new Dictionary<string, string>();
paras.Add("mch_appid", appId);
paras.Add("mchid", mchId);
paras.Add("nonce_str", nonceStr);
paras.Add("partner_trade_no", partnerTradeNo);
paras.Add("openid", openid);
paras.Add("check_name", "NO_CHECK");
paras.Add("amount", amount.ToString());
paras.Add("desc", description);
paras.Add("spbill_create_ip", spbillCreateIp);
//生成签名
string sign = GenerateSign(paras, key);
//构造XML请求参数
StringBuilder sb = new StringBuilder();
sb.Append("<xml>");
sb.Append("<mch_appid>" + appId + "</mch_appid>");
sb.Append("<mchid>" + mchId + "</mchid>");
sb.Append("<nonce_str>" + nonceStr + "</nonce_str>");
sb.Append("<partner_trade_no>" + partnerTradeNo + "</partner_trade_no>");
sb.Append("<openid>" + openid + "</openid>");
sb.Append("<check_name>NO_CHECK</check_name>");
sb.Append("<amount>" + amount + "</amount>");
sb.Append("<desc>" + description + "</desc>");
sb.Append("<spbill_create_ip>" + spbillCreateIp + "</spbill_create_ip>");
sb.Append("<sign>" + sign + "</sign>");
sb.Append("</xml>");
//调用微信支付API
string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
string response = HttpService.Post(url, sb.ToString());
//解析XML响应结果
XmlDocument doc = new XmlDocument();
doc.LoadXml(response);
XmlNode root = doc.SelectSingleNode("xml");
string returnCode = root.SelectSingleNode("return_code").InnerText;
string returnMsg = root.SelectSingleNode("return_msg").InnerText;
string resultCode = root.SelectSingleNode("result_code").InnerText;
string errCode = root.SelectSingleNode("err_code").InnerText;
string errCodeDes = root.SelectSingleNode("err_code_des").InnerText;
string paymentNo = root.SelectSingleNode("payment_no").InnerText;
string paymentTime = root.SelectSingleNode("payment_time").InnerText;
//输出结果
Console.WriteLine("return_code: " + returnCode);
Console.WriteLine("return_msg: " + returnMsg);
Console.WriteLine("result_code: " + resultCode);
Console.WriteLine("err_code: " + errCode);
Console.WriteLine("err_code_des: " + errCodeDes);
Console.WriteLine("payment_no: " + paymentNo);
Console.WriteLine("payment_time: " + paymentTime);
Console.ReadLine();
}
//生成签名
private static string GenerateSign(Dictionary<string, string> paras, string key)
{
//按照参数名ASCII码从小到大排序
var dic = paras.OrderBy(d => d.Key);
//使用URL键值对的格式拼接成字符串
var sb = new StringBuilder();
foreach (var d in dic)
{
if (!string.IsNullOrEmpty(d.Value) && d.Key != "sign")
{
sb.Append(d.Key + "=" + d.Value + "&");
}
}
sb.Append("key=" + key);
//MD5加密
var md5 = System.Security.Cryptography.MD5.Create();
var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()));
var sb2 = new StringBuilder();
foreach (byte b in bs)
{
sb2.Append(b.ToString("x2"));
}
return sb2.ToString().ToUpper();
}
}
//HTTP请求工具类
public class HttpService
{
public static string Post(string url, string postData)
{
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] postDataBytes = System.Text.Encoding.UTF8.GetBytes(postData);
byte[] responseData = webClient.UploadData(url, "POST", postDataBytes);
string responseString = System.Text.Encoding.UTF8.GetString(responseData);
return responseString;
}
}
}
```
阅读全文