public ActionResult UpdateSponsor(UpdateSponsorModel Model) { //here we update or reject the status of battle if (!ModelState.IsValid) { return(Json(new { Success = false, Message = "Invalid Data" })); } //because a sponsor may have performed multiple transactions for a battle, we'd update all of them at once //first get the battle //TODO: Remove comment when picture battles are ready var battle = _videoBattleService.GetById(Model.BattleId); // Model.BattleType == BattleType.Video ? _videoBattleService.GetById(Model.BattleId) : null; //only battle owner should be able to accept or reject the sponsorship if (battle.ChallengerId != _workContext.CurrentCustomer.Id && Model.SponsorshipStatus != SponsorshipStatus.Cancelled) { return(Json(new { Success = false, Message = "Unauthorized" })); } //similarly only the sponsor should be able to withdraw it if (Model.SponsorCustomerId != _workContext.CurrentCustomer.Id && Model.SponsorshipStatus == SponsorshipStatus.Cancelled) { return(Json(new { Success = false, Message = "Unauthorized" })); } //so lets update all the sponsors for this battle and this user _sponsorService.UpdateSponsorStatus(Model.SponsorCustomerId, Model.BattleId, Model.BattleType, Model.SponsorshipStatus); Customer customer; //send sponsorship update status to battle owner and admin if (Model.SponsorshipStatus == SponsorshipStatus.Cancelled) { customer = _customerService.GetCustomerById(battle.ChallengerId); //send this messge to battle owner } else { //send this message to sponsor customer = _customerService.GetCustomerById(Model.SponsorCustomerId); } //send notification _mobSocialMessageService.SendSponsorshipStatusChangeNotification(customer, Model.SponsorshipStatus, battle, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id); return(Json(new { Success = true })); }
public IHttpActionResult UpdateSponsor(UpdateSponsorModel requestModel) { //here we update or reject the status of battle if (!ModelState.IsValid) { return(Json(new { Success = false, Message = "Invalid Data" })); } //because a sponsor may have performed multiple transactions for a battle, we'd update all of them at once //first get the battle //TODO: Remove comment when picture battles are ready var battle = _videoBattleService.Get(requestModel.BattleId); // Model.BattleType == BattleType.Video ? _videoBattleService.GetById(Model.BattleId) : null; //only battle owner should be able to accept or reject the sponsorship if (battle.ChallengerId != ApplicationContext.Current.CurrentUser.Id && requestModel.SponsorshipStatus != SponsorshipStatus.Cancelled) { return(Json(new { Success = false, Message = "Unauthorized" })); } //similarly only the sponsor should be able to withdraw it if (requestModel.SponsorCustomerId != ApplicationContext.Current.CurrentUser.Id && requestModel.SponsorshipStatus == SponsorshipStatus.Cancelled) { return(Json(new { Success = false, Message = "Unauthorized" })); } var sponsorCustomer = _userService.Get(requestModel.SponsorCustomerId); if (sponsorCustomer == null) { VerboseReporter.ReportError("Invalid customer Id", "update_sponsor"); return(RespondFailure()); } if (requestModel.SponsorshipStatus == SponsorshipStatus.Accepted) { //update sponsorship status _sponsorService.UpdateSponsorStatus(sponsorCustomer.Id, battle.Id, BattleType.Video, SponsorshipStatus.Accepted); } else if (requestModel.SponsorshipStatus == SponsorshipStatus.Cancelled || requestModel.SponsorshipStatus == SponsorshipStatus.Rejected) { //update sponsorship status _sponsorService.UpdateSponsorStatus(sponsorCustomer.Id, battle.Id, BattleType.Video, requestModel.SponsorshipStatus); //in case the sponsorship is cancelled or rejected, the appropriate number of credits should be refunded to the sponsor user //to get the total sponsorship credits, let's first query var sponsorships = _sponsorService.GetSponsors(sponsorCustomer.Id, battle.Id, BattleType.Video, null); //sum all the sponsorships to get total credits var totalCredits = sponsorships.Sum(x => x.SponsorshipAmount); var refundCredit = new Credit() { CreatedOnUtc = DateTime.UtcNow, CreditTransactionType = CreditTransactionType.Refund, CreditContextKey = string.Format(CreditContextKeyNames.BattleSponsor, battle.Id), CreditCount = totalCredits, CreditType = CreditType.Transactional, Remarks = $"Refund towards cancellation of sponsorship for video battle '{battle.Name}'", UserId = sponsorCustomer.Id }; //save the credit _creditService.Insert(refundCredit); //send sponsorship update status to battle owner and admin var user = _userService.Get(requestModel.SponsorshipStatus == SponsorshipStatus.Cancelled ? battle.ChallengerId : requestModel.SponsorCustomerId); //send notification _emailSender.SendSponsorshipStatusChangeNotification(user, requestModel.SponsorshipStatus, battle); } return(RespondSuccess()); }
public IHttpActionResult UpdateSponsor(UpdateSponsorModel Model) { //here we update or reject the status of battle if (!ModelState.IsValid) { return(Json(new { Success = false, Message = "Invalid Data" })); } //because a sponsor may have performed multiple transactions for a battle, we'd update all of them at once //first get the battle //TODO: Remove comment when picture battles are ready var battle = _videoBattleService.GetById(Model.BattleId); // Model.BattleType == BattleType.Video ? _videoBattleService.GetById(Model.BattleId) : null; //only battle owner should be able to accept or reject the sponsorship if (battle.ChallengerId != _workContext.CurrentCustomer.Id && Model.SponsorshipStatus != SponsorshipStatus.Cancelled) { return(Json(new { Success = false, Message = "Unauthorized" })); } //similarly only the sponsor should be able to withdraw it if (Model.SponsorCustomerId != _workContext.CurrentCustomer.Id && Model.SponsorshipStatus == SponsorshipStatus.Cancelled) { return(Json(new { Success = false, Message = "Unauthorized" })); } var captureVoidSuccess = true; //now depending on whether it's being approved or rejected, the transaction will be either captured or voided //get all the orders and capture/void the transactions var orders = _sponsorPassService.GetSponsorPassOrders(Model.SponsorCustomerId, Model.BattleId, Model.BattleType).Where(x => x.PaymentStatus == PaymentStatus.Authorized); if (Model.SponsorshipStatus == SponsorshipStatus.Accepted) { foreach (var order in orders) { var captureResult = _paymentProcessingService.CapturePayment(order); if (captureResult.Success) { order.PaymentStatus = captureResult.NewPaymentStatus; order.CaptureTransactionId = captureResult.CaptureTransactionId; order.CaptureTransactionResult = captureResult.CaptureTransactionResult; _orderService.UpdateOrder(order); } else { captureVoidSuccess = false; } } } else if (Model.SponsorshipStatus == SponsorshipStatus.Cancelled || Model.SponsorshipStatus == SponsorshipStatus.Rejected) { foreach (var order in orders) { var voidResult = _paymentProcessingService.VoidPayment(order); if (voidResult.Success) { order.PaymentStatus = voidResult.NewPaymentStatus; _orderService.UpdateOrder(order); } else { captureVoidSuccess = false; } } } //only if all transactions are captured/void if (captureVoidSuccess) { //so lets update all the sponsors for this battle and this user _sponsorService.UpdateSponsorStatus(Model.SponsorCustomerId, Model.BattleId, Model.BattleType, Model.SponsorshipStatus); //send sponsorship update status to battle owner and admin var customer = _customerService.GetCustomerById(Model.SponsorshipStatus == SponsorshipStatus.Cancelled ? battle.ChallengerId : Model.SponsorCustomerId); //send notification _mobSocialMessageService.SendSponsorshipStatusChangeNotification(customer, Model.SponsorshipStatus, battle, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id); return(Json(new { Success = true })); } else { return(Json(new { Success = false, Message = "Failed to capture or void the payment" })); } }