public async Task <CaptureResponse> CaptureAsync(CaptureRequest request, MerchantCredentials credentials = null)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (_credentials == null && credentials == null)
            {
                throw new InvalidOperationException("Credentials are null");
            }

            var currentCredentials = credentials ?? _credentials;

            if (string.IsNullOrWhiteSpace(currentCredentials.MerchantId))
            {
                throw new InvalidOperationException("Invalid credentials: MerchantId is null");
            }

            if (string.IsNullOrWhiteSpace(currentCredentials.MerchantKey))
            {
                throw new InvalidOperationException("Invalid credentials: MerchantKey is null");
            }

            var httpRequest = new RestRequest(@"v2/sales/{paymentId}/capture", Method.PUT)
            {
                RequestFormat = DataFormat.Json
            };

            httpRequest.AddHeader("Content-Type", "application/json");
            httpRequest.AddHeader("MerchantId", currentCredentials.MerchantId);
            httpRequest.AddHeader("MerchantKey", currentCredentials.MerchantKey);
            httpRequest.AddHeader("RequestId", Guid.NewGuid().ToString());
            httpRequest.AddUrlSegment("paymentId", request.PaymentId);
            httpRequest.AddQueryParameter("amount", request.Amount.ToString(CultureInfo.InvariantCulture));

            if (request.ServiceTaxAmount.HasValue)
            {
                httpRequest.AddQueryParameter("serviceTaxAmount", request.ServiceTaxAmount.Value.ToString(CultureInfo.InvariantCulture));
            }

            var cancellationTokenSource = new CancellationTokenSource();

            var httpResponse = await RestClientApi.ExecuteTaskAsync(httpRequest, cancellationTokenSource.Token);

            if (httpResponse.StatusCode != HttpStatusCode.OK)
            {
                return(new CaptureResponse
                {
                    HttpStatus = httpResponse.StatusCode,
                    ErrorDataCollection = JsonDeserializer.Deserialize <List <ErrorData> >(httpResponse)
                });
            }

            var jsonResponse = JsonDeserializer.Deserialize <CaptureResponse>(httpResponse);

            jsonResponse.HttpStatus = httpResponse.StatusCode;
            return(jsonResponse);
        }
예제 #2
0
        public static void Mainext()
        {
            Configuration config = new Configuration();

            config.BasePath = "https://api.mercadolibre.com";
            var apiInstance = new RestClientApi(config);
            var resource    = "items";
            var accessToken = "access_token";
            List <ItemPictures> pictures = new List <ItemPictures>();

            pictures.Add(new ItemPictures()
            {
                Source = "https://http2.mlstatic.com/storage/developers-site-cms-admin/openapi/319968615067-mp3.jpg"
            });
            List <AttributesValues> attrValues = new List <AttributesValues>();

            attrValues.Add(new AttributesValues()
            {
                Name = "8 GB",
            });
            List <Attributes> attributes = new List <Attributes>();

            attributes.Add(new Attributes()
            {
                Id                 = "DATA_STORAGE_CAPACITY",
                Name               = "Capacidad de almacenamiento de datos",
                ValueName          = "8 GB",
                Values             = attrValues,
                AttributeGroupId   = "OTHERS",
                AttributeGroupName = "Otros",
            });
            var item = new Item(
                "Item de test - No Ofertar",
                "MLA5991",
                350,
                "ARS",
                "12",
                "buy_it_now",
                "bronze",
                "new",
                "Item de Teste. Mercado Livre SDK",
                "RXWn6kftTHY",
                pictures,
                attributes
                );

            try
            {
                // Return a Item.
                apiInstance.ResourcePost(resource, accessToken, item);
            }
            catch (ApiException e)
            {
                Debug.Print("Exception when calling ItemsApi.ItemsPost: " + e.Message);
                Debug.Print("Status Code: " + e.ErrorCode);
                Debug.Print(e.StackTrace);
            }
        }
        public async Task <SaleResponse> CreateSaleAsync(SaleRequest request, MerchantCredentials credentials = null)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (_credentials == null && credentials == null)
            {
                throw new InvalidOperationException("Credentials are null");
            }

            var currentCredentials = credentials ?? _credentials;

            if (string.IsNullOrWhiteSpace(currentCredentials.MerchantId))
            {
                throw new InvalidOperationException("Invalid credentials: MerchantId is null");
            }

            if (string.IsNullOrWhiteSpace(currentCredentials.MerchantKey))
            {
                throw new InvalidOperationException("Invalid credentials: MerchantKey is null");
            }

            var httpRequest = new RestRequest(@"v2/sales/", Method.POST)
            {
                RequestFormat = DataFormat.Json
            };

            httpRequest.AddHeader("Content-Type", "application/json");
            httpRequest.AddHeader("MerchantId", currentCredentials.MerchantId);
            httpRequest.AddHeader("MerchantKey", currentCredentials.MerchantKey);
            httpRequest.AddHeader("RequestId", Guid.NewGuid().ToString());
            httpRequest.AddBody(new { request.MerchantOrderId, request.Customer, request.Payment });

            var cancellationTokenSource = new CancellationTokenSource();

            var httpResponse = await RestClientApi.ExecuteTaskAsync(httpRequest, cancellationTokenSource.Token);

            if (httpResponse.StatusCode != HttpStatusCode.Created)
            {
                return(new SaleResponse
                {
                    HttpStatus = httpResponse.StatusCode,
                    ErrorDataCollection = httpResponse.StatusCode != HttpStatusCode.Forbidden ? JsonDeserializer.Deserialize <List <ErrorData> >(httpResponse) : null
                });
            }

            var jsonResponse = JsonDeserializer.Deserialize <SaleResponse>(httpResponse);

            jsonResponse.HttpStatus = httpResponse.StatusCode;
            return(jsonResponse);
        }
예제 #4
0
 public RestClientApiTests()
 {
     instance = new RestClientApi();
 }