Braintree令牌或客户端令牌在Android应用中无效

时间:2019-03-03 00:07:10

标签: android braintree

我正在尝试在Android应用中实施Braintree测试付款。

我正在遵循一个应该有效的教程。

此代码:

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_CODE = 1234;
    final String API_GET_TOKEN = "https://..main.php";
    final String API_CHECK_OUT = "..checkout.php";
    String token, amount;
    HashMap<String,String> paramsHash;


    Button btn_pay;
    EditText edt_amount;
    LinearLayout group_waiting, group_payment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        group_waiting = (LinearLayout) findViewById(R.id.waiting_group);
        group_payment = (LinearLayout) findViewById(R.id.payment_group);

        btn_pay = (Button) findViewById(R.id.btn_pay);
        edt_amount = (EditText) findViewById(R.id.amount);

      new  getToken().execute();
      btn_pay.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              submitPayment();
          }
      });

    }

    private void submitPayment() {

        DropInRequest dropInRequest = new DropInRequest().clientToken(token);
        startActivityForResult(dropInRequest.getIntent(this),REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){

        if (requestCode == REQUEST_CODE){

            if (resultCode == RESULT_OK){

                DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
                PaymentMethodNonce nonce = result.getPaymentMethodNonce();
                String strNonce = nonce.getNonce();

                if (!edt_amount.getText().toString().isEmpty()){
                    amount = edt_amount.getText().toString();
                    paramsHash = new HashMap<>();
                    paramsHash.put("amount", amount);
                    paramsHash.put("nonce",strNonce);

                    sendPayments();
                }
                else
                {
                    Toast.makeText(this,"Por favor, introduce un importe correcto.",Toast.LENGTH_LONG).show();
                }
            }
            else if (resultCode == RESULT_CANCELED)
                Toast.makeText(this,"Operación cancelada por el usuario",Toast.LENGTH_LONG).show();
            else
            {
                Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);
                Log.d("EDMT_ERROR",error.toString());
            }
        }


    }

    private void sendPayments() {
        Log.d("EDMT_LOG","esyoy en sendpayment");
        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

        StringRequest stringRequest = new StringRequest(Request.Method.POST, API_CHECK_OUT,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        if (response.toString().contains("Successful"))
                            Toast.makeText(MainActivity.this, "Transacción correcta", Toast.LENGTH_LONG).show();
                        else
                        {
                            Toast.makeText(MainActivity.this,"Error en la transacción",Toast.LENGTH_LONG).show();

                        }
                        Log.d("EDMT_LOG",response.toString());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("EDMT_error",error.toString());

            }

    })
        {
            @Override
            protected Map<String,String> getParams() throws AuthFailureError {
                if (paramsHash == null)
                    return null;
                Map<String, String> params = new HashMap<>();
                for (String key : params.keySet()) {
                    params.put(key, paramsHash.get(key));
                }
                return params;
            }
            @Override
            public Map<String,String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<>();
                params.put("Content-Type","application/x-www-form-urlencoded");
                return params;
            }
        };
        queue.add(stringRequest);
    }

    private class getToken extends AsyncTask {

        ProgressDialog mDialog;
        @Override
        protected void onPreExecute(){
            mDialog  = new ProgressDialog(MainActivity.this,android.R.style.Theme_DeviceDefault_Dialog);
            mDialog.setCancelable(false);
            mDialog.setMessage("Por favor, espere");
            mDialog.show();
        }
        @Override
        protected Object doInBackground(Object[] objects) {

            HttpClient client = new HttpClient();
            client.get(API_GET_TOKEN, new HttpResponseCallback() {
                @Override
                public void success(final String responseBody) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            group_waiting.setVisibility(View.GONE);
                            group_payment.setVisibility(View.VISIBLE);
                            token = responseBody;
                        }
                    });
                }

                @Override
                public void failure(Exception exception) {

                    Log.d("ERROR EN LA TRANSACCION",exception.toString());

                }
            });
            return null;
        }

        @Override
        protected void onPostExecute(Object o){
            super.onPostExecute(o);

            mDialog.dismiss();

        }

    }
}

我在方法onActivityResult上收到以下异常消息:

com.braintreepayments.api.exceptions.InvalidArgumentException: Tokenization Key or client token was invalid

我已经检查了所有私钥,公钥以及环境和商家ID。 在我项目的服务器端,文件main.php如果直接从浏览器执行,则会生成一个令牌。

请看一下代码,然后告诉我我在做什么错

编辑:点击付款按钮后,dropIn没有显示,我猜是由于令牌无效。

0 个答案:

没有答案