Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom W 39 posts 96 karma points
    Sep 29, 2020 @ 09:21
    Tom W
    0

    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?

    Cheers,

    Tom

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Sep 29, 2020 @ 09:28
    Matt Brailsford
    0

    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

  • Tom W 39 posts 96 karma points
    Sep 29, 2020 @ 10:13
    Tom W
    101

    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;
            }
        }
    

    ... just in case it helps anyone else.

    Cheers Matt,

    Tom

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Oct 02, 2020 @ 08:24
    Matt Brailsford
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft