Ejemplo n.º 1
0
        /// <summary>
        /// Queries an order from Konduto's API.
        /// Syncronous
        /// @see <a href="http://docs.konduto.com">Konduto API Spec</a>
        /// </summary>
        /// <param name="orderId">the order identifier</param>
        /// <returns>a {@link KondutoOrder} instance</returns>
        /// <exception cref="KondutoHTTPException"></exception>
        /// <exception cref="KondutoUnexpectedAPIResponseException"></exception>
        public KondutoOrder GetOrder(String orderId)
        {
            HttpClient client = CreateHttpClient();

            this.requestBody = orderId;

            var response = client.GetAsync(KondutoGetOrderSuffix(orderId)).Result;

            if (response.IsSuccessStatusCode)
            {
                var responseContent = response.Content;

                String responseString = responseContent.ReadAsStringAsync().Result;
                this.responseBody = responseString;

                JObject getResponse = JsonConvert.DeserializeObject <JObject>(responseString);

                KondutoOrder order = null;

                JToken jt;
                if (getResponse.TryGetValue("order", out jt))
                {
                    order = KondutoModel.FromJson <KondutoOrder>(jt.ToString());
                }

                return(order);
            }
            else
            {
                throw KondutoHTTPExceptionFactory.buildException((int)response.StatusCode, response.Content.ReadAsStringAsync().Result);
            }
        }
        public void BuildExceptionTest()
        {
            foreach (var entry in HTTP_STATUSES)
            {
                int    statusCode   = entry.Key;
                var    klass        = entry.Value;
                String responseBody = "{\"statusCode\": \"" + statusCode + "\"}";
                var    exception    = KondutoHTTPExceptionFactory.buildException(statusCode, responseBody);

                Assert.IsTrue(klass == exception.GetType());
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sends an order for Konduto and gets it analyzed
        /// (i.e with recommendation, score, device, geolocation and navigation info).
        /// @see <a href="http://docs.konduto.com">Konduto API Spec</a>
        /// </summary>
        /// <param name="order">a {@link KondutoOrder} instance</param>
        /// <exception cref="KondutoInvalidEntityException"></exception>
        /// <exception cref="KondutoHTTPException"></exception>
        /// <exception cref="KondutoUnexpectedAPIResponseException"></exception>
        public KondutoOrder Analyze(KondutoOrder order)
        {
            HttpClient httpClient = CreateHttpClient();

            var x = order.ToJson();

            var response = httpClient.PostAsync(KondutoPostOrderUrl(),
                                                new StringContent(order.ToJson(),
                                                                  Encoding.UTF8,
                                                                  "application/json"));

            this.requestBody = order.ToJson();

            if (response.Result.IsSuccessStatusCode)
            {
                // by calling. Result you are performing a synchronous call
                var responseContent = response.Result.Content;

                // by calling. Result you are synchronously reading the result
                String responseString = responseContent.ReadAsStringAsync().Result;

                this.responseBody = responseString;

                if (order.Analyze)
                {
                    order.MergeKondutoOrderResponse(KondutoAPIFullResponse.FromJson <KondutoAPIFullResponse>(responseString).Order);
                }

                return(order);
            }
            else
            {
                var responseContentError = response.Result.Content != null?response.Result.Content.ReadAsStringAsync().Result : "Error with response";

                throw KondutoHTTPExceptionFactory.buildException((int)response.Result.StatusCode,
                                                                 responseContentError);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Updates an order status.
        /// @see <a href="http://docs.konduto.com">Konduto API Spec</a>
        /// </summary>
        /// <param name="order">the order to update</param>
        /// <param name="newStatus">the new status (APPROVED, DECLINED or FRAUD)</param>
        /// <param name="comments">some comments (an empty String is accepted, although we recommend setting it to at least a timestamp)</param>
        /// <exception cref="KondutoHTTPException"></exception>
        /// <exception cref="KondutoUnexpectedAPIResponseException"></exception>
        public void UpdateOrderStatus(String orderId, KondutoOrderStatus newStatus, String comments)
        {
            HashSet <KondutoOrderStatus> allowed = new HashSet <KondutoOrderStatus>
            {
                KondutoOrderStatus.approved,
                KondutoOrderStatus.declined,
                KondutoOrderStatus.fraud,
                KondutoOrderStatus.canceled,
                KondutoOrderStatus.not_authorized
            };

            if (!allowed.Contains(newStatus))
            {
                throw new ArgumentException("Illegal status: " + newStatus);
            }

            if (comments == null)
            {
                throw new NullReferenceException("Comments cannot be null.");
            }

            JObject requestBody = new JObject();

            requestBody.Add("status", newStatus.ToString().ToLower());
            requestBody.Add("comments", comments);

            HttpClient client = CreateHttpClient();

            var response = client.PutAsync(KondutoPutOrderUrl(orderId),
                                           new StringContent(
                                               requestBody.ToString(),
                                               Encoding.UTF8,
                                               "application/json"));

            if (response.Result.IsSuccessStatusCode)
            {
                JObject responseBody = JsonConvert.DeserializeObject <JObject>(
                    response.Result.Content.ReadAsStringAsync().Result);

                this.responseBody = responseBody.ToString();

                JToken orderUpdateResponse;

                if (responseBody.TryGetValue("order", out orderUpdateResponse))
                {
                    JObject updatedOrder = JsonConvert.DeserializeObject <JObject>(orderUpdateResponse.ToString());

                    JToken statusResponse;

                    if (!updatedOrder.TryGetValue("old_status", out statusResponse) ||
                        !updatedOrder.TryGetValue("new_status", out statusResponse))
                    {
                        throw new KondutoUnexpectedAPIResponseException(responseBody.ToString());
                    }
                }
            }
            else
            {
                throw KondutoHTTPExceptionFactory.buildException((int)response.Result.StatusCode,
                                                                 response.Result.Content.ReadAsStringAsync().Result);
            }
        }