Android PayPalPayment: invalid long

2

I have an error in android studio , my code is:

private void ProcessPayment() {
   montoPagar = montoTotal.getText().toString();
    PayPalPayment payPalPayment = new PayPalPayment(new BigDecimal(String.valueOf(montoPagar)),"$","USD",PayPalPayment.PAYMENT_INTENT_SALE);
    Intent intent = new Intent(this, PaymentActivity.class);
    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION,config);
    intent.putExtra(PaymentActivity.EXTRA_PAYMENT,payPalPayment);
    startActivityForResult(intent,PAYPAL_REQUEST_CODE);
}

when I'm going to run it this line

PayPalPayment payPalPayment = new PayPalPayment(new BigDecimal(String.valueOf(montoPagar)),"$","USD",PayPalPayment.PAYMENT_INTENT_SALE);

is the one that causes me the error that says invalid long ...

Some reference, it would be great

    
asked by Andrea Valentina 03.07.2018 в 17:31
source

1 answer

1

You are trying to convert the string "55.0 USD" to Long which is incorrect, for this reason you get NumberFormatException .

You do not have to define the currency type in your EditText montoTotal , since you are defining this when instantiating PayPalPayment(BigDecimal amount,                      String currencyCode ,                      String shortDescription,                      String paymentIntent) .

You are currently defining "USD":

 PayPalPayment payPalPayment = new PayPalPayment(new BigDecimal(String.valueOf(montoPagar)),"$","USD",PayPalPayment.PAYMENT_INTENT_SALE);

To solve this, you must write only the amount within montoTotal .

You can also validate by extracting only the numerical value of the text written in EditText montoTotal :

   montoPagar  = montoPagar.replaceAll("\D+","");

This would be the code with the validation:

private void ProcessPayment() {
   montoPagar = montoTotal.getText().toString();
   montoPagar  = montoPagar.replaceAll("\D+","");//*Obtiene solo el valor numerico.
    PayPalPayment payPalPayment = new PayPalPayment(new BigDecimal(String.valueOf(montoPagar)),"$","USD",PayPalPayment.PAYMENT_INTENT_SALE);
    Intent intent = new Intent(this, PaymentActivity.class);
    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION,config);
    intent.putExtra(PaymentActivity.EXTRA_PAYMENT,payPalPayment);
    startActivityForResult(intent,PAYPAL_REQUEST_CODE);
}

You can also define in your EditText that it only accepts numbers, establishing the property:

android:inputType = "numberPassword"
    
answered by 03.07.2018 / 18:10
source