Try your search with a different keyword or use * as a wildcard.
using Nop.Data;
using Nop.Plugin.Payments.PayPalCommerce.Domain;
namespace Nop.Plugin.Payments.PayPalCommerce.Services;
///
/// Represents the payment token service
///
public class PayPalTokenService
{
#region Fields
private readonly IRepository _tokenRepository;
#endregion
#region Ctor
public PayPalTokenService(IRepository tokenRepository)
{
_tokenRepository = tokenRepository;
}
#endregion
#region Methods
///
/// Get all payment tokens
///
/// Client identifier
/// Customer identifier; pass 0 to load all tokens
/// Vault identifier; pass null to load all tokens
/// Vault customer identifier; pass null to load all tokens
/// Token type; pass null to load all tokens
///
/// A task that represents the asynchronous operation
/// The task result contains the list of payment tokens
///
public async Task> GetAllTokensAsync(string clientId, int customerId = 0,
string vaultId = null, string vaultCustomerId = null, string type = null)
{
return await _tokenRepository.GetAllAsync(query =>
{
query = query.Where(token => token.ClientId == clientId);
if (customerId > 0)
query = query.Where(token => token.CustomerId == customerId);
if (!string.IsNullOrEmpty(vaultId))
query = query.Where(token => token.VaultId == vaultId);
if (!string.IsNullOrEmpty(vaultCustomerId))
query = query.Where(token => token.VaultCustomerId == vaultCustomerId);
if (!string.IsNullOrEmpty(type))
query = query.Where(token => token.Type == type);
return query;
}, null);
}
///
/// Get a customer payment token
///
/// Client identifier
/// Customer identifier; pass 0 to load all tokens
/// Vault identifier; pass null to load all tokens
///
/// A task that represents the asynchronous operation
/// The task result contains the payment token
///
public async Task GetTokenAsync(string clientId, int customerId = 0, string vaultId = null)
{
var tokens = await GetAllTokensAsync(clientId, customerId, vaultId);
return tokens.FirstOrDefault();
}
///
/// Get a payment token by identifier
///
/// Token identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the payment token
///
public async Task GetByIdAsync(int id)
{
return await _tokenRepository.GetByIdAsync(id, null);
}
///
/// Insert the payment token
///
/// Payment token
/// A task that represents the asynchronous operation
public async Task InsertAsync(PayPalToken token)
{
//whether a token with such parameters already exists
var tokens = await GetAllTokensAsync(token.ClientId, token.CustomerId);
var existingToken = tokens
.FirstOrDefault(existing => existing.VaultId == token.VaultId && existing.VaultCustomerId == token.VaultCustomerId);
if (existingToken is not null)
{
//then just update transaction id
if (!string.Equals(existingToken.TransactionId, token.TransactionId, StringComparison.InvariantCultureIgnoreCase))
{
existingToken.TransactionId = token.TransactionId;
await UpdateAsync(existingToken);
}
return;
}
//don't insert tokens with no customer identifier
if (token.CustomerId == 0)
return;
if (!tokens.Any())
token.IsPrimaryMethod = true;
//or insert a new one
await _tokenRepository.InsertAsync(token, false);
}
///
/// Update the payment token
///
/// Payment token
/// A task that represents the asynchronous operation
public async Task UpdateAsync(PayPalToken token)
{
await _tokenRepository.UpdateAsync(token, false);
}
///
/// Delete the payment token
///
/// Payment token
/// A task that represents the asynchronous operation
public async Task DeleteAsync(PayPalToken token)
{
await _tokenRepository.DeleteAsync(token, false);
//mark one of the remaining tokens as default
var tokens = await GetAllTokensAsync(token.ClientId, token.CustomerId);
if (tokens.Any(existing => existing.IsPrimaryMethod))
return;
var existingToken = tokens.FirstOrDefault();
if (existingToken is null)
return;
existingToken.IsPrimaryMethod = true;
await UpdateAsync(existingToken);
}
///
/// Delete payment tokens
///
/// Payment tokens
/// A task that represents the asynchronous operation
public async Task DeleteAsync(IList tokens)
{
await _tokenRepository.DeleteAsync(tokens, false);
}
#endregion
}