关于微信提现到余额,本次仅做参考:
微信提现:首先需要获取手机微信的openID,默认的IP号(如:192.168.1.1),微信的商户号,APPID,KYE值,以及公众号的apiclient_cert.p12文件的绝对位置:
并使用一下方法:获取weiPayResult.result_code的返回值是否成功,以此判断本次提现是否成功。
方法一:
public static WeiPayResult SingleWeiPay(int amount, string desc, string openid, string realname, string tradeno, int UserId,string txtip, string WeixinPartnerID,string WeixinAppId,string WeixinPartnerKey,string BatchWeixinPayCheckRealName, string WeixinCertPath,string WeixinCertPassword)
{
OutPayHelp.WeiXinMchid = WeixinPartnerID;
OutPayHelp.WeiXinAppid = WeixinAppId;
OutPayHelp.WeiXinKey = WeixinPartnerKey;
OutPayHelp.BatchWeixinPayCheckRealName = BatchWeixinPayCheckRealName.ToString();
OutPayHelp.WeiXinCertPath = WeixinCertPath;
OutPayHelp.WeixinCertPassword = WeixinCertPassword;
string batchWeixinPayCheckRealName = OutPayHelp.BatchWeixinPayCheckRealName;
if (batchWeixinPayCheckRealName != null)
{
if (!(batchWeixinPayCheckRealName == "0"))
{
if (!(batchWeixinPayCheckRealName == "1"))
{
if (batchWeixinPayCheckRealName == "2")
{
OutPayHelp.BatchWeixinPayCheckRealName = "OPTION_CHECK";
}
}
else
{
OutPayHelp.BatchWeixinPayCheckRealName = "FORCE_CHECK";
}
}
else
{
OutPayHelp.BatchWeixinPayCheckRealName = "NO_CHECK";
}
}
WeiPayResult weiPayResult = new WeiPayResult();
weiPayResult.return_code = "SUCCESS";
weiPayResult.err_code = "";
weiPayResult.return_msg = "微信企业付款参数配置错误";
if (OutPayHelp.WeiXinMchid == "")
{
weiPayResult.return_code = "FAIL";
weiPayResult.return_msg = "商户号未配置!";
}
else if (OutPayHelp.WeiXinAppid == "")
{
weiPayResult.return_code = "FAIL";
weiPayResult.return_msg = "公众号APPID未配置!";
}
else if (OutPayHelp.WeiXinKey == "")
{
weiPayResult.return_code = "FAIL";
weiPayResult.return_msg = "商户密钥未配置!";
}
WeiPayResult result;
if (weiPayResult.return_code == "FAIL")
{
result = weiPayResult;
}
else
{
weiPayResult.return_code = "FAIL";
weiPayResult.return_msg = "用户参数出错了!";
result = OutPayHelp.WeiXinPayOut(new OutPayWeiInfo
{
Amount = amount,
Partner_Trade_No = tradeno,
Openid = useropenid,
Re_User_Name = realname,
Desc = desc,
UserId = UserId,
device_info = "",
Nonce_Str = OutPayHelp.GetRandomString(20)
}, OutPayHelp.WeiXinAppid, OutPayHelp.WeiXinMchid, OutPayHelp.BatchWeixinPayCheckRealName, OutPayHelp.WeiXinKey, txtip);
}
return result;
}
方法二:
public static WeiPayResult WeiXinPayOut(OutPayWeiInfo payinfos, string Mch_appid, string Mchid, string Check_Name, string _key,string txtip)
{
SortedDictionary<string, string> sortedDictionary = new SortedDictionary<string, string>();
sortedDictionary.Add("mch_appid", Mch_appid);
sortedDictionary.Add("mchid", Mchid);
sortedDictionary.Add("nonce_str", payinfos.Nonce_Str);
sortedDictionary.Add("partner_trade_no", payinfos.Partner_Trade_No);
sortedDictionary.Add("openid", payinfos.Openid);
sortedDictionary.Add("check_name", Check_Name);
sortedDictionary.Add("amount", payinfos.Amount.ToString());
sortedDictionary.Add("desc", payinfos.Desc);
sortedDictionary.Add("spbill_create_ip", txtip);
sortedDictionary.Add("re_user_name", payinfos.Re_User_Name);
sortedDictionary.Add("device_info", "");
string text = "";
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("<xml>");
foreach (string current in sortedDictionary.Keys)
{
if (sortedDictionary[current] != "")
{
string text2 = text;
text = string.Concat(new string[]
{
text2,
"&",
current,
"=",
sortedDictionary[current]
});
stringBuilder.AppendLine(string.Concat(new string[]
{
"<",
current,
">",
sortedDictionary[current],
"</",
current,
">"
}));
}
}
text = text.Remove(0, 1);
text = text + "&key=" + _key;
text = OutPayHelp.GetMD5(text, "utf-8");
text = text.ToUpper();
stringBuilder.AppendLine("<sign>" + text + "</sign>");
stringBuilder.AppendLine("</xml>");
HttpHelp httpHelp = new HttpHelp();
//通过此地的text3的内容来判断整个方法的返回
string text3 = httpHelp.DoPost(OutPayHelp.WeiPayUrl, stringBuilder.ToString(), OutPayHelp.WeixinCertPassword, OutPayHelp.WeiXinCertPath);
WeiPayResult weiPayResult = new WeiPayResult();
weiPayResult.return_code = "FAIL";
weiPayResult.return_msg = "访问服务器出错了!";
weiPayResult.err_code = "SERVERERR";
weiPayResult.UserId = payinfos.UserId;
weiPayResult.Amount = payinfos.Amount;
weiPayResult.partner_trade_no = payinfos.Partner_Trade_No;
WeiPayResult result;
if (httpHelp.errstr != "")
{
weiPayResult.return_msg = httpHelp.errstr;
result = weiPayResult;
}
else
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(text3);
weiPayResult.return_code = xmlDocument.SelectSingleNode("/xml/return_code").InnerText;
weiPayResult.return_msg = xmlDocument.SelectSingleNode("/xml/return_msg").InnerText;
if (weiPayResult.return_code.ToUpper() == "SUCCESS")
{
weiPayResult.result_code = xmlDocument.SelectSingleNode("/xml/result_code").InnerText;
if (weiPayResult.result_code.ToUpper() == "SUCCESS")
{
weiPayResult.mch_appid = xmlDocument.SelectSingleNode("/xml/mch_appid").InnerText;
weiPayResult.mchid = xmlDocument.SelectSingleNode("/xml/mchid").InnerText;
weiPayResult.device_info = xmlDocument.SelectSingleNode("/xml/device_info").InnerText;
weiPayResult.nonce_str = xmlDocument.SelectSingleNode("/xml/nonce_str").InnerText;
weiPayResult.result_code = xmlDocument.SelectSingleNode("/xml/result_code").InnerText;
weiPayResult.partner_trade_no = xmlDocument.SelectSingleNode("/xml/partner_trade_no").InnerText;
weiPayResult.payment_no = xmlDocument.SelectSingleNode("/xml/payment_no").InnerText;
weiPayResult.payment_time = xmlDocument.SelectSingleNode("/xml/payment_time").InnerText;
}
else
{
weiPayResult.err_code = xmlDocument.SelectSingleNode("/xml/err_code").InnerText;
}
}
else
{
weiPayResult.err_code = "FAIL";
}
}
catch (Exception ex)
{
weiPayResult.return_code = "FAIL";
weiPayResult.return_msg = ex.Message.ToString();
}
result = weiPayResult;
}
return result;
}
注:微信提现时为保证整数,所传的金额都会乘以100倍。