♻️ Splitted wallet service

This commit is contained in:
2026-02-03 02:18:02 +08:00
parent bb9105c78c
commit 9a1f36ee26
43 changed files with 623 additions and 590 deletions

View File

@@ -0,0 +1,142 @@
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
namespace DysonNetwork.Shared.Registry;
public class RemotePaymentService(DysonNetwork.Shared.Proto.PaymentService.PaymentServiceClient payment)
{
public async Task<DysonNetwork.Shared.Proto.Order> CreateOrder(
string currency,
string amount,
string? payeeWalletId = null,
TimeSpan? expiration = null,
string? appIdentifier = null,
string? productIdentifier = null,
byte[]? meta = null,
string? remarks = null,
bool reuseable = false)
{
var request = new DysonNetwork.Shared.Proto.CreateOrderRequest
{
Currency = currency,
Amount = amount,
Reuseable = reuseable
};
if (payeeWalletId != null)
request.PayeeWalletId = payeeWalletId;
if (expiration.HasValue)
request.Expiration = Duration.FromTimeSpan(expiration.Value);
if (appIdentifier != null)
request.AppIdentifier = appIdentifier;
if (productIdentifier != null)
request.ProductIdentifier = productIdentifier;
if (meta != null)
request.Meta = ByteString.CopyFrom(meta);
if (remarks != null)
request.Remarks = remarks;
var response = await payment.CreateOrderAsync(request);
return response;
}
public async Task<DysonNetwork.Shared.Proto.Transaction> CreateTransaction(
string? payerWalletId,
string? payeeWalletId,
string currency,
string amount,
string? remarks = null,
DysonNetwork.Shared.Proto.TransactionType type = DysonNetwork.Shared.Proto.TransactionType.Unspecified)
{
var request = new DysonNetwork.Shared.Proto.CreateTransactionRequest
{
Currency = currency,
Amount = amount,
Type = type
};
if (payerWalletId != null)
request.PayerWalletId = payerWalletId;
if (payeeWalletId != null)
request.PayeeWalletId = payeeWalletId;
if (remarks != null)
request.Remarks = remarks;
var response = await payment.CreateTransactionAsync(request);
return response;
}
public async Task<DysonNetwork.Shared.Proto.Transaction> CreateTransactionWithAccount(
string? payerAccountId,
string? payeeAccountId,
string currency,
string amount,
string? remarks = null,
DysonNetwork.Shared.Proto.TransactionType type = DysonNetwork.Shared.Proto.TransactionType.Unspecified)
{
var request = new DysonNetwork.Shared.Proto.CreateTransactionWithAccountRequest
{
Currency = currency,
Amount = amount,
Type = type
};
if (payerAccountId != null)
request.PayerAccountId = payerAccountId;
if (payeeAccountId != null)
request.PayeeAccountId = payeeAccountId;
if (remarks != null)
request.Remarks = remarks;
var response = await payment.CreateTransactionWithAccountAsync(request);
return response;
}
public async Task<DysonNetwork.Shared.Proto.Transaction> Transfer(
Guid payerAccountId,
Guid payeeAccountId,
string currency,
string amount)
{
var request = new DysonNetwork.Shared.Proto.TransferRequest
{
PayerAccountId = payerAccountId.ToString(),
PayeeAccountId = payeeAccountId.ToString(),
Currency = currency,
Amount = amount
};
var response = await payment.TransferAsync(request);
return response;
}
public async Task<DysonNetwork.Shared.Proto.Order> CancelOrder(string orderId)
{
var request = new DysonNetwork.Shared.Proto.CancelOrderRequest { OrderId = orderId };
var response = await payment.CancelOrderAsync(request);
return response;
}
public async Task<DysonNetwork.Shared.Proto.RefundOrderResponse> RefundOrder(string orderId)
{
var request = new DysonNetwork.Shared.Proto.RefundOrderRequest { OrderId = orderId };
var response = await payment.RefundOrderAsync(request);
return response;
}
public async Task<DysonNetwork.Shared.Proto.WalletFund> GetWalletFund(string fundId)
{
var request = new DysonNetwork.Shared.Proto.GetWalletFundRequest { FundId = fundId };
var response = await payment.GetWalletFundAsync(request);
return response;
}
}

