/// <summary> /// 调用微信发送红包接口 /// 请求Url https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack /// 是否需要证书 是(证书及使用说明详见商户证书) /// 请求方式 POST /// </summary> /// <param name="certFilePath">证书位置</param> /// <param name="certPwd">证书密码</param> /// <param name="apiSecretKey">API安全密钥,设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置</param> /// <param name="mch_billno"> /// String(28) /// 商户订单号(每个订单号必须唯一) /// 组成:mch_id+yyyymmdd+10位一天内不能重复的数字。 /// 接口根据商户订单号支持重入,如出现超时可再调用。</param> /// <param name="mch_id">String(32) 微信支付分配的商户号</param> /// <param name="wxappid"> /// 微信分配的公众账号ID(企业号corpid即为此appId)。 /// 接口传入的所有appid应该为公众号的appid(在mp.weixin.qq.com申请的), /// 不能为APP的appid(在open.weixin.qq.com申请的)。 /// </param> /// <param name="send_name">String(32) 红包发送者名称</param> /// <param name="re_openid">String(32) 接受红包的用户 用户在wxappid下的openid</param> /// <param name="total_amount">付款金额,单位分</param> /// <param name="total_num">红包发放总人数,total_num=1</param> /// <param name="wishing">String(128) 红包祝福语</param> /// <param name="client_ip">String(15) 调用接口的机器Ip地址</param> /// <param name="act_name">String(32) 活动名称</param> /// <param name="remark">String(256) 备注信息</param> /// <returns></returns> public Entities.SendRedPackResult SendRedPack(string certFilePath, string certPwd, string apiSecretKey, string mch_billno, string mch_id, string wxappid, string send_name, string re_openid, int total_amount, int total_num, string wishing, string client_ip, string act_name, string remark) { var nonce_str = WeiXinHelper.GetNonceStr(); SortedDictionary <string, string> sortedDictParams = new SortedDictionary <string, string>(); sortedDictParams.Add("nonce_str", nonce_str); sortedDictParams.Add("mch_billno", mch_billno); sortedDictParams.Add("mch_id", mch_id); sortedDictParams.Add("wxappid", wxappid); sortedDictParams.Add("send_name", send_name); sortedDictParams.Add("re_openid", re_openid); sortedDictParams.Add("total_amount", total_amount.ToString()); sortedDictParams.Add("total_num", total_num.ToString()); sortedDictParams.Add("wishing", wishing); sortedDictParams.Add("client_ip", client_ip); sortedDictParams.Add("act_name", act_name); sortedDictParams.Add("remark", remark); var sign = WeiXinPayHelper.GetSign(sortedDictParams, apiSecretKey); return(SendRedPack(certFilePath, certPwd, nonce_str, sign, mch_billno, mch_id, wxappid, send_name, re_openid, total_amount, total_num, wishing, client_ip, act_name, remark)); }
/// <summary> /// 记录请求支付日志信息 /// </summary> /// <param name="response"></param> /// <param name="para"></param> /// <param name="bll"></param> /// <param name="entity"></param> /// <param name="channel"></param> /// <returns></returns> private static string CreatePayRecord(CreateOrderResponse response, CreateOrderParameters para, AppOrderBLL bll, AppOrderEntity entity, PayChannelEntity channel) { string url = string.Empty; //用于记录支付平台的请求和响应 string requestJson = string.Empty; string responseJson = string.Empty; var recordBll = new PayRequestRecordBLL(new Utility.BasicUserInfo()); var recordEntity = new PayRequestRecordEntity() { ChannelID = channel.ChannelID, ClientID = entity.AppClientID, UserID = entity.AppUserID }; #region 根据Channel类型创建支付订单 try { switch (channel.PayType) { case 1: recordEntity.Platform = 1; #region 银联Wap支付 UnionPayChannel unionWapPaychannel = channel.ChannelParameters.DeserializeJSONTo <UnionPayChannel>(); PreOrderRequest Wapreq = new PreOrderRequest() { BackUrl = ConfigurationManager.AppSettings["UnionPayWapNotifyUrl"].Trim('?') + string.Format("?ChannelID={0}", channel.ChannelID), FrontUrl = string.IsNullOrEmpty(para.ReturnUrl) ? ConfigurationManager.AppSettings["UnionPayCallBackUrl"] : para.ReturnUrl, MerchantID = unionWapPaychannel.MerchantID, SendTime = DateTime.Now, MerchantOrderCurrency = Currencys.RMB, MerchantOrderDesc = entity.AppOrderDesc, MerchantOrderAmt = entity.AppOrderAmount, MerchantOrderID = entity.OrderID.ToString(), SendSeqID = Guid.NewGuid().ToString("N"), MerchantOrderTime = entity.AppOrderTime }; requestJson = Wapreq.ToJSON(); var unionWapResponse = JIT.Utility.Pay.UnionPay.Interface.Wap.WapGateway.PreOrder(unionWapPaychannel, Wapreq); responseJson = unionWapResponse.ToJSON(); if (unionWapResponse.IsSuccess) { entity.PayUrl = unionWapResponse.RedirectURL; entity.Status = 1; bll.Update(entity); url = unionWapResponse.RedirectURL; Loggers.Debug(new DebugLogInfo() { Message = string.Format("银联Wap创建订单成功{0}【请求】:{1}{0}【响应】:{2}", Environment.NewLine, Wapreq.ToJSON(), unionWapResponse.ToJSON()) }); } else { Loggers.Debug(new DebugLogInfo() { Message = string.Format("银联Wap创建订单失败{0}【请求】:{1}{0}【响应】:{2}", Environment.NewLine, Wapreq.ToJSON(), unionWapResponse.ToJSON()) }); response.ResultCode = 100; response.Message = unionWapResponse.Description; } #endregion break; case 2: recordEntity.Platform = 1; #region 银联语音支付 UnionPayChannel unionIVRPaychannel = channel.ChannelParameters.DeserializeJSONTo <UnionPayChannel>(); JIT.Utility.Pay.UnionPay.Interface.IVR.Request.PreOrderRequest IVRreq = new Utility.Pay.UnionPay.Interface.IVR.Request.PreOrderRequest() { SendTime = DateTime.Now, SendSeqID = Guid.NewGuid().ToString("N"), FrontUrl = string.IsNullOrEmpty(para.ReturnUrl) ? ConfigurationManager.AppSettings["UnionPayCallBackUrl"] : para.ReturnUrl, BackUrl = ConfigurationManager.AppSettings["UnionPayIVRNotifyUrl"].Trim('?') + string.Format("?ChannelID={0}", channel.ChannelID), MerchantOrderDesc = entity.AppOrderDesc, Mode = IVRModes.Callback, TransTimeout = entity.AppOrderTime, MerchantOrderCurrency = Currencys.RMB, MerchantOrderAmt = entity.AppOrderAmount, MerchantID = unionIVRPaychannel.MerchantID, MerchantOrderTime = entity.AppOrderTime, MerchantOrderID = entity.OrderID.ToString(), MobileNum = entity.MobileNO }; requestJson = IVRreq.ToJSON(); var IvrResponse = IVRGateway.PreOrder(unionIVRPaychannel, IVRreq); responseJson = IvrResponse.ToJSON(); if (IvrResponse.IsSuccess) { entity.Status = 1; bll.Update(entity); Loggers.Debug(new DebugLogInfo() { Message = string.Format("银联IVR创建订单成功{0}【请求】:{1}{0}【响应】:{2}", Environment.NewLine, IVRreq.ToJSON(), IvrResponse.ToJSON()) }); } else { Loggers.Debug(new DebugLogInfo() { Message = string.Format("银联IVR创建订单失败{0}【请求】:{1}{0}【响应】:{2}", Environment.NewLine, IVRreq.ToJSON(), IvrResponse.ToJSON()) }); response.ResultCode = 200; response.Message = IvrResponse.Description; } #endregion break; case 3: recordEntity.Platform = 2; #region 阿里Wap支付 AliPayChannel aliPayWapChannel = channel.ChannelParameters.DeserializeJSONTo <AliPayChannel>(); AliPayWapTokenRequest tokenRequest = new AliPayWapTokenRequest(aliPayWapChannel) { CallBackUrl = string.IsNullOrEmpty(para.ReturnUrl) ? ConfigurationManager.AppSettings["AliPayCallBackUrl"] : para.ReturnUrl, NotifyUrl = ConfigurationManager.AppSettings["AlipayWapNotify"].Trim('?') + string.Format("?ChannelID={0}", channel.ChannelID), OutTradeNo = entity.OrderID.ToString(), Partner = aliPayWapChannel.Partner, SellerAccountName = aliPayWapChannel.SellerAccountName, Subject = entity.AppOrderDesc, TotalFee = Math.Round((Convert.ToDecimal(entity.AppOrderAmount) / 100), 2).ToString(), ReqID = Guid.NewGuid().ToString().Replace("-", "") }; requestJson = tokenRequest.ToJSON(); var aliPayWapResponse = AliPayWapGeteway.GetQueryTradeResponse(tokenRequest, aliPayWapChannel); responseJson = aliPayWapResponse.ToJSON(); if (aliPayWapResponse.IsSucess) { entity.PayUrl = aliPayWapResponse.RedirectURL; entity.Status = 1; bll.Update(entity); url = aliPayWapResponse.RedirectURL; Loggers.Debug(new DebugLogInfo() { Message = string.Format("AliPayWap创建订单成功{0}【请求】:{1}{0}【响应】:{2}", Environment.NewLine, tokenRequest.ToJSON(), aliPayWapResponse.ToJSON()) }); } else { Loggers.Debug(new DebugLogInfo() { Message = string.Format("AliPayWap创建订单失败{0}【请求】:{1}{0}【响应】:{2}", Environment.NewLine, tokenRequest.ToJSON(), aliPayWapResponse.ToJSON()) }); response.ResultCode = 300; response.Message = aliPayWapResponse.Message; } #endregion break; case 4: recordEntity.Platform = 2; #region 阿里OffLine支付 AliPayChannel aliPayChannel = channel.ChannelParameters.DeserializeJSONTo <AliPayChannel>(); //根据DynamicID判断是否预定单支付,DynamicID为空或者Null时调用OffLine预订单接口 if (string.IsNullOrWhiteSpace(para.DynamicID)) { OfflineQRCodePreRequest qrRequest = new OfflineQRCodePreRequest(aliPayChannel) { OutTradeNo = entity.OrderID.ToString(), NotifyUrl = ConfigurationManager.AppSettings["AlipayOfflineNotify"].Trim('?') + string.Format("?ChannelID={0}", channel.ChannelID), Subject = entity.AppOrderDesc, TotalFee = Math.Round((Convert.ToDecimal(entity.AppOrderAmount) / 100), 2).ToString(), //下面是测试数据,正式须更改 ExtendParams = new { MACHINE_ID = "BJ_001", //? AGENT_ID = aliPayChannel.AgentID, STORE_TYPE = "0", //? STORE_ID = "12314", //? TERMINAL_ID = "111", //? SHOP_ID = "only" //? }.ToJSON() }; requestJson = qrRequest.ToJSON(); var offlineQrResponse = AliPayOffLineGeteway.OfflineQRPay(qrRequest); responseJson = offlineQrResponse.ToJSON(); if (offlineQrResponse.IsSucess) { entity.Status = 1; entity.PayUrl = offlineQrResponse.PicUrl; bll.Update(entity); response.QrCodeUrl = offlineQrResponse.PicUrl; Loggers.Debug(new DebugLogInfo() { Message = string.Format("AliPayOffline二维码支付创建订单成功{0}【请求】:{1}{0}【响应】:{2}", Environment.NewLine, qrRequest.ToJSON(), offlineQrResponse.ToJSON()) }); } else { Loggers.Debug(new DebugLogInfo() { Message = string.Format("AliPayOffline二维码支付创建订单失败{0}【请求】:{1}{0}【响应】:{2}", Environment.NewLine, qrRequest.ToJSON(), offlineQrResponse.ToJSON()) }); response.ResultCode = 400; response.Message = offlineQrResponse.DetailErrorCode + ":" + offlineQrResponse.DetailErrorDes; } } else { CreateAndPayRequest createAndPayrequest = new CreateAndPayRequest(aliPayChannel) { Subject = entity.AppOrderDesc, TotalFee = Math.Round((Convert.ToDecimal(entity.AppOrderAmount) / 100), 2).ToString(), NotifyUrl = ConfigurationManager.AppSettings["AlipayOfflineNotify"].Trim('?') + string.Format("?ChannelID={0}", channel.ChannelID), OutTradeNo = entity.OrderID.ToString(), DynamicIDType = para.DynamicIDType, DynamicID = para.DynamicID, }; if (!string.IsNullOrEmpty(aliPayChannel.AgentID)) { createAndPayrequest.ExtendParams = (new { AGENT_ID = aliPayChannel.AgentID, MACHINE_ID = "BJ_001", STORE_TYPE = "0", STORE_ID = "BJ_ZZ_001", TERMINAL_ID = "A80001", SHOP_ID = "only" }).ToJSON(); } requestJson = createAndPayrequest.ToJSON(); var offlineCreateAndPayResponse = AliPayOffLineGeteway.OfflineCreateAndPay(createAndPayrequest); responseJson = offlineCreateAndPayResponse.ToJSON(); if (offlineCreateAndPayResponse.IsSuccess) { entity.Status = 2; bll.Update(entity); Loggers.Debug(new DebugLogInfo() { Message = string.Format("AliPayOffline即支付创建订单成功{0}【请求】:{1}{0}【响应】:{2}", Environment.NewLine, createAndPayrequest.ToJSON(), offlineCreateAndPayResponse.ToJSON()) }); } else if (offlineCreateAndPayResponse.ResultCode == ResultCodes.ORDER_SUCCESS_PAY_FAIL.ToString()) { entity.Status = 1; bll.Update(entity); Loggers.Debug(new DebugLogInfo() { Message = string.Format("AliPayOffline即支付创建订单成功{0},支付失败【请求】:{1}{0}【响应】:{2}", Environment.NewLine, createAndPayrequest.ToJSON(), offlineCreateAndPayResponse.ToJSON()) }); } else if (offlineCreateAndPayResponse.ResultCode == ResultCodes.ORDER_SUCCESS_PAY_INPROCESS.ToString()) { entity.Status = 1; bll.Update(entity); Loggers.Debug(new DebugLogInfo() { Message = string.Format("AliPayOffline即支付创建订单成功{0},支付处理中【请求】:{1}{0}【响应】:{2}", Environment.NewLine, createAndPayrequest.ToJSON(), offlineCreateAndPayResponse.ToJSON()) }); } else { Loggers.Debug(new DebugLogInfo() { Message = string.Format("AliPayOffline即支付创建订单失败{0}【请求】:{1}{0}【响应】:{2}", Environment.NewLine, createAndPayrequest.ToJSON(), offlineCreateAndPayResponse.ToJSON()) }); response.ResultCode = 400; response.Message = offlineCreateAndPayResponse.DetailErrorCode + ":" + offlineCreateAndPayResponse.DetailErrorDes; } } #endregion break; case 5: //Native case 6: //微信JS case 7: //微信App recordEntity.Platform = 3; #region 微信Native支付,JS支付 //把channel里的参数传了过去 WeiXinPayHelper helper = new WeiXinPayHelper(channel.ChannelParameters.DeserializeJSONTo <WeiXinPayHelper.Channel>()); entity.PayUrl = ConfigurationManager.AppSettings["WeiXinPrePay"]; entity.NotifyUrl = ConfigurationManager.AppSettings["WeiXinPayNotify"]; WeiXinPayHelper.PrePayResult result = null; if (para.PaymentMode == 1) //PaymentMode=1标示微信扫码支付进入 { helper.channel.trade_type = "NATIVE"; channel.PayType = 5; //走扫码回调返回参数 } Loggers.Debug(new DebugLogInfo() { Message = "isSpPay:" + helper.channel.isSpPay }); if (helper.channel.isSpPay == "1") //isSpPay=1 服务商支付 { result = helper.serPrePay(entity); //统一下单,服务商支付 } else { result = helper.prePay(entity); //统一下单,获取微信网页支付的预支付信息,app支付和js支付是一样的 } Loggers.Debug(new DebugLogInfo() { Message = "微信支付_预支付返回结果:" + result.ToJSON() }); requestJson = helper.prePayRequest; responseJson = helper.prePayResponse; if (result.return_code == "SUCCESS" && result.result_code == "SUCCESS") { if (channel.PayType == 6) { response.WXPackage = helper.getJsParamater(result); } else if (channel.PayType == 7) //后面传的参数和js支付的就不一样了 { response.WXPackage = helper.getAppParamater(result); response.OrderID = entity.OrderID; } else //Native { response.WXPackage = result.ToJSON(); response.QrCodeUrl = result.code_url; } Loggers.Debug(new DebugLogInfo() { Message = "WXPackage:" + response.WXPackage }); } else { response.ResultCode = 101; //支付失败 if (!string.IsNullOrEmpty(result.return_msg)) //不同错误,错误信息位置不同 { response.Message = result.return_msg; } else { response.Message = result.err_code_des; } } #endregion break; case 8: //旺财支付 return(string.Empty); case 9: //新版支付宝扫码支付 #region AliPayChannel aliPayScanChannel = channel.ChannelParameters.DeserializeJSONTo <AliPayChannel>(); RequestScanEntity scRequest = new RequestScanEntity(aliPayScanChannel); scRequest.notify_url = "" + ConfigurationManager.AppSettings["AlipayOfflineNotify"]; RequstScanDetail scanDetail = new RequstScanDetail(); scanDetail.out_trade_no = "" + entity.OrderID; scanDetail.total_amount = "" + Math.Round((Convert.ToDecimal(entity.AppOrderAmount) / 100), 2); scanDetail.seller_id = aliPayScanChannel.Partner; scanDetail.subject = entity.AppOrderDesc; var scResponseJson = AliPayScanGeteway.GetResponseStr(scRequest, scanDetail.ToJSON(), aliPayScanChannel.RSA_PrivateKey); var scResponse = scResponseJson.DeserializeJSONTo <ResponsetScanEntity>(); if (scResponse.alipay_trade_precreate_response.code == "10000") { response.QrCodeUrl = scResponse.alipay_trade_precreate_response.qr_code; Loggers.Debug(new DebugLogInfo() { Message = string.Format("aliPayScanChannel二维码支付创建订单成功{0}【请求】:{1}{0}【响应】:{2}", Environment.NewLine, requestJson, scResponseJson) }); } else { Loggers.Debug(new DebugLogInfo() { Message = string.Format("AliPayOffline二维码支付创建订单失败{0}【请求】:{1}{0}【响应】:{2}", Environment.NewLine, requestJson, scResponseJson) }); response.ResultCode = 400; response.Message = scResponse.alipay_trade_precreate_response.code + ":" + scResponse.alipay_trade_precreate_response.msg; } #endregion break; default: break; } recordEntity.RequestJson = requestJson; recordEntity.ResponseJson = responseJson; recordBll.Create(recordEntity); } catch (Exception ex) { recordEntity.RequestJson = requestJson; recordEntity.ResponseJson = responseJson; recordBll.Create(recordEntity); throw ex; } #endregion return(url); }
public WeiXinHandler() { var tmpConfig = Config as WeiXinPayConfig; weixin = new WeiXinPayHelper(tmpConfig); }