public async Task <string> PlayerLogin(string jsonRequest)
        {
            if (m_LocalNode == null)
            {
                return(m_LocalNode.GetJsonHelper().ToJsonString(new
                {
                    error_code = -1,
                    error_message = "Service is not available"
                }));
            }

            m_LocalNode.GetLogger().Info("call merchant api: player-login");

            dynamic req         = m_LocalNode.GetJsonHelper().ToJsonObject(jsonRequest);
            string  merchantUrl = req.merchant_url.ToString();

            m_LocalNode.GetLogger().Info("Player login - [" + req.merchant_code.ToString() + "] " + req.player_id.ToString());
            m_LocalNode.GetLogger().Info("Merchant URL - " + merchantUrl);

            dynamic ret = null;

            var apiReq = new
            {
                req.merchant_code,
                req.currency_code,
                req.player_id,
                req.login_token
            };

            try
            {
                var rawret = await RemoteCaller.Request <Tuple <int, string> >(merchantUrl + "/player/validate-login", apiReq, null, 10 * 1000);

                m_LocalNode.GetLogger().Info("3-Way Login Reply Code: " + rawret.Item1);
                ret = m_LocalNode.GetJsonHelper().ToJsonObject(rawret.Item2);
            }
            catch (Exception ex)
            {
                ret = null;
                m_LocalNode.GetLogger().Error("Three-Way Login Error - Failed to call merchant API: " + ex.Message);
            }

            if (ret == null)
            {
                return(null);
            }

            if (ret.error_code != 0)
            {
                m_LocalNode.GetLogger().Error("Three-Way Login Error: " + (ret == null ? "Failed to call merchant API" : ret.error_message));
                return(m_LocalNode.GetJsonHelper().ToJsonString(new
                {
                    error_code = -1,
                    error_message = "Three-Way Login Error"
                }));
            }

            return(m_LocalNode.GetJsonHelper().ToJsonString(ret));
        }
        public async Task <string> GetPlayerBalance(string jsonRequest)
        {
            if (m_LocalNode == null)
            {
                return(m_LocalNode.GetJsonHelper().ToJsonString(new
                {
                    error_code = -1,
                    error_message = "Service is not available"
                }));
            }

            //m_LocalNode.GetLogger().Info("call merchant api: get-player-balance");

            dynamic req         = m_LocalNode.GetJsonHelper().ToJsonObject(jsonRequest);
            string  merchantUrl = req.merchant_url.ToString();

            //m_LocalNode.GetLogger().Info("Merchant URL - " + merchantUrl);

            dynamic ret = null;

            var apiReq = new
            {
                req.merchant_code,
                req.currency_code,
                req.player_id
            };

            try
            {
                ret = await RemoteCaller.Request(merchantUrl + "/player/get-balance", apiReq, null, 10 * 1000);
            }
            catch (Exception ex)
            {
                ret = null;
                m_LocalNode.GetLogger().Error("Get Player Balance Error - Failed to call merchant API: " + ex.Message);
            }

            if (ret == null)
            {
                return(null);
            }

            if (ret.error_code != 0)
            {
                m_LocalNode.GetLogger().Error("Get Player Balance Error: " + (ret == null ? "Failed to call merchant API" : ret.error_message));
                return(m_LocalNode.GetJsonHelper().ToJsonString(new
                {
                    error_code = -1,
                    error_message = "Get Player Balance Error"
                }));
            }
            return(m_LocalNode.GetJsonHelper().ToJsonString(ret));
        }
        public async Task <string> CreditForSettling(string jsonRequest)
        {
            if (m_LocalNode == null)
            {
                m_LocalNode.GetLogger().Error("CreditForSettling Error: Service is not available");
                return(null);
            }
            ;

            //m_LocalNode.GetLogger().Info("call merchant api: credit-for-settling");

            dynamic req         = m_LocalNode.GetJsonHelper().ToJsonObject(jsonRequest);
            string  merchantUrl = req.merchant_url.ToString();

            //m_LocalNode.GetLogger().Info("Merchant URL - " + merchantUrl);

            dynamic ret = null;

            var apiReq = new
            {
                req.credit_uuid,
                req.bet_uuid,
                req.merchant_code,
                req.currency_code,
                req.player_id,
                req.round_id,
                req.bet_pool,
                req.credit_amount,
                req.bet_settle_time,
                req.request_times,
                req.is_cancelled
            };

            try
            {
                ret = await RemoteCaller.Request(merchantUrl + "/bet/credit-for-settling-bet", apiReq, 20 * 1000);
            }
            catch (Exception ex)
            {
                ret = null;
                m_LocalNode.GetLogger().Error("CreditForSettling Error - Failed to call merchant API: " + ex.Message);
            }

            if (ret == null)
            {
                return(null);
            }

            return(m_LocalNode.GetJsonHelper().ToJsonString(ret));
        }
        public async Task <string> CancelDebit(string jsonRequest)
        {
            if (m_LocalNode == null)
            {
                m_LocalNode.GetLogger().Error("CancelDebit Error: Service is not available");
                return(null);
            }
            ;

            //m_LocalNode.GetLogger().Info("call merchant api: debit-for-betting");

            dynamic req         = m_LocalNode.GetJsonHelper().ToJsonObject(jsonRequest);
            string  merchantUrl = req.merchant_url.ToString();

            //m_LocalNode.GetLogger().Info("Merchant URL - " + merchantUrl);

            dynamic ret = null;

            var apiReq = new
            {
                req.trans_uuid,
                req.debit_uuid,
                req.merchant_code,
                req.currency_code,
                req.amount
            };

            try
            {
                ret = await RemoteCaller.Request(merchantUrl + "/bet/cancel-debit", apiReq, null, 20 * 1000);
            }
            catch (Exception ex)
            {
                ret = null;
                m_LocalNode.GetLogger().Error("CancelDebit Error - Failed to call merchant API: " + ex.Message);
            }

            if (ret == null)
            {
                return(null);
            }

            return(m_LocalNode.GetJsonHelper().ToJsonString(ret));
        }
