public IHttpActionResult GetAvailablePaymentMethods([FromUri] UserPaymentModel requestModel)
        {
            if (requestModel == null || !ModelState.IsValid)
            {
                return(Json(new { Success = false, Message = "Invalid data supplied" }));
            }
            ;
            //first check if the customer has an address, otherwise first address form will be shown

            var currentUser = ApplicationContext.Current.CurrentUser;

            var battleId     = requestModel.BattleId;
            var battleType   = requestModel.BattleType;
            var purchaseType = requestModel.PurchaseType;

            //TODO: Remove comment when picture battles are ready
            var battle = _videoBattleService.Get(battleId); // Model.BattleType == BattleType.Video ? _videoBattleService.GetById(Model.BattleId) : null;

            var responseModel = new UserPaymentPublicModel {
                IsAmountVariable     = purchaseType == PurchaseType.SponsorPass || battle.CanVoterIncreaseVotingCharge,
                MinimumPaymentAmount = purchaseType == PurchaseType.SponsorPass ? battle.MinimumSponsorshipAmount : battle.MinimumVotingCharge,
                PurchaseType         = requestModel.PurchaseType
            };

            //if purchase type is sponsor and customer is already a sponsor, he should not have a minimum amount criteria
            if (purchaseType == PurchaseType.SponsorPass)
            {
                var alreadySponsor = _sponsorService.Get(x => x.BattleId == battleId && x.BattleType == battleType, null).Any();
                if (alreadySponsor)
                {
                    responseModel.MinimumPaymentAmount = 1;
                }
            }

            //get available credits
            responseModel.AvailableCredits = _creditService.GetAvailableCreditsCount(currentUser.Id, null);

            //set the usable credits now
            responseModel.UsableCredits = _creditService.GetUsableCreditsCount(currentUser.Id);

            //let's get the payment methods for the logged in user
            var paymentMethods = _paymentMethodService.GetCustomerPaymentMethods(currentUser.Id);

            foreach (var pm in paymentMethods)
            {
                responseModel.UserPaymentMethods.Add(new System.Web.Mvc.SelectListItem()
                {
                    Text  = pm.NameOnCard + " (" + pm.CardNumberMasked + ")",
                    Value = pm.Id.ToString()
                });
            }
            if (battle.VideoBattleStatus == BattleStatus.Complete)
            {
                return(Json(new { Success = false, Message = "Battle completed" }));
            }
            //battle should not be complete before payment form can be opened
            return(Json(new { Success = true, AvailablePaymentMethods = responseModel }));
        }
        public IHttpActionResult SaveSponsor(SponsorModel requestModel)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { Success = false, Message = "Invalid Data" }));
            }

            var battle = _videoBattleService.Get(requestModel.BattleId); //todo: query picture battles when ready

            if (battle == null)
            {
                return(Json(new { Success = false, Message = "Invalid Battle" }));
            }

            var isProductOnlySponsorship = requestModel.SponsorshipType == SponsorshipType.OnlyProducts;

            var userId = ApplicationContext.Current.CurrentUser.Id;

            //there is a possibility that a sponsor is increasing the sponsorship amount. In that case, the new sponsorship status will automatically
            //be of same status as that of previous one for the same battle
            //lets find if the sponsor already exist for this battle?
            var existingSponsors = _sponsorService.GetSponsors(userId, requestModel.BattleId, requestModel.BattleType, null);

            var newSponsorStatus = SponsorshipStatus.Pending;

            //so is the current customer already an sponsor for this battle?
            if (existingSponsors.Any())
            {
                newSponsorStatus = existingSponsors.First().SponsorshipStatus;
            }

            if (!isProductOnlySponsorship)
            {
                //are issued credits sufficient?
                var issuedCreditsCount = _creditService.GetUsableCreditsCount(userId);
                if ((issuedCreditsCount < battle.MinimumSponsorshipAmount && newSponsorStatus != SponsorshipStatus.Accepted) || requestModel.SponsorshipCredits > issuedCreditsCount)
                {
                    VerboseReporter.ReportError("Insufficient credits to become sponsor", "save_sponsor");
                    return(RespondFailure());
                }
            }

            //mark the credits as spent credits
            var spentCredit = new Credit()
            {
                CreditContextKey      = string.Format(CreditContextKeyNames.BattleSponsor, battle.Id),
                CreditCount           = requestModel.SponsorshipCredits,
                CreditTransactionType = CreditTransactionType.Spent,
                CreditType            = CreditType.Transactional,
                PaymentTransactionId  = 0,
                CreatedOnUtc          = DateTime.UtcNow,
                UserId = userId
            };

            _creditService.Insert(spentCredit);

            //create the sponsor and set the status to pending or an existing status as determined above. (the status will be marked accepted or rejected or cancelled by battle owner)
            var sponsor = new Sponsor()
            {
                BattleId          = requestModel.BattleId,
                BattleType        = requestModel.BattleType,
                SponsorshipAmount = isProductOnlySponsorship ? 0 : requestModel.SponsorshipCredits,
                UserId            = userId,
                SponsorshipStatus = newSponsorStatus,
                DateCreated       = DateTime.UtcNow,
                DateUpdated       = DateTime.UtcNow
            };

            //save the sponsor
            _sponsorService.Insert(sponsor);

            if (!isProductOnlySponsorship)
            {
                //save the prizes only sponsorship prizes
                SaveSponsorProductPrizes(requestModel.Prizes);
            }


            var battleOwner = _userService.Get(battle.ChallengerId);

            var sponsorCustomer = _userService.Get(userId);

            //send notification to battle owner
            _emailSender.SendSponsorAppliedNotificationToBattleOwner(battleOwner, sponsorCustomer, battle);

            return(Json(new { Success = true }));
        }
