Esempio n. 1
0
        public async Task <IActionResult> UseAsync(
            [FromServices] OrderService orderService,
            [FromServices] IService <Models.Voucher> voucherService,
            [FromBody] UseBindings bindings,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // var currency = bindings.Currency ?? _appSettings.Currencies.FirstOrDefault();
            var orderId = bindings.OrderId;
            var code    = bindings.Code;

            var order = await orderService.GetByIdAsync(orderId, cancellationToken);

            if (order == null)
            {
                return(NotFound($"Order with id = {orderId} not found"));
            }

            var voucher = (await voucherService.FindAsync(Filter.Eq("Code", code), cancellationToken: cancellationToken)).FirstOrDefault();

            if (voucher == null)
            {
                return(NotFound($"Voucher with code = {code} not found"));
            }

            if (voucher.Used)
            {
                return(BadRequest($"Voucher with code = {code} already used"));
            }

            if (voucher.Expired)
            {
                return(BadRequest($"Voucher with code = {code} expired on {voucher.Expiration}"));
            }

            var amount = Math.Min(voucher.Value, order.Total - order.PaidAmount);

            voucher.Value   -= amount;
            voucher.Used     = !voucher.MultipleUse || voucher.Value <= 0;
            voucher.OrderIds = voucher.OrderIds.Concat(new[] { orderId });

            await voucherService.UpdateAsync(voucher, cancellationToken);

            var payment = new Payment()
            {
                Title     = "Voucher Payment",
                Provider  = Name,
                Reference = $"voucher",
                Status    = PaymentStatus.Paid,
                Date      = DateTime.UtcNow,
                Method    = PaymentMethod.Voucher,
                Details   = $"Payment Order #{order.Reference}",
                Currency  = voucher.Currency,
                Amount    = amount
            };

            await orderService.AddPayment(order, payment, cancellationToken);

            return(Ok(ApiModel.AsSuccess(payment)));
        }
Esempio n. 2
0
        public async Task <IActionResult> ValidateAsync(
            [FromServices] OrderService orderService,
            [FromServices] IService <Models.Voucher> voucherService,
            [FromBody] UseBindings bindings,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // var currency = bindings.Currency ?? _appSettings.Currencies.FirstOrDefault();
            var orderId = bindings.OrderId;
            var code    = bindings.Code;

            var order = await orderService.GetByIdAsync(orderId, cancellationToken);

            if (order == null)
            {
                return(NotFound($"Order with id = {orderId} not found"));
            }

            var voucher = (await voucherService.FindAsync(Filter.Eq("Code", code), cancellationToken: cancellationToken)).FirstOrDefault();

            if (voucher == null)
            {
                return(Ok(ApiModel.AsSuccess(new ValidateResponse {
                    Result = false, Reason = "notfound"
                })));
            }

            if (voucher.Used)
            {
                return(Ok(ApiModel.AsSuccess(new ValidateResponse {
                    Result = false, Reason = "used"
                })));
            }

            if (voucher.Expired)
            {
                return(Ok(ApiModel.AsSuccess(new ValidateResponse {
                    Result = false, Reason = "expired"
                })));
            }

            if (voucher.Value <= 0)
            {
                return(Ok(ApiModel.AsSuccess(new ValidateResponse {
                    Result = false, Reason = "empty"
                })));
            }


            return(Ok(ApiModel.AsSuccess(new ValidateResponse {
                Result = true
            })));
        }