private async Task <OrderEntity> GetOrderForBuyerToPayOrThrow(CreateCheckoutRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                throw new KnownException("Invalid request model!");
            }

            EnsureBuyerProfile();
            OrdersRepo.ChainQueryable(q => q.Include(o => o.Buyer));
            var order = await OrdersRepo.GetOne(model.OrderId);

            if (order == null)
            {
                throw new KnownException("Comanda nu a fost găsită!");
            }

            if (order.Buyer != CurrentBuyerProfile)
            {
                throw new KnownException("Nu poți plăti această comandă deoarece nu îți aparține!");
            }

            if (order.State == OrderState.Paid || order.State == OrderState.WaitingProcessing)
            {
                throw new KnownException("Această comandă este deja plătită!");
            }

            return(order);
        }
        public async Task <IActionResult> CreateCheckout([FromBody] CreateCheckoutRequestModel model,
                                                         [FromQuery] bool force = false)
        {
            var order = await GetOrderForBuyerToPayOrThrow(model);

            if (!string.IsNullOrEmpty(order.WePayCheckoutId) && !force)
            {
                throw new KnownException(
                          "Nu poți plăti această comandă. Există deja o cerere de plată asociată pe WePay.");
            }

            var wePayRequestBody = new
            {
                account_id        = WePayAccountId,
                amount            = order.TotalPrice / 4.33,
                short_description = "iTEC Shop: payment for order " + order.Id,
                type            = "goods",
                currency        = "USD",
                hosted_checkout = new
                {
                    redirect_uri = EnvVarManager.GetOrThrow("EXTERNAL_URL") + "/payment-redirect"
                }
            };
            var wePayRequestBodyJson =
                new StringContent(JsonConvert.SerializeObject(wePayRequestBody), Encoding.UTF8, "application/json");

            var response =
                await WePayHttpClient.PostAsync("https://stage.wepayapi.com/v2/checkout/create", wePayRequestBodyJson);

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

            try
            {
                response.EnsureSuccessStatusCode();
                var responseJson = JsonConvert.DeserializeObject <Dictionary <string, object> >(responseText);

                var checkoutId  = responseJson["checkout_id"].ToString();
                var checkoutUri = (responseJson["hosted_checkout"] as JObject)?["checkout_uri"].ToString();

                order.WePayCheckoutId = checkoutId;
                order.State           = OrderState.WaitingPayment;
                await DataLayer.SaveChangesAsync();

                return(Ok(new { checkoutId, checkoutUri }));
            }
            catch
            {
                Console.WriteLine(responseText);
                throw;
            }
        }