Example #1
0
        public List <TransactionDataTable> GetTransactions(string username, int page, int pageSize)
        {
            var transactionAllRecord =
                (from PM in _paymentRepository.GetAllQueryable()
                 join CDC in _creditCardRepository.GetAllQueryable() on PM.CreditCartId equals CDC.Id
                 join Cus in _customerRepository.GetAllQueryable() on CDC.CustomerId equals Cus.Id
                 where Cus.Username == username
                 select new TransactionDataTable()
            {
                Amount = PM.Amount,
                Description = PM.Description,
                CreatedAtUTC = PM.CreatedAtUTC,
                Type = "Payment"
            }
                ).Union(
                    from RP in _refundRepository.GetAllQueryable()
                    join PM in _paymentRepository.GetAllQueryable() on RP.PaymentId equals PM.Id
                    join CDC in _creditCardRepository.GetAllQueryable() on PM.CreditCartId equals CDC.Id
                    join Cus in _customerRepository.GetAllQueryable() on CDC.CustomerId equals Cus.Id
                    where Cus.Username == username
                    select new TransactionDataTable()
            {
                Amount       = RP.Amount,
                Description  = RP.Description,
                CreatedAtUTC = RP.CreatedAtUTC,
                Type         = "Refund"
            }
                    )
                .Union(
                    from PO in _payoutRepository.GetAllQueryable()
                    //join PM in _paymentRepository.GetAllQueryable() on PO.PaymentId equals PM.Id
                    //join CDC in _creditCardRepository.GetAllQueryable() on PM.CreditCartId equals CDC.Id
                    //join Cus in _customerRepository.GetAllQueryable() on CDC.CustomerId equals Cus.Id
                    where PO.Ticket.Seller.Username == username
                    select new TransactionDataTable()
            {
                Amount       = PO.Amount,
                Description  = PO.Description,
                CreatedAtUTC = PO.CreatedAtUTC,
                Type         = "Payout"
            }
                    ).OrderByDescending(x => x.CreatedAtUTC).Skip((page - 1) * pageSize).Take(pageSize).ToList();


            ////var listRefund =
            ////    (from RP in _refundRepository.GetAllQueryable()
            ////     join PM in _paymentRepository.GetAllQueryable() on RP.PaymentId equals PM.Id
            ////     join CDC in _creditCardRepository.GetAllQueryable() on PM.CreditCartId equals CDC.Id
            ////     join Cus in _customerRepository.GetAllQueryable() on CDC.CustomerId equals Cus.Id
            ////     where Cus.Username == username
            ////     select new TransactionDataTable()
            ////     {
            ////         Amount = RP.Amount,
            ////         Description = RP.Description,
            ////         CreatedAtUTC = RP.CreatedAtUTC
            ////     }
            ////    );
            ////var listPayout =
            ////    (from PO in _payoutRepository.GetAllQueryable()
            ////     join PM in _paymentRepository.GetAllQueryable() on PO.PaymentId equals PM.Id
            ////     join CDC in _creditCardRepository.GetAllQueryable() on PM.CreditCartId equals CDC.Id
            ////     join Cus in _customerRepository.GetAllQueryable() on CDC.CustomerId equals Cus.Id
            ////     where Cus.Username == username
            ////     select new TransactionDataTable()
            ////     {
            ////         Amount = PO.Amount,
            ////         Description = PO.Description,
            ////         CreatedAtUTC = PO.CreatedAtUTC
            ////     }
            ////    );

            //////add 3 list thành 1 list
            ////List<TransactionDataTable> transactionAllRecord = null;
            ////foreach(var item in listPayment)
            ////{
            ////    var paymentItem = _mapper.Map<PaymentTransactionViewModel, TransactionDataTable>(item);
            ////    transactionAllRecord.Add(paymentItem);
            ////}
            ////foreach (var item in listPayout)
            ////{
            ////    var payoutItem = _mapper.Map<PayoutTransactionViewModel, TransactionDataTable>(item);
            ////    transactionAllRecord.Add(payoutItem);
            ////}
            ////foreach (var item in listRefund)
            ////{
            ////    var refundItem = _mapper.Map<RefundTransactionViewModel, TransactionDataTable>(item);
            ////    transactionAllRecord.Add(refundItem);
            ////}

            return(transactionAllRecord);
        }
