public void ExecuteRequest_Post_Json() { MercadoPagoRestClient client = new MercadoPagoRestClient(); JObject jsonObject = new JObject { { "firstName", "Clark" }, { "lastName", "Kent" }, { "year", 2018 } }; DummyClass dummy = new DummyClass("Dummy description", DateTime.Now, 1000); WebHeaderCollection headers = new WebHeaderCollection { { "x-idempotency-key", dummy.GetType().GUID.ToString() } }; MercadoPagoAPIResponse response = client.ExecuteRequest(HttpMethod.POST, "", PayloadType.JSON, jsonObject, headers, 0, 0); JObject jsonResponse = JObject.Parse(response.StringResponse.ToString()); List <JToken> lastName = MercadoPagoCoreUtils.FindTokens(jsonResponse, "lastName"); Assert.AreEqual("Kent", lastName.First().ToString()); List <JToken> year = MercadoPagoCoreUtils.FindTokens(jsonResponse, "year"); Assert.AreEqual("2018", year.First().ToString()); }
public static string GetAccessToken() { if (string.IsNullOrEmpty(MercadoPagoSDK.ClientId) || string.IsNullOrEmpty(MercadoPagoSDK.ClientSecret)) { throw new MercadoPagoException("\"client_id\" and \"client_secret\" can not be \"null\" when getting the \"access_token\""); } JObject jsonPayload = new JObject { { "grant_type", "client_credentials" }, { "client_id", MercadoPagoSDK.ClientId }, { "client_secret", MercadoPagoSDK.ClientSecret } }; string access_token; MercadoPagoAPIResponse response = new MercadoPagoRestClient().ExecuteRequest( HttpMethod.POST, MercadoPagoSDK.BaseUrl + "/oauth/token", PayloadType.X_WWW_FORM_URLENCODED, jsonPayload, null, 0, 0); JObject jsonResponse = JObject.Parse(response.StringResponse.ToString()); if (response.StatusCode == 200) { List <JToken> accessTokenElem = MercadoPagoCoreUtils.FindTokens(jsonResponse, "access_token"); List <JToken> refreshTokenElem = MercadoPagoCoreUtils.FindTokens(jsonResponse, "refresh_token"); if (accessTokenElem != null && accessTokenElem.Count == 1) { access_token = accessTokenElem.First().ToString(); } else { throw new MercadoPagoException("Can not retrieve the \"access_token\""); } // Making refresh token an optional param if (refreshTokenElem != null && refreshTokenElem.Count == 1) { string refresh_token = refreshTokenElem.First().ToString(); MercadoPagoSDK.RefreshToken = refresh_token; } } else { throw new MercadoPagoException("Can not retrieve the \"access_token\""); } return(access_token); }
public static string SingleUseCardToken(string publicKey, string status) { JObject payload = JObject.Parse(CardDummyWithSpecificStatus(status)); MercadoPagoRestClient client = new MercadoPagoRestClient(); MercadoPagoAPIResponse response = client.ExecuteRequest(HttpMethod.POST, $"https://api.mercadopago.com/v1/card_tokens?public_key={publicKey}", PayloadType.JSON, payload, null, 0, 1); JObject jsonResponse = JObject.Parse(response.StringResponse.ToString()); List <JToken> tokens = MercadoPagoCoreUtils.FindTokens(jsonResponse, "id"); return(tokens.First().ToString()); }
public void ExecuteRequest_Post() { MercadoPagoRestClient client = new MercadoPagoRestClient(); JObject jsonObject = new JObject { { "firstName", "Clark" }, { "lastName", "Kent" }, { "year", 2018 } }; MercadoPagoAPIResponse response = client.ExecuteRequest(HttpMethod.POST, "https://httpbin.org/post", PayloadType.X_WWW_FORM_URLENCODED, jsonObject, null, 0, 0); JObject jsonResponse = JObject.Parse(response.StringResponse.ToString()); List <JToken> contentType = MercadoPagoCoreUtils.FindTokens(jsonResponse, "Content-Type"); Assert.AreEqual("application/x-www-form-urlencoded", contentType.First().ToString()); }
public void ExecuteRequest_PostAndPutMustHavePayload() { MercadoPagoRestClient client = new MercadoPagoRestClient(); try { MercadoPagoAPIResponse response = client.ExecuteRequest(HttpMethod.POST, "https://httpbin.org/post", PayloadType.X_WWW_FORM_URLENCODED, null, null, 0, 0); } catch (MercadoPagoRestException exception) { Assert.AreEqual("Must include payload for this method.", exception.Message); } try { MercadoPagoAPIResponse response = client.ExecuteRequest(HttpMethod.PUT, "https://httpbin.org/put", PayloadType.X_WWW_FORM_URLENCODED, null, null, 0, 0); } catch (MercadoPagoRestException exception) { Assert.AreEqual("Must include payload for this method.", exception.Message); } }
public void ExecuteRequest_GetAndDeleteNustNotHavePayload() { MercadoPagoRestClient client = new MercadoPagoRestClient(); try { MercadoPagoAPIResponse response = client.ExecuteRequest(HttpMethod.GET, "https://httpbin.org/get", PayloadType.X_WWW_FORM_URLENCODED, new JObject(), null, 0, 0); } catch (MercadoPagoRestException exception) { Assert.AreEqual("Payload not supported for this method.", exception.Message); } try { MercadoPagoAPIResponse response = client.ExecuteRequest(HttpMethod.DELETE, "https://httpbin.org/delete", PayloadType.X_WWW_FORM_URLENCODED, new JObject(), null, 0, 0); } catch (MercadoPagoRestException exception) { Assert.AreEqual("Payload not supported for this method.", exception.Message); } }
public void ExecuteRequest_Get_ShortTimeoutWillFail() { MercadoPagoRestClient client = new MercadoPagoRestClient(); JObject jsonObject = new JObject { { "firstName", "Comander" }, { "lastName", "Shepard" }, { "year", 2126 } }; try { MercadoPagoAPIResponse response = client.ExecuteRequest(HttpMethod.POST, "https://httpbin.org/post", PayloadType.JSON, jsonObject, null, 5, 0); } catch (Exception) { Assert.Pass(); } Assert.Fail(); }
public void ExecuteRequest_Get_ProperTimeoutWillWork() { MercadoPagoRestClient client = new MercadoPagoRestClient(); JObject jsonObject = new JObject { { "firstName", "Comander" }, { "lastName", "Shepard" }, { "year", 2126 } }; MercadoPagoAPIResponse response = client.ExecuteRequest(HttpMethod.POST, "https://httpbin.org/post", PayloadType.JSON, jsonObject, null, 20000, 0); Assert.AreEqual(200, response.StatusCode); JObject jsonResponse = response.JsonObjectResponse; List <JToken> lastName = MercadoPagoCoreUtils.FindTokens(jsonResponse, "lastName"); Assert.AreEqual("Shepard", lastName.First().ToString()); List <JToken> year = MercadoPagoCoreUtils.FindTokens(jsonResponse, "year"); Assert.AreEqual("2126", year.First().ToString()); }