Beispiel #1
0
        public virtual async Task CreateAsync(CreatePaymentDto input)
        {
            var orders = new List <OrderDto>();

            foreach (var orderId in input.OrderIds)
            {
                orders.Add(await _orderAppService.GetAsync(orderId));
            }

            var extraProperties = new Dictionary <string, object> {
                { "StoreId", orders.First().StoreId }
            };

            await _payableChecker.CheckAsync(input, orders, extraProperties);

            // Todo: should avoid duplicate creations.

            await _distributedEventBus.PublishAsync(new CreatePaymentEto
            {
                TenantId        = CurrentTenant.Id,
                UserId          = CurrentUser.GetId(),
                PaymentMethod   = input.PaymentMethod,
                Currency        = orders.First().Currency,
                ExtraProperties = extraProperties,
                PaymentItems    = orders.Select(order => new CreatePaymentItemEto
                {
                    ItemType = PaymentsConsts.PaymentItemType,
                    ItemKey  = order.Id,
                    Currency = order.Currency,
                    OriginalPaymentAmount = order.TotalPrice
                }).ToList()
            });
        }
Beispiel #2
0
        public virtual async Task CreateAsync(CreateEShopRefundInput input)
        {
            await AuthorizationService.CheckAsync(PaymentsPermissions.Refunds.Manage);

            var payment = await _paymentRepository.GetAsync(input.PaymentId);

            var createRefundInput = new CreateRefundInput
            {
                PaymentId      = input.PaymentId,
                DisplayReason  = input.DisplayReason,
                CustomerRemark = input.CustomerRemark,
                StaffRemark    = input.StaffRemark
            };

            foreach (var refundItem in input.RefundItems)
            {
                var order = await _orderAppService.GetAsync(refundItem.OrderId);

                var paymentItem = payment.PaymentItems.SingleOrDefault(x => x.ItemKey == refundItem.OrderId.ToString());

                if (order.PaymentId != input.PaymentId || paymentItem == null)
                {
                    throw new OrderIsNotInSpecifiedPaymentException(order.Id, payment.Id);
                }

                // Todo: Check if current user is an admin of the store.

                foreach (var orderLineRefundInfoModel in refundItem.OrderLines)
                {
                    var orderLine = order.OrderLines.Single(x => x.Id == orderLineRefundInfoModel.OrderLineId);

                    if (orderLine.RefundedQuantity + orderLineRefundInfoModel.Quantity > orderLine.Quantity)
                    {
                        throw new InvalidRefundQuantityException(orderLineRefundInfoModel.Quantity);
                    }
                }

                createRefundInput.RefundItems.Add(new CreateRefundItemInput
                {
                    PaymentItemId   = paymentItem.Id,
                    RefundAmount    = refundItem.OrderLines.Sum(x => x.TotalAmount),
                    CustomerRemark  = refundItem.CustomerRemark,
                    StaffRemark     = refundItem.StaffRemark,
                    ExtraProperties = new Dictionary <string, object>
                    {
                        { "StoreId", order.StoreId.ToString() },
                        { "OrderId", order.Id.ToString() },
                        { "OrderLines", _jsonSerializer.Serialize(refundItem.OrderLines) }
                    }
                });
            }

            await _distributedEventBus.PublishAsync(new RefundPaymentEto
            {
                TenantId          = CurrentTenant.Id,
                CreateRefundInput = createRefundInput
            });
        }
Beispiel #3
0
        /* Shows how to perform an HTTP request to the API using ABP's dynamic c# proxy
         * feature. It is just simple as calling a local service method.
         * Authorization and HTTP request details are handled by the ABP framework.
         */
        private async Task TestWithDynamicProxiesAsync()
        {
            Console.WriteLine();
            Console.WriteLine($"***** {nameof(TestWithDynamicProxiesAsync)} *****");

            var result = await _orderAppService.GetAsync();

            Console.WriteLine("Result: " + result.Value);

            result = await _orderAppService.GetAuthorizedAsync();

            Console.WriteLine("Result (authorized): " + result.Value);
        }
