public NotificationArgs(TData data, string method, string uuid, NotificationResponseDelegate onOK, NotificationFailResponseDelegate onFailed)
        {
            this.Data = data;

            this._method = method;
            this._uuid   = uuid;

            this._onOK     = onOK;
            this._onFailed = onFailed;
        }
Пример #2
0
        public int HandleNotificationFromString(string jsonString, NotificationResponseDelegate onOK = null, NotificationFailResponseDelegate onFailed = null)
        {
            var jsonToken = JToken.Parse(jsonString);

            var methodToken = jsonToken.SelectToken("$.method");
            var methodValue = methodToken.Value <string>().ToLower(CultureInfo.InvariantCulture);

            var mapper = this._methodToNotificationMapper.ContainsKey(methodValue)
                ? this._methodToNotificationMapper[methodValue]
                : this._methodToNotificationMapper[string.Empty];

            // This will then call generic method HandleNotificationFromString below.
            // We do it this way to keep the dynamic generic parameters intact.
            return(mapper(jsonString, onOK, onFailed));
        }
Пример #3
0
        private int HandleNotificationFromString <TReqData>(
            string jsonString,
            EventHandler <NotificationArgs <TReqData> > eventHandler,
            NotificationResponseDelegate onOK,
            NotificationFailResponseDelegate onFailed
            )
            where TReqData : IRequestParamsData
        {
            var rpcRequest = JsonConvert.DeserializeObject <JsonRpcRequest <TReqData> >(jsonString);

            // Verify the notification (RpcRequest from Trustly) signature.
            if (!this._signer.Verify(rpcRequest))
            {
                throw new TrustlySignatureException("Could not validate signature of notification from Trustly. Is the public key for Trustly the correct one, for test or production?");
            }

            // Validate the incoming request instance.
            // Most likely this will do nothing, since we are lenient on things sent from Trustly server.
            // But we do this in case anything is needed to be validated on the local domain classes in the future.
            this._validator.Validate(rpcRequest);

            if (eventHandler == null || eventHandler.GetInvocationList().Length == 0)
            {
                throw new TrustlyNoNotificationListenerException($"Received an incoming '{rpcRequest.Method}' notification, but there was no event listener subscribing to it");
            }

            try
            {
                eventHandler(this, new NotificationArgs <TReqData>(rpcRequest.Params.Data, rpcRequest.Method, rpcRequest.Params.UUID, onOK, onFailed));
            }
            catch (Exception ex)
            {
                var message = this.Settings.IncludeExceptionMessageInNotificationResponse ? ex.Message : null;
                onFailed(rpcRequest.Method, rpcRequest.Params.UUID, message);
            }

            return(eventHandler.GetInvocationList().Length);
        }
Пример #4
0
        public async Task <int> HandleNotificationFromRequestAsync(HttpRequest request, NotificationResponseDelegate onOK = null, NotificationFailResponseDelegate onFailed = null)
        {
            if (string.Equals(request.Method, "post", StringComparison.InvariantCultureIgnoreCase))
            {
                using (var sr = new StreamReader(request.Body))
                {
                    var requestStringBody = await sr.ReadToEndAsync();

                    return(this.HandleNotificationFromString(requestStringBody, onOK, onFailed));
                }
            }
            else
            {
                throw new TrustlyNotificationException("Notifications are only allowed to be received as a HTTP Post");
            }
        }