public async Task <StoreConsumeDetail> GetConsumeDetailAsync(Guid accountId, Guid orderId, bool isZH) { var storeOrder = await new StoreOrderDAC().GetByIdAsync(orderId); var storeUserAccount = new UserAccountDAC().GetById(storeOrder.UserAccountId); if (storeUserAccount.Id != accountId) { throw new ApplicationException(); } var coin = new CryptocurrencyDAC().GetById(storeOrder.CryptoId); var priceInfo = new PriceInfoDAC().GetPriceByName(storeOrder.FiatCurrency, storeOrder.CryptoCode); var iRate = ((priceInfo - storeOrder.ExchangeRate) / storeOrder.ExchangeRate) * 100; return(new StoreConsumeDetail { Id = storeOrder.Id, Type = Resources.Payment, CryptoActualAmount = storeOrder.CryptoAmount.ToString(coin.DecimalPlace), CryptoCode = storeOrder.CryptoCode, Status = new UserStatementComponent().GetStatusStr(2, (int)storeOrder.Status, isZH), MerchantName = storeOrder.MerchantInfoName, FiatCurrency = storeOrder.FiatCurrency, FiatAmount = storeOrder.FiatAmount.ToString(2), MarkUp = $"{(storeOrder.Markup * 100).ToString(2)}%", ExchangeRate = $"1 {coin.Code} = {storeOrder.ExchangeRate.ToString(4)} {storeOrder.FiatCurrency}", CurrentExchangeRate = coin.Enable == 1 ? $"1 {coin.Code} = {priceInfo.ToString(4)} {storeOrder.FiatCurrency}" : "--", IncreaseRate = coin.Enable == 1 ? (iRate > 0 ? $"+{iRate.ToString(2)}" : iRate.ToString(2)) : "--", Timestamp = storeOrder.Timestamp.ToUnixTime().ToString(), OrderNo = storeOrder.OrderNo }); }
public MarketPriceInfo GetMarketPrice(string currency, string cryptoName) { var dac = new PriceInfoDAC(); var price = dac.GetPrice(currency, cryptoName).Select(s => { if (s != null) { return new MarketPriceInfo { CryptoName = s.CryptoName, Currency = s.Currency, Price = s.Price } } ; return(null); }).FirstOrDefault() ?? new MarketPriceInfo { CryptoName = cryptoName, Currency = currency, MarkupPrice = 0.0001M, Price = 0.0001M }; return(price); }
public List <MarketPriceInfo> GetMarketPrice(string currency) { var dac = new PriceInfoDAC(); var list = dac.GetPrice(currency).Select(s => new MarketPriceInfo { CryptoName = s.CryptoName, Currency = s.Currency, Price = s.Price }).ToList(); return(list); }
public PrePayOM PrePay(UserAccount user, PrePayIM im) { var merchantAccount = GetMerchantAccountByIdOrCode(im.MerchantId, im.MerchantCode); var userWallets = new UserWalletDAC().GetUserWallets(user.Id); var merchantWallets = new MerchantWalletDAC().GetByAccountId(merchantAccount.Id); var coins = new CryptocurrencyDAC().GetAllActived(); var priceList = new PriceInfoDAC().GetPrice(merchantAccount.FiatCurrency); bool showWhiteLable = false; if (merchantAccount.POSId.HasValue) { var pos = new POSDAC().GetById(merchantAccount.POSId.Value); if (pos.IsWhiteLabel) { showWhiteLable = true; } } if (!showWhiteLable) { var whilteLabelCryptoCode = new POSDAC().GetWhiteLabelCryptoCode(); coins.RemoveAll(t => t.Code == whilteLabelCryptoCode); } return(new PrePayOM { FiatCurrency = merchantAccount.FiatCurrency, MarkupRate = merchantAccount.Markup.ToString(CultureInfo.InvariantCulture), WaletList = coins.Select(a => { var userWallet = userWallets.FirstOrDefault(b => b.CryptoId == a.Id); decimal rate = 0; rate = priceList.Where(t => t.CryptoID == a.Id).Select(t => t.Price).FirstOrDefault(); return GetItem(userWallet, a, merchantWallets, rate); }).OrderByDescending(a => a.MerchantSupported).ThenBy(a => a.PayRank).Select(a => new WalletItem { Code = a.Code, NewStatus = a.NewStatus, ExchangeRate = a.ExchangeRate, FrozenBalance = a.FrozenBalance, IconUrl = a.IconUrl, Id = a.Id, MerchantSupported = a.MerchantSupported, Name = a.Name, UseableBalance = a.UseableBalance, FiatBalance = a.FiatBalance, DecimalPlace = a.DecimalPlace, CryptoEnable = a.CryptoEnable }).ToList() }); }
public BillerPrePayOM PrePay(BillerPrePayIM im) { var exchangeRate = new PriceInfoDAC() .GetPriceInfo(new CurrenciesDAC().GetByCode(im.FiatCurrency).ID, im.CryptoId).Price; var discount = im.CryptoCode.Equals("Fiii", StringComparison.InvariantCultureIgnoreCase) ? 1 - decimal.Parse(new MasterSettingDAC().SelectByGroup("BillerMaxAmount").First(item => item.Name.Equals("DiscountRate", StringComparison.CurrentCultureIgnoreCase)).Value) : 1; var errorTolerantRate = decimal.Parse(new MasterSettingDAC().Single("BillerMaxAmount", "Error_Tolerant_Rate").Value); if (!(im.CryptoAmount <= ((im.FiatAmount / exchangeRate) * discount) * (1 + errorTolerantRate) && im.CryptoAmount >= ((im.FiatAmount / exchangeRate) * discount) * (1 - errorTolerantRate))) { throw new CommonException(ReasonCode.BillerInvalidValues, Resources.BillerExchangeRateError); } var dac = new BillerOrderDAC(); var maxValue = decimal.Parse(new MasterSettingDAC().Single("BillerMaxAmount", "Biller_MaxAmount ").Value); if (im.FiatAmount > maxValue) { throw new CommonException(ReasonCode.BillerOverMaxAmount, string.Format(Resources.BillerOverMaxAmount, $"{maxValue} {im.FiatCurrency}")); } var totalMonthAmount = dac.GetMonthAmount(DateTime.UtcNow); if (totalMonthAmount + im.FiatAmount > decimal.Parse(new MasterSettingDAC().Single("BillerMaxAmount", "Biller_Month_MaxAmount").Value)) { throw new CommonException(ReasonCode.BillerOverMonthMaxAmount, Resources.BillerOverMonthMaxAmount); } var totalDayAmount = dac.GetDayAmount(DateTime.UtcNow); if (totalDayAmount + im.FiatAmount > decimal.Parse(new MasterSettingDAC().Single("BillerMaxAmount", "Biller_Day_MaxAmount").Value)) { throw new CommonException(ReasonCode.BillerOverDayMaxAmount, Resources.BillerOverDayMaxAmount); } if (new CountryComponent().GetById(im.CountryId) == null) { return(new BillerPrePayOM() { Status = 5 }); } return(new BillerPrePayOM() { Status = 0 }); }
public IndexOM Index(UserAccount user) { try { var isBillerEnable = bool.Parse(new MasterSettingDAC().Single("BillerMaxAmount", "BillerEnable").Value); var userWallets = new UserWalletDAC().GetUserWallets(user.Id); var currencyId = new CurrenciesDAC().GetByCode(user.FiatCurrency).ID; var exchangeRateList = new PriceInfoDAC().GetByCurrencyId(currencyId); var exchangeRate = exchangeRateList.ToDictionary(item => item.CryptoID); var coins = new CryptocurrencyDAC().GetAllActived().OrderBy(e => e.Sequence).ToList(); var list = coins.Select(a => { var userWallet = userWallets.FirstOrDefault(b => b.CryptoId == a.Id); var rate = exchangeRate.ContainsKey(a.Id) ? exchangeRate[a.Id].Price : 0m; return(GetItem(userWallet, a, rate)); }).Where(a => a.ShowInHomePage) //.OrderBy(a => a.HomePageRank) .Select(a => new CurrencyItem { Id = a.Id, Code = a.Code, NewStatus = a.NewStatus, FrozenBalance = a.FrozenBalance, IconUrl = a.IconUrl, UseableBalance = a.UseableBalance, FiatBalance = a.FiatBalance, FiatExchangeRate = exchangeRate.ContainsKey(a.Id) ? exchangeRate[a.Id].Price.ToString(4) : 0m.ToString(4), CryptoEnable = a.CryptoEnable }).ToList(); return(new IndexOM { TotalAmount = GetUserTotalAmount(user, userWallets, coins, exchangeRateList), CurrencyItemList = list, IsLV1Verified = user.L1VerifyStatus == VerifyStatus.Certified, HasSetPin = !string.IsNullOrEmpty(user.Pin), FiatCurrency = user.FiatCurrency, IsBillerEnable = isBillerEnable }); } catch (Exception exception) { Error(exception.StackTrace); return(new IndexOM { TotalAmount = "--", CurrencyItemList = null, FiatCurrency = user.FiatCurrency }); } }
public ListForDepositOM ListForDepositAndWithdrawal(UserAccount user, string fiatCurrency) { try { var userWallets = new UserWalletDAC().GetUserWallets(user.Id); var coins = new CryptocurrencyDAC().GetAllActived().OrderBy(e => e.Sequence); //var rates = GetExchangeRates(user.CountryId, user.FiatCurrency); var priceList = new PriceInfoDAC().GetPrice(fiatCurrency); var list = coins.Select(a => { var userWallet = userWallets.FirstOrDefault(b => b.CryptoId == a.Id); decimal rate = 0; if (userWallet != null) { rate = priceList.Where(t => t.CryptoID == userWallet.CryptoId).Select(t => t.Price).FirstOrDefault(); } return(GetDepositOMItemTempItem(userWallet, a, rate)); }) //.OrderBy(a => a.PayRank) .Select(a => new ListForDepositOMItem { Id = a.Id, Code = a.Code, NewStatus = a.NewStatus, FrozenBalance = a.FrozenBalance, DecimalPlace = a.DecimalPlace, IconUrl = a.IconUrl, FiatBalance = a.FiatBalance, UseableBalance = a.UseableBalance, Name = a.Name, CryptoEnable = a.CryptoEnable }).ToList(); return(new ListForDepositOM { FiatCurrency = user.FiatCurrency, List = list }); } catch (Exception exception) { Error(exception.Message); } return(null); }
public List <MerchantWalletDTO> GetMerchantWalletList(Guid merchantAccountId) { var account = new MerchantAccountDAC().GetById(merchantAccountId); var cryptoList = new CryptocurrencyDAC().GetAllActived(); cryptoList.MoveTop(t => t.Code == FIIICOIN_CODE); var pos = new POSDAC().GetById(account.POSId.Value); if (!pos.IsWhiteLabel) { cryptoList.RemoveAll(t => t.IsWhiteLabel == 1); } else { cryptoList.MoveTop(t => t.Code == pos.FirstCrypto); } var walletList = new MerchantWalletDAC().GetByAccountId(merchantAccountId); var currencyId = new CurrenciesDAC().GetByCode(account.FiatCurrency).ID; var exchangeRate = new PriceInfoDAC().GetByCurrencyId(currencyId).ToDictionary(item => item.CryptoID); return(cryptoList.Select(e => { var wallet = walletList.FirstOrDefault(w => w.CryptoId == e.Id); return new MerchantWalletDTO { WalletId = wallet?.Id ?? 0, CryptoId = e.Id, CryptoStatus = e.Status, CryptoCode = e.Code, CryptoName = e.Name, IconURL = e.IconURL, DecimalPlace = e.DecimalPlace, Balance = (wallet?.Balance ?? 0).ToString(e.DecimalPlace), FrozenBalance = (wallet?.FrozenBalance ?? 0).ToString(e.DecimalPlace), FiatExchangeRate = (exchangeRate.ContainsKey(e.Id) ? exchangeRate[e.Id].Price : 0m).ToString(4), FiatBalance = (((wallet?.Balance ?? 0) + (wallet?.FrozenBalance ?? 0)) * (exchangeRate.ContainsKey(e.Id) ? exchangeRate[e.Id].Price : 0m)).ToString(4), FiatCode = account.FiatCurrency, CryptoEnable = e.Enable }; }).ToList()); }
private PrePayOM GetUserPrePayOM(Guid userAccountId, MerchantInformation merchantInfo) { var userDAC = new UserAccountDAC(); var merchantDAC = new MerchantInformationDAC(); var merchantUserAccount = userDAC.GetById(merchantInfo.MerchantAccountId); var supportList = new MerchantSupportCryptoDAC().GetList(merchantInfo.Id).ToList(); var userWallets = new UserWalletDAC().GetUserWallets(userAccountId); var coins = new CryptocurrencyDAC().GetAllActived(); var priceList = new PriceInfoDAC().GetPrice(merchantUserAccount.FiatCurrency); var whilteLabelCryptoCode = new POSDAC().GetWhiteLabelCryptoCode(); coins.RemoveAll(t => t.Code == whilteLabelCryptoCode); return(new PrePayOM { FiatCurrency = merchantUserAccount.FiatCurrency, MarkupRate = merchantInfo.Markup.ToString(CultureInfo.InvariantCulture), WaletList = coins.Select(a => { var userWallet = userWallets.FirstOrDefault(b => b.CryptoId == a.Id); decimal rate = 0; rate = priceList.Where(t => t.CryptoID == a.Id).Select(t => t.Price).FirstOrDefault(); return GetUserSupportItem(userWallet, a, supportList, rate); }).OrderByDescending(a => a.MerchantSupported).ThenBy(a => a.PayRank).Select(a => new WalletItem { Code = a.Code, NewStatus = a.NewStatus, ExchangeRate = a.ExchangeRate, FrozenBalance = a.FrozenBalance, IconUrl = a.IconUrl, Id = a.Id, MerchantSupported = a.MerchantSupported, Name = a.Name, UseableBalance = a.UseableBalance, FiatBalance = a.FiatBalance, DecimalPlace = a.DecimalPlace, CryptoEnable = a.CryptoEnable }).ToList() }); }
private static decimal ConvertFiatAmount(decimal fiatAmountUSD, string currency) { if (fiatAmountUSD <= 0) { return(0M); } var price = new PriceInfoDAC().GetPriceByName("USD", "USDT"); if (price == 0) { price = 0.01M; } var usdt = fiatAmountUSD / price; var cprice = new PriceInfoDAC().GetPriceByName(currency, "USDT"); var result = Math.Round(cprice * usdt, 2); return(result <= 0 ? 0.01M : result); }
public IEnumerable <BillerCryptoItemOM> GetBillerCryptoCurrency(UserAccount user, string fiatCurrency) { var dac = new MasterSettingDAC(); var discount = dac.SelectByGroup("BillerMaxAmount").First(item => item.Name.Equals("DiscountRate", StringComparison.CurrentCultureIgnoreCase)); var coins = new UserWalletComponent().ListForDepositAndWithdrawal(user, fiatCurrency).List; var priceInfoDict = new PriceInfoDAC().GetByCurrencyId(new CurrenciesDAC().GetByCode(fiatCurrency).ID).ToDictionary(item => item.CryptoID); foreach (var item in coins) { var model = new BillerCryptoItemOM() { Code = item.Code, Id = item.Id, Name = item.Name, CryptoEnable = item.CryptoEnable, DecimalPlace = item.DecimalPlace, FiatBalance = item.FiatBalance, FrozenBalance = item.FrozenBalance, IconUrl = item.IconUrl, NewStatus = item.NewStatus, UseableBalance = item.UseableBalance, ExchangeRate = priceInfoDict.ContainsKey(item.Id) ? priceInfoDict[item.Id].Price.ToString(4) : 0.ToString() }; if (item.Code.Equals("fiii", StringComparison.InvariantCultureIgnoreCase)) { model.Discount = decimal.Parse(discount.Value).ToString(); yield return(model); } else { model.Discount = "0"; yield return(model); } } }
public MerchantWalletDTO GetWalletInfoById(Guid accountId, int cryptoId) { var crypto = new CryptocurrencyDAC().GetById(cryptoId); var wallet = new MerchantWalletDAC().GetByAccountId(accountId, cryptoId); var merchant = new MerchantAccountDAC().GetById(accountId); var currencyId = new CurrenciesDAC().GetByCode(merchant.FiatCurrency).ID; var exchangeRate = new PriceInfoDAC().GetByCurrencyId(currencyId).ToDictionary(item => item.CryptoID); return(new MerchantWalletDTO { WalletId = wallet?.Id ?? 0, CryptoId = crypto.Id, CryptoCode = crypto.Code, CryptoName = crypto.Name, IconURL = crypto.IconURL, DecimalPlace = crypto.DecimalPlace, Balance = (wallet?.Balance ?? 0).ToString(crypto.DecimalPlace), FrozenBalance = (wallet?.FrozenBalance ?? 0).ToString(crypto.DecimalPlace), FiatExchangeRate = (exchangeRate.ContainsKey(crypto.Id) ? exchangeRate[crypto.Id].Price : 0m).ToString(4), FiatBalance = (((wallet?.Balance ?? 0) + (wallet?.FrozenBalance ?? 0)) * (exchangeRate.ContainsKey(crypto.Id) ? exchangeRate[crypto.Id].Price : 0m)).ToString(4), FiatCode = merchant.FiatCurrency }); }
public async Task <ScanMerchantQRCodeOM> ScanMerchantQRCode(UserAccount user, string code) { var codeEntity = QRCode.Deserialize(code); if (codeEntity.SystemPlatform != SystemPlatform.FiiiPOS || codeEntity.QrCodeEnum != QRCodeEnum.MerchantScanPay) { throw new CommonException(ReasonCode.INVALID_QRCODE, MessageResources.InvalidQRCode); } Guid merchantId = Guid.Parse(codeEntity.QRCodeKey); ScanMerchantQRCodeOM om = new ScanMerchantQRCodeOM(); var merchantAccount = GetMerchantAccountByIdOrCode(merchantId, null); //if (merchantAccount.POSId == null) //{ // throw new CommonException(ReasonCode.INVALID_QRCODE, MessageResources.InvalidQRCode); //} if (merchantAccount.Status == AccountStatus.Locked || !merchantAccount.IsAllowAcceptPayment) { throw new CommonException(ReasonCode.MERCHANT_ACCOUNT_DISABLED, MessageResources.MerchantAccountDisabled); } Guid.TryParse(merchantAccount.Photo, out Guid merchantAvatar); var uwDAC = new UserWalletDAC(); var mwDAC = new MerchantWalletDAC(); var userWallets = uwDAC.GetUserWallets(user.Id); var merchantWallets = mwDAC.GetByAccountId(merchantAccount.Id); var coins = new CryptocurrencyDAC().GetAllActived(); var priceList = new PriceInfoDAC().GetPrice(merchantAccount.FiatCurrency); //判断Pos机是否白标用户 bool showWhiteLable = false; if (merchantAccount.POSId.HasValue) { var pos = new POSDAC().GetById(merchantAccount.POSId.Value); if (pos.IsWhiteLabel) { showWhiteLable = true; } } if (!showWhiteLable) { var whilteLabelCryptoCode = new POSDAC().GetWhiteLabelCryptoCode(); coins.RemoveAll(t => t.Code == whilteLabelCryptoCode); } return(await Task.FromResult(new ScanMerchantQRCodeOM { MerchantId = merchantAccount.Id, MerchantName = merchantAccount.MerchantName, Avatar = merchantAvatar, L1VerifyStatus = (byte)merchantAccount.L1VerifyStatus, L2VerifyStatus = (byte)merchantAccount.L2VerifyStatus, FiatCurrency = merchantAccount.FiatCurrency, MarkupRate = merchantAccount.Markup.ToString(CultureInfo.InvariantCulture), WaletInfoList = coins.Select(a => { var userWallet = userWallets.FirstOrDefault(b => b.CryptoId == a.Id); decimal rate = 0; rate = priceList.Where(t => t.CryptoID == a.Id).Select(t => t.Price).FirstOrDefault(); return GetItem(userWallet, a, merchantWallets, rate); }).OrderByDescending(a => a.MerchantSupported).ThenBy(a => a.PayRank).Select(a => new WalletItem { Code = a.Code, NewStatus = a.NewStatus, ExchangeRate = a.ExchangeRate, FrozenBalance = a.FrozenBalance, IconUrl = a.IconUrl, Id = a.Id, MerchantSupported = a.MerchantSupported, Name = a.Name, UseableBalance = a.UseableBalance, FiatBalance = a.FiatBalance, DecimalPlace = a.DecimalPlace, CryptoEnable = a.CryptoEnable }).ToList() })); }
private static decimal GetMarketPrice(string currency, string cryptoName) { var dac = new PriceInfoDAC(); return(dac.GetPriceByName(currency, cryptoName)); }
public RedPocket Push(UserAccount userAccount, Guid accountId, int cryptoId, decimal amount, int count, string userPIN, string pin, string message = "") { if (userAccount.L1VerifyStatus != Entities.Enums.VerifyStatus.Certified) { throw new CommonException(ReasonCode.NOT_VERIFY_LV1, Resources.EMNeedLV1Verfied); } if (amount <= 0 || cryptoId <= 0) { throw new CommonException(Argument_Error, MessageResources.InvalidDataFormat); } if (count <= 0) { throw new CommonException(Argument_Error, MessageResources.InvalidDataFormat); } if (count > MaxCount) { throw new CommonException(Argument_Error, string.Format(MessageResources.RedPocket_MaxCount, MaxCount)); } if (!string.IsNullOrWhiteSpace(message) && message.Length > 46) { throw new CommonException(Argument_Error, MessageResources.RedPocket_MaxMessage); } SecurityVerify.Verify(new PinVerifier(), SystemPlatform.FiiiPay, accountId.ToString(), userPIN, pin); var cryptoDAC = new CryptocurrencyDAC(); var priceDAC = new PriceInfoDAC(); var crypto = cryptoDAC.GetById(cryptoId); if (crypto == null) { throw new SystemErrorException(); } if (!crypto.Status.HasFlag(Foundation.Entities.Enum.CryptoStatus.RedPocket) || crypto.Enable == 0) { throw new CommonException(ReasonCode.CURRENCY_FORBIDDEN, MessageResources.CurrencyForbidden); } var min = crypto.DecimalPlace == 8 ? 0.00000001M : crypto.DecimalPlace == 6 ? 0.000001M : 0.00000001M; var minAmount = count * min; if (amount < minAmount) { throw new CommonException(Push_MinAmount, string.Format(MessageResources.RedPocket_MinAmount, minAmount, crypto.Code)); } var price = priceDAC.GetPriceByName("USD", crypto.Code); var max = crypto.DecimalPlace == 8 ? 999999999.999999999M : crypto.DecimalPlace == 6 ? 999999999.999999M : 999999999.999999999M; var maxAmount = Math.Round(MaxAmount / price, crypto.DecimalPlace); if (count > 1) { maxAmount = Math.Round(MaxAmount * count / price, crypto.DecimalPlace); } if (amount >= max || Math.Round(amount, crypto.DecimalPlace) > maxAmount) { throw new CommonException(Argument_Error, string.Format(MessageResources.RedPocket_MaxAmount, maxAmount, crypto.Code)); } var userWalletDAC = new UserWalletDAC(); var redPocketDAC = new RedPocketDAC(); var userWalletStatementDAC = new UserWalletStatementDAC(); var userTransactionDAC = new UserTransactionDAC(); var wallet = userWalletDAC.GetByAccountId(accountId, cryptoId); if (wallet == null || wallet.Balance < amount) { throw new CommonException(ReasonCode.INSUFFICIENT_BALANCE, MessageResources.InsufficientBalance); } var fiatAmount = price * amount; using (var scope = new TransactionScope()) { try { var redPocket = new RedPocket { AccountId = accountId, Status = RedPocketStatus.Actived, PassCode = GeneralPassCode(), CryptoCode = crypto.Code, Amount = amount, Balance = amount, Count = count, RemainCount = count, Message = message, Timestamp = DateTime.UtcNow, ExpirationDate = DateTime.UtcNow.AddMinutes(_expried), CryptoId = cryptoId, OrderNo = IdentityHelper.OrderNo(), FiatAmount = fiatAmount <= 0 ? 0M : Math.Round(fiatAmount, 8) }; var redPocketId = redPocketDAC.Insert(redPocket); userTransactionDAC.Insert(new UserTransaction { Id = Guid.NewGuid(), AccountId = accountId, CryptoId = redPocket.CryptoId, CryptoCode = redPocket.CryptoCode, Type = UserTransactionType.PushRedPocket, DetailId = redPocketId.ToString(), Status = (byte)redPocket.Status, Timestamp = redPocket.Timestamp, Amount = redPocket.Amount, OrderNo = redPocket.OrderNo }); userWalletDAC.Decrease(wallet.Id, amount); userWalletStatementDAC.Insert(new UserWalletStatement { WalletId = wallet.Id, Balance = wallet.Balance - amount, Amount = 0 - amount, FrozenAmount = 0, FrozenBalance = wallet.FrozenBalance, Action = "Push Red Pocket", Timestamp = DateTime.UtcNow }); QueueHelper.DelaySender.Send("FiiiPay_RedPocket", redPocketId); scope.Complete(); return(redPocket); } catch (Exception ex) { _log.Error(ex); throw new SystemErrorException(); } } }
public RedPocketDetailOM Receive(UserAccount userAccount, string passcode, bool isZH = false) { if (userAccount == null) { throw new SystemErrorException(); } if (string.IsNullOrWhiteSpace(passcode)) { throw new CommonException(Argument_Error, MessageResources.InvalidDataFormat); } if (userAccount.L1VerifyStatus != Entities.Enums.VerifyStatus.Certified) { throw new CommonException(ReasonCode.NOT_VERIFY_LV1, Resources.EMNeedLV1Verfied); } var count = RedisHelper.StringGet(LockDbIndex, string.Format(KeyFormat, userAccount.Id)); if (!string.IsNullOrWhiteSpace(count) && int.Parse(count) >= 10) { var ttl = RedisHelper.KeyTimeToLive(LockDbIndex, string.Format(KeyFormat, userAccount.Id)); if (ttl != null) { var message = ""; var t = ""; try { t = TimeConvert(ttl.Value, isZH); message = string.Format(MessageResources.RedPocket_PassCodeErrorMaxCount, t); } catch (Exception exception) { _log.Error(exception.Message + " " + MessageResources.RedPocket_PassCodeErrorMaxCount + " " + t); } throw new CommonException(MaxError, message); } } var redPocketDAC = new RedPocketDAC(); var redPocket = redPocketDAC.GetByPassCode(passcode); if (redPocket == null) { var errorCount = ErrorCount(userAccount.Id, count, isZH); throw new CommonException(PassCodeError, string.Format(MessageResources.RedPocket_PassCodeError, errorCount, 10 - errorCount)); } if (redPocket.ExpirationDate < DateTime.UtcNow) { var errorCount = ErrorCount(userAccount.Id, count, isZH); throw new CommonException(PassCodeExpired, MessageResources.RedPocket_ReceiveExpired + Environment.NewLine + string.Format(MessageResources.RedPocket_PassCodeError, errorCount, 10 - errorCount)); } var crypto = new CryptocurrencyDAC().GetByCode(redPocket.CryptoCode); if (!crypto.Status.HasFlag(Foundation.Entities.Enum.CryptoStatus.RedPocket) || crypto.Enable == 0) { throw new CommonException(ReasonCode.CURRENCY_FORBIDDEN, MessageResources.CurrencyForbidden); } var om = new RedPocketDetailOM(); var redPocketReceiveDAC = new RedPocketReceiverDAC(); var hasReceive = redPocketReceiveDAC.HasReceive(userAccount.Id, redPocket.Id); if (redPocket.Status == RedPocketStatus.Actived && hasReceive == null) { var userWalletDAC = new UserWalletDAC(); var userWalletStatementDAC = new UserWalletStatementDAC(); var uwComponent = new UserWalletComponent(); var userTransactionDAC = new UserTransactionDAC(); var wallet = userWalletDAC.GetByCryptoCode(userAccount.Id, redPocket.CryptoCode); if (wallet == null) { wallet = uwComponent.GenerateWallet(userAccount.Id, redPocket.CryptoCode); } var min = crypto.DecimalPlace == 8 ? 0.00000001M : crypto.DecimalPlace == 6 ? 0.000001M : 0.00000001M; var n = crypto.DecimalPlace == 8 ? 100000000 : crypto.DecimalPlace == 6 ? 1000000 : 100000000; var amount = GetRandomMoney(redPocket, min, n); var priceDAC = new PriceInfoDAC(); var price = priceDAC.GetPriceByName("USD", crypto.Code); var fiatAmount = price * amount; using (var scope = new TransactionScope()) { try { var redPocketReceiver = new RedPocketReceiver { PocketId = redPocket.Id, AccountId = userAccount.Id, SendAccountId = redPocket.AccountId, CryptoCode = redPocket.CryptoCode, Amount = amount, Timestamp = DateTime.UtcNow, IsBestLuck = redPocket.Count == 1, OrderNo = IdentityHelper.OrderNo(), FiatAmount = fiatAmount <= 0 ? 0M : Math.Round(fiatAmount, 8) }; var id = redPocketReceiveDAC.Insert(redPocketReceiver); userTransactionDAC.Insert(new UserTransaction { Id = Guid.NewGuid(), AccountId = redPocketReceiver.AccountId, CryptoId = redPocket.CryptoId, CryptoCode = redPocket.CryptoCode, Type = UserTransactionType.ReceiveRedPocket, DetailId = id.ToString(), Status = (byte)redPocket.Status, Timestamp = redPocketReceiver.Timestamp, Amount = redPocketReceiver.Amount, OrderNo = redPocketReceiver.OrderNo }); userWalletDAC.Increase(wallet.Id, amount); userWalletStatementDAC.Insert(new UserWalletStatement { WalletId = wallet.Id, Balance = wallet.Balance + amount, Amount = amount, FrozenAmount = 0, FrozenBalance = wallet.FrozenBalance, Action = "Receive Red Pocket", Timestamp = DateTime.UtcNow }); redPocketDAC.UpdateRemain(redPocket.Id, amount); if (redPocket.RemainCount == 0) { redPocketDAC.UpdateStatus(redPocket.Id, RedPocketStatus.Complate); userTransactionDAC.UpdateStatus(UserTransactionType.PushRedPocket, redPocket.Id.ToString(), redPocket.AccountId, (byte)RedPocketStatus.Complate); if (redPocket.Count > 1) { redPocketReceiveDAC.UpdateBestLuck(redPocket.Id); } } scope.Complete(); } catch (Exception ex) { _log.Error(ex); throw new CommonException(); } } om.SelfAmount = amount.ToString(); om.ReceiveStatus = ReceiveStatusEnum.Receive; om.HasExpried = false; redPocket.Balance -= amount; } else { if (hasReceive != null) { om.ReceiveStatus = ReceiveStatusEnum.HasReceive; om.SelfAmount = hasReceive.Amount.ToString(); } else { om.ReceiveStatus = ReceiveStatusEnum.None; om.SelfAmount = string.Empty; } } var account = new UserAccountComponent().GetById(redPocket.AccountId); om.Message = redPocket.Message; om.SnederNickname = account.Nickname; om.TotalAmount = redPocket.Amount.ToString(); om.TotalCount = redPocket.Count; om.CryptoCode = redPocket.CryptoCode; om.ReceiveAmount = (redPocket.Amount - redPocket.Balance).ToString(); om.ReceiveCount = redPocket.Count - redPocket.RemainCount; om.Id = redPocket.Id; om.HasExpried = redPocket.ExpirationDate < DateTime.UtcNow; om.HasSelfSned = redPocket.AccountId == userAccount.Id; return(om); }
public List <MerchantSupportReceiptWalletDTO> SupportReceiptList(Guid accountId) { var account = new MerchantAccountDAC().GetById(accountId); var dac = new MerchantWalletDAC(); var cryptoList = new CryptocurrencyDAC().GetAllActived(); var fiiiCrypto = cryptoList.First(w => w.Code == FIIICOIN_CODE); var fiiiWallet = dac.GetByAccountId(account.Id, fiiiCrypto.Id) ?? GenerateWallet(accountId, fiiiCrypto.Id, fiiiCrypto.Code, FIIICOIN_SEQUENCE, true); if (!fiiiWallet.SupportReceipt || fiiiWallet.Sequence != FIIICOIN_SEQUENCE) { dac.Support(account.Id, fiiiWallet.CryptoId, FIIICOIN_SEQUENCE); } List <MerchantWallet> supportReceiptList; var pos = new POSDAC().GetById(account.POSId.Value); if (pos.IsWhiteLabel) { var whiteCrypto = cryptoList.First(w => w.Code == pos.FirstCrypto); var whiteWallet = dac.GetByAccountId(account.Id, whiteCrypto.Id); if (whiteWallet == null) { GenerateWallet(accountId, whiteCrypto.Id, whiteCrypto.Code, WHITECOIN_SEQUENCE, true); } else if (!whiteWallet.SupportReceipt || whiteWallet.Sequence != WHITECOIN_SEQUENCE) { dac.Support(account.Id, whiteWallet.CryptoId, WHITECOIN_SEQUENCE); } supportReceiptList = dac.SupportReceiptList(accountId).OrderBy(e => e.Sequence).ToList(); } else { var whiteCryptoList = cryptoList.Where(e => e.IsWhiteLabel == 1).ToList(); supportReceiptList = dac.SupportReceiptList(accountId).OrderBy(e => e.Sequence).ToList(); supportReceiptList.RemoveAll(e => whiteCryptoList.Exists(a => a.Id == e.CryptoId)); } var marketPriceList = new PriceInfoDAC().GetPrice(account.FiatCurrency); return(supportReceiptList.Select(e => { var crypto = cryptoList.FirstOrDefault(w => w.Id == e.CryptoId); if (crypto == null) { return null; } return new MerchantSupportReceiptWalletDTO { WalletId = e.Id, CryptoId = crypto.Id, CryptoStatus = crypto.Status, CryptoCode = crypto.Code, CryptoName = crypto.Name, IconURL = crypto.IconURL, DecimalPlace = crypto.DecimalPlace, Markup = account.Markup, MarketPrice = marketPriceList.FirstOrDefault(m => crypto.Code == m.CryptoName)?.Price.ToString(4), Balance = string.Equals("FIII", crypto.Code, StringComparison.CurrentCultureIgnoreCase) ? e.Balance : 0, IsDefault = e.CryptoId == fiiiCrypto.Id || (pos.IsWhiteLabel && crypto.Code == pos.FirstCrypto) ? 1 : 0, CryptoEnable = crypto.Enable }; }).Where(w => w != null).ToList()); }
public async Task <PayOrderOM> PayAsync(UserAccount user, StoreOrderPayIM im) { #region 验证 if (im.FiatAmount <= 0) { throw new ApplicationException(); } SecurityVerify.Verify(new PinVerifier(), SystemPlatform.FiiiPay, user.Id.ToString(), user.Pin, im.Pin); if (!user.IsAllowExpense.HasValue || !user.IsAllowExpense.Value) { throw new CommonException(ReasonCode.Not_Allow_Expense, MessageResources.PaymentForbidden); } var coin = new CryptocurrencyDAC().GetById(im.CoinId); if (!coin.Status.HasFlag(CryptoStatus.Pay) || coin.Enable == 0) { throw new CommonException(ReasonCode.CURRENCY_FORBIDDEN, MessageResources.CurrencyForbidden); } var merchantInfo = new MerchantInformationDAC().GetById(im.MerchantInfoId); if (merchantInfo.Status != Status.Enabled || merchantInfo.VerifyStatus != VerifyStatus.Certified || merchantInfo.IsPublic != Status.Enabled) { throw new CommonException(ReasonCode.Not_Allow_AcceptPayment, MessageResources.MerchantExceptionTransClose); } if (!merchantInfo.IsAllowExpense) { throw new CommonException(ReasonCode.Not_Allow_AcceptPayment, MessageResources.MerchantReceiveNotAllowed); } if (merchantInfo.AccountType == AccountType.Merchant) { throw new ApplicationException(); } var storeAccount = new UserAccountDAC().GetById(merchantInfo.MerchantAccountId); if (storeAccount.Id == user.Id) { throw new CommonException(ReasonCode.Not_Allow_AcceptPayment, MessageResources.PaytoSelf); } if (storeAccount == null || storeAccount.Status.Value != (byte)AccountStatus.Active) { throw new CommonException(ReasonCode.Not_Allow_AcceptPayment, MessageResources.MerchantFiiipayAbnormal); } var paySetting = await new StorePaySettingDAC().GetByCountryIdAsync(merchantInfo.CountryId); if (paySetting != null) { if (im.FiatAmount > paySetting.LimitAmount) { throw new CommonException(ReasonCode.TRANSFER_AMOUNT_OVERFLOW, string.Format(MessageResources.TransferAmountOverflow, paySetting.LimitAmount, paySetting.FiatCurrency)); } } #endregion var walletDAC = new UserWalletDAC(); var statementDAC = new UserWalletStatementDAC(); var storeOrderDAC = new StoreOrderDAC(); var utDAC = new UserTransactionDAC(); #region 计算 decimal markup = merchantInfo.Markup; decimal feeRate = merchantInfo.FeeRate; var exchangeRate = new PriceInfoDAC().GetPriceByName(storeAccount.FiatCurrency, coin.Code); decimal failTotalAmount = im.FiatAmount + (im.FiatAmount * markup).ToSpecificDecimal(4); decimal transactionFiatFee = (im.FiatAmount * feeRate).ToSpecificDecimal(4); decimal transactionFee = (transactionFiatFee / exchangeRate).ToSpecificDecimal(coin.DecimalPlace); decimal cryptoAmount = (failTotalAmount / exchangeRate).ToSpecificDecimal(coin.DecimalPlace); var fromWallet = walletDAC.GetUserWallet(user.Id, im.CoinId); if (fromWallet == null || fromWallet.Balance < cryptoAmount) { throw new CommonException(ReasonCode.INSUFFICIENT_BALANCE, MessageResources.InsufficientBalance); } var toWallet = walletDAC.GetUserWallet(storeAccount.Id, im.CoinId); if (toWallet == null) { toWallet = new UserWalletComponent().GenerateWallet(storeAccount.Id, im.CoinId); } #endregion #region entity DateTime dtNow = DateTime.UtcNow; StoreOrder order = new StoreOrder { Id = Guid.NewGuid(), OrderNo = IdentityHelper.OrderNo(), Timestamp = dtNow, Status = OrderStatus.Completed, MerchantInfoId = merchantInfo.Id, MerchantInfoName = merchantInfo.MerchantName, UserAccountId = user.Id, CryptoId = im.CoinId, CryptoCode = coin.Code, CryptoAmount = cryptoAmount, CryptoActualAmount = cryptoAmount - transactionFee, ExchangeRate = exchangeRate, Markup = markup, FiatCurrency = storeAccount.FiatCurrency, FiatAmount = im.FiatAmount, FiatActualAmount = failTotalAmount, FeeRate = feeRate, TransactionFee = transactionFee, PaymentTime = dtNow }; UserWalletStatement fromStatement = new UserWalletStatement { WalletId = fromWallet.Id, Action = UserWalletStatementAction.StoreOrderOut, Amount = -order.CryptoAmount, Balance = fromWallet.Balance - order.CryptoAmount, FrozenAmount = 0, FrozenBalance = fromWallet.FrozenBalance, Timestamp = dtNow }; UserWalletStatement toStatement = new UserWalletStatement { WalletId = toWallet.Id, Action = UserWalletStatementAction.StoreOrderIn, Amount = order.CryptoActualAmount, Balance = toWallet.Balance + order.CryptoActualAmount, FrozenAmount = 0, FrozenBalance = toWallet.FrozenBalance, Timestamp = dtNow }; #endregion using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await storeOrderDAC.CreateAsync(order); await utDAC.InsertAsync(new UserTransaction { Id = Guid.NewGuid(), AccountId = fromWallet.UserAccountId, CryptoId = order.CryptoId, CryptoCode = order.CryptoCode, Type = UserTransactionType.StoreOrderConsume, DetailId = order.Id.ToString(), Status = (byte)order.Status, Timestamp = order.PaymentTime.Value, Amount = order.CryptoAmount, OrderNo = order.OrderNo, MerchantName = order.MerchantInfoName }); await utDAC.InsertAsync(new UserTransaction { Id = Guid.NewGuid(), AccountId = toWallet.UserAccountId, CryptoId = order.CryptoId, CryptoCode = order.CryptoCode, Type = UserTransactionType.StoreOrderIncome, DetailId = order.Id.ToString(), Status = (byte)order.Status, Timestamp = order.Timestamp, Amount = order.CryptoActualAmount, OrderNo = order.OrderNo, MerchantName = order.MerchantInfoName }); walletDAC.Decrease(fromWallet.Id, order.CryptoAmount); walletDAC.Increase(toWallet.Id, order.CryptoActualAmount); statementDAC.Insert(fromStatement); statementDAC.Insert(toStatement); scope.Complete(); } var pushObj = new { order.Id, order.UserAccountId, order.CryptoCode }; RabbitMQSender.SendMessage("StoreOrderPayed", new { order.Id, MerchantInfoId = merchantInfo.MerchantAccountId, order.UserAccountId, order.CryptoCode }); return(new PayOrderOM { Amount = order.CryptoAmount.ToString(coin.DecimalPlace), Currency = coin.Code, OrderId = order.Id.ToString(), OrderNo = order.OrderNo, Timestamp = dtNow.ToUnixTime().ToString() }); }