Exemplo n.º 5
0
        public async Task CreditForBetting(RequestContext ctx)
        {
            ctx.Logger.Info("Credit for settling-bet...");

            string reqstr = ctx.Data.ToString();

            if (reqstr.Trim().Length <= 0)
            {
                await ctx.Session.Send(ctx.JsonHelper.ToJsonString(new
                {
                    error_code    = -1,
                    error_message = "Invalid request"
                }));

                return;
            }

            dynamic req = ctx.JsonHelper.ToJsonObject(reqstr);

            ctx.Logger.Info("Create credit record in db...");

            var saveReq = new
            {
                bet_uuid      = req.bet_uuid,
                table_code    = req.table_code,
                shoe_code     = req.shoe_code,
                round_number  = req.round_number,
                bet_pool      = req.bet_pool,
                merchant_code = req.merchant_code,
                player_id     = req.player_id,
                pay_amount    = req.pay_amount
            };
            string dbReplyStr = await RemoteCaller.RandomCall(ctx.RemoteServices,
                                                              "transaction-data", "create-credit", ctx.JsonHelper.ToJsonString(saveReq));

            if (dbReplyStr.Trim().Length <= 0 || !dbReplyStr.Contains('{'))
            {
                await ctx.Session.Send(ctx.JsonHelper.ToJsonString(new
                {
                    error_code    = -1,
                    error_message = "Failed to create credit record in db: " + dbReplyStr
                }));

                return;
            }

            ctx.Logger.Info("Call merchant site to credit...");

            dynamic dbReply = ctx.JsonHelper.ToJsonObject(dbReplyStr);
            string  apiUrl  = dbReply.request_url;

            var apiReq = new
            {
                dbReply.credit_uuid,
                req.bet_uuid,
                req.merchant_code,
                req.player_id,
                dbReply.round_id,
                req.bet_pool,
                credit_amount   = req.pay_amount,
                bet_settle_time = req.settle_time,
                is_cancelled    = false
            };
            dynamic ret = await RemoteCaller.Request(apiUrl, apiReq, 10 * 1000);

            if (ret == null)
            {
                ctx.Logger.Info("Failed to call credit function from merchant site");

                var updateReq = new
                {
                    dbReply.credit_uuid,
                    req.merchant_code,
                    req.player_id,
                    request_times  = 1,
                    is_success     = 0,
                    network_error  = 1,
                    response_error = 0
                };
                string dbReplyStr2 = await RemoteCaller.RandomCall(ctx.RemoteServices,
                                                                   "transaction-data", "update-credit", ctx.JsonHelper.ToJsonString(updateReq));

                dynamic dbReply2 = ctx.JsonHelper.ToJsonObject(dbReplyStr2);

                await ctx.Session.Send(ctx.JsonHelper.ToJsonString(new
                {
                    error_code    = -1,
                    error_message = "Failed to call credit function from merchant site"
                }));
            }
            else
            {
                ctx.Logger.Info("Update credit record in db...");

                if (ret.error_code == 0)
                {
                    var updateReq = new
                    {
                        dbReply.credit_uuid,
                        req.merchant_code,
                        req.player_id,
                        request_times  = 1,
                        is_success     = 1,
                        network_error  = 0,
                        response_error = 0
                    };
                    string dbReplyStr2 = await RemoteCaller.RandomCall(ctx.RemoteServices,
                                                                       "transaction-data", "update-credit", ctx.JsonHelper.ToJsonString(updateReq));

                    if (String.IsNullOrEmpty(dbReplyStr2))
                    {
                        ctx.Logger.Info("Failed to update credit record in db");
                    }
                    else
                    {
                        dynamic dbReply2 = ctx.JsonHelper.ToJsonObject(dbReplyStr2);
                        ctx.Logger.Info("Update credit record in db - error code: " + dbReply2.error_code.ToString());
                    }
                }
                else
                {
                    var updateReq = new
                    {
                        dbReply.credit_uuid,
                        req.merchant_code,
                        req.player_id,
                        request_times  = 1,
                        is_success     = 0,
                        network_error  = 0,
                        response_error = ret.error_code
                    };
                    string dbReplyStr2 = await RemoteCaller.RandomCall(ctx.RemoteServices,
                                                                       "transaction-data", "update-credit", ctx.JsonHelper.ToJsonString(updateReq));

                    dynamic dbReply2 = ctx.JsonHelper.ToJsonObject(dbReplyStr2);
                }

                await ctx.Session.Send(ctx.JsonHelper.ToJsonString(ret));
            }
        }
        public async Task PlayerLogin(RequestContext ctx)
        {
            string reqstr = ctx.Data.ToString();

            if (reqstr.Trim().Length <= 0)
            {
                await ctx.Session.Send(ctx.JsonHelper.ToJsonString(new
                {
                    error_code    = -1,
                    error_message = "Invalid request"
                }));

                return;
            }

            dynamic req = ctx.JsonHelper.ToJsonObject(reqstr);

            string merchantUrl = await RemoteCaller.RandomCall(m_LocalNode.GetRemoteServices(),
                                                               "merchant-data", "get-merchant-url", req.merchant_code.ToString());

            if (String.IsNullOrEmpty(merchantUrl))
            {
                await ctx.Session.Send(ctx.JsonHelper.ToJsonString(new
                {
                    error_code    = -1,
                    error_message = "Merchant API URL not found: " + req.merchant_code.ToString()
                }));

                return;
            }

            ctx.Logger.Info("Player login - [" + req.merchant_code.ToString() + "] " + req.player_id.ToString());
            ctx.Logger.Info("Merchant URL - " + merchantUrl);

            var apiReq = new
            {
                req.merchant_code,
                req.player_id,
                req.login_token
            };
            dynamic ret = await RemoteCaller.Request(merchantUrl + "/player/validate-login", apiReq, null, 10 * 1000);

            if (ret == null || ret.error_code != 0)
            {
                ctx.Logger.Error("Three-Way Login Error: " + (ret == null ? "Failed to call merchant API" : ret.error_message));
                await ctx.Session.Send(ctx.JsonHelper.ToJsonString(new
                {
                    error_code    = -1,
                    error_message = "Three-Way Login Error"
                }));

                return;
            }

            var okay      = false;
            var sessionId = Guid.NewGuid().ToString();

            var dbhelper = m_LocalNode.GetDataHelper();

            using (var cnn = dbhelper.OpenDatabase(m_MainCache))
            {
                using (var cmd = cnn.CreateCommand())
                {
                    dbhelper.AddParam(cmd, "@session_id", sessionId);
                    dbhelper.AddParam(cmd, "@merchant_code", req.merchant_code);
                    dbhelper.AddParam(cmd, "@player_id", req.player_id);

                    cmd.CommandText = " update tbl_player_session "
                                      + " set session_id = @session_id , update_time = NOW() "
                                      + " where merchant_code = @merchant_code and player_id = @player_id ";

                    okay = cmd.ExecuteNonQuery() > 0;

                    if (!okay)
                    {
                        using (var cmd2 = cnn.CreateCommand())
                        {
                            dbhelper.AddParam(cmd2, "@session_id", sessionId);
                            dbhelper.AddParam(cmd2, "@merchant_code", req.merchant_code);
                            dbhelper.AddParam(cmd2, "@player_id", req.player_id);

                            cmd2.CommandText = " insert into tbl_player_session "
                                               + " ( session_id , merchant_code, player_id, update_time ) values "
                                               + " ( @session_id, @merchant_code, @player_id, NOW() ) ";

                            okay = cmd2.ExecuteNonQuery() > 0;
                        }
                    }
                }
            }

            if (!okay)
            {
                ctx.Logger.Error("Three-Way Login Failed!");
                await ctx.Session.Send(ctx.JsonHelper.ToJsonString(new
                {
                    error_code    = -1,
                    error_message = "Three-Way Login Failed"
                }));

                return;
            }

            var remoteServices = m_LocalNode.GetRemoteServices();

            var frontEndUrl  = RandomPickPublicServiceUrl(remoteServices, "table-info");
            var betServerUrl = RandomPickPublicServiceUrl(remoteServices, "accept-bet");

            await ctx.Session.Send(ctx.JsonHelper.ToJsonString(new
            {
                req.merchant_code,
                req.player_id,
                ret.player_balance,
                session_id    = sessionId,
                front_end     = frontEndUrl,
                bet_server    = betServerUrl,
                error_code    = 0,
                error_message = "Three-Way Login Okay"
            }));
        }