private MerchantWithdrawal CancelWithdrawal(MerchantWithdrawal fromWithdraw) { fromWithdraw.Status = TransactionStatus.Cancelled; fromWithdraw.TransactionId = Guid.NewGuid().ToString("N"); fromWithdraw.SelfPlatform = true;//平台内提币 using (var scope = new TransactionScope()) { fromWithdraw = new MerchantWithdrawalDAC().Create(fromWithdraw); var fromWithdrawFee = new MerchantWithdrawalFee { Amount = fromWithdraw.Amount, Fee = 0, Timestamp = DateTime.UtcNow, WithdrawalId = fromWithdraw.Id }; new MerchantWithdrawalFeeDAC().Create(fromWithdrawFee); scope.Complete(); } MerchantMSMQ.PubMerchantWithdrawReject(fromWithdraw.Id, 0); return(fromWithdraw); }
private MerchantWithdrawal WithdrawalToOutside(MerchantWallet fromWallet, MerchantWithdrawal fromWithdraw, Cryptocurrency cryptocurrency, MerchantAccount account, decimal amount, decimal fee, string address, string tag, string clientIP) { var actualAmount = amount - fee; ILog _logger = LogManager.GetLogger("LogicError"); using (var scope = new TransactionScope()) { try { fromWithdraw.Status = TransactionStatus.UnSubmit; fromWithdraw = new MerchantWithdrawalDAC().Create(fromWithdraw); var fromWithdrawFee = new MerchantWithdrawalFee { Amount = amount, Fee = fee, Timestamp = DateTime.UtcNow, WithdrawalId = fromWithdraw.Id }; new MerchantWithdrawalFeeDAC().Create(fromWithdrawFee); new MerchantWalletDAC().Freeze(fromWallet.Id, amount); //var requestInfo = new FiiiFinanceAgent().TryCreateWithdraw(requestModel); //new MerchantWithdrawalDAC().UpdateTransactionId(fromWithdraw.Id, requestInfo.RequestID, // requestInfo.TransactionId); scope.Complete(); } catch (CommonException) { throw; } catch (Exception ex) { _logger.Info($"Withdraw ApplyWithdrawal faild.WithdrawID:{fromWithdraw.Id},OrderNo:{fromWithdraw.OrderNo}.request Parameter - . Error message:{ex.Message}"); throw; } } var requestModel = new CreateWithdrawModel { AccountID = account.Id, AccountType = AccountTypeEnum.Merchant, CryptoName = cryptocurrency.Code, ReceivingAddress = address, DestinationTag = tag, Amount = actualAmount, IPAddress = clientIP, TransactionFee = fee, WithdrawalId = fromWithdraw.Id }; RabbitMQSender.SendMessage("WithdrawSubmit", requestModel); return(fromWithdraw); }
public MerchantWithdrawal Create(MerchantWithdrawal merchantWithdrawal) { const string sql = @"INSERT INTO [dbo].[MerchantWithdrawals] ([MerchantAccountId], [MerchantWalletId], [Address],[Tag], [Amount], [Status], [Timestamp], [Remark],[OrderNo],[TransactionId],[SelfPlatform],[RequestId],[CryptoId],[CryptoCode]) VALUES (@MerchantAccountId, @MerchantWalletId, @Address,@Tag, @Amount, @Status, @Timestamp, @Remark, @OrderNo, @TransactionId,@SelfPlatform,@RequestId,@CryptoId,@CryptoCode); SELECT SCOPE_IDENTITY()"; using (var conn = WriteConnection()) { merchantWithdrawal.Id = conn.ExecuteScalar <long>(sql, merchantWithdrawal); return(merchantWithdrawal); } }
//private MerchantWithdrawal WithdrawalToMerchantAccount(MerchantWallet fromWallet, MerchantWithdrawal fromWithdraw, MerchantWallet toWallet) //{ // MerchantDeposit deposit = null; // using (var scope = new TransactionScope()) // { // //提币 // fromWithdraw.Status = TransactionStatus.Confirmed; // fromWithdraw.TransactionId = toWallet.MerchantAccountId.ToString("N"); // fromWithdraw.SelfPlatform = true;//平台内提币 // fromWithdraw = new MerchantWithdrawalDAC().Create(fromWithdraw); // var fromWithdrawFee = new MerchantWithdrawalFee // { // Amount = fromWithdraw.Amount, // Timestamp = DateTime.UtcNow, // Fee = fromWithdraw.Amount * WithdrawalMasterSetting.ToMerchantHandleFeeTier, // WithdrawalId = fromWithdraw.Id // }; // new MerchantWithdrawalFeeDAC().Create(fromWithdrawFee); // new MerchantWalletDAC().Decrease(fromWallet.Id, fromWithdraw.Amount); // new MerchantWalletStatementDAC().Insert(new MerchantWalletStatement // { // WalletId = fromWallet.Id, // Action = MerchantWalletStatementAction.Withdrawal, // Amount = -fromWithdraw.Amount, // Balance = fromWallet.Balance - fromWithdraw.Amount, // Timestamp = DateTime.UtcNow // }); // //充币 // var amount = fromWithdraw.Amount - fromWithdrawFee.Fee; // if (amount <= 0) // { // throw new CommonException(ReasonCode.GENERAL_ERROR, Resources.到账数量不能为零或者负数); // } // deposit = new MerchantDepositDAC().Insert(new MerchantDeposit // { // MerchantAccountId = toWallet.MerchantAccountId, // MerchantWalletId = toWallet.Id, // FromAddress = fromWallet.Address, // ToAddress = toWallet.Address, // Amount = fromWithdraw.Amount - fromWithdrawFee.Fee, // Status = TransactionStatus.Confirmed, // Timestamp = DateTime.UtcNow, // OrderNo = CreateOrderno(), // TransactionId = fromWallet.MerchantAccountId.ToString("N"), // SelfPlatform = true // }); // new MerchantWalletDAC().Increase(toWallet.Id, amount); // new MerchantWalletStatementDAC().Insert(new MerchantWalletStatement // { // WalletId = toWallet.Id, // Action = MerchantWalletStatementAction.Deposit, // Amount = amount, // Balance = toWallet.Balance + amount, // Timestamp = DateTime.UtcNow // }); // scope.Complete(); // } // var withdrawId = fromWithdraw.Id; // var depositId = deposit.Id; // var t1 = Task.Run(() => new FiiiPOSPushComponent().PushWithdrawCompleted(withdrawId)); // var t2 = Task.Run(() => new FiiiPOSPushComponent().PushDeposit(depositId)); // Task.WaitAll(t1, t2); // return fromWithdraw; //} private MerchantWithdrawal WithdrawalToUserAccount(MerchantWallet fromWallet, MerchantWithdrawal fromWithdraw, UserWallet toWallet, decimal fee) { UserDeposit deposit; using (var scope = new TransactionScope()) { //提币 fromWithdraw.Status = TransactionStatus.Confirmed; fromWithdraw.TransactionId = toWallet.UserAccountId.ToString("N"); fromWithdraw.SelfPlatform = true;//平台内提币 fromWithdraw = new MerchantWithdrawalDAC().Create(fromWithdraw); var fromWithdrawFee = new MerchantWithdrawalFee { Amount = fromWithdraw.Amount, Timestamp = DateTime.UtcNow, Fee = fee, WithdrawalId = fromWithdraw.Id }; new MerchantWithdrawalFeeDAC().Create(fromWithdrawFee); new MerchantWalletDAC().Decrease(fromWallet.Id, fromWithdraw.Amount); new MerchantWalletStatementDAC().Insert(new MerchantWalletStatement { WalletId = fromWallet.Id, Action = MerchantWalletStatementAction.Withdrawal, Amount = -fromWithdraw.Amount, Balance = fromWallet.Balance - fromWithdraw.Amount, Timestamp = DateTime.UtcNow }); //充币 var amount = fromWithdraw.Amount - fromWithdrawFee.Fee; new UserWalletDAC().Increase(toWallet.Id, amount); new UserWalletStatementDAC().Insert(new UserWalletStatement { WalletId = toWallet.Id, Action = UserWalletStatementAction.Deposit, Amount = amount, Balance = toWallet.Balance + amount, FrozenAmount = 0, FrozenBalance = toWallet.FrozenBalance, Timestamp = DateTime.UtcNow }); deposit = new UserDepositDAC().Insert(new UserDeposit { UserAccountId = toWallet.UserAccountId, UserWalletId = toWallet.Id, FromAddress = fromWallet.Address, FromTag = fromWallet.Tag, ToAddress = toWallet.Address, ToTag = toWallet.Tag, Amount = amount, Status = TransactionStatus.Confirmed, Timestamp = DateTime.UtcNow, OrderNo = NumberGenerator.GenerateUnixOrderNo(), TransactionId = fromWallet.MerchantAccountId.ToString("N"), SelfPlatform = true, CryptoCode = toWallet.CryptoCode }); new UserTransactionDAC().Insert(new UserTransaction { Id = Guid.NewGuid(), AccountId = toWallet.UserAccountId, CryptoId = toWallet.CryptoId, CryptoCode = toWallet.CryptoCode, Type = UserTransactionType.Deposit, DetailId = deposit.Id.ToString(), Status = (byte)deposit.Status, Timestamp = deposit.Timestamp, Amount = amount, OrderNo = deposit.OrderNo }); scope.Complete(); } var withdrawId = fromWithdraw.Id; var depositId = deposit.Id; MerchantMSMQ.PubMerchantWithdrawCompleted(withdrawId, 0); MerchantMSMQ.PubUserDeposit(depositId, 0); return(fromWithdraw); }
public MerchantWithdrawal Withdrawal(Guid accountId, decimal amount, int cryptoId, string address, string tag, string clientIP) { SecurityVerify.Verify <WithdrawVerify>(new CustomVerifier("MerchantWithdraw"), SystemPlatform.FiiiPOS, accountId.ToString(), (model) => { return(model.PinVerified && model.CombinedVerified); }); var cryptocurrency = new CryptocurrencyDAC().GetById(cryptoId); CryptoAddressValidation.ValidateAddress(cryptocurrency.Code, address); if (!string.IsNullOrEmpty(tag)) { CryptoAddressValidation.ValidateTag(cryptocurrency.Code, tag); } var account = new MerchantAccountDAC().GetById(accountId); if (!new ProfileComponent().ValidateLv1(accountId)) { throw new CommonException(ReasonCode.NOT_VERIFY_LV1, Resources.需要Lv1认证才能使用相关功能); } if (!account.IsAllowWithdrawal) { throw new CommonException(ReasonCode.Not_Allow_Withdrawal, Resources.禁止提币); } if (!cryptocurrency.Status.HasFlag(CryptoStatus.Withdrawal)) { throw new CommonException(ReasonCode.CURRENCY_FORBIDDEN, Resources.CurrencyForbidden); } if (cryptocurrency.Enable == (byte)CurrencyStatus.Forbidden) { throw new CommonException(ReasonCode.CURRENCY_FORBIDDEN, Resources.CurrencyForbidden); } var fromWallet = new MerchantWalletDAC().GetByAccountId(accountId, cryptoId); if (fromWallet == null) { throw new CommonException(ReasonCode.Not_Allow_Withdrawal, Resources.禁止提币); } if (fromWallet.Balance < amount) { throw new CommonException(ReasonCode.GENERAL_ERROR, Resources.余额不足); } var profileAgent = new MerchantProfileAgent(); var profile = profileAgent.GetMerchantProfile(accountId); int level = profile.L2VerifyStatus == VerifyStatus.Certified ? 2 : profile.L1VerifyStatus == VerifyStatus.Certified ? 1 : 0; var masterSetting = GetMerchantWithdrawalMasterSettingWithCrypto(cryptocurrency, level); if (amount > masterSetting.PerTxLimit) { throw new CommonException(ReasonCode.GENERAL_ERROR, Resources.能高于单次提币量); } var dac = new MerchantWithdrawalDAC(); var today = DateTime.UtcNow.Date; decimal dailyWithdrawal = dac.DailyWithdrawal(accountId, cryptoId, today); if (amount > masterSetting.PerDayLimit - dailyWithdrawal) { throw new CommonException(ReasonCode.GENERAL_ERROR, Resources.今日提币达到限额); } decimal monthlyWithdrawal = dac.MonthlyWithdrawal(accountId, cryptoId, new DateTime(today.Year, today.Month, 1)); if (amount > masterSetting.PerMonthLimit - monthlyWithdrawal) { throw new CommonException(ReasonCode.GENERAL_ERROR, Resources.本月提币达到限额); } var fromWithdraw = new MerchantWithdrawal { MerchantAccountId = accountId, MerchantWalletId = fromWallet.Id, Address = address, Tag = tag, Amount = amount, Status = TransactionStatus.UnSubmit, Timestamp = DateTime.UtcNow, OrderNo = NumberGenerator.GenerateUnixOrderNo(), CryptoId = fromWallet.CryptoId, CryptoCode = fromWallet.CryptoCode }; var merchantWalletDac = new MerchantWalletDAC(); var userWalletDac = new UserWalletDAC(); //是否是商户地址 var toMerchantWallet = merchantWalletDac.GetByAddress(address, cryptocurrency.NeedTag ? tag : null); if (toMerchantWallet != null) { //if (toMerchantWallet.CryptoId != cryptoId) // throw new CommonException(10000, string.Format(Resources.提币地址不是有效的地址, cryptocurrency.Code)); //if (toMerchantWallet.MerchantAccountId == accountId) // throw new CommonException(ReasonCode.CANNOT_TRANSFER_TO_YOURSELF, Resources.提币地址不能是自己账户的地址); //return WithdrawalToMerchantAccount(fromWallet, fromWithdraw, toMerchantWallet); // 042018 throw new CommonException(ReasonCode.CAN_NOT_WITHDRAW_TO_FiiiPOS, Resources.FiiiPOSCantWithdrawToFiiiPOS); } //是否是用户地址 var toUserWallet = userWalletDac.GetByAddressAndCrypto(cryptoId, address, cryptocurrency.NeedTag ? tag : null); if (toUserWallet != null) { if (toUserWallet.CryptoId != cryptoId) { throw new CommonException(ReasonCode.GENERAL_ERROR, FiiiPay.Framework.Component.Properties.GeneralResources.EMInvalidAddress); } if (amount < masterSetting.ToUserMinAmount) { throw new CommonException(10000, Resources.能低于最低提币量); } var fee = (fromWithdraw.Amount * masterSetting.ToUserHandleFeeTier).ToSpecificDecimal(cryptocurrency.DecimalPlace); if (amount <= fee) { throw new CommonException(ReasonCode.GENERAL_ERROR, Resources.到账数量不能为零或者负数); } return(WithdrawalToUserAccount(fromWallet, fromWithdraw, toUserWallet, fee)); } //平台内提币如果tag不对,创建一条失败记录 if (cryptocurrency.NeedTag && (userWalletDac.IsUserWalletAddress(address) || merchantWalletDac.IsMerchantWalletAddress(address))) { return(CancelWithdrawal(fromWithdraw)); } //如果都不是,提币到场外 if (amount < masterSetting.ToOutsideMinAmount) { throw new CommonException(ReasonCode.GENERAL_ERROR, Resources.能低于最低提币量); } var baseFee = cryptocurrency.Withdrawal_Fee ?? 0; var tier = cryptocurrency.Withdrawal_Tier ?? 0; var fee1 = (amount * tier).ToSpecificDecimal(cryptocurrency.DecimalPlace); var totalFee = baseFee + fee1; if (amount <= totalFee) { throw new CommonException(ReasonCode.GENERAL_ERROR, Resources.到账数量不能为零或者负数); } return(WithdrawalToOutside(fromWallet, fromWithdraw, cryptocurrency, account, amount, totalFee, address, tag, clientIP)); }