using System;
namespace IOL.VippsEcommerce.Models
{
///
/// Configuration fields for the vipps api and integration.
///
public class VippsConfiguration
{
///
/// Url for the vipps api.
/// https://apitest.vipps.no
/// https://api.vipps.no
/// Corresponding environment variable name: VIPPS_API_URL
///
[VippsConfigurationKeyName(VippsConfigurationKeyNames.VIPPS_API_URL)]
public string ApiUrl { get; set; }
///
/// Client ID for the merchant (the "username")
/// Corresponding environment variable name: VIPPS_CLIENT_ID
///
[VippsConfigurationKeyName(VippsConfigurationKeyNames.VIPPS_CLIENT_ID)]
public string ClientId { get; set; }
///
/// Client Secret for the merchant (the "password")
/// Corresponding environment variable name: VIPPS_CLIENT_SECRET
///
[VippsConfigurationKeyName(VippsConfigurationKeyNames.VIPPS_CLIENT_SECRET)]
public string ClientSecret { get; set; }
///
/// Primary subscription key for the API product. The primary subscription key take precedence over the secondary subscription key.
/// Corresponding environment variable name: VIPPS_SUBSCRIPTION_KEY_PRIMARY
///
[VippsConfigurationKeyName(VippsConfigurationKeyNames.VIPPS_SUBSCRIPTION_KEY_PRIMARY)]
public string PrimarySubscriptionKey { get; set; }
///
/// Secondary subscription key for the API product. The primary subscription key take precedence over the secondary subscription key.
/// Corresponding environment variable name: VIPPS_SUBSCRIPTION_KEY_SECONDARY
///
[VippsConfigurationKeyName(VippsConfigurationKeyNames.VIPPS_SUBSCRIPTION_KEY_SECONDARY)]
public string SecondarySubscriptionKey { get; set; }
///
/// The Merchant Serial Number (MSN) is a unique id for the sale unit that this payment is made for.
/// Corresponding environment variable name: VIPPS_MSN
///
[VippsConfigurationKeyName(VippsConfigurationKeyNames.VIPPS_MSN)]
public string MerchantSerialNumber { get; set; }
///
/// The name of the ecommerce solution. One word in lowercase letters is good.
/// Corresponding environment variable name: VIPPS_SYSTEM_NAME
///
[VippsConfigurationKeyName(VippsConfigurationKeyNames.VIPPS_SYSTEM_NAME)]
public string SystemName { get; set; }
///
/// The version number of the ecommerce solution.
/// Corresponding environment variable name: VIPPS_SYSTEM_VERSION
///
[VippsConfigurationKeyName(VippsConfigurationKeyNames.VIPPS_SYSTEM_VERSION)]
public string SystemVersion { get; set; }
///
/// The name of the ecommerce plugin (if applicable). One word in lowercase letters is good.
/// Corresponding environment variable name: VIPPS_SYSTEM_PLUGIN_NAME
///
[VippsConfigurationKeyName(VippsConfigurationKeyNames.VIPPS_SYSTEM_PLUGIN_NAME)]
public string SystemPluginName { get; set; }
///
/// The version number of the ecommerce plugin (if applicable).
/// Corresponding environment variable name: VIPPS_SYSTEM_PLUGIN_VERSION
///
[VippsConfigurationKeyName(VippsConfigurationKeyNames.VIPPS_SYSTEM_PLUGIN_VERSION)]
public string SystemPluginVersion { get; set; }
///
/// Optional path to a writable directory wherein a credential cache file can be placed.
/// Corresponding environment variable name: VIPPS_CACHE_PATH
///
[VippsConfigurationKeyName(VippsConfigurationKeyNames.VIPPS_CACHE_PATH)]
public string CredentialsCacheFilePath { get; set; }
///
/// Optional key for AES encryption of the credential cache file.
/// Corresponding environment variable name: VIPPS_CACHE_KEY
///
[VippsConfigurationKeyName(VippsConfigurationKeyNames.VIPPS_CACHE_KEY)]
public string CacheEncryptionKey { get; set; }
///
/// Use environment variables for configuration.
/// If this is true, all requested properties are looked for in the environment.
///
public bool UseEnvironment { get; set; }
///
/// Get value from configuration, either from Dependency injection or from the environment.
///
/// Configuration key.
/// Fallback value if the key is not found or empty.
/// A string containing the configuration value (or a fallback).
public string GetValue(string key, string fallback = default) {
if (UseEnvironment) {
return Environment.GetEnvironmentVariable(key) ?? fallback;
}
foreach (var prop in typeof(VippsConfiguration).GetProperties()) {
foreach (var attribute in prop.CustomAttributes) {
foreach (var argument in attribute.ConstructorArguments) {
if (argument.Value as string == key) {
#if DEBUG
var value = prop.GetValue(this, null)?.ToString();
Console.WriteLine("Key: " + key + " Value: " + value);
return value;
#else
return prop.GetValue(this, null)?.ToString();
#endif
}
}
}
}
return default;
}
}
}