private void esconderDialog() { if (progressDialog != null) { progressDialog.dismiss(); progressDialog = null; } } @Override protected void onDestroy() { super.onDestroy(); esconderDialog(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.produtos, menu); return true; } @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { if (newText != null) { executarRequestProdutos(newText); } return false; } /** * Verifying the mobile payment on the server to avoid fraudulent payment * */ private void verificarPagtoServidor(final String idPagto, final String jsonClientePagto) { // Showing progress dialog before making request progressDialog = new ProgressDialog(this); progressDialog.setMessage("Verificando pagamento..."); progressDialog.show(); StringRequest verifyReq = new StringRequest(Method.POST, Config.URL_VER_PAGTOS, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d(TAG, "verify payment: " + response.toString()); try { JSONObject res = new JSONObject(response); boolean erro = res.getBoolean("erro"); String msg = res.getString("msg"); // user error boolean flag to check for errors Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show(); if (!erro) { // empty the cart produtosCarrinho.clear(); } } catch (JSONException e) { e.printStackTrace(); } // hiding the progress dialog esconderDialog(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e(TAG, "Erro de verificaAAo: " + error.getMessage()); Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); // hiding the progress dialog esconderDialog(); } }) { @Override protected Map<String, String> getParams() { SharedPreferences preferences = getPreferences(MODE_PRIVATE); Integer idUsuario = preferences.getInt("usuario", 0); Map<String, String> params = new HashMap<String, String>(); params.put("idPagto", idPagto); params.put("idUsuario", idUsuario != 0 ? String.valueOf(idUsuario) : "1"); params.put("jsonClientePagto", jsonClientePagto); return params; } }; // Setting timeout to volley request as verification request takes sometime int socketTimeout = 60000; RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); verifyReq.setRetryPolicy(policy); // Adding request to request queue AppController.getInstance().addToRequestQueue(verifyReq); } /** * Preparing final cart amount that needs to be sent to PayPal for payment * */ private PayPalPayment prepararCarrinhoFinal() { PayPalItem[] itens = new PayPalItem[produtosCarrinho.size()]; itens = produtosCarrinho.toArray(itens); // Total amount BigDecimal subtotal = PayPalItem.getItemTotal(itens); // If you have frete cost, add it here BigDecimal frete = new BigDecimal("0.0"); // If you have tax, add it here BigDecimal taxa = new BigDecimal("0.0"); PayPalPaymentDetails detalhesPagto = new PayPalPaymentDetails(frete, subtotal, taxa); BigDecimal quantia = subtotal.add(frete).add(taxa); PayPalPayment pagto = new PayPalPayment( quantia, Config.DEFAULT_CURRENCY, "TransaAAo de compra na Loja DevMedia sendo processada...", Config.PAYMENT_INTENT); pagto.items(itens).paymentDetails(detalhesPagto); // Custom field like invoice_number etc., pagto.custom("Texto a ser associado com o pagto que a app vai usar."); return pagto; } /** * Launching PalPay payment activity to complete the payment * */ private void executarPagtoPayPal() { PayPalPayment coisasPraComprar = prepararCarrinhoFinal(); Intent intent = new Intent(ListaProdutosActivity.this, PaymentActivity.class); intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, paypalConfig); intent.putExtra(PaymentActivity.EXTRA_PAYMENT, coisasPraComprar); startActivityForResult(intent, CODIGO_PAGTO); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CODIGO_PAGTO) { if (resultCode == Activity.RESULT_OK) { PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION); if (confirm != null) { try { Log.e(TAG, confirm.toJSONObject().toString(4)); Log.e(TAG, confirm.getPayment().toJSONObject().toString(4)); String pagtoId = confirm.toJSONObject().getJSONObject("response").getString("id"); String jsonClientePagto = confirm.getPayment().toJSONObject().toString(); Log.e(TAG, "pagtoId: " + pagtoId + ", jsonClientePagto: " + jsonClientePagto); // Now verify the payment on the server side verificarPagtoServidor(pagtoId, jsonClientePagto); } catch (JSONException e) { Log.e(TAG, "Um erro ocorreu: ", e); } } } else if (resultCode == Activity.RESULT_CANCELED) { Log.e(TAG, "O usuArio cancelou a operaAAo."); } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) { Log.e(TAG, "Um pagamento