forked from xiongyuxing/tiku-backend.net
feat: add payment sdk and tenant secret foundation
This commit is contained in:
66
Tiku.Infrastructure/Commerce/AlipayProvider.cs
Normal file
66
Tiku.Infrastructure/Commerce/AlipayProvider.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
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<CreatePaymentProviderResult> 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<PaymentNotificationResult> 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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user