Beispiel #1
0
        public WithdrawRequestInfo CreateWithdraw(CreateWithdrawModel model)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GenerateToken());

                var         jsonStr = model.ToString();
                HttpContent content = new StringContent(jsonStr, Encoding.UTF8, "application/json");

                try
                {
                    var response = client.PostAsync($"{URL}/Wallet/Withdraw/Request", content).Result;

                    if (response.StatusCode >= HttpStatusCode.BadRequest)
                    {
                        LogHelper.Error($"Failed: Create withdraw bad request." +
                                        $"URL: {response.RequestMessage.RequestUri}\r\n" +
                                        $"Parameters: {jsonStr}\r\n" +
                                        $"ErrorCode: {(int)response.StatusCode}");
                        throw new CommonException((int)response.StatusCode, "create withdraw request failed.");
                    }

                    var result = response.Content.ReadAsStringAsync().Result;
                    LogHelper.Info($"Success: Create withdraw ok request." +
                                   $"URL: {response.RequestMessage.RequestUri}\r\n" +
                                   $"Parameters: {jsonStr}\r\n" +
                                   $"Result: {result}");
                    var data = JsonConvert.DeserializeObject <ServiceResult <WithdrawRequestInfo> >(result);
                    if (data.Code == 0)
                    {
                        return(data.Data);
                    }

                    if (data.Data == null)
                    {
                        data.Data = new WithdrawRequestInfo();
                    }

                    data.Data.Code = data.Code;
                    return(data.Data);
                }
                catch (Exception exception)
                {
                    LogHelper.ErrorFormat("Request Finance exception {0}, detail exception {1}", exception.Message, exception.InnerException?.Message);
                    //LogHelper.Error(exception);
                    throw exception;
                }

                //throw new CommonException(10000, "Create Withdraw Finance Error");
            }
        }
Beispiel #2
0
        public void SubmitWithdrawal(CreateWithdrawModel model)
        {
            if (model.AccountType == AccountTypeEnum.Merchant)
            {
                var withdrawalDAC = new MerchantWithdrawalDAC();
                var withdrawal    = withdrawalDAC.GetById(model.WithdrawalId);
                if (withdrawal == null || withdrawal.Status != Entities.Enums.TransactionStatus.UnSubmit)
                {
                    LogHelper.Info("Invalid withdrawal id or invalid withdrawal status");
                    return;
                }

                var agent = new FiiiFinanceAgent();

                try
                {
                    var requestInfo = agent.CreateWithdraw(model);

                    if (requestInfo.Code == 0)
                    {
                        withdrawalDAC.WithdrawalSubmited(model.WithdrawalId, requestInfo.RequestID,
                                                         requestInfo.TransactionId);
                    }
                    else if (requestInfo.Code == 20002 || requestInfo.Code == 300001)
                    {
                        var merchantWalletDac = new MerchantWalletDAC();
                        var merchantWallet    = merchantWalletDac.GetById(withdrawal.MerchantWalletId);
                        using (var scope = new TransactionScope())
                        {
                            try
                            {
                                withdrawalDAC.RejectById(withdrawal.Id, "Invalid address");
                                merchantWalletDac.Unfreeze(merchantWallet.Id, withdrawal.Amount);
                                new MerchantWalletStatementDAC().Insert(new MerchantWalletStatement
                                {
                                    WalletId  = merchantWallet.Id,
                                    Action    = UserWalletStatementAction.Withdrawal,
                                    Amount    = withdrawal.Amount,
                                    Balance   = merchantWallet.Balance + withdrawal.Amount,
                                    Timestamp = DateTime.UtcNow
                                });
                                scope.Complete();
                            }
                            catch (Exception exception)
                            {
                                _log.Error(exception);
                            }
                        }
                    }
                    else
                    {
                        RabbitMQSender.SendMessage("WithdrawSubmit_Delay", model);
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error($"UpdateTransactionId erorr, message:{JsonConvert.SerializeObject(model)}", ex);

                    RabbitMQSender.SendMessage("WithdrawSubmit_Delay", model);
                }
            }
            else if (model.AccountType == AccountTypeEnum.User)
            {
                var withdrawalDAC = new UserWithdrawalDAC();
                var withdrawal    = withdrawalDAC.GetById(model.WithdrawalId);
                if (withdrawal == null || withdrawal.Status != Entities.Enums.TransactionStatus.UnSubmit)
                {
                    LogHelper.Info("Invalid withdrawal id or invalid withdrawal status");
                    return;
                }

                var agent = new FiiiFinanceAgent();

                try
                {
                    var requestInfo = agent.CreateWithdraw(model);

                    if (requestInfo.Code == 0)
                    {
                        withdrawalDAC.WithdrawalSubmited(model.WithdrawalId, requestInfo.RequestID, requestInfo.TransactionId);
                    }
                    else if (requestInfo.Code == 20002 || requestInfo.Code == 300001)
                    {
                        var userWalletDac = new UserWalletDAC();
                        var userWallet    = userWalletDac.GetById(withdrawal.UserWalletId);
                        using (TransactionScope scope = new TransactionScope())
                        {
                            try
                            {
                                withdrawalDAC.RejectById(withdrawal.Id, "Invalid address");
                                userWalletDac.Unfreeze(withdrawal.UserWalletId, withdrawal.Amount);

                                new UserWalletStatementDAC().Insert(new Entities.UserWalletStatement
                                {
                                    WalletId      = userWallet.Id,
                                    Action        = Entities.UserWalletStatementAction.Withdrawal,
                                    Amount        = withdrawal.Amount,
                                    Balance       = userWallet.Balance + withdrawal.Amount,
                                    FrozenAmount  = -withdrawal.Amount,
                                    FrozenBalance = userWallet.FrozenBalance - withdrawal.Amount,
                                    Timestamp     = DateTime.UtcNow
                                });

                                scope.Complete();
                            }
                            catch (Exception exception)
                            {
                                _log.Error(exception);
                            }
                        }
                    }
                    else
                    {
                        RabbitMQSender.SendMessage("WithdrawSubmit_Delay", model);
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error($"UpdateTransactionId erorr, message:{JsonConvert.SerializeObject(model)}", ex);
                    RabbitMQSender.SendMessage("WithdrawSubmit_Delay", model);
                }
            }
            else
            {
                LogHelper.Info("Invalid AccountType");
            }
        }