public async Task <Result> Payment(int orderId)
        {
            var order = await _orderRepository.Query().FirstOrDefaultAsync(e => e.Id == orderId);

            if (order == null)
            {
                return(Result.Fail(ResultCodes.IdInvalid));
            }

            var status = new OrderStatus[] { OrderStatus.PendingPayment, OrderStatus.PaymentFailed };

            if (!status.Contains(order.OrderStatus))
            {
                return(Result.Fail(ResultCodes.RequestParamError, "当前订单状态不允许付款"));
            }

            if (order.ActuallyAmount <= 0)
            {
                return(Result.Fail(ResultCodes.RequestParamError, "订单金额不能为0"));
            }
            if (order.ActuallyAmount - order.WalletAmount <= 0)
            {
                return(Result.Fail(ResultCodes.RequestParamError, "订单金额不能为0"));
            }

            var user = await _customerManager.GetUserAsync();

            try
            {
                var wechatResponse = await _paymentService.GeneratePaymentOrder(new GeneratePaymentOrderRequest
                {
                    OrderNo  = order.OrderNo,
                    OpenId   = user.OpenId,
                    TotalFee = order.ActuallyAmount - order.WalletAmount,
                    Subject  = "大脉商城",
                });

                var paymentTimeoutEvent = new PaymentTimeoutEvent()
                {
                    OrderId = order.Id,
                    OrderNo = order.OrderNo
                };

                var headers = new Dictionary <string, object>()
                {
                    { "x-delay", 10 * 60 * 1000 } // 延迟10分钟
                };
                var properties = _client.CreateProperties();
                properties.Headers = headers;

                _client.PushMessage(MQConstants.Exchange, MQConstants.PaymentTimeOutRouteKey, paymentTimeoutEvent, properties);

                return(Result.Ok(wechatResponse));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);

                return(Result.Fail(ResultCodes.PaymentRequestError, ex.Message));
            }
        }
Beispiel #2
0
        public async Task <Result> SetTrackingNumber(AdminShipmentSetTrackingNumberRequest request)
        {
            var shipment = await _shipmentRepository.Query()
                           .Include(e => e.ShipmentOrderItems).ThenInclude(e => e.OrderItem)
                           .Include(e => e.ShippingAddress)
                           .FirstOrDefaultAsync(e => e.Id == request.Id);

            if (shipment == null)
            {
                return(Result.Fail(ResultCodes.IdInvalid));
            }

            var status = new[] { ShippingStatus.PendingShipment, ShippingStatus.Shipped };

            if (!status.Contains(shipment.ShippingStatus))
            {
                return(Result.Fail(ResultCodes.RequestParamError, "当前订单不允许修改状态"));
            }

            if (string.IsNullOrEmpty(request.CourierCompany))
            {
                shipment.CourierCompany = CourierCompanyHelper.GetCompanyName(request.CourierCompanyCode);
            }
            else
            {
                shipment.CourierCompany = request.CourierCompany;
            }
            shipment.TrackingNumber     = request.TrackingNumber;
            shipment.CourierCompanyCode = request.CourierCompanyCode;
            if (shipment.ShippingStatus != ShippingStatus.Shipped)
            {
                shipment.ShippingStatus = ShippingStatus.Shipped;
                shipment.ShippingTime   = DateTime.Now;

                var orderItems = shipment.ShipmentOrderItems.Select(e => e.OrderItem);
                foreach (var item in orderItems)
                {
                    item.Status = OrderItemStatus.Shipped;
                }

                var message = new AutoReceivedShippingEvent
                {
                    ShipmentId = shipment.Id
                };
                var headers = new Dictionary <string, object>()
                {
                    { "x-delay", 10 * 24 * 60 * 60 * 1000 } // 延迟10天自动收货
                };
                var properties = _client.CreateProperties();
                properties.Headers = headers;

                _client.PushMessage(MQConstants.Exchange, MQConstants.AutoReceivedShippingKey, message, properties);

                var address = shipment.ShippingAddress;
                var notify  = new ShippingNotify
                {
                    TrackingNumber = request.TrackingNumber,
                    UserName       = address.UserName,
                    CourierCompany = shipment.CourierCompany
                };
                await _smsService.SendAsync(address.TelNumber, notify);
            }

            _shipmentRepository.Update(shipment, false);

            using (var transaction = _shipmentRepository.BeginTransaction())
            {
                await _shipmentRepository.SaveAsync();

                transaction.Commit();
            }

            return(Result.Ok());
        }