Кто-нибудь знает, в чем проблемы с моим кодом? Он работает, и у меня нет ошибок. Я пытаюсь включить PayPal Payment SDK и настроить все в среде учетной записи песочницы PayPal для разработчиков. У меня есть идентификатор клиента в конфиге. Код запроса правильный или неправильный. Когда пользователь нажимает «да», он должен показываться, но вылетает. Logcat не показывает ошибок для этого.
public class Config {
public static final String PAYPAL_CLIENT_ID = "";
}
public class Cart {
public static final int PAYPAL_REQUEST_CODE = 9999;
static PayPalConfiguration config = new PayPalConfiguration()
.environment(PayPalConfiguration.ENVIRONMENT_SANDBOX)
.clientId(Config.PAYPAL_CLIENT_ID);
String address;
protected void onCreate(Bundle savedInstanceState) {
//init paypal
Intent intent = new Intent(this, PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION,config);
startService(intent);
}
private void showAlertDialog() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(Cart.this);
alertDialog.setTitle("One more step!");
alertDialog.setMessage("Enter your address: ");
final EditText editAddress = new EditText(Cart.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
editAddress.setLayoutParams(lp);
alertDialog.setView(editAddress); //Add edit Text to aleart dialog
alertDialog.setIcon(R.drawable.ic_shopping_cart);
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
address = editAddress.getText().toString();
String formatAmount = textTotalPrice.getText().toString()
.replace("$", "")
.replace(",", "");
PayPalPayment payPalPayment = new PayPalPayment(new BigDecimal(formatAmount),
"USD",
"Kevins Cafe Order",
PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(getApplicationContext(), PaymentActivity.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payPalPayment);
startActivityForResult(intent, PAYPAL_REQUEST_CODE);
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PAYPAL_REQUEST_CODE)
{
if(resultCode == RESULT_OK)
{
PaymentConfirmation confirmation = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if(confirmation != null)
{
try{
String paymentDetail = confirmation.toJSONObject().toString(4);
JSONObject jsonObject = new JSONObject(paymentDetail);
//Create new Request
Request request = new Request(
Common.currentUser.getPhone(),
Common.currentUser.getName(),
address,
textTotalPrice.getText().toString(),
"0",
jsonObject.getJSONObject("response").getString("state"),
cart
);
//Submit to Firebase
//We will using System.CurrentMills to key
requests.child(String.valueOf(System.currentTimeMillis()))
.setValue(request);
//Delete cart
new Database(getBaseContext()).cleanCart();
Toast.makeText(Cart.this, "Thank you, Order Place.", Toast.LENGTH_SHORT).show();
finish();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
else if(resultCode == Activity.RESULT_CANCELED)
Toast.makeText(this ,"Payment Cancel", Toast.LENGTH_SHORT);
else if(resultCode == PaymentActivity.RESULT_EXTRAS_INVALID)
Toast.makeText(this ,"Invalid Payment", Toast.LENGTH_SHORT);
}
}