public void finalizeTransactions(string status)
        {
            var api = GoPlayApi.Instance;
            if (coinTransaction != null && creditTransaction != null)
            {
                if (coinTransaction is coin_transaction)
                {
                    api.UpdateCoinTransactionStatus(coinTransaction.id, status);
                }
                else
                {
                    api.UpdateFreeCoinTransactionStatus(coinTransaction.id, status);
                }
                api.UpdateCreditTransactionStatus(creditTransaction.id, status);
                coinTransaction.status = status;
                creditTransaction.status = status;
            }
            if (status == "success")
            {
                //prepare TransactionJSON to submit to GTOKEN
                var token_transaction = new TokenTransactionJson()
                {
                    username = this.user.username,
                    order_id = coinTransaction.order_id,
                    transaction_type = "consumption",
                    description = coinTransaction.description,
                    amount = coinTransaction.amount
                };
                if (creditTransaction.coin_transaction_id.HasValue)
                {
                    token_transaction.token_type = "Play Token";
                }
                else
                {
                    token_transaction.token_type = "Free Play Token";
                }

                var gTokenTransaction = GoPlayApi.Instance.GTokenAPITransaction(new GtokenModelTransactionAction
                {
                    token_transaction = token_transaction,
                    ip_address = ip.ToString()
                });
            }

        }
        public object purchasePlayToken(PartnerViewModel param)
        {
            var api = GoPlayApi.Instance;
            string username = param.username;
            var user = api.GetUserByUserName(username).Data;
            //string status = param.status;
            string token = param.token;
            decimal amount = 0;

            var partner = api.GetPartner(ConfigurationManager.AppSettings["PARTNER_CLIENT_ID"]).Data;
            ErrorCodes? err = null;
            if (partner == null)
            {
                err = ErrorCodes.INVALID_PARTNER_ID;
            }
            else if (user == null)
            {
                err = ErrorCodes.NON_EXISTING_USER;
            }
            else
            {
                string tokenRawString = username + param.amount + partner.client_id;
                string generatedToken = Helper.CalculateMD5Hash(tokenRawString);

                if (generatedToken != token)
                    err = ErrorCodes.INVALID_TOKEN;

                decimal.TryParse(param.amount, out amount);
                if (amount <= 0)
                    err = ErrorCodes.INSUFFICIENT_AMOUNT;
            }
            if (err == null)
            {
                var transaction = new coin_transaction()
                {
                    order_id = Guid.NewGuid().ToString(),
                    customer_account_id = user.id,
                    amount = amount,
                    price = amount,
                    partner_account_id = partner.id,
                    description = string.Format("You received {0} Play Token from {1}", Helper.displayDecimal(amount), partner.name),
                    status = Helper.GetDescription(TransactionStatus.Success)
                };
                var result = api.CreateCoinTransaction(transaction);
                if (!result.HasData)
                    err = result.Error;
                else
                {
                    var tokenTrans = new TokenTransactionJson()
                    {
                        token_type = GoPlayConstantValues.S_PLAY_TOKEN,
                        transaction_type = ConstantValues.S_TRANSACTION_TYPE_CONSUMPTION,
                        username = user.username,
                        order_id = transaction.order_id,
                        description = transaction.description,
                        amount = transaction.amount
                    };
                    api.GTokenAPITransaction(new GtokenModelTransactionAction()
                    {
                        enumAction = EGtokenAction.RecordTransaction,
                        ip_address = Request.UserHostAddress,
                        token_transaction = tokenTrans
                    });
                }

            }

            api.LogApi("1", "/partner/purchase-play-token",
               err == null,
               Request.UserAgent != null ? Request.UserAgent.ToString() : string.Empty,
               0, user == null ? 0 : user.id,
               HttpContext.Request.UserHostAddress,
               err.ToErrorCode(), JsonConvert.SerializeObject(param));

            return JsonConvert.SerializeObject(new
            {
                success = err == null,
                message = err.ToErrorMessage(),
                error_code = err.ToErrorCode()
            });
        }
        public ActionResult Transfer(TransferModel model)
        {
            var param = HttpContext.Request.Params;
            var api = GoPlayApi.Instance;
            model.senderId = CurrentUser.Id;
            model.maxAmount = CurrentUser.play_token;
            customer_account receiver = null;
            var errors = model.IsValid(out receiver);
            string description = string.Empty;

            if (!ModelState.IsValid || (errors != null && errors.Any()))
            {
                if (errors != null && errors.Any())
                {
                    foreach (var error in errors)
                    {
                        ModelState.AddModelError(error.Item1, error.Item2);
                    }
                }
                return Json(new { errors = Errors(ModelState) });
            }
            else if (model.forValidate)
            {
                return Json(new { correct = true });
            }
            else
            {
                //# Save 2 coin_transactions (positive and negative) to Gtoken using record API
                //# Then, we will save them to GoPlay DB later

                #region Sender transaction, negative amount
                description = string.Format(GoPlayConstantValues.S_TRANSFER_TOKEN_DESCRIPTION,
                     model.playTokenAmount,
                     receiver.GetDisplayName());
                coin_transaction sendingTransaction = new coin_transaction(CurrentUser.Id, receiver.id, description);
                sendingTransaction.amount = model.playTokenAmount * -1;
                sendingTransaction.use_gtoken = true;

                TokenTransactionJson transactionJSON = new TokenTransactionJson(CurrentUser.UserName, sendingTransaction.order_id, sendingTransaction.description, sendingTransaction.amount.Value, GoPlayConstantValues.S_PLAY_TOKEN);
                var isRecordGtoken = api.GTokenAPITransaction(new GtokenModelTransactionAction
                {
                    enumAction = EGtokenAction.RecordTransaction,
                    token_transaction = transactionJSON
                });

                if (!isRecordGtoken.Succeeded)
                {
                    ModelState.AddModelError(GoPlayConstantValues.S_RECEIVER_ID, isRecordGtoken.Error.Value.ToErrorMessage());
                    return Json(new { errors = Errors(ModelState) });
                }
                #endregion

                #region Receiver transaction, positive amount
                description = string.Format(GoPlayConstantValues.S_RECEIVE_TOKEN_DESCRIPTION,
                     model.playTokenAmount,
                     CurrentUser.GetDisplayName());
                coin_transaction receivingTransaction = new coin_transaction(receiver.id, CurrentUser.Id, description);
                receivingTransaction.amount = model.playTokenAmount;

                transactionJSON = new TokenTransactionJson(receiver.username, receivingTransaction.order_id, receivingTransaction.description, receivingTransaction.amount.Value, GoPlayConstantValues.S_PLAY_TOKEN);
                isRecordGtoken = api.GTokenAPITransaction(new GtokenModelTransactionAction
                {
                    enumAction = EGtokenAction.RecordTransaction,
                    token_transaction = transactionJSON
                });

                if (!isRecordGtoken.Succeeded)
                {
                    ModelState.AddModelError(GoPlayConstantValues.S_RECEIVER_ID, isRecordGtoken.Error.Value.ToErrorMessage());
                    return Json(new { errors = Errors(ModelState) });
                }
                #endregion

                //Save to DB this coin_transaction if all Gtoken work well
                api.CreateTransferCoinTransaction(sendingTransaction);
                api.CreateTransferCoinTransaction(receivingTransaction);
            }

            description = string.Format(GoPlayConstantValues.S_TRANSFER_TOKEN_DESCRIPTION,
                model.playTokenAmount,
                receiver.GetDisplayName());

            return Json(new { correct = true, message = description, amount = -model.playTokenAmount });
        }