public async Task <IActionResult> GetByPaymentsFilter( PaymentType type, PaymentsFilterPeriod period, List <PayInvoice.Client.Models.Invoice.InvoiceStatus> statuses, string searchText, int take) { var paymentsResponse = await _paymentsService.GetByPaymentsFilter( User.GetMerchantId(), type, statuses, period, searchText, take); return(Ok(Mapper.Map <PaymentsResponse>(paymentsResponse))); }
public static (DateTime?DateFrom, DateTime?DateTo) GetDates(this PaymentsFilterPeriod period) { DateTime?dateFrom = null; DateTime?dateTo = null; switch (period) { case PaymentsFilterPeriod.ThisWeek: dateFrom = DateTime.UtcNow.GetBeginOfWeek(); break; case PaymentsFilterPeriod.LastWeek: dateTo = DateTime.UtcNow.GetBeginOfWeek(); dateFrom = dateTo.Value.AddDays(-7); break; case PaymentsFilterPeriod.ThisMonth: dateFrom = DateTime.UtcNow.GetBeginOfMonth(); break; case PaymentsFilterPeriod.LastMonth: dateTo = DateTime.UtcNow.GetBeginOfMonth(); dateFrom = dateTo.Value.AddMonths(-1); break; case PaymentsFilterPeriod.ThreeMonths: dateFrom = DateTime.UtcNow.Date.AddMonths(-3); break; case PaymentsFilterPeriod.ThisYear: dateFrom = new DateTime(DateTime.UtcNow.Year, 1, 1); break; case PaymentsFilterPeriod.LastYear: dateTo = new DateTime(DateTime.UtcNow.Year, 1, 1); dateFrom = dateTo.Value.AddYears(-1); break; } return(dateFrom, dateTo); }
public async Task <IActionResult> ExportPayments( PaymentType type, PaymentsFilterPeriod period, List <PayInvoice.Client.Models.Invoice.InvoiceStatus> statuses, string searchText ) { var paymentsResponse = await _paymentsService.GetByPaymentsFilter( User.GetMerchantId(), type, statuses, period, searchText, null ); IEnumerable <Payment> payments = paymentsResponse.Payments; string content; using (TextWriter stringWriter = new StringWriter()) { var svcWriter = new CsvHelper.CsvWriter(stringWriter); svcWriter.WriteRecords(payments.Select(o => new InvoiceCsvRowModel { Number = o.Number, ClientName = o.ClientName, ClientEmail = o.ClientEmail, Amount = o.Amount, Currency = o.SettlementAsset.DisplayId, Status = o.Status.ToString(), DueDate = o.DueDate.ToIsoDateTime(), CreatedDate = o.CreatedDate.ToIsoDateTime() })); content = stringWriter.ToString(); } return(File(new MemoryStream(Encoding.UTF8.GetBytes(content)), "text/csv", "payments.csv")); }
public async Task <IActionResult> GetByInvoiceIdAsync( [FromRoute][Guid] string invoiceId, [FromQuery] PaymentsFilterPeriod period, [FromQuery] string searchText) { try { var model = await _paymentsService.GetByInvoiceIdAsync(invoiceId); // check whether satisfy filter conditions var dates = period.GetDates(); if (dates.DateFrom.HasValue && model.CreatedDate < dates.DateFrom) { model = null; } else if (dates.DateTo.HasValue && model.CreatedDate > dates.DateTo) { model = null; } else if (!string.IsNullOrEmpty(searchText) && !(model.Number.Contains(searchText) || model.ClientName.Contains(searchText) || model.ClientEmail.Contains(searchText))) { model = null; } return(Ok(Mapper.Map <PaymentModel>(model))); } catch (ErrorResponseException ex) when(ex.StatusCode == HttpStatusCode.NotFound) { return(NotFound()); } }
/// <summary> /// Gets payments - the combined result of invoices and payment requests /// </summary> public async Task <PaymentsResponse> GetByPaymentsFilter( string merchantId, PaymentType type, IReadOnlyList <InvoiceStatus> statuses, PaymentsFilterPeriod period, string searchText, int?take ) { var dates = period.GetDates(); searchText = searchText?.Trim(); Task <PayInvoice.Client.Models.Invoice.GetByPaymentsFilterResponse> invoicesTask = null; Task <PayInternal.Client.Models.PaymentRequest.GetByPaymentsFilterResponse> paymentRequestsTask = null; if (type == PaymentType.All || type == PaymentType.Invoice) { invoicesTask = _payInvoiceClient.GetByPaymentsFilterAsync( merchantId, statuses.Select(_ => _.ToString()), dates.DateFrom, dates.DateTo, searchText, take); } var isOnlyInvoices = statuses.Count == 1 && statuses.First() == InvoiceStatus.Draft; if ((type == PaymentType.All || type == PaymentType.Api) && !isOnlyInvoices) { var paymentRequestStatuses = new List <PaymentRequestStatus>(); var processingErrors = new List <PaymentRequestProcessingError>(); foreach (var invoiceStatus in statuses) { var converted = invoiceStatus.ToPaymentRequestStatus(); if (converted.PaymentRequestStatus == PaymentRequestStatus.None) { continue; } if (!paymentRequestStatuses.Contains(converted.PaymentRequestStatus)) { paymentRequestStatuses.Add(converted.PaymentRequestStatus); } if (converted.ProcessingErrors == null) { continue; } foreach (var processingError in converted.ProcessingErrors) { if (processingError == PaymentRequestProcessingError.None) { continue; } if (!processingErrors.Contains(processingError)) { processingErrors.Add(processingError); } } } paymentRequestsTask = _payInternalClient.GetByPaymentsFilterAsync( merchantId, paymentRequestStatuses.Select(_ => _.ToString()), processingErrors.Select(_ => _.ToString()), dates.DateFrom, dates.DateTo, take); } var result = new PaymentsResponse { HasAnyPayment = true }; switch (type) { case PaymentType.All: if (invoicesTask != null || paymentRequestsTask != null) { await Task.WhenAll(invoicesTask ?? Task.CompletedTask, paymentRequestsTask ?? Task.CompletedTask); var payments = Mapper.Map <List <Payment> >(invoicesTask?.Result.Invoices); if (paymentRequestsTask != null) { payments.AddRange(Mapper.Map <List <Payment> >(paymentRequestsTask?.Result.PaymeentRequests)); } result.Payments = payments.OrderByDescending(x => x.CreatedDate).ToList(); result.HasMorePayments = (invoicesTask?.Result.HasMoreInvoices ?? false) || (paymentRequestsTask?.Result.HasMorePaymentRequests ?? false); } break; case PaymentType.Invoice: if (invoicesTask != null) { var invoicesTaskResponse = await invoicesTask; result.Payments = Mapper.Map <List <Payment> >(invoicesTaskResponse.Invoices); result.HasMorePayments = invoicesTaskResponse.HasMoreInvoices; } break; case PaymentType.Api: if (paymentRequestsTask != null) { var paymentRequestsTaskResponse = await paymentRequestsTask; result.Payments = Mapper.Map <List <Payment> >(paymentRequestsTaskResponse.PaymeentRequests); result.HasMorePayments = paymentRequestsTaskResponse.HasMorePaymentRequests; } break; } foreach (var payment in result.Payments) { payment.SettlementAsset = await _lykkeAssetsResolver.TryGetAssetAsync(payment.SettlementAssetId); } if (result.Payments.Count == 0 && type == PaymentType.All && statuses.Count == 0 && period != PaymentsFilterPeriod.AllTime && string.IsNullOrEmpty(searchText)) { var hasAnyInvoiceTask = _payInvoiceClient.HasAnyInvoiceAsync(merchantId); var hasAnyPaymentRequestTask = _payInternalClient.HasAnyPaymentRequestAsync(merchantId); await Task.WhenAll(hasAnyInvoiceTask, hasAnyPaymentRequestTask); result.HasAnyPayment = hasAnyInvoiceTask.Result || hasAnyPaymentRequestTask.Result; } return(result); }