public NormalResult ClosePayOrder(string outTradeNo, AuthorizerPayConfig config) { NormalResult result = new NormalResult(); if (config == null) { result.Success = false; result.Message = "当前公众号没有微信支付所需配置信息。"; return(result); } WeixinPayCloseOrderArgs closeOrderArgs = new WeixinPayCloseOrderArgs(); closeOrderArgs.AppId = config.AppId; closeOrderArgs.MchId = config.MchId; closeOrderArgs.OutTradeNo = outTradeNo; WxPayArgs wxPayArgs = config.GetWxPayArgs(false); RequestPayApiResult <WeixinPayCloseOrderResult> closeOrderResult = WxPayApi.CloseOrder(closeOrderArgs, wxPayArgs); if (closeOrderResult.Success == false) { _log.Write("CloseOrder 失败", closeOrderResult.Message + "\r\n" + JsonHelper.Serializer(closeOrderArgs) + " " + JsonHelper.Serializer(closeOrderResult), TraceEventType.Warning); result.Success = false; result.Message = closeOrderResult.Message; return(result); } result.Success = true; return(result); }
// GET: Api/Pay public ActionResult Redpack() { //判断有没有参与投票活动 int campaignCount = _campaignManager.PictureVote.GetMemberPictureVoteItemCount( Guid.Parse("16bc2e8e-8dbd-405a-85cc-34fb1879fd8a"), MemberContext.Member.Id, EnumCampaignPictureVoteItemApproveStatus.Approved); if (campaignCount == 0) { return(RespondResult(false, "请参与晒宝宝赢红包活动,上传宝宝照片并通过审核后,即可拆红包!~")); } if (_memberManager.Redpack(DomainContext.Domain.Id, MemberContext.Member.Id) == false) { return(RespondResult(false, "您已经领取过本轮红包啦!~")); } string billno = _random.Next(0, 99999999).ToString(); billno = "0000000000" + billno; WeixinPaySendRedpackArgs args = new WeixinPaySendRedpackArgs(); args.MchBillno = "10000" + DateTime.Now.ToString("yyyymmdd") + billno.Substring(billno.Length - 10, 10); args.MchId = "1277619601"; args.WxAppId = "wx8c36b3c0000a0a49"; args.SendName = "升讯威"; args.ReOpenid = MemberContext.Member.OpenId; args.TotalAmount = _random.Next(100, 120); args.Wishing = "新的快乐!"; args.ClientIp = "121.40.198.87"; args.ActName = "test"; args.Remark = "红包"; Log.Write("Redpack 发送:", JsonConvert.SerializeObject(args), TraceEventType.Verbose); ApiResult apiResult = new ApiResult(); WxPayArgs wxPayArgs = new WxPayArgs(); wxPayArgs.UseCertificate = true; wxPayArgs.CertificatePath = @"D:\wwwroot\WeixinConstruction\cert\apiclient_cert.p12"; wxPayArgs.CertificatePassword = "******"; wxPayArgs.Key = "192006250b4c09247ec02edce69f6a99"; RequestPayApiResult <WeixinPaySendRedpackResult> result = WxPayApi.SendRedpack(args, wxPayArgs); Log.Write("Redpack 返回:", JsonConvert.SerializeObject(result), TraceEventType.Verbose); if (result.Success == false) { //apiResult.Message = result.Message; Log.Write("Redpack 失败:", result.Message, TraceEventType.Warning); apiResult.Message = "您来晚了哦,本轮红包已派发完毕,新年期间我们还将继续发起红包大派发活动,敬请留意~"; return(RespondResult(apiResult)); } if (result.ApiResult.ReturnCode == "SUCCESS" && result.ApiResult.ResultCode == "SUCCESS") { apiResult.Success = true; return(RespondResult()); } else { //apiResult.Message = result.ApiResult.ErrCode + result.ApiResult.ErrCodeDes; Log.Write("Redpack 失败:", result.ApiResult.ErrCode + result.ApiResult.ErrCodeDes, TraceEventType.Warning); apiResult.Message = "您来晚了哦,本轮红包已派发完毕,新年期间我们还将继续发起红包大派发活动,敬请留意~"; return(RespondResult(apiResult)); } }
public NormalResult Notify(string xml, AuthorizerPayConfig config) { _log.Write("收到支付结果通知", xml, TraceEventType.Verbose); NormalResult result = new NormalResult(); WxPayArgs wxPayArgs = config.GetWxPayArgs(false); RequestPayApiResult <WeixinPayNotify> notifyResult = WxPayApi.Notify(xml, wxPayArgs); if (notifyResult.Success == false) { _log.Write("支付结果通知显示失败", notifyResult.Message, TraceEventType.Verbose); result.Success = false; result.Message = notifyResult.Message; return(result); } WeixinPayNotify notify = notifyResult.ApiResult; Guid payOrderId; if (Guid.TryParse(notify.Attach, out payOrderId) == false) { string attacth = String.Empty; if (notify.Attach != null) { attacth = notify.Attach; } _log.Write("Attach 无法转为本地订单Id" + attacth, notify.OutTradeNo, TraceEventType.Verbose); result.Success = false; result.Message = "Attach 无法转为本地订单Id。" + attacth; return(result); } bool updateNotifyStatus = UpdateNotifyStatus(payOrderId); if (updateNotifyStatus == false) { //已经处理过了 //处理过的还是返回true,防止微信服务器反复推送通知 //微信文档上说通知频率为15/15/30/180/1800/1800/1800/1800/3600,单位:秒 //但是实测支付成功后连续推送通知过来 result.Success = true; result.Message = "已经接收到过此订单的支付通知。"; return(result); } SqlStructureBuild sqlBuild = new SqlStructureBuild(); sqlBuild.Table = "PayOrder"; sqlBuild.Type = SqlExpressionType.Update; sqlBuild.AddParameter("Id", payOrderId, true); sqlBuild.AddParameter("Notify", true); if (notify.ReturnCode == "SUCCESS" && notify.ResultCode == "SUCCESS") { sqlBuild.AddParameter("TradeState", EnumPayTradeState.SUCCESS); } else { sqlBuild.AddParameter("TradeState", EnumPayTradeState.PAYERROR); } sqlBuild.AddParameter("BankType", notify.BankType); sqlBuild.AddParameter("FeeType", notify.FeeType); sqlBuild.AddParameter("CouponFee", notify.CouponFee); sqlBuild.AddParameter("CouponCount", notify.CouponCount); sqlBuild.AddParameter("TransactionId", notify.TransactionId); sqlBuild.AddParameter("TimeEnd", WeixinApiHelper.ConvertStringToDateTime(notify.TimeEnd)); sqlBuild.AddParameter("Notify_ReturnCode", notify.ReturnCode); sqlBuild.AddParameter("Notify_ReturnMsg", notify.ReturnMsg); sqlBuild.AddParameter("Notify_ResultCode", notify.ResultCode); sqlBuild.AddParameter("Notify_ErrCode", notify.ErrCode); sqlBuild.AddParameter("Notify_ErrCodeDes", notify.ErrCodeDes); int affectedRowCount = _dataBase.ExcuteSqlExpression(sqlBuild.GetSqlExpression()); if (affectedRowCount == 0) { _log.Write("本地订单不存在", notify.OutTradeNo, TraceEventType.Verbose); result.Success = false; result.Message = "本地订单不存在。"; return(result); } PayOrderEntity payOrder = GetPayOrder(payOrderId); if (notify.CouponCount > 0) { foreach (WeixinPayNotify_Coupon coupon in notify.CouponList) { coupon.PayOrderId = payOrder.Id; _dataBase.Insert(coupon); } } if (notify.ReturnCode == "SUCCESS" && notify.ResultCode == "SUCCESS") { switch (payOrder.Type) { case EnumPayOrderType.Unknow: _log.Write("收到支付结果通知", "未知订单类型", TraceEventType.Warning); break; case EnumPayOrderType.Deposit: ProcessDepositPayNotify(payOrder); break; case EnumPayOrderType.PointCommodity: ProcessPointCommodityPayNotify(payOrder); break; case EnumPayOrderType.Donation: ProcessDonationPayNotify(payOrder); break; default: _log.Write("收到支付结果通知", "订单类型未处理:" + payOrder.Type.ToString(), TraceEventType.Warning); break; } } //更新一下订单状态 RefreshPayOrder(payOrder.OutTradeNo, config); result.Success = true; return(result); }
public NormalResult RefreshPayOrder(string outTradeNo, AuthorizerPayConfig config) { NormalResult result = new NormalResult(); if (config == null) { result.Success = false; result.Message = "当前公众号没有微信支付所需配置信息。"; return(result); } WeixinPayOrderQueryArgs orderQueryArgs = new WeixinPayOrderQueryArgs(); orderQueryArgs.AppId = config.AppId; orderQueryArgs.MchId = config.MchId; orderQueryArgs.OutTradeNo = outTradeNo; WxPayArgs wxPayArgs = config.GetWxPayArgs(false); RequestPayApiResult <WeixinPayOrderQueryResult> orderQueryResult = WxPayApi.OrderQuery(orderQueryArgs, wxPayArgs); if (orderQueryResult.Success == false) { _log.Write("OrderQuery 失败", orderQueryResult.Message + "\r\n" + JsonHelper.Serializer(orderQueryArgs) + " " + JsonHelper.Serializer(orderQueryResult), TraceEventType.Warning); result.Success = false; result.Message = orderQueryResult.Message; return(result); } WeixinPayOrderQueryResult orderQuery = orderQueryResult.ApiResult; Guid payOrderId; if (Guid.TryParse(orderQuery.Attach, out payOrderId) == false) { string attacth = String.Empty; if (orderQuery.Attach != null) { attacth = orderQuery.Attach; } _log.Write("Attach 无法转为本地订单Id" + attacth, orderQuery.OutTradeNo, TraceEventType.Verbose); result.Success = false; result.Message = "Attach 无法转为本地订单Id。" + attacth; return(result); } PayOrderEntity payOrder = GetPayOrder(payOrderId); SqlStructureBuild sqlBuild = new SqlStructureBuild(); sqlBuild.Table = "PayOrder"; sqlBuild.Type = SqlExpressionType.Update; sqlBuild.AddParameter("Id", payOrderId, true); sqlBuild.AddParameter("TradeState", EnumHelper.GetEnumFieldByValue <EnumPayTradeState>(orderQuery.TradeState)); sqlBuild.AddParameter("TradeStateDesc", orderQuery.TradeStateDesc); sqlBuild.AddParameter("BankType", orderQuery.BankType); sqlBuild.AddParameter("FeeType", orderQuery.FeeType); sqlBuild.AddParameter("CouponFee", orderQuery.CouponFee); sqlBuild.AddParameter("CouponCount", orderQuery.CouponCount); sqlBuild.AddParameter("TransactionId", orderQuery.TransactionId); if (orderQuery.TimeEnd != null) { sqlBuild.AddParameter("TimeEnd", WeixinApiHelper.ConvertStringToDateTime(orderQuery.TimeEnd)); } sqlBuild.AddParameter("Notify_ReturnCode", orderQuery.ReturnCode); sqlBuild.AddParameter("Notify_ReturnMsg", orderQuery.ReturnMsg); sqlBuild.AddParameter("Notify_ResultCode", orderQuery.ResultCode); sqlBuild.AddParameter("Notify_ErrCode", orderQuery.ErrCode); sqlBuild.AddParameter("Notify_ErrCodeDes", orderQuery.ErrCodeDes); int affectedRowCount = _dataBase.ExcuteSqlExpression(sqlBuild.GetSqlExpression()); if (affectedRowCount == 0) { _log.Write("本地订单不存在", orderQuery.OutTradeNo, TraceEventType.Warning); result.Success = false; result.Message = "本地订单不存在。"; return(result); } if (orderQuery.CouponCount > 0) { List <CommandParameter> parameterList = new List <CommandParameter>(); parameterList.Add(new CommandParameter("@payOrderId", payOrder.Id)); _dataBase.ExecuteNonQuery( "DELETE FROM [PayOrderCoupon] WHERE [PayOrderId] = @payOrderId", parameterList); foreach (WeixinPayOrderQueryResult_Coupon coupon in orderQuery.CouponList) { coupon.PayOrderId = payOrder.Id; _dataBase.Insert(coupon); } } result.Success = true; return(result); }
/// <summary> /// 创建一个微信支付订单 /// 默认在2小时后过期 /// </summary> /// <param name="args"></param> /// <param name="config"></param> /// <returns></returns> public NormalResult <CreatePayOrderResult> CreatePayOrder(CreatePayOrderArgs args, AuthorizerPayConfig config) { NormalResult <CreatePayOrderResult> result = new NormalResult <CreatePayOrderResult>(false); if (config == null) { result.Message = "当前公众号没有微信支付所需配置信息。"; return(result); } //TODO:这里只判断了 Deposit if (ExistUnfinishedOrder(config.Domain, config.AppId, args.MemberId, EnumPayOrderType.Deposit)) { result.Message = "存在尚未结束的订单,请先完成当前订单的支付或将其关闭。"; return(result); } //string outTradeNo =Guid.NewGuid().ToString().Replace("-", ""); DateTime timeStart = DateTime.Now; DateTime timeExpire = DateTime.Now.AddHours(2); Guid payOrderId = Guid.NewGuid(); WeixinPayUnifiedOrderArgs unifiedOrderArgs = new WeixinPayUnifiedOrderArgs(); unifiedOrderArgs.AppId = config.AppId; unifiedOrderArgs.MchId = config.MchId; unifiedOrderArgs.DeviceInfo = "WEB"; unifiedOrderArgs.Body = args.Body; //"会员充值"; unifiedOrderArgs.OutTradeNo = args.OutTradeNo; // outTradeNo; unifiedOrderArgs.TotalFee = (int)args.Fee; unifiedOrderArgs.SpbillCreateIp = args.SpbillCreateIp; if (unifiedOrderArgs.SpbillCreateIp == "::1") { unifiedOrderArgs.SpbillCreateIp = "127.0.0.1"; } unifiedOrderArgs.TimeStart = timeStart.ToString("yyyyMMddHHmmss"); unifiedOrderArgs.TimeExpire = timeExpire.ToString("yyyyMMddHHmmss"); unifiedOrderArgs.NotifyUrl = ""; unifiedOrderArgs.TradeType = "JSAPI"; unifiedOrderArgs.OpenId = args.OpenId; unifiedOrderArgs.NotifyUrl = SettingsManager.Instance.GetClientAddress(config.AppId) + "Api/Pay/PayNotify/" + config.Domain; unifiedOrderArgs.Attach = payOrderId.ToString(); WxPayArgs wxPayArgs = config.GetWxPayArgs(false); RequestPayApiResult <WeixinPayUnifiedOrderResult> unifiedOrderResult = WxPayApi.UnifiedOrder(unifiedOrderArgs, wxPayArgs); if (unifiedOrderResult.Success == false) { _log.Write("UnifiedOrder 失败", unifiedOrderResult.Message + "\r\n" + JsonHelper.Serializer(unifiedOrderArgs) + " " + JsonHelper.Serializer(unifiedOrderResult), TraceEventType.Warning); result.Success = false; result.Message = unifiedOrderResult.Message; return(result); } PayOrderEntity orderEntity = new PayOrderEntity(); orderEntity.Id = payOrderId; orderEntity.Domain = config.Domain; orderEntity.Member = args.MemberId; orderEntity.Type = args.OrderType; orderEntity.AppId = config.AppId; orderEntity.MchId = config.MchId; orderEntity.DeviceInfo = unifiedOrderArgs.DeviceInfo; orderEntity.Body = unifiedOrderArgs.Body; orderEntity.OutTradeNo = args.OutTradeNo;//outTradeNo; orderEntity.TotalFee = unifiedOrderArgs.TotalFee; orderEntity.SpbillCreateIp = unifiedOrderArgs.SpbillCreateIp; orderEntity.TimeStart = timeStart; orderEntity.TimeExpire = timeExpire; orderEntity.TradeType = unifiedOrderArgs.TradeType; orderEntity.OpenId = unifiedOrderArgs.OpenId; orderEntity.PrepayId = unifiedOrderResult.ApiResult.PrepayId; orderEntity.TradeState = EnumPayTradeState.NOTPAY; _dataBase.Insert(orderEntity); CreatePayOrderResult depositPayResult = new CreatePayOrderResult(); depositPayResult.PayOrderId = orderEntity.Id; depositPayResult.PrepayId = unifiedOrderResult.ApiResult.PrepayId; result.Data = depositPayResult; result.Success = true; return(result); }