Esempio n. 1
0
        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 }));
        }
Esempio n. 2
0
        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" }));
            }
        }