Exemplo n.º 1
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            // some of the data models don't have OnModelCreating for now, commenting them

            AddressInvoiceData.OnModelCreating(builder);
            APIKeyData.OnModelCreating(builder);
            AppData.OnModelCreating(builder);
            //StoredFile.OnModelCreating(builder);
            HistoricalAddressInvoiceData.OnModelCreating(builder);
            InvoiceEventData.OnModelCreating(builder);
            InvoiceSearchData.OnModelCreating(builder);
            InvoiceWebhookDeliveryData.OnModelCreating(builder);
            InvoiceData.OnModelCreating(builder);
            NotificationData.OnModelCreating(builder);
            //OffchainTransactionData.OnModelCreating(builder);
            Data.PairedSINData.OnModelCreating(builder);
            PairingCodeData.OnModelCreating(builder);
            //PayjoinLock.OnModelCreating(builder);
            PaymentRequestData.OnModelCreating(builder);
            PaymentData.OnModelCreating(builder);
            PayoutData.OnModelCreating(builder);
            PendingInvoiceData.OnModelCreating(builder);
            //PlannedTransaction.OnModelCreating(builder);
            PullPaymentData.OnModelCreating(builder);
            RefundData.OnModelCreating(builder);
            //SettingData.OnModelCreating(builder);
            StoreWebhookData.OnModelCreating(builder);
            //StoreData.OnModelCreating(builder);
            U2FDevice.OnModelCreating(builder);
            Fido2Credential.OnModelCreating(builder);
            Data.UserStore.OnModelCreating(builder);
            //WalletData.OnModelCreating(builder);
            WalletTransactionData.OnModelCreating(builder);
            WebhookDeliveryData.OnModelCreating(builder);
            //WebhookData.OnModelCreating(builder);


            if (Database.IsSqlite() && !_designTime)
            {
                // SQLite does not have proper support for DateTimeOffset via Entity Framework Core, see the limitations
                // here: https://docs.microsoft.com/en-us/ef/core/providers/sqlite/limitations#query-limitations
                // To work around this, when the Sqlite database provider is used, all model properties of type DateTimeOffset
                // use the DateTimeOffsetToBinaryConverter
                // Based on: https://github.com/aspnet/EntityFrameworkCore/issues/10784#issuecomment-415769754
                // This only supports millisecond precision, but should be sufficient for most use cases.
                foreach (var entityType in builder.Model.GetEntityTypes())
                {
                    var properties = entityType.ClrType.GetProperties().Where(p => p.PropertyType == typeof(DateTimeOffset));
                    foreach (var property in properties)
                    {
                        builder
                        .Entity(entityType.Name)
                        .Property(property.Name)
                        .HasConversion(new Microsoft.EntityFrameworkCore.Storage.ValueConversion.DateTimeOffsetToBinaryConverter());
                    }
                }
            }
        }