Beispiel #4
0
        public async Task CreateAsync(CreatePaymentDto input)
        {
            var orders = new List <OrderDto>();

            foreach (var orderId in input.OrderIds)
            {
                var order = await _orderAppService.GetAsync(orderId);

                orders.Add(order);

                if (order.PaymentId.HasValue || order.PaidTime.HasValue)
                {
                    throw new OrderPaymentAlreadyExistsException(orderId);
                }
            }

            if (orders.Select(order => order.Currency).Distinct().Count() != 1)
            {
                throw new MultiCurrencyNotSupportedException();
            }

            if (orders.Select(order => order.StoreId).Distinct().Count() != 1)
            {
                throw new MultiStorePaymentNotSupportedException();
            }

            // Todo: should avoid duplicate creations.

            var extraProperties = new Dictionary <string, object> {
                { "StoreId", orders.First().StoreId }
            };

            await _distributedEventBus.PublishAsync(new CreatePaymentEto
            {
                TenantId        = CurrentTenant.Id,
                UserId          = CurrentUser.GetId(),
                PaymentMethod   = input.PaymentMethod,
                Currency        = orders.First().Currency,
                ExtraProperties = extraProperties,
                PaymentItems    = orders.Select(order => new CreatePaymentItemEto
                {
                    ItemType = PaymentsConsts.PaymentItemType,
                    ItemKey  = order.Id,
                    Currency = order.Currency,
                    OriginalPaymentAmount = order.TotalPrice
                }).ToList()
            });
        }
Beispiel #5
0
        public virtual async Task CreateAsync(CreatePaymentDto input)
        {
            // Todo: should avoid duplicate creations. (concurrent lock)

            var orders = new List <OrderDto>();

            foreach (var orderId in input.OrderIds)
            {
                orders.Add(await _orderAppService.GetAsync(orderId));
            }

            await AuthorizationService.CheckAsync(
                new PaymentCreationResource
            {
                Input  = input,
                Orders = orders
            },
                new PaymentOperationAuthorizationRequirement(PaymentOperation.Creation)
                );

            var paymentItems = orders.Select(order =>
            {
                var eto = new CreatePaymentItemEto
                {
                    ItemType = PaymentsConsts.PaymentItemType,
                    ItemKey  = order.Id.ToString(),
                    OriginalPaymentAmount = order.ActualTotalPrice
                };

                eto.SetProperty(nameof(PaymentItem.StoreId), order.StoreId);

                return(eto);
            }).ToList();

            var createPaymentEto = new CreatePaymentEto(
                CurrentTenant.Id,
                CurrentUser.GetId(),
                input.PaymentMethod,
                orders.First().Currency,
                paymentItems
                );

            input.MapExtraPropertiesTo(createPaymentEto);

            await _distributedEventBus.PublishAsync(createPaymentEto);
        }
Beispiel #6
0
        public virtual async Task CreateAsync(CreatePaymentDto input)
        {
            // Todo: should avoid duplicate creations. (concurrent lock)

            var orders = new List <OrderDto>();

            foreach (var orderId in input.OrderIds)
            {
                orders.Add(await _orderAppService.GetAsync(orderId));
            }

            await AuthorizationService.CheckAsync(
                new PaymentCreationResource
            {
                Input  = input,
                Orders = orders
            },
                new PaymentOperationAuthorizationRequirement(PaymentOperation.Creation)
                );

            var createPaymentEto = new CreatePaymentEto
            {
                TenantId        = CurrentTenant.Id,
                UserId          = CurrentUser.GetId(),
                PaymentMethod   = input.PaymentMethod,
                Currency        = orders.First().Currency,
                ExtraProperties = new Dictionary <string, object>(),
                PaymentItems    = orders.Select(order => new CreatePaymentItemEto
                {
                    ItemType = PaymentsConsts.PaymentItemType,
                    ItemKey  = order.Id.ToString(),
                    OriginalPaymentAmount = order.ActualTotalPrice,
                    ExtraProperties       = new Dictionary <string, object> {
                        { "StoreId", order.StoreId.ToString() }
                    }
                }).ToList()
            };

            await _distributedEventBus.PublishAsync(createPaymentEto);
        }
