private static void Update(TransactionResult item, Func <string, string> getter)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (getter == null)
            {
                throw new ArgumentNullException("getter");
            }

            item.Alias            = getter("ALIAS");
            item.CardCode         = getter("CARDCODE");
            item.CardCountry      = getter("CARDCOUNTRY");
            item.CardFullName     = getter("CARDFULLNAME");
            item.CardType         = getter("CARDTYPE");
            item.CardValidityDate = getter("CARDVALIDITYDATE");
            item.ClientEmail      = getter("CLIENTEMAIL");
            item.ClientIdent      = getter("CLIENTIDENT");
            item.Currency         = getter("CURRENCY");
            item.Descriptor       = getter("DESCRIPTOR");
            item.ExecCode         = getter("EXECCODE");
            item.ExtraData        = getter("EXTRADATA");
            item.Hash             = getter("HASH");
            item.Identifier       = getter("IDENTIFIER");
            item.Is3DSecure       = getter("3DSECURE");
            item.Language         = getter("LANGUAGE");
            item.Message          = getter("MESSAGE");
            item.OperationType    = getter("OPERATIONTYPE");
            item.OrderId          = getter("ORDERID");
            item.TransactionId    = getter("TRANSACTIONID");
            item.Version          = getter("VERSION");

            {
                decimal value;
                if (decimal.TryParse(getter("AMOUNT"), out value))
                {
                    item.AmountCents = value;
                }
            }
        }
示例#2
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);
        }