Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String criarPedidoMoip(String CPF, String idMOIPMorador, int valor, String descricao, String produto) {
- //Para receber retorno do método
- final String[] code = {null};
- try {
- //Definicao estrutura JSON Criar conta Moip
- JSONObject jsonObject = new JSONObject();
- JSONObject amount = new JSONObject();
- JSONObject items = new JSONObject();
- JSONObject customer = new JSONObject();
- JSONObject subtotals = new JSONObject();
- //Subtotals
- subtotals.put("shipping", 0);
- //Amount
- amount.put("currency", "BRL");
- amount.put("subtotals", subtotals);
- //Items
- items.put("product", produto);
- items.put("price", valor);
- items.put("detail", descricao);
- items.put("quantity", 1);
- items.put("category", "CLOTHING");
- //Customer
- customer.put("id", idMOIPMorador);
- jsonObject.put("ownId", CPF);
- jsonObject.put("amount", amount);
- jsonObject.put("items", items);
- jsonObject.put("customer", customer);
- post("https://sandbox.moip.com.br/v2/orders/", jsonObject.toString(), new Callback() {
- @Override
- public void onFailure(Call call, IOException e) {
- //Something went wrong
- Log.i("PED", "deu errado");
- }
- @Override
- public void onResponse(Call call, Response response) throws IOException {
- String responseStr = response.body().string();
- Log.i("PED", responseStr);
- if (response.isSuccessful()) {
- try {
- JSONObject jsonObject1 = new JSONObject(responseStr);
- //code[0] = jsonObject1.getString("id");
- Log.i("PED", responseStr);
- //Executa na UI Trhead
- getActivity().runOnUiThread(new Runnable() {
- public void run() {
- }
- });
- } catch (JSONException e) {
- e.printStackTrace();
- }
- } else {
- //Pega retorno em caso de falha
- try {
- JSONObject jsonObject1 = new JSONObject(responseStr);
- JSONArray jsonArray = jsonObject1.getJSONArray("errors");
- JSONObject retorno = jsonArray.getJSONObject(0);
- //code[0] = retorno.getString("code");
- //Executa na UI Trhead
- getActivity().runOnUiThread(new Runnable() {
- public void run() {
- }
- });
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- });
- } catch (JSONException ex) {
- Log.d("Exception", "JSON exception", ex);
- }
- return code[0];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement