public void WxOpenProfit(string OrderID)
{
WeixinTrace.Log("\r\n\r\n----------------------------分账开始-----------------------------------------------\r\n\r\n");
Logger logger = LogManager.GetLogger("SimpleDemo");
string mch_id = Globals.Configuration["AppSettings:TenPayV3_MchId"].ToString();
string appid = Globals.Configuration["AppSettings:WxOpenAppId"].ToString();
string TenPayV3_Key = Globals.Configuration["AppSettings:TenPayV3_Key"].ToString();
string nonce_str = TenPayV3Util.GetNoncestr();
ShopOrder order = this.CreateService<IShopOrderService>().GetModel(OrderID);
List<Shop_ShareOrder> share_list = this.CreateService<IShop_ShareService>().GetShopShareOrderList(OrderID);
List<Receiver> receivers = new List<Receiver>();
Receiver receiver = null;
foreach (var item in share_list)
{
if (item.Tag == "客户经理")
{
receiver = new Receiver();
receiver.account = item.Account;
receiver.amount = Convert.ToInt32(item.ShareTotal * 100);
receiver.description = item.OrderCode + "分账给客户经理:" + item.IDName;
receiver.type = item.SortName;
receivers.Add(receiver);
}
}
string jsons = Newtonsoft.Json.JsonConvert.SerializeObject(receivers);
logger.Info("\r\n\r\n*********************************分账表************************************\r\n\r\n");
logger.Info(jsons);
var IPF = this.CreateService<IProfitService>();
Profit profitInfo = IPF.GetModelByOrderID(OrderID);
//微信支付订单号
string transaction_id = profitInfo.wxTransactionID;
//商户分账单号
string out_order_no = profitInfo.wxOutTradeNo;
string stringA = "appid=" + appid + "&mch_id=" + mch_id + "&nonce_str=" + nonce_str + "&out_order_no=" + out_order_no + "&receivers=" + jsons + "&transaction_id=" + transaction_id;
string stringSignTemp = stringA + "&key=" + TenPayV3_Key; //注:key为商户平台设置的密钥key+
string sign = EncryptHelper.GetHmacSha256(stringSignTemp, TenPayV3_Key).ToUpper();
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("appid", appid);
dic.Add("mch_id", mch_id);
dic.Add("nonce_str", nonce_str);
dic.Add("out_order_no", out_order_no);
dic.Add("transaction_id", transaction_id);
dic.Add("sign", sign);
dic.Add("receivers", jsons);
StringBuilder sb = new StringBuilder();
sb.Append("<xml>");
foreach (string k in dic.Keys)
{
string v = (string)dic[k];
sb.Append("<" + k + ">" + v + "</" + k + ">");
}
sb.Append("</xml>");
string post_xml = sb.ToString();
logger.Info("\r\n\r\n*********************************发送数据************************************\r\n\r\n");
logger.Info(post_xml);
string result = string.Empty;
var handler = new HttpClientHandler();
X509Certificate2 certificate = GetMyX509Certificate(mch_id);
logger.Info("\r\n\r\n*********************************证书************************************\r\n\r\n");
logger.Info(certificate.ToJson());
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.SslProtocols = SslProtocols.Tls12;
handler.ClientCertificates.Add(certificate);
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
var content = new StringContent(post_xml);
content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
var response = httpClient.PostAsync("https://api.mch.weixin.qq.com/secapi/pay/profitsharing", content).Result;
result = response.Content.ReadAsStringAsync().Result;
}
logger.Info("\r\n\r\n*********************************返回数据************************************\r\n\r\n");
logger.Info(result);
wxProfit return_profit = JsonConvert.DeserializeObject<wxProfit>(result);
if (return_profit.return_code == "SUCCESS")
{
if (return_profit.result_code == "SUCCESS")
{
UpdateProfitInput update = new UpdateProfitInput();
update.Attach = profitInfo.Attach;
update.CreateDate = profitInfo.CreateDate;
update.Id = profitInfo.Id;
update.OpenID = profitInfo.OpenID;
update.OrderID = profitInfo.OrderID;
update.ResultCode = return_profit.result_code;
update.ReturnCode = return_profit.return_code;
update.ShopID = profitInfo.ShopID;
update.TimeEnd = DateTime.Now;
update.WxOrderID = return_profit.order_id;
update.wxOutTradeNo = profitInfo.wxOutTradeNo;
update.wxTransactionID = profitInfo.wxTransactionID;
update.ReMark = "ok";
IPF.Update(update);
List<Profit_Item> profit_Items = new List<Profit_Item>();
Profit_Item profit_Item = null;
foreach (var item in share_list)
{
profit_Item = new Profit_Item();
profit_Item.Account = item.Account;
profit_Item.Amount = Convert.ToInt32(item.ShareTotal * 100);
profit_Item.Id = "";
profit_Item.ProfitID = profitInfo.Id;
profit_Item.Type = item.SortName;
profit_Item.UserID = item.UserID;
if (item.Tag == "客户经理")
{
profit_Item.Description = item.OrderCode + "自动分账给客户经理:" + item.IDName;
}
else
{
profit_Item.Description = item.OrderCode + "手动分账给商家:" + item.IDName;
}
profit_Items.Add(profit_Item);
}
this.CreateService<IProfit_ItemService>().BatchInsert(profit_Items);//保存分账列表
//return this.SuccessMsg("分账成功");
}
else
{
UpdateProfitInput update = new UpdateProfitInput();
update.Attach = profitInfo.Attach;
update.CreateDate = profitInfo.CreateDate;
update.Id = profitInfo.Id;
update.OpenID = profitInfo.OpenID;
update.OrderID = profitInfo.OrderID;
update.ResultCode = return_profit.result_code;
update.ReturnCode = return_profit.return_code;
update.ShopID = profitInfo.ShopID;
update.TimeEnd = DateTime.Now;
update.WxOrderID = "";
update.wxOutTradeNo = profitInfo.wxOutTradeNo;
update.wxTransactionID = profitInfo.wxTransactionID;
update.ReMark = return_profit.err_code + ":" + return_profit.err_code_des;
IPF.Update(update);
//return this.FailedMsg();
}
}
else
{
UpdateProfitInput update = new UpdateProfitInput();
update.Attach = profitInfo.Attach;
update.CreateDate = profitInfo.CreateDate;
update.Id = profitInfo.Id;
update.OpenID = profitInfo.OpenID;
update.OrderID = profitInfo.OrderID;
update.ResultCode = "";
update.ReturnCode = return_profit.return_code;
update.ShopID = profitInfo.ShopID;
update.TimeEnd = DateTime.Now;
update.WxOrderID = "";
update.wxOutTradeNo = profitInfo.wxOutTradeNo;
update.wxTransactionID = profitInfo.wxTransactionID;
update.ReMark = "通信错误";
IPF.Update(update);
//return this.FailedMsg();
}
logger.Info("\r\n\r\n----------------------分账结束-----------------------------------------------------------------\r\n\r\n");
}