public Uri GenerateLaunchURL()
        {
            Uri uri = new Uri(CCTERMINAL_BASE_URL);
            Dictionary <string, string> parameters = this.GenerateParams();

            return(ChargeUtils.UriWithAdditionalParams(uri, parameters));
        }
        public string CreateAndStoreNonce()
        {
            string nonce = ChargeUtils.GenerateNonce();

            ChargeUtils.SaveLocalData(ChargeResponse.Keys.NONCE, nonce);
            return(nonce);
        }
Exemple #3
0
        private const int NONCE_LENGTH      = 27; // same size as base64-encoded SHA1 seems good

        public static string GenerateNonce()
        {
            var nonceString = new StringBuilder();

            for (int i = 0; i < NONCE_LENGTH; i++)
            {
                uint position = ChargeUtils.GenerateRandomNumber() % (uint)NONCE_ALPHABET.Length;
                nonceString.Append(NONCE_ALPHABET[(int)position]);
            }

            return(nonceString.ToString());
        }
        public void SetReturnURL(string returnURL, Dictionary <string, string> extraParams)
        {
            Uri uri = new Uri(returnURL);

            // genereate nonce and add it to extra params
            if (null == extraParams)
            {
                extraParams = new Dictionary <string, string>();
            }
            string nonce = CreateAndStoreNonce();

            extraParams.Add(ChargeResponse.Keys.NONCE, nonce);

            this.ReturnURL = ChargeUtils.UriWithAdditionalParams(uri, extraParams).ToString();
        }
        private void ValidateNonce(string nonce)
        {
            // Validate nonce
            string savedNonce = (string)ChargeUtils.RetrieveLocalData(Keys.NONCE);
            if (String.IsNullOrEmpty(savedNonce))
            {
                throw new ChargeException("No nonce was saved.");
            }

            if (!nonce.Equals(savedNonce, StringComparison.Ordinal))
            {
                throw new ChargeException("Nonce doesn't match.");
            }

            // Nonce validated, clear saved nonce
            ChargeUtils.DeleteLocalData(Keys.NONCE);
        }
Exemple #6
0
        public static Uri UriWithAdditionalParams(Uri uri, Dictionary <string, string> newParameters)
        {
            Dictionary <string, string> parameters;

            if (String.IsNullOrEmpty(uri.Query))
            {
                parameters = newParameters;
            }
            else
            {
                // extract existing query string parameters
                parameters = ChargeUtils.ParseQueryString(uri.Query);

                // add new parameters -- new parameters will overwrite old ones if key already exists
                foreach (var param in newParameters)
                {
                    parameters[param.Key] = param.Value;
                }
            }

            if (parameters.Count == 0)
            {
                // no parameters = no query string.
                return(uri);
            }

            // loop through parameters to generate param list
            List <string> paramList = new List <string>();

            foreach (var param in parameters)
            {
                if (param.Value != null)
                {
                    paramList.Add(
                        String.Format(
                            CultureInfo.InvariantCulture,
                            "{0}={1}",
                            Uri.EscapeDataString(param.Key),
                            Uri.EscapeDataString(param.Value)
                            )
                        );
                }
            }

            // join param list with & and prefix it with ?
            string queryString = String.Join("&", paramList);

            queryString = String.Format(CultureInfo.InvariantCulture, "?{0}", queryString);

            String uriString = null;

            if (uri.IsDefaultPort)
            {
                uriString = String.Format(
                    CultureInfo.InvariantCulture,
                    "{0}://{1}{2}{3}",
                    uri.Scheme,
                    uri.Host,
                    uri.AbsolutePath,
                    queryString);
            }
            else
            {
                uriString = String.Format(
                    CultureInfo.InvariantCulture,
                    "{0}://{1}:{2}{3}{4}",
                    uri.Scheme,
                    uri.Host,
                    uri.Port,
                    uri.AbsolutePath,
                    queryString);
            }

            return(new Uri(uriString));
        }
        public ChargeResponse(Uri responseUri)
        {
            if (null == responseUri)
            {
                throw new ArgumentNullException("uri");
            }

            string query = responseUri.Query;
            if (String.IsNullOrEmpty(query))
            {
                throw new ChargeException("Invalid Request: Query string is empty");
            }

            Dictionary<string, string> parameters = ChargeUtils.ParseQueryString(query);
            if (parameters.Count == 0)
            {
                throw new ChargeException("Invalid Request: Query string has no params");
            }

            // Validate nonce
            if (!parameters.ContainsKey(Keys.NONCE) || String.IsNullOrEmpty(parameters[Keys.NONCE]))
            {
                throw new ChargeException("No nonce was provided.");
            }
            string nonce = parameters[Keys.NONCE];
            this.ValidateNonce(nonce);

            // Parse parameters
            if (parameters.ContainsKey(Keys.AMOUNT))
            {
                this.Amount = parameters[Keys.AMOUNT];
            }
            if (parameters.ContainsKey(Keys.CARD_TYPE))
            {
                this.CardType = parameters[Keys.CARD_TYPE];
            }
            if (parameters.ContainsKey(Keys.CURRENCY))
            {
                this.Currency = parameters[Keys.CURRENCY];
            }
            if (parameters.ContainsKey(Keys.ERROR_MESSAGE))
            {
                this.ErrorMessage = parameters[Keys.ERROR_MESSAGE];
            }
            if (parameters.ContainsKey(Keys.REDACTED_CARD_NUMBER))
            {
                this.RedactedCardNumber = parameters[Keys.REDACTED_CARD_NUMBER];
            }
            if (parameters.ContainsKey(Keys.RESPONSE_TYPE))
            {
                this.ResponseType = parameters[Keys.RESPONSE_TYPE];
            }
            if (parameters.ContainsKey(Keys.TAX_AMOUNT))
            {
                this.TaxAmount = parameters[Keys.TAX_AMOUNT];
            }
            if (parameters.ContainsKey(Keys.TAX_RATE))
            {
                this.TaxRate = parameters[Keys.TAX_RATE];
            }
            if (parameters.ContainsKey(Keys.TIP_AMOUNT))
            {
                this.TipAmount = parameters[Keys.TIP_AMOUNT];
            }
            if (parameters.ContainsKey(Keys.TRANSACTION_ID))
            {
                this.TransactionId = parameters[Keys.TRANSACTION_ID];
            }

            this.ValidateFields();

            if (this.ResponseType == Type.APPROVED)
            {
                this.ResponseCode = Code.APPROVED;
            }
            else if (this.ResponseType == Type.CANCELLED)
            {
                this.ResponseCode = Code.CANCELLED;
            }
            else if (this.ResponseType == Type.DECLINED)
            {
                this.ResponseCode = Code.DECLINED;
            }
            else
            {
                this.ResponseCode = Code.ERROR;
            }

            this.ExtraParams = new Dictionary<string, string>();
            foreach (var parameter in parameters)
            {
                if (!parameter.Key.StartsWith("ifcc_", StringComparison.Ordinal))
                {
                    this.ExtraParams[parameter.Key] = parameter.Value;
                }
            }
        }