Exemplo n.º 2
0
        public async Task UpdatePaymentRequestStateIfNeeded(PaymentRequestData pr)
        {
            var blob          = pr.GetBlob();
            var currentStatus = pr.Status;

            if (blob.ExpiryDate.HasValue)
            {
                if (blob.ExpiryDate.Value <= DateTimeOffset.UtcNow)
                {
                    currentStatus = Client.Models.PaymentRequestData.PaymentRequestStatus.Expired;
                }
            }

            if (currentStatus == Client.Models.PaymentRequestData.PaymentRequestStatus.Pending)
            {
                var rateRules = pr.StoreData.GetStoreBlob().GetRateRules(_BtcPayNetworkProvider);
                var invoices  = await _PaymentRequestRepository.GetInvoicesForPaymentRequest(pr.Id);

                var contributions = _AppService.GetContributionsByPaymentMethodId(blob.Currency, invoices, true);
                if (contributions.TotalCurrency >= blob.Amount)
                {
                    currentStatus = Client.Models.PaymentRequestData.PaymentRequestStatus.Completed;
                }
            }

            if (currentStatus != pr.Status)
            {
                pr.Status = currentStatus;
                await _PaymentRequestRepository.UpdatePaymentRequestStatus(pr.Id, currentStatus);
            }
        }
        public static PaymentRequestBaseData GetBlob(this PaymentRequestData paymentRequestData)
        {
            var result = paymentRequestData.Blob == null
                ? new PaymentRequestBaseData()
                : JObject.Parse(ZipUtils.Unzip(paymentRequestData.Blob)).ToObject <PaymentRequestBaseData>();

            return(result);
        }
        public static PaymentRequestBaseData GetBlob(this PaymentRequestData paymentRequestData)
        {
            var result = paymentRequestData.Blob == null
                ? new PaymentRequestBaseData()
                : ParseBlob(paymentRequestData.Blob);

            return(result);
        }
        public static bool SetBlob(this PaymentRequestData paymentRequestData, PaymentRequestBaseData blob)
        {
            var original = new Serializer(null).ToString(paymentRequestData.GetBlob());
            var newBlob  = new Serializer(null).ToString(blob);

            if (original == newBlob)
            {
                return(false);
            }
            paymentRequestData.Blob = ZipUtils.Zip(newBlob);
            return(true);
        }
        private static Client.Models.PaymentRequestData FromModel(PaymentRequestData data)
        {
            var blob = data.GetBlob();

            return(new Client.Models.PaymentRequestData()
            {
                Created = data.Created,
                Id = data.Id,
                Status = data.Status,
                Archived = data.Archived,
                Amount = blob.Amount,
                Currency = blob.Currency,
                Description = blob.Description,
                Title = blob.Title,
                ExpiryDate = blob.ExpiryDate,
                Email = blob.Email,
                AllowCustomPaymentAmounts = blob.AllowCustomPaymentAmounts,
                EmbeddedCSS = blob.EmbeddedCSS,
                CustomCSSLink = blob.CustomCSSLink
            });
        }
        public async Task <IActionResult> CreatePaymentRequest(string storeId,
                                                               CreatePaymentRequestRequest request)
        {
            var validationResult = Validate(request);

            if (validationResult != null)
            {
                return(validationResult);
            }

            var pr = new PaymentRequestData()
            {
                StoreDataId = storeId,
                Status      = Client.Models.PaymentRequestData.PaymentRequestStatus.Pending,
                Created     = DateTimeOffset.Now
            };

            pr.SetBlob(request);
            pr = await _paymentRequestRepository.CreateOrUpdatePaymentRequest(pr);

            return(Ok(FromModel(pr)));
        }
        public UpdatePaymentRequestViewModel(PaymentRequestData data)
        {
            if (data == null)
            {
                return;
            }

            Id       = data.Id;
            StoreId  = data.StoreDataId;
            Archived = data.Archived;
            var blob = data.GetBlob();

            Title                     = blob.Title;
            Amount                    = blob.Amount;
            Currency                  = blob.Currency;
            Description               = blob.Description;
            ExpiryDate                = blob.ExpiryDate;
            Email                     = blob.Email;
            CustomCSSLink             = blob.CustomCSSLink;
            EmbeddedCSS               = blob.EmbeddedCSS;
            AllowCustomPaymentAmounts = blob.AllowCustomPaymentAmounts;
        }
Exemplo n.º 9
0
        public ViewPaymentRequestViewModel(PaymentRequestData data)
        {
            Id      = data.Id;
            StoreId = data.StoreDataId;
            var blob = data.GetBlob();

            Archived                  = data.Archived;
            Title                     = blob.Title;
            Amount                    = blob.Amount;
            Currency                  = blob.Currency;
            Description               = blob.Description;
            ExpiryDate                = blob.ExpiryDate?.UtcDateTime;
            Email                     = blob.Email;
            EmbeddedCSS               = blob.EmbeddedCSS;
            CustomCSSLink             = blob.CustomCSSLink;
            AllowCustomPaymentAmounts = blob.AllowCustomPaymentAmounts;
            if (!string.IsNullOrEmpty(EmbeddedCSS))
            {
                EmbeddedCSS = $"<style>{EmbeddedCSS}</style>";
            }
            switch (data.Status)
            {
            case Client.Models.PaymentRequestData.PaymentRequestStatus.Pending:
                Status    = ExpiryDate.HasValue ? $"Expires on {ExpiryDate.Value:g}" : "Pending";
                IsPending = true;
                break;

            case Client.Models.PaymentRequestData.PaymentRequestStatus.Completed:
                Status = "Settled";
                break;

            case Client.Models.PaymentRequestData.PaymentRequestStatus.Expired:
                Status = "Expired";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 10
0
        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 (data?.Archived is true && viewModel?.Archived is true)
            {
                ModelState.AddModelError(string.Empty, "You cannot edit an archived payment request.");
            }

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

                return(View(nameof(EditPaymentRequest), viewModel));
            }

            if (data == null)
            {
                data = 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;
            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(nameof(EditPaymentRequest), new { id = data.Id }));
        }