Пример #1
0
        public IDictionary <string, string> Execute(string url, string method, string jsonData, string authorization, DateTime dateNow)
        {
            try
            {
                _logger.Debug(string.Format(
                                  "Start request to {0}, method: {1}, with json: {2}, authorization: {3}, dateTime: {4}",
                                  url, method, string.IsNullOrEmpty(jsonData) ? "-" : jsonData, authorization, dateNow));

                var request = WebRequest.Create(url) as HttpWebRequest;
                if (request == null)
                {
                    return(null);
                }

                request.Method      = method;
                request.UserAgent   = "puntopagos-sdk-" + Assembly.GetExecutingAssembly().GetName().Version;
                request.Accept      = "application/json";
                request.ContentType = "application/json; charset=utf-8";
                request.Headers.Add("Fecha", dateNow.ToString("r"));
                request.Headers.Add("Autorizacion", authorization);
                _logger.Debug("Add all headers");
                if (!string.IsNullOrEmpty(jsonData))
                {
                    _logger.Debug("Start to write stream json");
                    var content = GetContentByte(jsonData);
                    request.ContentLength = content.Length;

                    var dataStream = request.GetRequestStream();
                    dataStream.Write(content, 0, content.Length);
                    dataStream.Close();
                    _logger.Debug("End to write stream json, close stream");
                }
                _logger.Debug("Start to call response");
                var response = request.GetResponse() as HttpWebResponse;
                if (response == null)
                {
                    return(null);
                }
                _logger.Debug("End to call response");
                _logger.Debug("Start to read response");
                var responseDataStream = response.GetResponseStream();
                if (responseDataStream == null)
                {
                    return(null);
                }

                var reader = new StreamReader(responseDataStream);
                var json   = reader.ReadToEnd();
                _logger.Debug("End to read response, start to Deserialize Json");
                return(JsonSerializerService.DeserializeFromString(json));
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Error when execute url: {0} and method: {1}", url, method), ex);
                throw;
            }
        }
Пример #2
0
        public void given_error_json_response_from_punto_pago_when_call_json_serializer_then_return_check_transaction_response_dto()
        {
            const string json = "{\"respuesta\":\"99\",\"token\":\"9XJ08401WN0071839\",\"error\":\"Pago Rechazado\"}";

            var checkTransactionDto = new CheckTransactionResponseDto(JsonSerializerService.DeserializeFromString(json));

            Assert.IsTrue(checkTransactionDto.WithError, "WithError");
            Assert.AreEqual(checkTransactionDto.Token, "9XJ08401WN0071839", "Token");
            Assert.AreEqual(checkTransactionDto.ErrorMessage, "Pago Rechazado", "ErrorMessage");
        }
        public void given_json_response_with_error_from_punto_pago_when_call_json_serializer_then_return_transaction_response_dto_with_error()
        {
            const string json = "{\"respuesta\":\"99\",\"token\":\"9XJ08401WN0071839\",\"trx_id\":\"9787415132\"}";

            _configuration.Setup(x => x.GetProcessTransactionUrl()).Returns("/url");
            var transactionResponseDto = new CreateTransactionResponseDto(JsonSerializerService.DeserializeFromString(json), _configuration.Object);

            Assert.AreEqual("9XJ08401WN0071839", transactionResponseDto.Token);
            Assert.AreEqual(9787415132, transactionResponseDto.TransactionId);
            Assert.IsTrue(transactionResponseDto.WithError);
            Assert.IsNull(transactionResponseDto.PaymentMethod);
            Assert.IsNull(transactionResponseDto.Currency);
            Assert.IsNullOrEmpty(transactionResponseDto.ErrorMessage);
            Assert.AreEqual("/url/9XJ08401WN0071839", transactionResponseDto.ProcessUrl);
        }
Пример #4
0
        public void given_default_json_response_from_punto_pago_when_call_json_serializer_then_return_check_transaction_response_dto()
        {
            const string json =
                "{\"respuesta\":\"00\",\"token\":\"9XJ08401WN0071839\",\"trx_id\":9787415132,\"medio_pago\":\"999\",\"monto\":1000000.00,\"fecha_aprobacion\":\"2009-06-15T20:49:00\",\"numero_operacion\":\"7897851487\",\"codigo_autorizacion\":\"34581\"}";

            var checkTransactionDto = new CheckTransactionResponseDto(JsonSerializerService.DeserializeFromString(json));

            Assert.IsFalse(checkTransactionDto.WithError, "WithError");
            Assert.AreEqual(checkTransactionDto.Token, "9XJ08401WN0071839", "Token");
            Assert.AreEqual(checkTransactionDto.TransactionId, 9787415132, "TransactionId");
            Assert.IsNull(checkTransactionDto.PaymentMethod, "PaymentMethod");
            Assert.AreEqual(checkTransactionDto.Currency.Amount, 1000000, "Amount");
            Assert.AreEqual(checkTransactionDto.DateTimeAcceptance, new DateTime(2009, 6, 15, 20, 49, 00), "DateTimeAcceptance");
            Assert.AreEqual(checkTransactionDto.OperationNumber, "7897851487", "OperationNumber");
            Assert.AreEqual(checkTransactionDto.AuthorizationCode, "34581", "AuthorizationCode");
        }
Пример #5
0
        public IDictionary <string, string> GetDataFromRequest(WebRequest request)
        {
            try
            {
                _logger.Debug("Start to read request.");
                var responseDataStream = request.GetRequestStream();

                var reader = new StreamReader(responseDataStream);
                var json   = reader.ReadToEnd();
                _logger.Debug("End to read request, start to Deserialize Json");
                return(JsonSerializerService.DeserializeFromString(json));
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Error when get response from url: {0} and method: {1}", request.RequestUri, request.Method), ex);
                throw;
            }
        }