Example #1
0
        private async Task CancelPurchase(Purchase purchase)
        {
            await _mobilePayPaymentsService.CancelPayment(Guid.Parse(purchase.TransactionId));
            purchase.Completed = false;
            purchase.Status = PurchaseStatus.Cancelled;
            await _context.SaveChangesAsync();

            Log.Information("Purchase has been cancelled Purchase Id {PurchaseId}, Transaction Id {TransactionId}", purchase.Id, purchase.TransactionId);
        }
Example #2
0
        public async Task<InitiatePurchaseResponse> InitiatePurchase(InitiatePurchaseRequest initiateRequest, User user)
        {
            var product = await _context.Products.Include(p => p.ProductUserGroup).FirstOrDefaultAsync(p => p.Id == initiateRequest.ProductId);
            if (product == null)
            {
                Log.Error("No product was found by Product Id: {Id}", initiateRequest.ProductId);
                throw new EntityNotFoundException($"No purchase was found by Purchase Id: {initiateRequest.ProductId}");
                // Exception mapper 
            }
            
            var productUserGroup = product.ProductUserGroup.Select(pug => pug.UserGroup);
            var isUserInProductUserGroup = productUserGroup.Contains(user.UserGroup);

            if (!isUserInProductUserGroup)
            {
                Log.Information("User {UserId} is not authorized to purchase Product Id: {ProductId} as user is not in Product User Group", user.Id, product.Id);
                throw new ApiException("Product is not available for user to purchase", StatusCodes.Status403Forbidden);
            }

            var paymentDetails = await _mobilePayPaymentsService.InitiatePayment(new MobilePayPaymentRequest
            {
                Amount = product.Price,
                OrderId = await GenerateUniqueOrderId(),
                Description = product.Name
            });

            // FIXME State management, PaymentType
            var purchase = new Purchase
            {
                ProductName = product.Name,
                ProductId = product.Id,
                Price = product.Price,
                NumberOfTickets = product.NumberOfTickets,
                DateCreated = DateTime.UtcNow,
                Completed = false,
                OrderId = paymentDetails.OrderId,
                TransactionId = paymentDetails.PaymentId,
                PurchasedBy = user,
                Status = PurchaseStatus.PendingPayment
            };
            await _context.Purchases.AddAsync(purchase);
            await _context.SaveChangesAsync();

            return new InitiatePurchaseResponse
            {
                Id = purchase.Id,
                DateCreated = purchase.DateCreated,
                ProductId = product.Id,
                ProductName = product.Name,
                TotalAmount = purchase.Price,
                PurchaseStatus = purchase.Status,
                PaymentDetails = paymentDetails
            };
        }
Example #3
0
        private async Task CompletePurchase(Purchase purchase)
        {
            await _mobilePayPaymentsService.CapturePayment(Guid.Parse(purchase.TransactionId), purchase.Price);
            await _ticketService.IssueTickets(purchase);
            
            purchase.Completed = true;
            purchase.Status = PurchaseStatus.Completed;
            await _context.SaveChangesAsync();

            await _emailService.SendInvoiceAsyncV2(purchase, purchase.PurchasedBy);
        }