69 lines
2.6 KiB
C#
69 lines
2.6 KiB
C#
using System.Text.Json;
|
|
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
|
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
|
|
using Tiku.Application.Commerce;
|
|
|
|
namespace Tiku.Infrastructure.Commerce;
|
|
|
|
internal sealed class WechatPayProvider : IPaymentProvider
|
|
{
|
|
public string Provider => PaymentProviders.WechatPay;
|
|
|
|
public Task<CreatePaymentProviderResult> CreatePaymentAsync(
|
|
PaymentProviderAccount account,
|
|
CreatePaymentProviderRequest request,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
_ = BuildClient(account);
|
|
throw new PaymentProviderException(
|
|
"WeChat Pay SDK is configured; checkout call is enabled in the commerce checkout batch.",
|
|
"wechat_pay_checkout_not_enabled");
|
|
}
|
|
|
|
public Task<PaymentNotificationResult> ParsePaymentNotificationAsync(
|
|
PaymentProviderAccount account,
|
|
PaymentNotificationRequest request,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
_ = BuildClient(account);
|
|
throw new PaymentProviderException(
|
|
"WeChat Pay SDK is configured; notification parsing is enabled in the notification batch.",
|
|
"wechat_pay_notification_not_enabled");
|
|
}
|
|
|
|
private static WechatTenpayClient BuildClient(PaymentProviderAccount account)
|
|
{
|
|
var options = new WechatTenpayClientOptions
|
|
{
|
|
MerchantId = Required(account.ConfigPublic, "merchantId", "mchId", "mchid"),
|
|
MerchantV3Secret = Required(account.SecretPayload, "apiV3Key", "merchantV3Secret"),
|
|
MerchantCertificateSerialNumber = Required(account.ConfigPublic, "certificateSerialNumber", "serialNo"),
|
|
MerchantCertificatePrivateKey = Required(account.SecretPayload, "privateKey", "merchantCertificatePrivateKey"),
|
|
PlatformAuthScheme = PlatformAuthScheme.PublicKey,
|
|
PlatformPublicKeyManager = new InMemoryPublicKeyManager()
|
|
};
|
|
|
|
return WechatTenpayClientBuilder.Create(options).Build();
|
|
}
|
|
|
|
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(
|
|
"WeChat Pay provider configuration is incomplete.",
|
|
"wechat_pay_config_incomplete");
|
|
}
|
|
}
|