public async Task <IActionResult> EditPaymentRequest(string id, UpdatePaymentRequestViewModel viewModel)
        {
            if (string.IsNullOrEmpty(viewModel.Currency) ||
                _Currencies.GetCurrencyData(viewModel.Currency, false) == null)
            {
                ModelState.AddModelError(nameof(viewModel.Currency), "Invalid currency");
            }

            var data = await _PaymentRequestRepository.FindPaymentRequest(id, GetUserId());

            if (data == null && !string.IsNullOrEmpty(id))
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                viewModel.Stores = new SelectList(await _StoreRepository.GetStoresByUserId(GetUserId()),
                                                  nameof(StoreData.Id),
                                                  nameof(StoreData.StoreName), data?.StoreDataId);

                return(View(viewModel));
            }

            if (data == null)
            {
                data = new PaymentRequestData();
            }

            data.StoreDataId = viewModel.StoreId;
            var blob = data.GetBlob();

            blob.Title                     = viewModel.Title;
            blob.Email                     = viewModel.Email;
            blob.Description               = viewModel.Description;
            blob.Amount                    = viewModel.Amount;
            blob.ExpiryDate                = viewModel.ExpiryDate?.ToUniversalTime();
            blob.Currency                  = viewModel.Currency;
            blob.EmbeddedCSS               = viewModel.EmbeddedCSS;
            blob.CustomCSSLink             = viewModel.CustomCSSLink;
            blob.AllowCustomPaymentAmounts = viewModel.AllowCustomPaymentAmounts;

            data.SetBlob(blob);
            if (string.IsNullOrEmpty(id))
            {
                data.Created = DateTimeOffset.UtcNow;
            }
            data = await _PaymentRequestRepository.CreateOrUpdatePaymentRequest(data);

            _EventAggregator.Publish(new PaymentRequestUpdated()
            {
                Data             = data,
                PaymentRequestId = data.Id,
            });

            TempData[WellKnownTempData.SuccessMessage] = "Saved";
            return(RedirectToAction("EditPaymentRequest", new { id = data.Id }));
        }
        public async Task <ActionResult> ArchivePaymentRequest(string storeId, string paymentRequestId)
        {
            var pr = await _paymentRequestRepository.FindPaymentRequests(
                new PaymentRequestQuery()
            {
                StoreId = storeId, Ids = new[] { paymentRequestId }, IncludeArchived = false
            });

            if (pr.Total == 0)
            {
                return(NotFound());
            }

            var updatedPr = pr.Items.First();

            updatedPr.Archived = true;
            await _paymentRequestRepository.CreateOrUpdatePaymentRequest(updatedPr);

            return(Ok());
        }
        public async Task <IActionResult> EditPaymentRequest(string payReqId, UpdatePaymentRequestViewModel viewModel)
        {
            if (!string.IsNullOrEmpty(viewModel.Currency) &&
                _Currencies.GetCurrencyData(viewModel.Currency, false) == null)
            {
                ModelState.AddModelError(nameof(viewModel.Currency), "Invalid currency");
            }
            if (string.IsNullOrEmpty(viewModel.Currency))
            {
                viewModel.Currency = null;
            }
            var store          = GetCurrentStore();
            var paymentRequest = GetCurrentPaymentRequest();

            if (paymentRequest == null && !string.IsNullOrEmpty(payReqId))
            {
                return(NotFound());
            }

            if (paymentRequest?.Archived is true && viewModel.Archived)
            {
                ModelState.AddModelError(string.Empty, "You cannot edit an archived payment request.");
            }

            if (!ModelState.IsValid)
            {
                return(View(nameof(EditPaymentRequest), viewModel));
            }

            var data = paymentRequest ?? new PaymentRequestData();

            data.StoreDataId = viewModel.StoreId;
            data.Archived    = viewModel.Archived;

            var blob = data.GetBlob();

            blob.Title                     = viewModel.Title;
            blob.Email                     = viewModel.Email;
            blob.Description               = viewModel.Description;
            blob.Amount                    = viewModel.Amount;
            blob.ExpiryDate                = viewModel.ExpiryDate?.ToUniversalTime();
            blob.Currency                  = viewModel.Currency ?? store.GetStoreBlob().DefaultCurrency;
            blob.EmbeddedCSS               = viewModel.EmbeddedCSS;
            blob.CustomCSSLink             = viewModel.CustomCSSLink;
            blob.AllowCustomPaymentAmounts = viewModel.AllowCustomPaymentAmounts;

            data.SetBlob(blob);
            if (string.IsNullOrEmpty(payReqId))
            {
                data.Created = DateTimeOffset.UtcNow;
            }

            data = await _PaymentRequestRepository.CreateOrUpdatePaymentRequest(data);

            _EventAggregator.Publish(new PaymentRequestUpdated {
                Data = data, PaymentRequestId = data.Id,
            });

            TempData[WellKnownTempData.SuccessMessage] = "Saved";
            return(RedirectToAction(nameof(EditPaymentRequest), new { storeId = store.Id, payReqId = data.Id }));
        }