Beispiel #7
0
        public virtual async Task CreateAsync(CreateEShopRefundInput input)
        {
            await AuthorizationService.CheckAsync(PaymentsPermissions.Refunds.Manage);

            var payment = await _paymentRepository.GetAsync(input.PaymentId);

            if (payment.PendingRefundAmount != decimal.Zero)
            {
                throw new AnotherRefundTaskIsOnGoingException(payment.Id);
            }

            var createRefundInput = new CreateRefundInput
            {
                PaymentId      = input.PaymentId,
                DisplayReason  = input.DisplayReason,
                CustomerRemark = input.CustomerRemark,
                StaffRemark    = input.StaffRemark
            };

            foreach (var refundItem in input.RefundItems)
            {
                var order = await _orderAppService.GetAsync(refundItem.OrderId);

                var paymentItem = payment.PaymentItems.SingleOrDefault(x => x.ItemKey == refundItem.OrderId.ToString());

                if (order.PaymentId != input.PaymentId || paymentItem == null)
                {
                    throw new OrderIsNotInSpecifiedPaymentException(order.Id, payment.Id);
                }

                await AuthorizationService.CheckMultiStorePolicyAsync(paymentItem.StoreId,
                                                                      PaymentsPermissions.Refunds.Manage, PaymentsPermissions.Refunds.CrossStore);

                var refundAmount = refundItem.OrderLines.Sum(x => x.TotalAmount) +
                                   refundItem.OrderExtraFees.Sum(x => x.TotalAmount);

                if (refundAmount + paymentItem.RefundAmount > paymentItem.ActualPaymentAmount)
                {
                    throw new InvalidRefundAmountException(payment.Id, paymentItem.Id, refundAmount);
                }

                foreach (var model in refundItem.OrderLines)
                {
                    var orderLine = order.OrderLines.Find(x => x.Id == model.OrderLineId);

                    if (orderLine is null)
                    {
                        throw new OrderLineNotFoundException(order.Id, model.OrderLineId);
                    }

                    if (orderLine.RefundedQuantity + model.Quantity > orderLine.Quantity)
                    {
                        throw new InvalidRefundQuantityException(model.Quantity);
                    }
                }

                foreach (var model in refundItem.OrderExtraFees)
                {
                    var orderExtraFee = order.OrderExtraFees.Find(x => x.Name == model.Name && x.Key == model.Key);

                    if (orderExtraFee is null)
                    {
                        throw new OrderExtraFeeNotFoundException(order.Id, model.Name, model.Key);
                    }
                }

                var eto = new CreateRefundItemInput
                {
                    PaymentItemId  = paymentItem.Id,
                    RefundAmount   = refundAmount,
                    CustomerRemark = refundItem.CustomerRemark,
                    StaffRemark    = refundItem.StaffRemark
                };

                eto.SetProperty(nameof(RefundItem.StoreId), order.StoreId);
                eto.SetProperty(nameof(RefundItem.OrderId), order.Id);
                eto.SetProperty(nameof(RefundItem.OrderLines), _jsonSerializer.Serialize(refundItem.OrderLines));
                eto.SetProperty(nameof(RefundItem.OrderExtraFees), _jsonSerializer.Serialize(refundItem.OrderExtraFees));

                createRefundInput.RefundItems.Add(eto);
            }

            await _distributedEventBus.PublishAsync(new RefundPaymentEto(CurrentTenant.Id, createRefundInput));
        }
Beispiel #8
0
 public Task <OrderDto> GetAsync(Guid id)
 {
     return(_service.GetAsync(id));
 }
Beispiel #9
0
 public async Task <ActionResult <OrderDto> > GetAsync([FromRoute] long id)
 {
     return(await _orderSrv.GetAsync(id));
 }
        public async Task GetAsync()
        {
            var result = await _orderAppService.GetAsync();

            result.Value.ShouldBe(42);
        }
        public async Task OnGetAsync()
        {
            var dto = await _service.GetAsync(Id);

            ViewModel = ObjectMapper.Map <OrderDto, CreateEditOrderViewModel>(dto);
        }
