/// <summary> /// 发放现金红包 /// </summary> /// <param name="webApplicationId">微信关联程序Id</param> /// <param name="weiXinQRCodePromotionActivityAccountId">微信参加二维码推广活动用户Id</param> /// <param name="usePoints">消费积分</param> /// <param name="exchangeMoney">兑换金额,单位分</param> public void CreateSendCashRedPack(Guid webApplicationId, Guid weiXinQRCodePromotionActivityAccountId, int usePoints, int exchangeMoney) { if (webApplicationId == Guid.Empty) { throw new BaseException("weiXinApplicationId不能为空"); } if (weiXinQRCodePromotionActivityAccountId == Guid.Empty) { throw new BaseException("weiXinQRCodePromotionActivityAccountId不能为空"); } if (usePoints <= 0) { throw new BaseException("消费积分不能少于或等于0"); } if (exchangeMoney <= 0) { throw new BaseException("兑换金额不能少于或等于0"); } var account = this.ServiceContext.IWeiXinQRCodePromotionActivityAccountService.FindById(weiXinQRCodePromotionActivityAccountId); if (account == null) { throw new BaseException(string.Format("ID为({0})的用户数据为空", weiXinQRCodePromotionActivityAccountId)); } if (account.Points < usePoints) { throw new BaseException(string.Format("积分不够进行兑换操作")); } var weiXinAccount = account.LoadWeiXinAccount(); if (weiXinAccount.IsSubscribe == false) { throw new BaseException(string.Format("该用户没有关注公众号")); } var webApplication = this.ServiceContext.ICWXWeiXinApplicationService.FindById(account.WebApplicationId); if (webApplication == null) { throw new BaseException(string.Format("ID({0})微信关联程序数据为空", account.WebApplicationId)); } if (string.IsNullOrEmpty(webApplication.MerchantId)) { throw new BaseException(string.Format("ID({0})微信关联程序的MerchantId商户数据为空", account.WebApplicationId)); } var merchantId = webApplication.MerchantId; var beforePoints = account.Points; var afterPoints = account.Points - usePoints; var pointsHistory = new WeiXinQRCodePromotionActivityAccountPointsHistoryDto() { ID = Guid.NewGuid(), BeforePoints = account.Points, AfterPoints = afterPoints, CreatedDate = DateTime.Now, ExchangeMoney = exchangeMoney, Remark = string.Format("使用积分{0},兑换{1}元", usePoints, (((decimal)exchangeMoney) / 100).ToString("f2")), UsePoints = usePoints, WeiXinQRCodePromotionActivityAccountId = account.ID }; account.Points = afterPoints; this.ServiceContext.IWeiXinQRCodePromotionActivityAccountPointsHistoryService.Insert(pointsHistory); this.ServiceContext.IWeiXinQRCodePromotionActivityAccountService.Update(account); var merchantBillNo = this.ServiceContext.IWeiXinMerchantBIllNoService .CreateOrGetNextWeiXinMerchantBillNo(webApplication.ID, DateTime.Now.Date); var weiXinQRCodePromotionActivityConfigService = new WeiXinQRCodePromotionActivityConfigService(); var config = weiXinQRCodePromotionActivityConfigService.CreateOrGet(webApplication.ID); var iAppContext = ToolFactory.CreateIAppContext(); var certFilePath = iAppContext.GetMapExecPath("~/apiclient_cert.p12"); var sendRedPack = this.WeiXinAPIContext.IPayCashRedPack.SendRedPack( certFilePath, config.CertFilePwd, webApplication.APISecretKey, merchantBillNo, webApplication.MerchantId, webApplication.AppID, config.RedPackSendName, account.OpenId, exchangeMoney, 1, config.RedPackWishing, config.RedPackClientIP, config.RedPackActivityName, config.RedPackRemark ); if (sendRedPack.IsFailureReturnCode) { throw new BaseException(sendRedPack.return_msg); } if (sendRedPack.IsFailureResultCode) { throw new BaseException(sendRedPack.err_code_des); } }