Esempio n. 1
0
        public async Task UpdateOrderAsync(Guid paymentId)
        {
            MOrder order = await ordersRepository.GetOrderAsync(paymentId);

            if (order == null)
            {
                logger.LogWarning("Order for payment id {0} not found", paymentId);
                return;
            }

            if (order.Status == OrderConstants.Status.Completed)
            {
                logger.LogWarning("Order #{0} was already completed", order.Id);
                return;
            }

            order.Status     = OrderConstants.Status.Completed;
            order.LastUpdate = DateTime.Now;
            await ordersRepository.UpdateOrderAsync(order);

            foreach (MOrderItem item in order.Items)
            {
                await productsRepository.AddProductCustomerAsync(new MProductCustomer()
                {
                    UserId    = order.BuyerId,
                    ProductId = item.ProductId
                });
            }

            discordService.SendPurchaseNotification(order);
            logger.LogInformation($"Successfully finished the order #{order.Id}");
        }
Esempio n. 2
0
        public async Task ProcessPaymentAsync(HttpRequest request)
        {
            string requestBody;

            using (var reader = new StreamReader(request.Body, Encoding.ASCII))
            {
                requestBody = await reader.ReadToEndAsync();
            }

            var content = new StringContent("cmd=_notify-validate&" + requestBody);

            var response = await httpClientFactory.CreateClient().PostAsync(PayPalUrl, content);

            var verification = await response.Content.ReadAsStringAsync();

            if (!verification.Equals("VERIFIED"))
            {
                return;
            }

            var dict = HttpUtility.ParseQueryString(requestBody);

            if (!int.TryParse(dict["custom"], out int orderId))
            {
                throw new Exception($"Invalid payment custom: {dict["custom"]}");
            }

            var order = await ordersRepository.GetOrderAsync(orderId);

            if (dict["receiver_email"] != order.PaymentReceiver)
            {
                logger.LogWarning($"Payment receive email ");
                return;
            }

            if (decimal.Parse(dict["mc_gross"]) < order.TotalPrice || dict["mc_currency"] != order.Currency)
            {
                logger.LogWarning($"Payment price or currency doesn't match order {dict["custom"]}");
                return;
            }

            order.TransactionId = dict["txn_id"];
            order.Status        = dict["payment_status"];
            order.PaymentPayer  = dict["payer_email"];

            await ordersRepository.UpdateOrderAsync(order);

            if (order.Status == "Completed")
            {
                foreach (var item in order.Items)
                {
                    await productsRepository.AddProductCustomerAsync(new ProductCustomerModel()
                    {
                        UserId    = order.BuyerId,
                        ProductId = item.ProductId
                    });
                }
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> PostProductCustomerAsync([FromBody] ProductCustomerModel customer)
        {
            if (!await productsRepository.IsProductSellerAsync(customer.ProductId, int.Parse(User.Identity.Name)))
            {
                return(BadRequest());
            }

            return(Ok(await productsRepository.AddProductCustomerAsync(customer)));
        }
Esempio n. 4
0
        public async Task <IActionResult> PostProductCustomerAsync([FromBody] MProductCustomer customer)
        {
            if (!User.IsInRole(RoleConstants.AdminRoleId) && !await productsRepository.IsProductSellerAsync(customer.ProductId, int.Parse(User.Identity.Name)))
            {
                return(BadRequest());
            }

            return(Ok(await productsRepository.AddProductCustomerAsync(customer)));
        }