using System.Text.Json; using Aop.Api; using Tiku.Application.Commerce; namespace Tiku.Infrastructure.Commerce; internal sealed class AlipayProvider : IPaymentProvider { public string Provider => PaymentProviders.Alipay; public Task CreatePaymentAsync( PaymentProviderAccount account, CreatePaymentProviderRequest request, CancellationToken cancellationToken = default) { _ = BuildClient(account); throw new PaymentProviderException( "Alipay SDK is configured; checkout call is enabled in the commerce checkout batch.", "alipay_checkout_not_enabled"); } public Task ParsePaymentNotificationAsync( PaymentProviderAccount account, PaymentNotificationRequest request, CancellationToken cancellationToken = default) { _ = BuildClient(account); throw new PaymentProviderException( "Alipay SDK is configured; notification parsing is enabled in the notification batch.", "alipay_notification_not_enabled"); } private static DefaultAopClient BuildClient(PaymentProviderAccount account) { return new DefaultAopClient( Required(account.ConfigPublic, "gatewayUrl", "gateway"), Required(account.ConfigPublic, "appId"), Required(account.SecretPayload, "privateKey", "appPrivateKey"), "json", "1.0", "RSA2", Required(account.ConfigPublic, "alipayPublicKey"), "UTF-8", false); } private static string Required(JsonElement element, params string[] keys) { if (element.ValueKind == JsonValueKind.Object) { foreach (var key in keys) { if (element.TryGetProperty(key, out var property) && property.ValueKind == JsonValueKind.String && !string.IsNullOrWhiteSpace(property.GetString())) { return property.GetString()!; } } } throw new PaymentProviderException( "Alipay provider configuration is incomplete.", "alipay_config_incomplete"); } }