View File

@@ -0,0 +1,65 @@
using DysonNetwork.Shared.Proto;
namespace DysonNetwork.Shared.Registry;
public class RemoteSubscriptionService(SubscriptionService.SubscriptionServiceClient subscription)
{
public async Task<Subscription> GetSubscription(Guid accountId, string identifier)
{
var request = new GetSubscriptionRequest
{
AccountId = accountId.ToString(),
Identifier = identifier
};
var response = await subscription.GetSubscriptionAsync(request);
return response;
}
public async Task<Subscription> GetPerkSubscription(Guid accountId)
{
var request = new GetPerkSubscriptionRequest { AccountId = accountId.ToString() };
var response = await subscription.GetPerkSubscriptionAsync(request);
return response;
}
public async Task<List<Subscription>> GetPerkSubscriptions(List<Guid> accountIds)
{
var request = new GetPerkSubscriptionsRequest();
request.AccountIds.AddRange(accountIds.Select(id => id.ToString()));
var response = await subscription.GetPerkSubscriptionsAsync(request);
return response.Subscriptions.ToList();
}
public async Task<Subscription> CreateSubscription(
Guid accountId,
string identifier,
string paymentMethod,
string? couponCode = null,
bool isFreeTrial = false)
{
var request = new CreateSubscriptionRequest
{
AccountId = accountId.ToString(),
Identifier = identifier,
PaymentMethod = paymentMethod,
IsFreeTrial = isFreeTrial
};
if (couponCode != null)
request.CouponCode = couponCode;
var response = await subscription.CreateSubscriptionAsync(request);
return response;
}
public async Task<Subscription> CancelSubscription(Guid accountId, string identifier)
{
var request = new CancelSubscriptionRequest
{
AccountId = accountId.ToString(),
Identifier = identifier
};
var response = await subscription.CancelSubscriptionAsync(request);
return response;
}
}

View File

@@ -0,0 +1,37 @@
using DysonNetwork.Shared.Proto;
namespace DysonNetwork.Shared.Registry;
public class RemoteWalletService(WalletService.WalletServiceClient wallet)
{
public async Task<Wallet> GetWallet(Guid accountId)
{
var request = new GetWalletRequest { AccountId = accountId.ToString() };
var response = await wallet.GetWalletAsync(request);
return response;
}
public async Task<Wallet> CreateWallet(Guid accountId)
{
var request = new CreateWalletRequest { AccountId = accountId.ToString() };
var response = await wallet.CreateWalletAsync(request);
return response;
}
public async Task<WalletPocket> GetOrCreateWalletPocket(Guid walletId, string currency, decimal? initialAmount = null)
{
var request = new GetOrCreateWalletPocketRequest
{
WalletId = walletId.ToString(),
Currency = currency
};
if (initialAmount.HasValue)
{
request.InitialAmount = initialAmount.Value.ToString();
}
var response = await wallet.GetOrCreateWalletPocketAsync(request);
return response;
}
}

View File

@@ -64,6 +64,26 @@ public static class ServiceInjectionHelper
return services;
}
public IServiceCollection AddWalletService()
{
services.AddGrpcClientWithSharedChannel<WalletService.WalletServiceClient>(
"https://_grpc.wallet",
"WalletService");
services.AddSingleton<RemoteWalletService>();
services.AddGrpcClientWithSharedChannel<PaymentService.PaymentServiceClient>(
"https://_grpc.wallet",
"PaymentService");
services.AddSingleton<RemotePaymentService>();
services.AddGrpcClientWithSharedChannel<SubscriptionService.SubscriptionServiceClient>(
"https://_grpc.wallet",
"SubscriptionService");
services.AddSingleton<RemoteSubscriptionService>();
return services;
}
public IServiceCollection AddDriveService()
{
services.AddGrpcClientWithSharedChannel<FileService.FileServiceClient>(