PaymentMethod
This commit is contained in:
@@ -1,16 +1,17 @@
|
|||||||
// src/Webshop.Api/Controllers/Public/PaymentMethodsController.cs
|
// src/Webshop.Api/Controllers/Public/PaymentMethodsController.cs
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Webshop.Application.DTOs.Payments; // PaymentMethodDto
|
using Webshop.Application.DTOs.Payments;
|
||||||
using Webshop.Application.Services.Public; // IPaymentMethodService
|
using Webshop.Application.Services.Public;
|
||||||
|
|
||||||
namespace Webshop.Api.Controllers.Public
|
namespace Webshop.Api.Controllers.Public
|
||||||
{
|
{
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/v1/public/[controller]")] // Saubere Route
|
[Route("api/v1/public/[controller]")]
|
||||||
[AllowAnonymous] // Jeder darf die verfügbaren Zahlungsmethoden sehen
|
[AllowAnonymous]
|
||||||
public class PaymentMethodsController : ControllerBase
|
public class PaymentMethodsController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IPaymentMethodService _paymentMethodService;
|
private readonly IPaymentMethodService _paymentMethodService;
|
||||||
@@ -21,10 +22,13 @@ namespace Webshop.Api.Controllers.Public
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<IEnumerable<PaymentMethodDto>>> GetActivePaymentMethods()
|
[ProducesResponseType(typeof(IEnumerable<PaymentMethodDto>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<IActionResult> GetActivePaymentMethods()
|
||||||
{
|
{
|
||||||
var paymentMethods = await _paymentMethodService.GetAllActiveAsync();
|
var result = await _paymentMethodService.GetAllActiveAsync();
|
||||||
return Ok(paymentMethods);
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
// src/Webshop.Application/Services/Public/IPaymentMethodService.cs
|
// src/Webshop.Application/Services/Public/IPaymentMethodService.cs
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
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
|
namespace Webshop.Application.Services.Public
|
||||||
{
|
{
|
||||||
@@ -10,7 +11,7 @@ namespace Webshop.Application.Services.Public
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ruft alle aktiven Zahlungsmethoden ab, die im Checkout für den Kunden sichtbar sein sollen.
|
/// Ruft alle aktiven Zahlungsmethoden ab, die im Checkout für den Kunden sichtbar sein sollen.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>Eine Liste von PaymentMethodDto mit öffentlichen Konfigurationsdaten.</returns>
|
/// <returns>Ein ServiceResult, das eine Liste von PaymentMethodDto mit öffentlichen Konfigurationsdaten enthält.</returns>
|
||||||
Task<IEnumerable<PaymentMethodDto>> GetAllActiveAsync();
|
Task<ServiceResult<IEnumerable<PaymentMethodDto>>> GetAllActiveAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
// src/Webshop.Application/Services/Public/PaymentMethodService.cs
|
// src/Webshop.Application/Services/Public/PaymentMethodService.cs
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.Json; // Für JSON-Verarbeitung
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Payments;
|
using Webshop.Application.DTOs.Payments;
|
||||||
using Webshop.Domain.Enums;
|
using Webshop.Domain.Enums;
|
||||||
using Webshop.Domain.Interfaces;
|
using Webshop.Domain.Interfaces;
|
||||||
|
|
||||||
|
|
||||||
namespace Webshop.Application.Services.Public
|
namespace Webshop.Application.Services.Public
|
||||||
{
|
{
|
||||||
public class PaymentMethodService : IPaymentMethodService
|
public class PaymentMethodService : IPaymentMethodService
|
||||||
@@ -19,11 +19,11 @@ namespace Webshop.Application.Services.Public
|
|||||||
_paymentMethodRepository = paymentMethodRepository;
|
_paymentMethodRepository = paymentMethodRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<PaymentMethodDto>> GetAllActiveAsync()
|
public async Task<ServiceResult<IEnumerable<PaymentMethodDto>>> GetAllActiveAsync()
|
||||||
{
|
{
|
||||||
var paymentMethods = await _paymentMethodRepository.GetAllAsync();
|
var paymentMethods = await _paymentMethodRepository.GetAllAsync();
|
||||||
|
|
||||||
return paymentMethods
|
var activeMethods = paymentMethods
|
||||||
.Where(pm => pm.IsActive)
|
.Where(pm => pm.IsActive)
|
||||||
.Select(pm => new PaymentMethodDto
|
.Select(pm => new PaymentMethodDto
|
||||||
{
|
{
|
||||||
@@ -34,34 +34,42 @@ namespace Webshop.Application.Services.Public
|
|||||||
ProcessingFee = pm.ProcessingFee,
|
ProcessingFee = pm.ProcessingFee,
|
||||||
PublicConfiguration = GetPublicConfiguration(pm.PaymentGatewayType, pm.Configuration)
|
PublicConfiguration = GetPublicConfiguration(pm.PaymentGatewayType, pm.Configuration)
|
||||||
}).ToList();
|
}).ToList();
|
||||||
|
|
||||||
|
return ServiceResult.Ok<IEnumerable<PaymentMethodDto>>(activeMethods);
|
||||||
}
|
}
|
||||||
|
|
||||||
private object? GetPublicConfiguration(PaymentGatewayType type, string? configJson)
|
private object? GetPublicConfiguration(PaymentGatewayType type, string? configJson)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(configJson)) return null;
|
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)
|
if (type == PaymentGatewayType.BankTransfer)
|
||||||
{
|
{
|
||||||
var config = JsonSerializer.Deserialize<Dictionary<string, string>>(configJson);
|
return new
|
||||||
if (config != null)
|
|
||||||
{
|
{
|
||||||
// Filter, um geheime Schlüssel NICHT an das Frontend zu senden
|
IBAN = config.ContainsKey("IBAN") ? config["IBAN"].GetString() : null,
|
||||||
return new { IBAN = config.GetValueOrDefault("IBAN"), BIC = config.GetValueOrDefault("BIC"), BankName = config.GetValueOrDefault("BankName") };
|
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)
|
if (type == PaymentGatewayType.Stripe)
|
||||||
{
|
{
|
||||||
var config = JsonSerializer.Deserialize<Dictionary<string, string>>(configJson);
|
return new { PublicKey = config.ContainsKey("PublicKey") ? config["PublicKey"].GetString() : null };
|
||||||
if (config != null)
|
|
||||||
{
|
|
||||||
return new { PublicKey = config.GetValueOrDefault("PublicKey") };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user