Example #2
0
        public string RefundToTalMoneyToCustomer(int TicketId, ResolveOption resolveOption, string username)
        {
            string staffId = _userRepository.Get(x => x.UserName == username).Id;
            // lấy routeTicket ứng vs ticketId
            var routeTicket = _routeTicketRepository.Get(x =>
                                                         x.TicketId == TicketId &
                                                         x.Deleted == false &
                                                         x.Route.Status == RouteStatus.Bought
                                                         );

            _unitOfWork.StartTransaction();
            var route = routeTicket.Route;

            if (route.Status == RouteStatus.Completed)
            {
                throw new InvalidOperationException();
            }
            route.Status      = RouteStatus.Completed;
            route.IsRefundAll = true;
            //route.ResolveOption = resolveOption;
            _routeRepository.Update(route);
            _unitOfWork.CommitChanges();

            //lấy Lịch sử chagre tiền
            var     paymentDetail      = _paymentRepository.Get(x => x.RouteId == routeTicket.RouteId && x.Route.Deleted == false);
            var     refundFromPayments = _refundRepository.GetAllQueryable().Where(x => x.PaymentId == paymentDetail.Id);
            decimal totalRefund        = 0;

            foreach (var refundFromPayment in refundFromPayments)
            {
                totalRefund = totalRefund + refundFromPayment.Amount;
            }
            //refund lại tiền
            StripeConfiguration.SetApiKey(SETTING.Value.SecretStripe);

            //cmt để test
            decimal remainRefund  = paymentDetail.Amount - totalRefund;
            var     refundOptions = new RefundCreateOptions()
            {
                ChargeId = paymentDetail.StripeChargeId,
                Amount   = Convert.ToInt64(remainRefund * 100)
            };
            var refundService = new Stripe.RefundService();

            Stripe.Refund         refund       = refundService.Create(refundOptions);
            RefundCreateViewModel refundCreate = new RefundCreateViewModel();

            refundCreate.PaymentId      = paymentDetail.Id;
            refundCreate.StripeRefundId = refund.Id;
            refundCreate.Amount         = remainRefund; //refund all
            refundCreate.Description    = "You have a refund for route " + routeTicket.Route.Code + ". We are sorry for the inconvenience.";

            refundCreate.Status = RefundStatus.Success;
            var refundAddIntoData = _mapper.Map <RefundCreateViewModel, Core.Models.Refund>(refundCreate);

            _refundRepository.Add(refundAddIntoData);
            //refund total amount


            //save log
            var routeTickets = route.RouteTickets.Where(x => x.Deleted == false);

            foreach (var rt in routeTickets)
            {
                var resolveOptionLog = _resolveOptionLogRepository.Get(x => x.RouteId == rt.RouteId && x.TicketId == rt.TicketId);
                if (resolveOptionLog == null || resolveOptionLog.Option == ResolveOption.PAYOUT)
                {
                    ResolveOptionLog newLog = new ResolveOptionLog()
                    {
                        RouteId  = rt.RouteId,
                        TicketId = rt.TicketId,
                        StaffId  = staffId,
                        Option   = ResolveOption.REFUNDTOTALAMOUNT,
                        Amount   = rt.Ticket.SellingPrice
                    };
                    _resolveOptionLogRepository.Add(newLog);
                }
            }
            _unitOfWork.CommitTransaction();
            //save log

            //push noti to seller bought ticket
            foreach (var boughtTicket in routeTicket.Route.RouteTickets)
            {
                var ticket = boughtTicket.Ticket;
                if (ticket.Status == TicketStatus.Bought)
                {
                    ticket.Status = TicketStatus.Valid;
                    //ticket.BuyerId = null;
                    _ticketRepository.Update(ticket);
                    var           message = "Buyer has canceled the transaction, ticket " + ticket.TicketCode + " is valid again.";
                    List <string> sellerTicketDeviceIds = GetCustomerDeviceIds(ticket, true);
                    _oneSignalService.PushNotificationCustomer(message, sellerTicketDeviceIds);

                    //Save notification
                    _notificationService.SaveNotification(
                        customerId: ticket.SellerId,
                        type: NotificationType.TicketIsRevalid,
                        message: $"Your Ticket {ticket.TicketCode} is valid again due to buyer has canceled the transaction.",
                        data: new { ticketId = ticket.Id }
                        );
                }
            }

            //push notification for buyer to notify his payment is refunded
            var messageRoute = "Route " + route.Code + " has been refunded. " +
                               remainRefund.ToString("N2") + "$ will be refunded within next 5 to 7 working days.";
            List <string> buyerDeviceIds = GetCustomerDeviceIds(routeTicket.Ticket, false);

            _oneSignalService.PushNotificationCustomer(messageRoute, buyerDeviceIds);

            //Save notification for buyer
            if (routeTicket.Ticket.BuyerId != null)
            {
                _notificationService.SaveNotification(
                    customerId: routeTicket.Ticket.BuyerId.Value,
                    type: NotificationType.RouteIsRefunded,
                    message: $"Route {route.Code} has been refunded, {remainRefund.ToString("N2")} will be refunded within next 5 to 7 working days.",
                    data: new { routeId = route.Id });
            }

            _sendGridService.SendEmailRefundForBuyerAllTicket(routeTicket.RouteId, remainRefund);
            return("");
        }