//Payment public void SendEmailReceiptForBuyer(int routeId) { try { var apiKey = SETTING.Value.SendGridKey; var client = new SendGridClient(apiKey); string emailTemplateHtml = _hostingEnvironment.ContentRootPath + "\\EmailTemplate\\EmailTemplate.html"; string ticketTemplatehtml = _hostingEnvironment.ContentRootPath + "\\EmailTemplate\\TicketsTemplate.html"; string body = string.Empty; string ticketList = string.Empty; var route = _routeRepository.Get(r => r.Id == routeId); var customer = _customerRepository.Get(c => c.Id == route.CustomerId); //using streamreader for reading my htmltemplate using (StreamReader reader = new StreamReader(emailTemplateHtml)) { body = reader.ReadToEnd(); } var customerEmail = _customerRepository.Get(c => c.Id == route.CustomerId).Email; var customerName = customer.FullName; var customerPhone = customer.PhoneNumber; var routeCode = route.Code; var totalAmount = route.TotalAmount; var date = DateTime.UtcNow; var routeTickets = _routeTicketRepository.GetAllQueryable().Where(r => r.RouteId == routeId); List <TicketRowViewModel> tickets = new List <TicketRowViewModel>(); foreach (var routeTicket in routeTickets) { var ticket = _ticketRepository.Get(t => t.Id == routeTicket.TicketId); var ticketRowViewModel = _mapper.Map <Ticket, TicketRowViewModel>(ticket); tickets.Add(ticketRowViewModel); } //replacing the required things body = body.Replace("{routeCode}", routeCode); body = body.Replace("{customerName}", customerName); body = body.Replace("{customerEmail}", customerEmail); body = body.Replace("{customerPhone}", customerPhone); body = body.Replace("{Date}", date.ToString()); body = body.Replace("{subTotal}", totalAmount.ToString()); body = body.Replace("{Total}", totalAmount.ToString()); body = body.Replace("{Term}", SETTING.Value.Term); body = body.Replace("{Title}", SETTING.Value.Title); body = body.Replace("{fromName}", SETTING.Value.FromName); body = body.Replace("{fromEmail}", SETTING.Value.FromEmail); body = body.Replace("{Street}", SETTING.Value.Street); body = body.Replace("{City}", SETTING.Value.City); body = body.Replace("{addressNumber}", SETTING.Value.AddressNumber); body = body.Replace("{phoneNumber}", SETTING.Value.PhoneNumber); body = body.Replace("{bussinessNumber}", SETTING.Value.BussinessNumber); foreach (var ticket in tickets) { using (StreamReader reader = new StreamReader(ticketTemplatehtml)) { var ticketRow = reader.ReadToEnd(); var departureStation = _stationRepository.Get(s => s.Id == ticket.DepartureStationId).Name; var arrivalStation = _stationRepository.Get(s => s.Id == ticket.ArrivalStationId).Name; ticketRow = ticketRow.Replace("{ticketCode}", ticket.TicketCode); ticketRow = ticketRow.Replace("{Description}", ticket.Description); ticketRow = ticketRow.Replace("{departureCity}", ticket.DepartureCity); ticketRow = ticketRow.Replace("{departureStation}", departureStation); ticketRow = ticketRow.Replace("{departureTime}", ticket.DepartureDateTime.ToString()); ticketRow = ticketRow.Replace("{arrivalCity}", ticket.ArrivalCity); ticketRow = ticketRow.Replace("{arrivalStation}", arrivalStation); ticketRow = ticketRow.Replace("{arrivalTime}", ticket.ArrivalDateTime.ToString()); ticketRow = ticketRow.Replace("{Amount}", ticket.SellingPrice.ToString()); ticketList += ticketRow; } } body = body.Replace("{ticketList}", ticketList); var from = new EmailAddress(SETTING.Value.FromEmail, SETTING.Value.FromName); var subject = routeCode + " - Receipt - Payment"; var to = new EmailAddress(customerEmail, customerName); var plainTextContent = ""; var htmlContent = body; var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent); client.SendEmailAsync(msg); } catch (Exception) { } }
public string MakePayoutToCustomer(int TicketId, string username) { string staffId = _userRepository.Get(x => x.UserName == username).Id; //lấy all routeTicke ứng vs cái Ticket ///// CÁCH 1 var route = (from ROUTE in _routeRepository.GetAllQueryable() join RT in _routeTicketRepository.GetAllQueryable() on ROUTE.Id equals RT.RouteId where ROUTE.Deleted == false && RT.Deleted == false && ROUTE.Status == RouteStatus.Bought && RT.TicketId == TicketId select ROUTE) .FirstOrDefault(); if (route == null) { return("Not found Route"); } var ticket = _ticketRepository.Get(x => x.Id == TicketId && x.Deleted == false); if (ticket.Status == TicketStatus.Completed) { throw new InvalidOperationException(); } _unitOfWork.StartTransaction(); ticket.Status = TicketStatus.Completed; _ticketRepository.Update(ticket); _unitOfWork.CommitChanges(); //make payout var paymentDetail = _paymentRepository.Get(x => x.RouteId == route.Id && x.Deleted == false); StripeConfiguration.SetApiKey(SETTING.Value.SecretStripe); var amount = ticket.SellingPrice * (100 - ticket.CommissionPercent) / 100; //số tiền chuyển đi var options = new TransferCreateOptions { Amount = Convert.ToInt64(amount * 100), Currency = "usd", Destination = ticket.Seller.StripeConnectAccountId, SourceTransaction = paymentDetail.StripeChargeId, Description = "Transfer for Ticket Code: " + ticket.TicketCode }; var service = new TransferService(); Transfer Transfer = service.Create(options); Core.Models.Payout payoutCreateIntoDatabase = new Core.Models.Payout(); payoutCreateIntoDatabase.StripePayoutId = Transfer.Id; payoutCreateIntoDatabase.TicketId = TicketId; payoutCreateIntoDatabase.PaymentId = paymentDetail.Id; payoutCreateIntoDatabase.Amount = amount; payoutCreateIntoDatabase.FeeAmount = ticket.SellingPrice * (ticket.CommissionPercent / 100); payoutCreateIntoDatabase.Description = "You receive money for ticket " + ticket.TicketCode + ". Thank you for using our service."; payoutCreateIntoDatabase.Status = PayoutStatus.Success; _payoutRepository.Add(payoutCreateIntoDatabase); //make payout //save log //_unitOfWork.StartTransaction(); ResolveOptionLog log = new ResolveOptionLog() { Option = ResolveOption.PAYOUT, RouteId = route.Id, TicketId = TicketId, StaffId = staffId, Amount = amount }; _resolveOptionLogRepository.Add(log); _unitOfWork.CommitChanges(); if (route.ResolveOptionLogs.Count() == route.RouteTickets.Where(x => x.Deleted == false).Count()) { route.Status = RouteStatus.Completed; _routeRepository.Update(route); } _unitOfWork.CommitTransaction(); //save log //push noti to buyer var message = "Ticket " + ticket.TicketCode + " has been payout. $" + (amount).ToString("N2") + " has been tranfered to your Stripe account."; var sellerDevices = ticket.Seller.CustomerDevices.Where(x => x.IsLogout == false); List <string> sellerDeviceIds = new List <string>(); foreach (var sellerDev in sellerDevices) { sellerDeviceIds.Add(sellerDev.DeviceId); } //push noti _oneSignalService.PushNotificationCustomer(message, sellerDeviceIds); //Save Notification _notificationService.SaveNotification( customerId: ticket.SellerId, type: NotificationType.TicketIsPayouted, message: $"Ticket {ticket.TicketCode} has been payout. ${(amount).ToString("N2")} has been transfered to your Stripe account.", data: new { ticketId = ticket.Id, }); //send Email _sendGridService.SendEmailReceiptForSeller(TicketId, amount); return(""); }