Exemple #1
0
 public static bool checkRolePermission(ApplicationUser currentUser, string requiredPermission)
 {
     if (currentUser != null)
     {
         var rbac = new RBAC(currentUser.Id);
         return rbac.HasPermission(requiredPermission);
     }
     return false;
 }
        public bool validate()
        {
            this.errors = new Dictionary<string, List<string>>();

            if (exchangeOption == null)
            {
                addError("exchange_option_id",
                         "Exchange Option has been removed");
                return false;
            }

            if (this.exchangeOption.is_archived)
            {
                this.addError("exchange_option_id", "Exchange Option has been removed");
            }

            if (this.exchangeOption.game_id != this.game.id)
            {
                this.addError("exchange_option_id", "Exchange Option does not belong to game");
            }

            //Game is not archived and active
            RBAC rbac = new RBAC(user.id);
            if (!(rbac.HasRole("admin") || rbac.HasRole("game_admin")))
            {
                if (!this.game.is_active || this.game.is_archived)
                    this.addError("game_id", "Game is not active or has been removed");
            }
            try
            {
                //self.inGameAmount = int(self.inGameAmount)
                if (this.exchangeOption is CreditType)
                {
                    if (this.inGameAmount < 0)
                    {
                        this.addError("exchange_amount", "Exchange amount needs to be a positive integer");
                    }
                }
                else if (this.exchangeOption is Package)
                {
                    if (this.inGameAmount != 1)
                    {
                        this.addError("exchange_amount", "Exchange amount needs to be 01");
                    }
                }

            }
            catch (Exception)
            {
                this.addError("exchange_amount", "Exchange amount needs to be a positive integer");
            }

            if (this.inGameAmount == 0)
            {
                this.addError("exchange_amount", "Exchange amount is required");
            }

            if (this.errors.Count == 0)
            {
                Tuple<decimal, decimal> tuple = calculate().Result;
                if (!(tuple.Item1 > 0 || tuple.Item2 > 0))
                    this.addError("exchange_amount", "Insufficient Balance");
            }
            if (this.errors.Count > 0) { return false; }
            return true;
        }
        public async Task<object> InAppPurchase([FromBody] APIInAppPurchaseModel model)
        {
            var request = HttpContext.Current.Request;
            GoPlayApi api = GoPlayApi.Instance;
            string gameUid = model.game_id;
            Game game = api.GetGame(gameUid).Data;
            string session = !string.IsNullOrEmpty(model.session)
                ? UrlHelperExtensions.GetSession(model.session)
                : request.Params["session"];
            customer_account user = api.LoadFromAccessToken(session).Data;

            int quantity = model.quantity > 0 ? model.quantity : model.amount;
            string order_id = model.order_id;
            string exchangeOptionType = model.exchange_option_type;
            int exchangeOptionID = model.exchange_option_id;
            string exchangeOptionIdentifier = model.exchange_option_identifier;
            ErrorCodes? error = null;

            dynamic exchangeOption = new ExpandoObject();
            BaseExchangeHandlerInterface hander = null;

            if (game == null)
                error = ErrorCodes.INVALID_GAME_ID;
            else if (user == null)
                error = ErrorCodes.INVALID_SESSION;
            else if (quantity == 0
                || (string.IsNullOrEmpty(exchangeOptionIdentifier)
                    && (exchangeOptionID == 0 || string.IsNullOrEmpty(order_id))
                   ))
                error = ErrorCodes.MISSING_FIELDS;
            else
            {
                RBAC rbac = new RBAC(user.id);
                bool? isActive = true;
                if (!rbac.HasRole(GoPlayConstantValues.S_ROLE_GAME_ADMIN) && !rbac.HasRole(GoPlayConstantValues.S_ROLE_ADMIN))
                {
                    isActive = null;
                }

                if (exchangeOptionID > 0 && !string.IsNullOrEmpty(order_id))
                {
                    exchangeOption = BaseExchangeHandler.retrieveExchangeOption(exchangeOptionType, exchangeOptionID, isActive);
                }
                else if (!string.IsNullOrEmpty(exchangeOptionIdentifier))
                {
                    exchangeOption = BaseExchangeHandler.retrieveExchangeOptionByStrIdentifier(exchangeOptionIdentifier, isActive);

                }

                hander = BaseExchangeHandler.retrieveExchangeHandler(user, game, exchangeOption, quantity, IPAddress.Parse(WebApiIpHelper.GetClientIp(Request)));

                var errorDict = new Dictionary<string, ErrorCodes>()
                {
                };

                if (!hander.validate())
                {
                    var errors = hander.getErrors();
                    error = EnumEx.GetValueFromDescription<ErrorCodes>(errors[errors.Keys.First()].First());
                }
                else if (!await hander.exchange())
                {
                    var errors = hander.getErrors();
                    error = EnumEx.GetValueFromDescription<ErrorCodes>(errors[errors.Keys.First()].First());
                }
            }

            api.LogApi("1", request.Url.LocalPath, error == null,
                request.UserAgent != null ? request.UserAgent.ToString() : string.Empty,
                game == null ? 0 : game.id, user == null ? 0 : user.id,
                request.UserHostAddress,
                error.ToErrorCode(), request.Params.ToString());

            if (error != null)
                return Json(new
                {
                    success = false,
                    message = error.ToErrorMessage(),
                    error_code = error.ToErrorCode()
                });

            return Json(new
            {
                success = true,
                transaction_id = new string[] { hander.getCoinTransaction().order_id },
                exchange = TransactionHelper.toDictionary(hander.getCoinTransaction())
            });
        }
        public bool ExchangeHandlerValidation(dynamic exchangeOption,
            out List<Tuple<string, string>> result,
            Game game, customer_account user)
        {
            result = new List<Tuple<string, string>>();
            if (exchangeOption == null)
            {
                result.Add(Tuple.Create<string, string>("exchange_option_id", "Exchange Option has been removed"));
                return false;
            }

            if (exchangeOption.is_archived)
            {
                result.Add(Tuple.Create<string, string>("exchange_option_id", "Exchange Option has been removed"));
            }

            if (exchangeOption.game != game.id)
            {
                result.Add(Tuple.Create<string, string>("exchange_option_id", "Exchange Option does not belong to game"));
            }

            RBAC rbac = new RBAC(CurrentUser.Id);
            if (!rbac.HasRole(GoPlayConstantValues.S_ROLE_GAME_ADMIN) && !rbac.HasRole(GoPlayConstantValues.S_ROLE_ADMIN))
            {
                if (!game.is_active || game.is_archived)
                {
                    result.Add(Tuple.Create<string, string>("exchange_option_id", "Exchange Option has been removed"));
                }
            }

            int in_game_amount = 0;
            bool isParse = Int32.TryParse(exchangeOption.inGameAmount, out in_game_amount);
            if (!isParse)
            {
                result.Add(Tuple.Create<string, string>("exchange_option_id", "Exchange amount needs to be a positive integer"));
            }

            if (in_game_amount == 0)
            {
                result.Add(Tuple.Create<string, string>("exchange_option_id", "Exchange amount is required"));
            }

            if (!result.Any())
            {
                decimal freePlayToken = 0;
                decimal playToken = 0;
                GoPlayApi.Instance.Calculate(exchangeOption, out freePlayToken, out playToken,
                    user, in_game_amount);

                if (playToken == 0 && freePlayToken == 0)
                    result.Add(Tuple.Create<string, string>("exchange_option_id", "Insufficient Balance"));
                else
                    return true;
            }

            return false;
        }
        public object UpdateExternalExchange()
        {
            var request = HttpContext.Current.Request;
            GoPlayApi api = GoPlayApi.Instance;
            string gameUid = request.Params["game_id"];
            Game game = api.GetGame(gameUid).Data;
            //string session = request.Params["session"];
            string session = !string.IsNullOrEmpty(request.Form["session"])
                ? UrlHelperExtensions.GetSession(request.Form["session"])
                : request.Params["session"];

            customer_account user = api.LoadFromAccessToken(session).Data;
            string identifier = request.Params["exchange_option_identifier"] == null
                ? string.Empty
                : request.Params["exchange_option_identifier"];
            string transactionId = request.Params["transaction_id"];

            ErrorCodes? error = null;

            if (game == null)
                error = ErrorCodes.INVALID_GAME_ID;
            else if (user == null)
                error = ErrorCodes.INVALID_SESSION;
            else
            {
                RBAC rbac = new RBAC(user.id);
                bool? isActive = true;
                //# To allow admin account to test exchange option without showing it to everyone
                if (rbac.HasRole(GoPlayConstantValues.S_ROLE_GAME_ADMIN) || rbac.HasRole(GoPlayConstantValues.S_ROLE_ADMIN))
                    isActive = null;

                Tuple<string, int> tuple = api.GetExchangeOption(identifier, isActive, false);
                if (tuple == null)
                    error = ErrorCodes.INVALID_EXCHANGE_OPTION;
                else
                {
                    var lstExternalExchange = api.GetExternalExchanges(user.id, game.id, null, transactionId);
                    if (lstExternalExchange != null && lstExternalExchange.Any())
                        error = ErrorCodes.EXCHANGE_RECORDED;
                    else
                    {
                        var externalExchange = new external_exchange
                        {
                            customer_account_id = user.id,
                            game_id = game.id,
                            exchange_option_identifier = identifier,
                            transaction_id = transactionId
                        };

                        if (string.Compare(tuple.Item1, GoPlayConstantValues.S_CREDIT_TYPE, StringComparison.OrdinalIgnoreCase) == 0)
                            externalExchange.credit_type_id = tuple.Item2;
                        else if (string.Compare(tuple.Item1, GoPlayConstantValues.S_PACKAGE, StringComparison.OrdinalIgnoreCase) == 0)
                            externalExchange.package_id = tuple.Item2;

                        if (!api.CreateExternalExchange(externalExchange))
                            error = ErrorCodes.ServerError;
                    }
                }
            }

            api.LogApi("1", request.Url.LocalPath, error == null,
                request.UserAgent != null ? request.UserAgent.ToString() : string.Empty,
                game == null ? 0 : game.id, user == null ? 0 : user.id,
                request.UserHostAddress,
                error.ToErrorCode(), request.Params.ToString());

            if (error == null)
                return Json(new { success = true });

            return Json(new
            {
                success = false,
                message = error.ToErrorMessage(),
                error_code = error.ToErrorCode()
            });
        }