コード例 #1
0
        /// <summary>
        /// Saves the provider settings.
        /// </summary>
        /// <param name="record">
        /// The record.
        /// </param>
        /// <param name="provider">
        /// The provider.
        /// </param>
        /// <param name="settings">
        /// The settings.
        /// </param>
        internal static void SaveProviderSettings(this IGatewayProviderSettings record, PaymentGatewayProviderBase provider, IPaymentProviderSettings settings)
        {
            var att = provider.ProviderSettingsMapping();

            if (att == null)
            {
                return;
            }

            if (settings.GetType() == att.SettingsType)
            {
                var jsonSerializerSettings = new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                };
                var json = JsonConvert.SerializeObject(settings, Formatting.None, jsonSerializerSettings);
                record.ExtendedData.SetValue(att.Key, json);
            }
        }
コード例 #2
0
        /// <summary>
        /// Gets the provider settings.
        /// </summary>
        /// <param name="provider">
        /// The provider.
        /// </param>
        /// <returns>
        /// The <see cref="IPaymentProviderSettings"/>.
        /// </returns>
        /// <exception cref="Exception">
        /// Throws an exception if the ProviderSettingsResolver is not initialize
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Throw an exception if provider passed is not decorated with the ProviderSettingsMapperAttribute
        /// </exception>
        /// <exception cref="NullReferenceException">
        /// Throws an exception if the default settings cannot be instantiated
        /// </exception>
        private static IPaymentProviderSettings GetProviderSettings(this PaymentGatewayProviderBase provider)
        {
            var logData = MultiLogger.GetBaseLoggingData();
            var att     = provider.ProviderSettingsMapping();

            if (att == null)
            {
                var invalidOp = new InvalidOperationException("Cannot use this method if the provider is not decorated with a ProviderSettingsMapperAttribute");
                MultiLogHelper.Error(typeof(ProviderSettingsExtensions), "PaymentGatewayProvider does not have attribute", invalidOp, logData);
                throw invalidOp;
            }

            if (provider.ExtendedData.ContainsKey(att.Key))
            {
                try
                {
                    return((IPaymentProviderSettings)JsonConvert.DeserializeObject(provider.ExtendedData.GetValue(att.Key), att.SettingsType));
                }
                catch (Exception ex)
                {
                    LogHelper.Error(typeof(ProviderSettingsExtensions), "Failed to deserialize provider settings attempting to return default settings.", ex);
                }
            }

            var attempt = ProviderSettingsResolver.Current.ResolveByType(provider.GetType());

            if (attempt.Success)
            {
                return(attempt.Result);
            }

            var nullReference = new NullReferenceException("Provider settings for provider was not mapped");

            MultiLogHelper.Error(
                typeof(ProviderSettingsExtensions),
                "Failed to create default provider settings",
                nullReference,
                logData);

            throw nullReference;
        }