/// <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); } }
/// <summary> /// 二维码推广活动扫码增加积分 /// </summary> /// <param name="webApplicationId">微信关联程序Id</param> /// <param name="sceneId">场景Id</param> /// <param name="scanOpenId">扫码微信用户OpenId</param> public void CreateSceneFollowRecord(Guid webApplicationId, int sceneId, string scanOpenId) { if (webApplicationId == Guid.Empty) { throw new BaseException("微信关联程序Id不能为空"); } if (sceneId == 0) { throw new BaseException("SceneId场景Id错误"); } if (string.IsNullOrEmpty(scanOpenId)) { throw new BaseException("扫码微信用户OpenId不能为空"); } var account = ServiceContext.IWeiXinQRCodePromotionActivityAccountService.FindAccountBySceneId(webApplicationId, sceneId); if (account == null) { throw new BaseException(string.Format("微信关联程序Id({1}),场景Id({0}),推广用户数据为空", sceneId, webApplicationId)); } var weiXinAccount = account.LoadWeiXinAccount(); if (weiXinAccount == null) { throw new BaseException(string.Format("微信关联程序Id({1}),场景Id({0}),推广用户获取微信用户数据为空", sceneId, webApplicationId)); } if (weiXinAccount.IsSubscribe == false) { throw new BaseException(string.Format("微信关联程序Id({1}),场景Id({0}),推广用户没有关注微信公众号", sceneId, webApplicationId)); } var config = ServiceContext.IWeiXinQRCodePromotionActivityConfigService.CreateOrGet(webApplicationId); if (config.GetFollowPulicWeiXinAccountPointsType == Entities.GetFollowPulicWeiXinAccountPointsEnum.NewUnique) { var count = this.ServiceContext.IWeiXinQRCodePromotionActivityMemberFromAccountService.Count <int>("ID", Where.Create() .Eq("WebApplicationId", webApplicationId) .Eq("MemberOpenId", scanOpenId) ); if (count > 0) { throw new BaseException(string.Format("微信关联程序Id({1}),场景Id({0}),OpenId({2})的微信用户已有关注记录", sceneId, webApplicationId, scanOpenId)); } } if (config.IsFollowPublicWeiXinAccountHasPoints == false) { return; } var sceneWeiXinAccount = ServiceContext.IWeiXinQRCodePromotionActivityAccountService.GetWeiXinAccount(webApplicationId, scanOpenId); WeiXinQRCodePromotionActivityMemberFromAccountDto memberFromAccount = new WeiXinQRCodePromotionActivityMemberFromAccountDto() { ActivityOpenId = account.OpenId, MemberOpenId = scanOpenId, WebApplicationId = webApplicationId, WeiXinQRCodePromotionActivityAccountId = account.ID, ID = Guid.NewGuid() }; var beforePoints = account.Points; var afterPoints = account.Points + config.FollowPoints; var history = new WeiXinQRCodePromotionActivityAccountPointsHistoryDto() { ID = Guid.NewGuid(), UsePoints = config.FollowPoints * -1, ExchangeMoney = null, Remark = string.Format("{0}关注公众号", sceneWeiXinAccount.nickname), CreatedDate = DateTime.Now, BeforePoints = beforePoints, WeiXinQRCodePromotionActivityAccountId = account.ID, MemberOpenId = scanOpenId, AfterPoints = afterPoints }; account.Points += config.FollowPoints; this.ServiceContext.IWeiXinQRCodePromotionActivityAccountService.Update(account); this.Insert(memberFromAccount); this.ServiceContext.IWeiXinQRCodePromotionActivityAccountPointsHistoryService.Insert(history); }