From 88f520a51bdbb926b3159bd2f9c981f84d4f63d4 Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Thu, 25 Sep 2025 16:31:19 +0200 Subject: [PATCH] PaymentMethod --- .../Public/PaymentMethodsController.cs | 18 ++++--- .../Interfaces/IPaymentMethodService.cs | 7 +-- .../Services/Public/PaymentMethodService.cs | 52 +++++++++++-------- 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/Webshop.Api/Controllers/Public/PaymentMethodsController.cs b/Webshop.Api/Controllers/Public/PaymentMethodsController.cs index 60c406d..ac426b7 100644 --- a/Webshop.Api/Controllers/Public/PaymentMethodsController.cs +++ b/Webshop.Api/Controllers/Public/PaymentMethodsController.cs @@ -1,16 +1,17 @@ // src/Webshop.Api/Controllers/Public/PaymentMethodsController.cs using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading.Tasks; -using Webshop.Application.DTOs.Payments; // PaymentMethodDto -using Webshop.Application.Services.Public; // IPaymentMethodService +using Webshop.Application.DTOs.Payments; +using Webshop.Application.Services.Public; namespace Webshop.Api.Controllers.Public { [ApiController] - [Route("api/v1/public/[controller]")] // Saubere Route - [AllowAnonymous] // Jeder darf die verfügbaren Zahlungsmethoden sehen + [Route("api/v1/public/[controller]")] + [AllowAnonymous] public class PaymentMethodsController : ControllerBase { private readonly IPaymentMethodService _paymentMethodService; @@ -21,10 +22,13 @@ namespace Webshop.Api.Controllers.Public } [HttpGet] - public async Task>> GetActivePaymentMethods() + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task GetActivePaymentMethods() { - var paymentMethods = await _paymentMethodService.GetAllActiveAsync(); - return Ok(paymentMethods); + var result = await _paymentMethodService.GetAllActiveAsync(); + // Ein GetAll sollte im Normalfall immer erfolgreich sein (und ggf. eine leere Liste zurückgeben). + // Daher können wir hier direkt auf den Wert zugreifen. + return Ok(result.Value); } } } \ No newline at end of file diff --git a/Webshop.Application/Services/Public/Interfaces/IPaymentMethodService.cs b/Webshop.Application/Services/Public/Interfaces/IPaymentMethodService.cs index 6eb7654..0faedc4 100644 --- a/Webshop.Application/Services/Public/Interfaces/IPaymentMethodService.cs +++ b/Webshop.Application/Services/Public/Interfaces/IPaymentMethodService.cs @@ -1,7 +1,8 @@ // src/Webshop.Application/Services/Public/IPaymentMethodService.cs using System.Collections.Generic; using System.Threading.Tasks; -using Webshop.Application.DTOs.Payments; // Für PaymentMethodDto +using Webshop.Application; +using Webshop.Application.DTOs.Payments; namespace Webshop.Application.Services.Public { @@ -10,7 +11,7 @@ namespace Webshop.Application.Services.Public /// /// Ruft alle aktiven Zahlungsmethoden ab, die im Checkout für den Kunden sichtbar sein sollen. /// - /// Eine Liste von PaymentMethodDto mit öffentlichen Konfigurationsdaten. - Task> GetAllActiveAsync(); + /// Ein ServiceResult, das eine Liste von PaymentMethodDto mit öffentlichen Konfigurationsdaten enthält. + Task>> GetAllActiveAsync(); } } \ No newline at end of file diff --git a/Webshop.Application/Services/Public/PaymentMethodService.cs b/Webshop.Application/Services/Public/PaymentMethodService.cs index 36ad87a..ecb50fa 100644 --- a/Webshop.Application/Services/Public/PaymentMethodService.cs +++ b/Webshop.Application/Services/Public/PaymentMethodService.cs @@ -1,13 +1,13 @@ // src/Webshop.Application/Services/Public/PaymentMethodService.cs using System.Collections.Generic; using System.Linq; -using System.Text.Json; // Für JSON-Verarbeitung +using System.Text.Json; using System.Threading.Tasks; +using Webshop.Application; using Webshop.Application.DTOs.Payments; using Webshop.Domain.Enums; using Webshop.Domain.Interfaces; - namespace Webshop.Application.Services.Public { public class PaymentMethodService : IPaymentMethodService @@ -19,11 +19,11 @@ namespace Webshop.Application.Services.Public _paymentMethodRepository = paymentMethodRepository; } - public async Task> GetAllActiveAsync() + public async Task>> GetAllActiveAsync() { var paymentMethods = await _paymentMethodRepository.GetAllAsync(); - return paymentMethods + var activeMethods = paymentMethods .Where(pm => pm.IsActive) .Select(pm => new PaymentMethodDto { @@ -34,34 +34,42 @@ namespace Webshop.Application.Services.Public ProcessingFee = pm.ProcessingFee, PublicConfiguration = GetPublicConfiguration(pm.PaymentGatewayType, pm.Configuration) }).ToList(); + + return ServiceResult.Ok>(activeMethods); } private object? GetPublicConfiguration(PaymentGatewayType type, string? configJson) { if (string.IsNullOrEmpty(configJson)) return null; - // Beispiel: Nur für BankTransfer geben wir IBAN etc. preis - if (type == PaymentGatewayType.BankTransfer) + try { - var config = JsonSerializer.Deserialize>(configJson); - if (config != null) - { - // Filter, um geheime Schlüssel NICHT an das Frontend zu senden - return new { IBAN = config.GetValueOrDefault("IBAN"), BIC = config.GetValueOrDefault("BIC"), BankName = config.GetValueOrDefault("BankName") }; - } - } + var config = JsonSerializer.Deserialize>(configJson); + if (config == null) return null; - // Für Stripe/PayPal geben wir nur den Public Key preis - if (type == PaymentGatewayType.Stripe) + if (type == PaymentGatewayType.BankTransfer) + { + return new + { + IBAN = config.ContainsKey("IBAN") ? config["IBAN"].GetString() : null, + BIC = config.ContainsKey("BIC") ? config["BIC"].GetString() : null, + BankName = config.ContainsKey("BankName") ? config["BankName"].GetString() : null + }; + } + + if (type == PaymentGatewayType.Stripe) + { + return new { PublicKey = config.ContainsKey("PublicKey") ? config["PublicKey"].GetString() : null }; + } + + // Für PayPal oder andere geben wir keine öffentlichen Konfigurationsdetails preis + return null; + } + catch (JsonException) { - var config = JsonSerializer.Deserialize>(configJson); - if (config != null) - { - return new { PublicKey = config.GetValueOrDefault("PublicKey") }; - } + // Log error if needed, but return null to the client + return null; } - - return null; // Für andere Typen geben wir keine Konfigurationsdetails preis } } } \ No newline at end of file