Beispiel #12
0
        public async Task Order_Should_Be_Created()
        {
            // Arrange
            var createOrderDto = new CreateOrderDto
            {
                CustomerRemark = "customer remark",
                StoreId        = OrderTestData.Store1Id,
                OrderLines     = new List <CreateOrderLineDto>
                {
                    new CreateOrderLineDto
                    {
                        ProductId    = OrderTestData.Product1Id,
                        ProductSkuId = OrderTestData.ProductSku1Id,
                        Quantity     = 10
                    }
                }
            };

            // Act
            var createResponse = await _orderAppService.CreateAsync(createOrderDto);

            var response = await _orderAppService.GetAsync(createResponse.Id);

            // Assert
            response.ShouldNotBeNull();
            response.Currency.ShouldBe("CNY");
            response.CanceledTime.ShouldBeNull();
            response.CancellationReason.ShouldBeNullOrEmpty();
            response.CompletionTime.ShouldBeNull();
            response.CustomerRemark.ShouldBe("customer remark");
            response.OrderNumber.ShouldNotBeNull();
            response.OrderStatus.ShouldBe(OrderStatus.Pending);
            response.PaidTime.ShouldBeNull();
            response.PaymentId.ShouldBeNull();
            response.RefundAmount.ShouldBe(0m);
            response.StaffRemark.ShouldBeNullOrEmpty();
            response.StoreId.ShouldBe(OrderTestData.Store1Id);
            response.TotalDiscount.ShouldBe(0m);
            response.TotalPrice.ShouldBe(10m);
            response.ActualTotalPrice.ShouldBe(10m);
            response.CustomerUserId.ShouldBe(Guid.Parse("2e701e62-0953-4dd3-910b-dc6cc93ccb0d"));
            response.ProductTotalPrice.ShouldBe(10m);
            response.ReducedInventoryAfterPaymentTime.ShouldBeNull();
            response.ReducedInventoryAfterPlacingTime.ShouldNotBeNull();
            response.OrderLines.Count.ShouldBe(1);

            var responseOrderLine = response.OrderLines.First();

            responseOrderLine.ProductId.ShouldBe(OrderTestData.Product1Id);
            responseOrderLine.ProductSkuId.ShouldBe(OrderTestData.ProductSku1Id);
            responseOrderLine.ProductDisplayName.ShouldBe("Hello pencil");
            responseOrderLine.ProductUniqueName.ShouldBe("Pencil");
            responseOrderLine.ProductGroupName.ShouldBe("Default");
            responseOrderLine.ProductGroupDisplayName.ShouldBe("Default");
            responseOrderLine.SkuName.ShouldBe("My SKU");
            responseOrderLine.UnitPrice.ShouldBe(1m);
            responseOrderLine.TotalPrice.ShouldBe(10m);
            responseOrderLine.TotalDiscount.ShouldBe(0m);
            responseOrderLine.ActualTotalPrice.ShouldBe(10m);
            responseOrderLine.Currency.ShouldBe("CNY");
            responseOrderLine.Quantity.ShouldBe(10);
            responseOrderLine.ProductModificationTime.ShouldBe(OrderTestData.ProductLastModificationTime);
            responseOrderLine.RefundAmount.ShouldBe(0m);
            responseOrderLine.RefundedQuantity.ShouldBe(0);

            UsingDbContext(context =>
            {
                context.Orders.Count().ShouldBe(1);
                var order = context.Orders.Include(x => x.OrderLines).First();
                order.ShouldNotBeNull();
                order.Currency.ShouldBe("CNY");
                order.CanceledTime.ShouldBeNull();
                order.CancellationReason.ShouldBeNullOrEmpty();
                order.CompletionTime.ShouldBeNull();
                order.CustomerRemark.ShouldBe("customer remark");
                order.OrderNumber.ShouldNotBeNull();
                order.OrderStatus.ShouldBe(OrderStatus.Pending);
                order.PaidTime.ShouldBeNull();
                order.PaymentId.ShouldBeNull();
                order.RefundAmount.ShouldBe(0m);
                order.StaffRemark.ShouldBeNullOrEmpty();
                order.StoreId.ShouldBe(OrderTestData.Store1Id);
                order.TotalDiscount.ShouldBe(0m);
                order.TotalPrice.ShouldBe(10m);
                order.ActualTotalPrice.ShouldBe(10m);
                order.CustomerUserId.ShouldBe(Guid.Parse("2e701e62-0953-4dd3-910b-dc6cc93ccb0d"));
                order.ProductTotalPrice.ShouldBe(10m);
                order.ReducedInventoryAfterPaymentTime.ShouldBeNull();
                order.ReducedInventoryAfterPlacingTime.ShouldNotBeNull();
                order.OrderLines.Count.ShouldBe(1);

                var orderLine = order.OrderLines.First();
                orderLine.ProductId.ShouldBe(OrderTestData.Product1Id);
                orderLine.ProductSkuId.ShouldBe(OrderTestData.ProductSku1Id);
                orderLine.ProductDisplayName.ShouldBe("Hello pencil");
                orderLine.ProductUniqueName.ShouldBe("Pencil");
                orderLine.ProductGroupName.ShouldBe("Default");
                orderLine.ProductGroupDisplayName.ShouldBe("Default");
                orderLine.SkuName.ShouldBe("My SKU");
                orderLine.UnitPrice.ShouldBe(1m);
                orderLine.TotalPrice.ShouldBe(10m);
                orderLine.TotalDiscount.ShouldBe(0m);
                orderLine.ActualTotalPrice.ShouldBe(10m);
                orderLine.Currency.ShouldBe("CNY");
                orderLine.Quantity.ShouldBe(10);
                orderLine.ProductModificationTime.ShouldBe(OrderTestData.ProductLastModificationTime);
                orderLine.RefundAmount.ShouldBe(0m);
                orderLine.RefundedQuantity.ShouldBe(0);
            });
        }
 public async Task <OrderDto> GetAsync()
 {
     return(await _orderAppService.GetAsync());
 }