/// <summary>
        /// Confirm a newly-created subscription, pre-authorzation or one-off
        /// payment. This method also checks that the resource response data includes
        /// a valid signature and will throw a {SignatureException} if the signature is
        /// invalid.
        /// </summary>
        /// <param name="requestContent">the response parameters returned by the API server</param>
        /// <returns>the confirmed resource object</returns>
        public ConfirmResource ConfirmResource(NameValueCollection requestContent)
        {
            var resource = new ConfirmResource
                               {
                                   ResourceId = requestContent["resource_id"],
                                   ResourceType = requestContent["resource_type"],
                                   ResourceUri = requestContent["resource_uri"],
                                   Signature = requestContent["signature"],
                                   State = requestContent["state"],
                               };

            if (resource.ResourceId == null) throw new ArgumentNullException("ResourceId");
            if (resource.ResourceType == null) throw new ArgumentNullException("ResourceType");
            if (resource.ResourceUri == null) throw new ArgumentNullException("ResourceUri");
            if (resource.Signature == null) throw new ArgumentNullException("Signature");

            var signature = resource.Signature;
            resource.Signature = null;

            if (signature != Utils.GetSignatureForParams(resource.ToHashParams(), GoCardless.AccountDetails.AppSecret))
            {
                throw new SignatureException("An invalid signature was detected");
            }

            var request = new RestRequest("confirm", Method.POST);
            request.RequestFormat = DataFormat.Json;
            request.AddBody(
                new
                    {
                        resource_id = resource.ResourceId,
                        resource_type = resource.ResourceType
                    });

            var client = new RestClient
                             {
                                 BaseUrl = ApiClient.ApiUrl,
                                 UserAgent = GoCardless.UserAgent
                             };
            var serializer = new JsonSerializer
            {
                ContractResolver = new UnderscoreToCamelCasePropertyResolver(),
            };
            client.AddHandler("application/json", new NewtonsoftJsonDeserializer(serializer));
            client.Authenticator = new HttpBasicAuthenticator(GoCardless.AccountDetails.AppId, GoCardless.AccountDetails.AppSecret);
            var response = client.Execute(request);
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new ApiException("Unexpected response : " + (int)response.StatusCode + " " + response.StatusCode);
            }

            return resource;
        }
Beispiel #2
0
        /// <summary>
        /// Validate the signature of a specified <c>ConfirmResource</c>.
        /// This method is visible for testing and should not be called by client applications.
        /// </summary>
        internal ConfirmResource DeserializeAndValidateRequestSignature(NameValueCollection requestContent)
        {
            var resource = new ConfirmResource
            {
                ResourceId   = requestContent["resource_id"],
                ResourceType = requestContent["resource_type"],
                ResourceUri  = Uri.UnescapeDataString(requestContent["resource_uri"]),
                Signature    = requestContent["signature"],
                State        = requestContent["state"],
            };

            if (resource.ResourceId == null)
            {
                throw new ArgumentNullException("ResourceId");
            }
            if (resource.ResourceType == null)
            {
                throw new ArgumentNullException("ResourceType");
            }
            if (resource.ResourceUri == null)
            {
                throw new ArgumentNullException("ResourceUri");
            }
            if (resource.Signature == null)
            {
                throw new ArgumentNullException("Signature");
            }

            var signature = resource.Signature;

            resource.Signature = null;

            if (signature != Utils.GetSignatureForParams(resource.ToHashParams(), GoCardless.AccountDetails.AppSecret))
            {
                throw new SignatureException("An invalid signature was detected");
            }

            return(resource);
        }
        /// <summary>
        /// Validate the signature of a specified <c>ConfirmResource</c>.
        /// This method is visible for testing and should not be called by client applications.
        /// </summary>
        internal ConfirmResource DeserializeAndValidateRequestSignature(NameValueCollection requestContent)
        {
            var resource = new ConfirmResource
            {
                ResourceId = requestContent["resource_id"],
                ResourceType = requestContent["resource_type"],
                ResourceUri = Uri.UnescapeDataString(requestContent["resource_uri"]),
                Signature = requestContent["signature"],
                State = requestContent["state"],
            };

            if (resource.ResourceId == null) throw new ArgumentNullException("ResourceId");
            if (resource.ResourceType == null) throw new ArgumentNullException("ResourceType");
            if (resource.ResourceUri == null) throw new ArgumentNullException("ResourceUri");
            if (resource.Signature == null) throw new ArgumentNullException("Signature");

            var signature = resource.Signature;
            resource.Signature = null;

            if (signature != Utils.GetSignatureForParams(resource.ToHashParams(), GoCardless.AccountDetails.AppSecret))
            {
                throw new SignatureException("An invalid signature was detected");
            }

            return resource;
        }
        /// <summary>
        /// Confirm a newly-created subscription, pre-authorzation or one-off
        /// payment. This method also checks that the resource response data includes
        /// a valid signature and will throw a {SignatureException} if the signature is
        /// invalid.
        /// </summary>
        /// <param name="requestContent">the response parameters returned by the API server</param>
        /// <returns>the confirmed resource object</returns>
        public ConfirmResource ConfirmResource(NameValueCollection requestContent)
        {
            var resource = new ConfirmResource
            {
                ResourceId   = requestContent["resource_id"],
                ResourceType = requestContent["resource_type"],
                ResourceUri  = requestContent["resource_uri"],
                Signature    = requestContent["signature"],
                State        = requestContent["state"],
            };

            if (resource.ResourceId == null)
            {
                throw new ArgumentNullException("ResourceId");
            }

            if (resource.ResourceType == null)
            {
                throw new ArgumentNullException("ResourceType");
            }

            if (resource.ResourceUri == null)
            {
                throw new ArgumentNullException("ResourceUri");
            }

            if (resource.Signature == null)
            {
                throw new ArgumentNullException("Signature");
            }

            var signature = resource.Signature;

            resource.Signature = null;

            if (signature != Utils.GetSignatureForParams(resource.ToHashParams(), GoCardless.AccountDetails.AppSecret))
            {
                throw new SignatureException("An invalid signature was detected");
            }

            var request = new RestRequest("confirm", Method.POST);

            request.RequestFormat = DataFormat.Json;
            request.AddBody(
                new
            {
                resource_id   = resource.ResourceId,
                resource_type = resource.ResourceType
            });

            var client = new RestClient
            {
                BaseUrl   = ApiClient.ApiUrl,
                UserAgent = GoCardless.UserAgent
            };
            var serializer = new JsonSerializer
            {
                ContractResolver = new UnderscoreToCamelCasePropertyResolver(),
            };

            client.AddHandler("application/json", new NewtonsoftJsonDeserializer(serializer));
            client.Authenticator = new HttpBasicAuthenticator(GoCardless.AccountDetails.AppId, GoCardless.AccountDetails.AppSecret);
            var response = client.Execute(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new ApiException("Unexpected response : " + (int)response.StatusCode + " " + response.StatusCode);
            }

            return(resource);
        }