示例#1
0
        private async Task <bool> PaymentWasCompleted(PaymentResultDTO paymentResultDTO, Order order)
        {
            if (order.BillingId == paymentResultDTO.BillingId.ToString())
            {
                return(false);
            }

            if (order.BillingId != null)
            {
                throw new Exception("Заказ с id={orderId} уже оплачен");
            }

            bool result = true;
            var  user   = _userContext.Users.First(g => g.Id == paymentResultDTO.UserId);

            if (paymentResultDTO.IsSuccessfully)
            {
                // 1. обновляем заказ
                order.BillingId   = paymentResultDTO.BillingId.ToString();
                order.PaidDateUtc = paymentResultDTO.PaymentDateUtc;
                order.Status      = OrderStatus.Processing;

                // 2. отправляем уведомление об успешной оплате
                await _mqSender.SendMessageAsync(new OrderWasPayment
                {
                    UserEmail   = user.UserName,
                    BillingId   = order.BillingId.ToString(),
                    OrderNumber = order.OrderNumber.ToString(),
                    Price       = order.TotalPrice,
                });
            }
            else
            {
                order.Status = OrderStatus.Error;
                // 1. снимаем с резервирования
                _warehouseServiceClient.AddHeader(Constants.USERID_HEADER, order.UserId.ToString());
                await _warehouseServiceClient.CancelAsync(order.OrderNumber.ToString());

                // 2. отправляем уведомление о плохой оплате
                await _mqSender.SendMessageAsync(new OrderCreatedError
                {
                    UserEmail = user.UserName,
                    Message   = $"Не удалось оплатить товар по причине {paymentResultDTO.ErrorDescription ?? string.Empty}. Заказ товара отменен. Попробуйте выполнить заказ повторно!",
                });

                result = false;
            }

            _orderContext.Update(order);
            await _orderContext.SaveChangesAsync();

            return(result);
        }
示例#2
0
        /// <summary>
        /// 得到一条付款条款信息
        /// </summary>
        /// <returns></returns>
        public PaymentResultDTO GetPayment(int id)
        {
            PaymentResultDTO result = null;
            var tcdmse = SingleQueryObject.GetObj();

            var pp = tcdmse.master_PaymentInfo.AsNoTracking().Where(p => p.PayID == id).FirstOrDefault();

            if (pp == null)
            {
                throw new Exception("此条信息不存在!");
            }
            result = Mapper.Map <master_PaymentInfo, PaymentResultDTO>(pp);

            return(result);
        }
示例#3
0
        public async Task <Order> OrderWasPaid(PaymentResultDTO paymentResultDTO, Guid orderId)
        {
            var order = await _orderContext.Orders.FirstOrDefaultAsync(g => g.OrderNumber == orderId);

            if (order == null)
            {
                _metricReporter.RegisterFailedOrder();
                throw new Exception($"Заказ c id={orderId} не существует");
            }
            if (await PaymentWasCompleted(paymentResultDTO, order))
            {
                order = await SendRequestOnShipment(order);

                _metricReporter.RegisterCreateOrder();
            }
            else
            {
                _metricReporter.RegisterFailedOrder();
            }
            return(order);
        }
        public async Task <ActionResult <OrderDTO> > PaymentComplete([FromRoute] Guid orderId, PaymentResultDTO paymentCompletedDTO)
        {
            var order = await _orderService.OrderWasPaid(paymentCompletedDTO, orderId);

            return(Ok(_mapper.Map <OrderDTO>(order)));
        }