public Guid PayInByPartner(Transfer transfer) { var res = Guid.Empty; var linkBank = _LinkingBankCollection.GetById(transfer.SourceLinkingBankId); if (linkBank != null) { // get chi tiết người nhận using (var sessionTask = _MongoDBClient.StartSessionAsync()) { var session = sessionTask.Result; session.StartTransaction(); try { var userDetail = _UserCollection.GetByAccountNumber(transfer.DestinationAccountNumber); if (userDetail != null) { transfer.Id = Guid.Empty; transfer.Fee = _Context.TransactionCost(transfer.Money); transfer.IsConfirmed = true; transfer.DestinationLinkingBankId = Guid.Empty; transfer.SignedData = transfer.SignedData; // Update số dư userDetail.CheckingAccount.AccountBalance += transfer.Money; if (!transfer.IsSenderPay) { userDetail.CheckingAccount.AccountBalance -= transfer.Fee; } // Create chuyển tiền _TransferCollection.Create(transfer); if (transfer.Id != Guid.Empty) { // Update người nhận var updateUser = _UserCollection.UpdateCheckingAccount(new UserFilter() { Id = userDetail.Id }, userDetail.CheckingAccount); if (updateUser > 0) { // Create giao dịch var transaction = new Transaction(); transaction.Id = Guid.Empty; transaction.CreateTime = DateTime.Now; transaction.ExpireTime = transaction.CreateTime.AddMinutes(_Setting.TransferExpiration); transaction.ConfirmTime = transaction.CreateTime; transaction.ReferenceId = transfer.Id; transaction.Type = 0; _TransactionCollection.Create(transaction); if (transaction.Id != Guid.Empty) { res = transaction.Id; } else { _Setting.Message.SetMessage("Không thể lưu thông tin giao dịch!"); } } else { _Setting.Message.SetMessage("Không thể lưu thông tin người nhận!"); } } else { _Setting.Message.SetMessage("Không thể lưu thông tin chuyển tiền!"); } } else { _Setting.Message.SetMessage("Không tìm thấy thông tin người nhận!"); } if (res != Guid.Empty) { session.CommitTransactionAsync(); } else { session.AbortTransactionAsync(); } } catch (Exception ex) { session.AbortTransactionAsync(); throw ex; } } } return(res); }
public bool PayIn(Guid userId, PayInfo payInfo) { var res = false; var details = _UserCollection.Get(new UserFilter() { AccountNumber = payInfo.AccountNumber, Username = payInfo.Username }); var detailsEmployee = _UserCollection.GetById(userId); if (details.Any() && detailsEmployee != null) { var detail = details.FirstOrDefault(); detail.CheckingAccount.AccountBalance += payInfo.Money; // luu giao dich var transfer = new Transfer(); transfer.Description = "Nạp tiền"; transfer.DestinationAccountNumber = detail.AccountNumber; transfer.DestinationLinkingBankId = Guid.Empty; transfer.Fee = 0; transfer.IsConfirmed = true; transfer.IsSenderPay = true; transfer.Money = payInfo.Money; transfer.SourceAccountNumber = detailsEmployee.AccountNumber; transfer.SourceLinkingBankId = Guid.Empty; transfer.IsPayIn = true; using (var sessionTask = _MongoDBClient.StartSessionAsync()) { var session = sessionTask.Result; session.StartTransaction(); try { _TransferCollection.Create(transfer); if (transfer.Id == Guid.Empty) { throw new Exception(); } var resUp = _UserCollection.UpdateCheckingAccount(new UserFilter() { AccountNumber = payInfo.AccountNumber, Id = detail.Id }, detail.CheckingAccount) > 0; if (!resUp) { throw new Exception(); } var transaction = new Transaction(); transaction.Id = Guid.Empty; transaction.ReferenceId = transfer.Id; transaction.Otp = ""; transaction.CreateTime = DateTime.Now; transaction.ExpireTime = transaction.CreateTime.AddMinutes(_Setting.TransferExpiration); transaction.Type = 0; transaction.ConfirmTime = transaction.CreateTime; _TransactionCollection.Create(transaction); if (transaction.Id == Guid.Empty) { throw new Exception(); } res = true; session.CommitTransactionAsync(); } catch (Exception) { session.AbortTransactionAsync(); } } } else { _Setting.Message.SetMessage("Không tìm thấy thông tin tài khoản!"); } return(res); }