/// <summary>
        /// Add a signature to a Hash of parameters. The signature will be generated
        /// from the app secret and the provided parameters, and should be used
        /// whenever signed data needs to be sent to GoCardless (e.g. when creating
        /// a new subscription). The signature will be added to the hash under the
        /// key signature.
        /// </summary>
        /// <param name="params">the parameters to sign</param>
        /// <returns>the parameters with the new signature key</returns>
        private static Utils.HashParams SignParams(Utils.HashParams @params)
        {
            var signature = Utils.GetSignatureForParams(@params, GoCardless.AccountDetails.AppSecret);

            @params.Add("signature", signature);
            return(@params);
        }
        /// <summary>
        /// Generate the URL for creating a limit, including the
        /// provided params, nonce, timestamp and signature
        /// </summary>
        /// <param name="type">the limit type (subscription, etc)</param>
        /// <param name="requestResource">the request values</param>
        /// <param name="redirectUri">optional override URI on success</param>
        /// <param name="cancelUri">optional override URI on cancel</param>
        /// <param name="state">optional state, gets passed back with the successful payload</param>
        /// <returns>the generated URL</returns>
        private static string GenerateNewLimitUrl(string type, object requestResource, string redirectUri = null, string cancelUri = null, string state = null)
        {
            var hash = new Utils.HashParams
            {
                { "client_id", GoCardless.AccountDetails.AppId },
                { "nonce", GoCardless.GenerateNonce() },
                { "timestamp", GoCardless.GetUtcNow().IsoFormatTime() },
            };

            hash = requestResource.ToHashParams(hash, type);

            if (redirectUri != null)
            {
                hash.Add("redirect_uri", redirectUri);
            }

            if (cancelUri != null)
            {
                hash.Add("cancel_uri", cancelUri);
            }

            if (state != null)
            {
                hash.Add("state", state);
            }

            hash = SignParams(hash);

            var url = GoCardless.BaseUrl + "/connect/" + type + "s/new?" + hash.ToQueryString();

            return(url);
        }
        public MerchantAccessTokenResponse ParseCreateMerchantResponse(string redirectUri, string code)
        {
            var hash = new Utils.HashParams
                           {
                               {"client_id", GoCardless.AccountDetails.AppId},
                               {"redirect_uri", redirectUri},
                               {"code", code},
                               {"grant_type", "authorization_code"},
                           };
            var tokenUrl = "oauth/access_token?" + hash.ToQueryString();

            var client = new RestClient
                             {
                                 BaseUrl = GoCardless.BaseUrl,
                                 UserAgent = GoCardless.UserAgent
                             };
            client.Authenticator = new HttpBasicAuthenticator(GoCardless.AccountDetails.AppId, GoCardless.AccountDetails.AppSecret);

            var restRequest = new RestRequest(tokenUrl);
            restRequest.RequestFormat = DataFormat.Json;
            restRequest.Method = Method.POST;

            var response = client.Execute<MerchantAccessTokenResponse>(restRequest);
            if (response.StatusCode == HttpStatusCode.OK)
            {
                return response.Data;
            }
            else
            {
                throw new ApiException("Invalid response getting access token from " + tokenUrl)
                {
                    Content = JObject.Parse(response.Content)
                };
            }
        }
Beispiel #4
0
        public MerchantAccessTokenResponse ParseCreateMerchantResponse(string redirectUri, string code)
        {
            var hash = new Utils.HashParams
            {
                { "client_id", GoCardless.AccountDetails.AppId },
                { "redirect_uri", redirectUri },
                { "code", code },
                { "grant_type", "authorization_code" },
            };
            var tokenUrl = "oauth/access_token?" + hash.ToQueryString();

            var client = new RestClient
            {
                BaseUrl   = GoCardless.BaseUrl,
                UserAgent = GoCardless.UserAgent
            };

            client.Authenticator = new HttpBasicAuthenticator(GoCardless.AccountDetails.AppId, GoCardless.AccountDetails.AppSecret);

            var restRequest = new RestRequest(tokenUrl);

            restRequest.RequestFormat = DataFormat.Json;
            restRequest.Method        = Method.POST;

            var response = client.Execute <MerchantAccessTokenResponse>(restRequest);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                return(response.Data);
            }
            else
            {
                throw new ApiException("Invalid response getting access token from " + tokenUrl)
                      {
                          Content = JObject.Parse(response.Content)
                      };
            }
        }
        /// <summary>
        /// Generate the URL for creating a limit, including the
        /// provided params, nonce, timestamp and signature
        /// </summary>
        /// <param name="type">the limit type (subscription, etc)</param>
        /// <param name="requestResource">the request values</param>
        /// <param name="redirectUri">optional override URI on success</param>
        /// <param name="cancelUri">optional override URI on cancel</param>
        /// <param name="state">optional state, gets passed back with the successful payload</param>
        /// <returns>the generated URL</returns>
        private static string GenerateNewLimitUrl(string type, object requestResource,
            string redirectUri = null, string cancelUri = null, string state = null)
        {
            var hash = new Utils.HashParams
                           {
                               {"client_id", GoCardless.AccountDetails.AppId},
                               {"nonce", GoCardless.GenerateNonce()},
                               {"timestamp", GoCardless.GetUtcNow().IsoFormatTime()},
                           };

            hash = requestResource.ToHashParams(hash, type);

            if (redirectUri != null)
            {
                hash.Add("redirect_uri", redirectUri);
            }
            if (cancelUri != null)
            {
                hash.Add("cancel_uri", cancelUri);
            }
            if (state != null)
            {
                hash.Add("state", state);
            }

            hash = SignParams(hash);

            var url = GoCardless.BaseUrl + "/connect/" + type + "s/new?" + hash.ToQueryString();
            return url;
        }