internal static Be2BillApiException Create(int code, string message, Exception inner, IDictionary <string, object> data, IDictionary <string, object> otherData)
        {
            var ex = new Be2BillApiException(code, message, inner);

            ex.Fill(data, otherData);
            return(ex);
        }
 internal static Be2BillApiException Create(int code, string message, Exception inner, IDictionary<string, object> data, IDictionary<string, object> otherData)
 {
     var ex = new Be2BillApiException(code, message, inner);
     ex.Fill(data, otherData);
     return ex;
 }
Exemple #3
0
        /// <summary>
        /// Call the API to pay with an alias.
        /// </summary>
        /// <param name="collection">The collection from CreateAuthorizationParameters.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// collection
        /// </exception>
        /// <exception cref="Be2BillApiException"></exception>
        public TransactionResult PayWithAlias(TransactionRequest collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException("collection");
            }

            var serializableData    = new Dictionary <string, object>();
            var nonSerializableData = new Dictionary <string, object>();

            // prepare request
            var requestModel = new Be2BillRestRequest
            {
                Method = collection[Names.Params.OperationType],
                Params = collection,
            };
            var postStringBuilder = new StringBuilder();

            postStringBuilder.Append("method=" + Uri.EscapeDataString(collection[Names.Params.OperationType]));
            foreach (var item in collection)
            {
                postStringBuilder.Append("&params[");
                postStringBuilder.Append(Uri.EscapeDataString(item.Key));
                postStringBuilder.Append("]=");
                postStringBuilder.Append(Uri.EscapeDataString(item.Value));
            }

            var postString = postStringBuilder.ToString();
            var postBytes  = Encoding.UTF8.GetBytes(postString);

            var request = (HttpWebRequest)HttpWebRequest.Create(configuration.RestUrl);

            request.ContentType   = "application/x-www-form-urlencoded; charset=UTF-8";
            request.ContentLength = postBytes.Length;
            request.Method        = "POST";
            request.UserAgent     = "Be2BillNet/1.0";
            nonSerializableData.Add("HttpRequest", request);
            serializableData.Add("HttpRequest.PostText", postString);

            ////Trace.TraceWarning(DateTime.UtcNow.ToString("o") + " Be2BillClient.PayWithAlias Request details for " + configuration.RestUrl + Environment.NewLine +
            ////    request.Method + " " + request.RequestUri.PathAndQuery + Environment.NewLine +
            ////    string.Join(Environment.NewLine, request.Headers.ToDictionary().Select(p => p.Key + ": " + p.Value)) + Environment.NewLine +
            ////    Environment.NewLine +
            ////    postString + Environment.NewLine);

            // send request
            try
            {
                var writeStream = request.GetRequestStream();
                writeStream.Write(postBytes, 0, postBytes.Length);
                writeStream.Flush();
                writeStream.Close();
            }
            catch (Exception ex)
            {
                throw Be2BillApiException.Create(1, "Failed to send API request", ex, serializableData, nonSerializableData);
            }

            // get response
            HttpWebResponse   response;
            TransactionResult result = null;

            try
            {
                response = (HttpWebResponse)request.GetResponse();
                nonSerializableData.Add("HttpResponse", response);
                var readStream = response.GetResponseStream();
                ////var reader = new StreamReader(readStream, Encoding.UTF8);
                ////json = reader.ReadToEnd();
                ////serializableData.Add("HttpResponse.Text", json);

                ////#warning REMOVE THIS DEBUGGING LINE
                ////Trace.TraceWarning(DateTime.UtcNow.ToString("o") + " Be2BillClient.PayWithAlias Response details for " + configuration.RestUrl + Environment.NewLine +
                ////    string.Join(Environment.NewLine, response.Headers.ToDictionary().Select(p => p.Key + ": " + p.Value)) + Environment.NewLine +
                ////    Environment.NewLine +
                ////    json + Environment.NewLine);

                // read response content
                try
                {
                    var serializer = new DataContractJsonSerializer(typeof(TransactionResult));
                    result = (TransactionResult)serializer.ReadObject(readStream);
                    nonSerializableData.Add("Result", result);
                }
                catch (Exception ex)
                {
                    throw Be2BillApiException.Create(2, "Failed to read API response", ex, serializableData, nonSerializableData);
                }

                // check HTTP code
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw Be2BillApiException.Create(3, "Be2Bill API returned HTTP Code " + (int)response.StatusCode + " " + response.StatusCode, null, serializableData, nonSerializableData);
                }
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;

                throw Be2BillApiException.Create(4, "Failed to get API response", ex, serializableData, nonSerializableData);
            }

            if (result == null)
            {
                throw Be2BillApiException.Create(6, "Be2Bill API returned a empty response ", null, serializableData, nonSerializableData);
            }

            serializableData.Add("Result.ExecCode", result.ExecCode);
            if (result.ExecCode == "1005")
            {
                throw Be2BillApiException.Create(7, "Be2Bill API returned EXEC Code (" + result.ExecCode + ") " + Be2BillUtility.GetNameForExecCode(result.ExecCode, CultureInfo.CurrentCulture) + " '" + result.Message + "'", null, serializableData, nonSerializableData);
            }

            return(result);
        }