Send a received textview

0

The case is that I sent a TextView from a Fragment to an activity. And that TextView that is already inside and appears in the activity I want to send as a total amount to pay from Paypal But it's not working. I use Paypal sdk

xml activity_paypal

public class PaypalActivity extends  AppCompatActivity implements View.OnClickListener {

    //The views
    Button buttonPay;
    TextView mtotal;

    //Payment Amount
    private String paymentAmount;


    //Paypal intent request code to track onActivityResult method
    public static final int PAYPAL_REQUEST_CODE = 123;


    //Paypal Configuration Object
    private static PayPalConfiguration config = new PayPalConfiguration()
            // Start with mock environment.  When ready, switch to sandbox (ENVIRONMENT_SANDBOX)
            // or live (ENVIRONMENT_PRODUCTION)
            .environment(PayPalConfiguration.ENVIRONMENT_SANDBOX)
            .clientId(PayPalConfig.PAYPAL_CLIENT_ID);


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

        String total = getIntent().getExtras().getString("total");
        TextView mtotal =(TextView) findViewById(R.id.tvTotal);
        mtotal.setText(total);


        buttonPay.setOnClickListener(this);

        Intent intent = new Intent(this, PayPalService.class);

        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

        startService(intent);

    }

    @Override
    public void onDestroy() {
        stopService(new Intent(this, PayPalService.class));
        super.onDestroy();
    }

    private void getPayment() {
        //Getting the amount from editText

        paymentAmount = mtotal.getText().toString();

        //Creating a paypalpayment
        PayPalPayment payment = new PayPalPayment(new BigDecimal(String.valueOf(paymentAmount)), "USD", "Total",
                PayPalPayment.PAYMENT_INTENT_SALE);

        //Creating Paypal Payment activity intent
        Intent intent = new Intent(this, PaymentActivity.class);

        //putting the paypal configuration to the intent
        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

        //Puting paypal payment to the intent
        intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);

        //Starting the intent activity for result
        //the request code will be used on the method onActivityResult
        startActivityForResult(intent, PAYPAL_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //If the result is from paypal
        if (requestCode == PAYPAL_REQUEST_CODE) {

            //If the result is OK i.e. user has not canceled the payment
            if (resultCode == Activity.RESULT_OK) {
                //Getting the payment confirmation
                PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);

                //if confirmation is not null
                if (confirm != null) {
                    try {
                        //Getting the payment details
                        String paymentDetails = confirm.toJSONObject().toString(4);
                        Log.i("paymentExample", paymentDetails);

                        //Starting a new activity for the payment details and also putting the payment details with intent
                        startActivity(new Intent(this, ConfirmationActivity.class)
                                .putExtra("PaymentDetails", paymentDetails)
                                .putExtra("PaymentAmount", paymentAmount));

                    } catch (JSONException e) {
                        Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
                    }
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Log.i("paymentExample", "The user canceled.");
            } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
                Log.i("paymentExample", "An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
            }
        }
    }

    @Override
    public void onClick(View v) {
        getPayment();
    }
}
    
asked by G J 21.09.2017 в 09:24
source

3 answers

2

The text of your TextView mtotal is not sent, because you are stating two variable mtotal . One at the beginning of the class and one at the onCreate() . The variable that you declare at the beginning of the class is null, since you are not initializing it and therefore when you get its value in the method getPayment() , you do not get anything. The variable that you initialize and to which you assign a value is the one that is declared within the onCreate() , but this variable can not be used outside the onCreate() , since it is a variable of the method.

To solve your problem you have to initialize in the onCreate() the variable that you declare at the beginning of the class.

public class PaypalActivity extends  AppCompatActivity implements View.OnClickListener {

    //The views
    TextView mtotal;

    ...

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

        String total = getIntent().getExtras().getString("total");

        // Al eliminar TextView de la variable mtotal, se inicializa la variable 
        // que declaraste al inicio de la clase, en vez de crear una variable nueva.
        mtotal =(TextView) findViewById(R.id.tvTotal);

        mtotal.setText(total);

        ...

    }

    ...

}
    
answered by 21.09.2017 в 13:19
1

I think the problem is here:

 PayPalPayment payment = new PayPalPayment(new BigDecimal(String.valueOf(paymentAmount)), "USD", "Total",
            PayPalPayment.PAYMENT_INTENT_SALE);

when parsing a string to a decimal it is not necessary to make "String.valueOf (paymentAmount)" since paymentAmount is already a String. You have to find the correct way to parse a String to a BigDecimal. String to BigDecimal

    
answered by 22.09.2017 в 04:28
0

First of all what you are saying,

  

The fact is that I sent a TextView from a Fragment to an activity.   Actually, a TextView is not sent, the value it contains is sent:

You already receive the value in your Activity and you are adding it to your TextView , but this same value can be sent in the Intent that makes the payment, this you have already done within getPayment() :

 private void getPayment() {
        //Getting the amount from editText

        paymentAmount = mtotal.getText().toString(); //Obtiene valor del TextView.

        //Creating a paypalpayment
        PayPalPayment payment = new PayPalPayment(new BigDecimal(String.valueOf(paymentAmount)), "USD", "Total",
                PayPalPayment.PAYMENT_INTENT_SALE);

        //Creating Paypal Payment activity intent
        Intent intent = new Intent(this, PaymentActivity.class);

        //putting the paypal configuration to the intent
        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

        //Puting paypal payment to the intent
        intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);

        //Starting the intent activity for result
        //the request code will be used on the method onActivityResult
        startActivityForResult(intent, PAYPAL_REQUEST_CODE);
    }

You just have to call this method!

I see that currently the method that makes the payment is called when clicking from a view defined in your file activity_paypal.xml that has the property android:onClick="onClick" , check what that view is:

@Override
    public void onClick(View v) {
        getPayment();
    }
    
answered by 21.09.2017 в 16:49