Я пытаюсь внедрить платежи Braintree в моем приложении для Android.
В настоящее время я использую платежи в песочнице.
Мое тестовое приложение работает хорошо для платежей по кредитным картам, но не дляПлатежи PayPal.
Я получаю этот экран, когда выбрано выпадающее меню Paypal:
Но затем, после нажатия кнопки Приступитьс покупкой в «песочнице» приложение зависает и никаких дальнейших действий не предпринимается.
Я думаю, что я включил весь необходимый код.
В AndroidManifest:
<activity
android:name="com.braintreepayments.api.BraintreeBrowserSwitchActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="${applicationId}.braintree" />
</intent-filter>
</activity>
И это полный код действия:
public class MainActivity extends AppCompatActivity {
final int REQUEST_CODE = 1;
final String get_token = "https://.../main.php";
//braintree account es
final String send_payment_details = "https://...checkout.php";
String token, amount;
HashMap<String, String> paramHash;
Button btnPay;
EditText etAmount;
LinearLayout llHolder;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llHolder = (LinearLayout) findViewById(R.id.llHolder);
etAmount = (EditText) findViewById(R.id.etPrice);
btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBraintreeSubmit();
}
});
new HttpRequest().execute();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
PaymentMethodNonce nonce = result.getPaymentMethodNonce();
String stringNonce = nonce.getNonce();
Log.d("mylog", "Result: " + stringNonce);
// Send payment price with the nonce
// use the result to update your UI and send the payment method nonce to your server
if (!etAmount.getText().toString().isEmpty()) {
amount = etAmount.getText().toString();
paramHash = new HashMap<>();
paramHash.put("amount", amount);
paramHash.put("nonce", stringNonce);
sendPaymentDetails();
} else
Toast.makeText(MainActivity.this, "Please enter a valid amount.", Toast.LENGTH_SHORT).show();
} else if (resultCode == Activity.RESULT_CANCELED) {
// the user canceled
Log.d("mylog", "user canceled");
} else {
// handle errors here, an exception may be available in
Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);
Log.d("mylog", "Error : " + error.toString());
}
}
}
public void onBraintreeSubmit() {
DropInRequest dropInRequest = new DropInRequest()
.clientToken(token);
startActivityForResult(dropInRequest.getIntent(this), REQUEST_CODE);
}
private class HttpRequest extends AsyncTask {
ProgressDialog progress;
@Override
protected void onPreExecute() {
super.onPreExecute();
progress = new ProgressDialog(MainActivity.this, android.R.style.Theme_DeviceDefault_Dialog);
progress.setCancelable(false);
progress.setMessage("We are contacting our servers for token, Please wait");
progress.setTitle("Getting token");
progress.show();
}
@Override
protected Object doInBackground(Object[] objects) {
HttpClient client = new HttpClient();
client.get(get_token, new HttpResponseCallback() {
@Override
public void success(String responseBody) {
Log.d("mylog", responseBody);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Successfully got token", Toast.LENGTH_SHORT).show();
llHolder.setVisibility(View.VISIBLE);
}
});
token = responseBody;
}
@Override
public void failure(Exception exception) {
final Exception ex = exception;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Failed to get token: " + ex.toString(), Toast.LENGTH_LONG).show();
}
});
}
});
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
progress.dismiss();
}
}
private void sendPaymentDetails() {
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.POST, send_payment_details,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.contains("Successful"))
{
Toast.makeText(MainActivity.this, "Transaction successful", Toast.LENGTH_LONG).show();
}
else Toast.makeText(MainActivity.this, "Transaction failed", Toast.LENGTH_LONG).show();
Log.d("mylog", "Final Response: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("mylog", "Volley error : " + error.toString());
}
}) {
@Override
protected Map<String, String> getParams() {
if (paramHash == null)
return null;
Map<String, String> params = new HashMap<>();
for (String key : paramHash.keySet()) {
params.put(key, paramHash.get(key));
Log.d("mylog", "Key : " + key + " Value : " + paramHash.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);
}
}