public IHttpActionResult SaveSponsor(SponsorModel Model) { if (!ModelState.IsValid) { return(Json(new { Success = false, Message = "Invalid Data" })); } var battle = _videoBattleService.GetById(Model.BattleId); //todo: query picture battles when ready if (battle == null) { return(Json(new { Success = false, Message = "Invalid Battle" })); } var isProductOnlySponsorship = Model.SponsorshipType == SponsorshipType.OnlyProducts; var customerId = _workContext.CurrentCustomer.Id; SponsorPass sponsorPass = null; Order order = null; if (!isProductOnlySponsorship) { //because somebody wants to be a sponsor, let's first query the sponsorship pass sponsorPass = _sponsorPassService.GetSponsorPassByOrderId(Model.SponsorPassId); //the sponsor pass must belong to the person in question && it shouldn't have been used if (sponsorPass == null || sponsorPass.CustomerId != customerId || sponsorPass.Status == PassStatus.Used) { return(Json(new { Success = false, Message = "Unauthorized" })); } } //lets find the associated order order = isProductOnlySponsorship ? null : _orderService.GetOrderById(sponsorPass.SponsorPassOrderId); //there is a possibility that a sponsor is increasing the sponsorship amount. In that case, the new order 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(customerId, Model.BattleId, Model.BattleType, null); var newSponsorStatus = SponsorshipStatus.Pending; //so is the current customer already an sponsor for this battle? if (existingSponsors.Any()) { newSponsorStatus = existingSponsors.First().SponsorshipStatus; } //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 = Model.BattleId, BattleType = Model.BattleType, SponsorshipAmount = isProductOnlySponsorship ? 0 : order.OrderTotal, CustomerId = customerId, SponsorshipStatus = newSponsorStatus, DateCreated = DateTime.UtcNow, DateUpdated = DateTime.UtcNow }; //save the sponsor _sponsorService.Insert(sponsor); if (!isProductOnlySponsorship) { //if it's already approved sponsor, it's better to immediately capture the order if (newSponsorStatus == SponsorshipStatus.Accepted) { var captureResult = _paymentProcessingService.CapturePayment(order); if (!captureResult.Success) { return(Json(new { Success = false, Message = "Failed to capture order" })); } order.PaymentStatus = captureResult.NewPaymentStatus; order.CaptureTransactionId = captureResult.CaptureTransactionId; order.CaptureTransactionResult = captureResult.CaptureTransactionResult; _orderService.UpdateOrder(order); } //and mark the sponsor pass used by this battle _sponsorPassService.MarkSponsorPassUsed(sponsorPass.SponsorPassOrderId, Model.BattleId, Model.BattleType); } else { //save the prizes only sponsorship prizes SaveSponsorProductPrizes(Model.Prizes); } var battleOwner = _customerService.GetCustomerById(battle.ChallengerId); var sponsorCustomer = _customerService.GetCustomerById(customerId); //send notification to battle owner _mobSocialMessageService.SendSponsorAppliedNotificationToBattleOwner(battleOwner, sponsorCustomer, battle, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id); return(Json(new { Success = true })); }
public ActionResult SaveSponsor(SponsorModel Model) { if (!ModelState.IsValid) { return(Json(new { Success = false, Message = "Invalid Data" })); } var battle = _videoBattleService.GetById(Model.BattleId); //todo: query picture battles when ready if (battle == null) { return(Json(new { Success = false, Message = "Invalid Battle" })); } //because somebody wants to be a sponsor, let's first query the sponsorship pass var sponsorPass = _sponsorPassService.GetSponsorPassByOrderId(Model.SponsorPassId); //the sponsor pass must belong to the person in question && it shouldn't have been used if (sponsorPass == null || sponsorPass.CustomerId != _workContext.CurrentCustomer.Id || sponsorPass.Status == PassStatus.Used) { return(Json(new { Success = false, Message = "Unauthorized" })); } //lets find the associated order var order = _orderService.GetOrderById(sponsorPass.SponsorPassOrderId); //there is a possibility that a sponsor is increasing the sponsorship amount. In that case, the new order 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(order.CustomerId, Model.BattleId, Model.BattleType, null); var newSponsorStatus = SponsorshipStatus.Pending; //so is the current customer already an sponsor for this battle? if (existingSponsors.Any()) { newSponsorStatus = existingSponsors.First().SponsorshipStatus; } //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 = Model.BattleId, BattleType = Model.BattleType, SponsorshipAmount = order.OrderTotal, CustomerId = sponsorPass.CustomerId, SponsorshipStatus = newSponsorStatus }; //save the sponsor _sponsorService.Insert(sponsor); //and mark the sponsor pass used by this battle _sponsorPassService.MarkSponsorPassUsed(sponsorPass.SponsorPassOrderId, Model.BattleId, Model.BattleType); var battleOwner = _customerService.GetCustomerById(battle.ChallengerId); var sponsorCustomer = _customerService.GetCustomerById(sponsorPass.CustomerId); //send notification to battle owner _mobSocialMessageService.SendSponsorAppliedNotificationToBattleOwner(battleOwner, sponsorCustomer, battle, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id); return(Json(new { Success = true })); }