PaymentMethod

This commit is contained in:
Tizian.Breuch
2025-09-25 16:31:19 +02:00
parent ccd574455f
commit 88f520a51b
3 changed files with 45 additions and 32 deletions

View File

@@ -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<ActionResult<IEnumerable<PaymentMethodDto>>> GetActivePaymentMethods()
[ProducesResponseType(typeof(IEnumerable<PaymentMethodDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> 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);
}
}
}

View File

@@ -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
/// <summary>
/// Ruft alle aktiven Zahlungsmethoden ab, die im Checkout für den Kunden sichtbar sein sollen.
/// </summary>
/// <returns>Eine Liste von PaymentMethodDto mit öffentlichen Konfigurationsdaten.</returns>
Task<IEnumerable<PaymentMethodDto>> GetAllActiveAsync();
/// <returns>Ein ServiceResult, das eine Liste von PaymentMethodDto mit öffentlichen Konfigurationsdaten enthält.</returns>
Task<ServiceResult<IEnumerable<PaymentMethodDto>>> GetAllActiveAsync();
}
}

View File

@@ -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<IEnumerable<PaymentMethodDto>> GetAllActiveAsync()
public async Task<ServiceResult<IEnumerable<PaymentMethodDto>>> 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<IEnumerable<PaymentMethodDto>>(activeMethods);
}
private object? GetPublicConfiguration(PaymentGatewayType type, string? configJson)
{
if (string.IsNullOrEmpty(configJson)) return null;
// Beispiel: Nur für BankTransfer geben wir IBAN etc. preis
try
{
var config = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(configJson);
if (config == null) return null;
if (type == PaymentGatewayType.BankTransfer)
{
var config = JsonSerializer.Deserialize<Dictionary<string, string>>(configJson);
if (config != null)
return new
{
// 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") };
}
IBAN = config.ContainsKey("IBAN") ? config["IBAN"].GetString() : null,
BIC = config.ContainsKey("BIC") ? config["BIC"].GetString() : null,
BankName = config.ContainsKey("BankName") ? config["BankName"].GetString() : null
};
}
// Für Stripe/PayPal geben wir nur den Public Key preis
if (type == PaymentGatewayType.Stripe)
{
var config = JsonSerializer.Deserialize<Dictionary<string, string>>(configJson);
if (config != null)
{
return new { PublicKey = config.GetValueOrDefault("PublicKey") };
}
return new { PublicKey = config.ContainsKey("PublicKey") ? config["PublicKey"].GetString() : null };
}
return null; // Für andere Typen geben wir keine Konfigurationsdetails preis
// Für PayPal oder andere geben wir keine öffentlichen Konfigurationsdetails preis
return null;
}
catch (JsonException)
{
// Log error if needed, but return null to the client
return null;
}
}
}
}