Ejemplo n.º 3
0
        private TimelinePostDisplayModel PrepareTimelinePostDisplayModel(TimelinePost post)
        {
            //total likes for this post
            var totalLikes = _customerLikeService.GetLikeCount <TimelinePost>(post.Id);
            //the like status for current customer
            var likeStatus =
                _customerLikeService.GetCustomerLike <TimelinePost>(ApplicationContext.Current.CurrentUser.Id, post.Id) == null
                    ? 0
                    : 1;

            //process post content to replace inline tags
            _timelinePostProcessor.ProcessInlineTags(post);

            var totalComments = _customerCommentService.GetCommentsCount(post.Id, typeof(TimelinePost).Name);
            var postModel     = new TimelinePostDisplayModel()
            {
                Id                       = post.Id,
                DateCreatedUtc           = post.DateCreated,
                DateUpdatedUtc           = post.DateUpdated,
                DateCreated              = DateTimeHelper.GetDateInUserTimeZone(post.DateCreated, DateTimeKind.Utc, ApplicationContext.Current.CurrentUser),
                DateUpdated              = DateTimeHelper.GetDateInUserTimeZone(post.DateUpdated, DateTimeKind.Utc, ApplicationContext.Current.CurrentUser),
                OwnerId                  = post.OwnerId,
                OwnerEntityType          = post.OwnerEntityType,
                PostTypeName             = post.PostTypeName,
                IsSponsored              = post.IsSponsored,
                Message                  = post.Message,
                AdditionalAttributeValue = post.AdditionalAttributeValue,
                CanDelete                = post.OwnerId == ApplicationContext.Current.CurrentUser.Id || ApplicationContext.Current.CurrentUser.IsAdministrator(),
                TotalLikes               = totalLikes,
                LikeStatus               = likeStatus,
                TotalComments            = totalComments
            };

            if (post.OwnerEntityType == TimelinePostOwnerTypeNames.Customer)
            {
                //get the customer to retrieve info such a profile image, profile url etc.
                var user = _userService.Get(post.OwnerId);

                postModel.OwnerName     = string.IsNullOrEmpty(user.Name) ? user.Email : user.Name;
                postModel.OwnerImageUrl = _pictureService.GetPictureUrl(user.GetPropertyValueAs <int>(PropertyNames.DefaultPictureId), PictureSizeNames.MediumProfileImage);
                if (string.IsNullOrEmpty(postModel.OwnerImageUrl))
                {
                    postModel.OwnerImageUrl = _mediaSettings.DefaultUserProfileImageUrl;
                }
            }
            //depending on the posttype, we may need to extract additional data e.g. in case of autopublished posts, we may need to query the linked entity
            switch (post.PostTypeName)
            {
            case TimelineAutoPostTypeNames.VideoBattle.Publish:
            case TimelineAutoPostTypeNames.VideoBattle.BattleStart:
            case TimelineAutoPostTypeNames.VideoBattle.BattleComplete:
                //we need to query the video battle
                if (post.LinkedToEntityId != 0)
                {
                    var battle = _videoBattleService.Get(post.LinkedToEntityId);
                    if (battle == null)
                    {
                        break;
                    }
                    var battleUrl = Url.Route("VideoBattlePage",
                                              new RouteValueDictionary()
                    {
                        { "SeName", battle.GetPermalink() }
                    });

                    //create a dynamic object for battle, we'll serialize this object to json and store as additional attribute value
                    //todo: to see if we have some better way of doing this
                    var coverImageUrl = "";
                    var coverImageId  = battle.GetPropertyValueAs <int>(PropertyNames.DefaultCoverId);
                    if (coverImageId != 0)
                    {
                        coverImageUrl = _pictureService.GetPictureUrl(coverImageId);
                    }
                    var obj = new {
                        Name             = battle.Name,
                        Url              = battleUrl,
                        Description      = battle.Description,
                        VotingStartDate  = battle.VotingStartDate,
                        VotingEndDate    = battle.VotingEndDate,
                        CoverImageUrl    = coverImageUrl,
                        RemainingSeconds = battle.GetRemainingSeconds(),
                        Status           = battle.VideoBattleStatus.ToString()
                    };

                    postModel.AdditionalAttributeValue = JsonConvert.SerializeObject(obj);
                }
                break;
            }

            //replace inline tags with html links

            return(postModel);
        }