Advertisement
renatoSouza

MOIP - PEDIDO

Oct 10th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. public String criarPedidoMoip(String CPF, String idMOIPMorador, int valor, String descricao, String produto) {
  2.  
  3.         //Para receber retorno do método
  4.         final String[] code = {null};
  5.  
  6.         try {
  7.             //Definicao estrutura JSON Criar conta Moip
  8.             JSONObject jsonObject = new JSONObject();
  9.             JSONObject amount = new JSONObject();
  10.             JSONObject items = new JSONObject();
  11.             JSONObject customer = new JSONObject();
  12.             JSONObject subtotals = new JSONObject();
  13.  
  14.             //Subtotals
  15.             subtotals.put("shipping", 0);
  16.  
  17.             //Amount
  18.             amount.put("currency", "BRL");
  19.             amount.put("subtotals", subtotals);
  20.  
  21.             //Items
  22.             items.put("product", produto);
  23.             items.put("price", valor);
  24.             items.put("detail", descricao);
  25.             items.put("quantity", 1);
  26.             items.put("category", "CLOTHING");
  27.  
  28.             //Customer
  29.             customer.put("id", idMOIPMorador);
  30.  
  31.             jsonObject.put("ownId", CPF);
  32.             jsonObject.put("amount", amount);
  33.             jsonObject.put("items", items);
  34.             jsonObject.put("customer", customer);
  35.  
  36.             post("https://sandbox.moip.com.br/v2/orders/", jsonObject.toString(), new Callback() {
  37.                 @Override
  38.                 public void onFailure(Call call, IOException e) {
  39.                     //Something went wrong
  40.                     Log.i("PED", "deu errado");
  41.                 }
  42.  
  43.                 @Override
  44.                 public void onResponse(Call call, Response response) throws IOException {
  45.  
  46.                     String responseStr = response.body().string();
  47.                     Log.i("PED", responseStr);
  48.                     if (response.isSuccessful()) {
  49.                         try {
  50.                             JSONObject jsonObject1 = new JSONObject(responseStr);
  51.                             //code[0] = jsonObject1.getString("id");
  52.                             Log.i("PED", responseStr);
  53.  
  54.                             //Executa na UI Trhead
  55.                             getActivity().runOnUiThread(new Runnable() {
  56.                                 public void run() {
  57.                                 }
  58.                             });
  59.                         } catch (JSONException e) {
  60.                             e.printStackTrace();
  61.                         }
  62.                     } else {
  63.                         //Pega retorno em caso de falha
  64.                         try {
  65.                             JSONObject jsonObject1 = new JSONObject(responseStr);
  66.                             JSONArray jsonArray = jsonObject1.getJSONArray("errors");
  67.                             JSONObject retorno = jsonArray.getJSONObject(0);
  68.                             //code[0] = retorno.getString("code");
  69.  
  70.                             //Executa na UI Trhead
  71.                             getActivity().runOnUiThread(new Runnable() {
  72.                                 public void run() {
  73.                                 }
  74.                             });
  75.  
  76.                         } catch (JSONException e) {
  77.                             e.printStackTrace();
  78.                         }
  79.  
  80.                     }
  81.                 }
  82.             });
  83.         } catch (JSONException ex) {
  84.             Log.d("Exception", "JSON exception", ex);
  85.         }
  86.         return code[0];
  87.  
  88.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement