Typed PaymentProviderSettings from PaymentProvider
Hi Matt,
A quick one, hopefully!
I see that I can get an IReadOnlyDictionary<string, string> from an instance of a payment method retrieved e.g. by Id
var method = _paymentMethodService.GetPaymentMethod(PaymentProviderId);
var settings = method.PaymentProviderSettings;
I was just wondering whether I can get this as the typed PaymentProviderSettings I set on the PaymentProvider? So as a POCO rather than a dictionary? I can't see any way to deserialize it... I suppose I could serialize the dictionary to JSON, then deserialize into the instance. Just wondering if there's a better way?
Not currently. This logic is wrapped in the PaymentProviderBase<T> class so if a payment provider uses a strongly typed model, when it is passed a dictionary of settings as stored on the payment method, it is then responsible for converting it into it's settings object. We don't currently have any means of accessing that outside of the payment provider.
We're using a payment provider called Citypay. They allow us to store cards in a wallet, with continuous auth, so that we can save the customer's cards. So, we won't be using the redirect method inherent to your payment providers, but will instead be using the newly exposed methods for starting and finalising a transaction.
Because of this, we need to be able to access some settings, like IsTestMode and TestApiURL and LicenseKey, etc, so we can call into the API outside of the PaymentProvider itself.
I could of course just use the exposed dictionary. But for now, I'm using a JsonConvertor like this, to convert "1" to true and "0" to false
private CitypayPaymentProviderSettings _settings;
public CitypayPaymentProviderSettings Settings
{
get
{
if (_settings == null)
{
var method = _paymentMethodService.GetPaymentMethod(PaymentProviderId);
_settings = JsonConvert.DeserializeObject<CitypayPaymentProviderSettings>(
JsonConvert.SerializeObject(method.PaymentProviderSettings), new StringToBooleanConverter());
}
return _settings;
}
}
where
public class StringToBooleanConverter: JsonConverter
{
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
switch ( reader.Value.ToString().ToLower().Trim() )
{
case "true":
case "yes":
case "y":
case "1":
return true;
case "false":
case "no":
case "n":
case "0":
return false;
}
return new JsonSerializer().Deserialize( reader, objectType );
}
public override bool CanConvert(Type objectType)
{
if (objectType == typeof(String) || objectType == typeof(Boolean))
{
return true;
}
return false;
}
}
Typed PaymentProviderSettings from PaymentProvider
Hi Matt,
A quick one, hopefully!
I see that I can get an
IReadOnlyDictionary<string, string>
from an instance of a payment method retrieved e.g. by IdI was just wondering whether I can get this as the typed PaymentProviderSettings I set on the PaymentProvider? So as a POCO rather than a dictionary? I can't see any way to deserialize it... I suppose I could serialize the dictionary to JSON, then deserialize into the instance. Just wondering if there's a better way?
Cheers,
Tom
Hey Tom,
Not currently. This logic is wrapped in the
PaymentProviderBase<T>
class so if a payment provider uses a strongly typed model, when it is passed a dictionary of settings as stored on the payment method, it is then responsible for converting it into it's settings object. We don't currently have any means of accessing that outside of the payment provider.What is your use case for needing this?
Matt
We're using a payment provider called Citypay. They allow us to store cards in a wallet, with continuous auth, so that we can save the customer's cards. So, we won't be using the redirect method inherent to your payment providers, but will instead be using the newly exposed methods for starting and finalising a transaction.
Because of this, we need to be able to access some settings, like
IsTestMode
andTestApiURL
andLicenseKey
, etc, so we can call into the API outside of the PaymentProvider itself.I could of course just use the exposed dictionary. But for now, I'm using a
JsonConvertor
like this, to convert "1" totrue
and "0" tofalse
where
... just in case it helps anyone else.
Cheers Matt,
Tom
Thanks Tom, that's great to clarify the use case. I'll review what could be done to make this easier, but that looks like a great workaround.
Thanks again for the feedback
Matt
is working on a reply...