예제 #1
0
        public ResultDTO NewTHTWithdrawal(NewTHTWithdrawalFormDTO form)
        {
            if (form.amount > 10000000)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                            "invalid amount"));
            }

            var fundService  = new FundService(db);
            var withdrawalId = fundService.NewTHTWithdrawal(UserId, form.amount);

            var withdrawal = db.THTWithdrawals.FirstOrDefault(o => o.Id == withdrawalId);

            var balanceType = db.BalanceTypes.FirstOrDefault(o => o.Id == withdrawal.BalanceTypeId);

            var request =
                WebRequest.CreateHttp(YJYGlobal.THT_BC_API_HOST + "refund?type=" + balanceType.Code.ToLower() + "&id=" +
                                      withdrawal.Id + "&to=" + withdrawal.To + "&value=" + withdrawal.Value);

            request.Method = "GET";
            request.Headers.Add("Authorization", "Bearer " + YJYGlobal.CALLBACK_AUTH_TOKEN);

            withdrawal.SendAt = DateTime.UtcNow;
            try
            {
                var response       = request.GetResponse() as HttpWebResponse;
                var responseStream = response.GetResponseStream();
                var sr             = new StreamReader(responseStream);
                var str            = sr.ReadToEnd();

                withdrawal.SendResult = response.StatusCode.ToString();
            }
            catch (Exception e)
            {
                withdrawal.SendResult = e.Message.TruncateMax(500);
            }
            db.SaveChanges();

            return(new ResultDTO(true));
        }