Exemplo n.º 1
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("");
        }
Exemplo n.º 2
0
        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("");
        }