Android Question Help implementation java inline or javaObject

Lucas Siqueira

Active Member
Licensed User
Longtime User
I'm trying to implement the following code using inline java, but without success...

how can I implement SumUpAPI.openLoginActivity using inline java or javaObject?
what should I replace in place of MainActivity.this ?


cut code:
cut code:
import android.app.Activity;
import android.content.Intent;
import com.sumup.merchant.reader.models.TransactionInfo;
import com.sumup.merchant.reader.api.SumUpAPI;
import com.sumup.merchant.reader.api.SumUpLogin;
import com.sumup.merchant.reader.api.SumUpPayment;

public class MainActivity extends Activity {

    private static final int REQUEST_CODE_LOGIN = 1;

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

        findViews();

        Button login = (Button) findViewById(R.id.button_login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SumUpLogin sumupLogin = SumUpLogin.builder("7ca84f17-84a5-4140-8df6-6ebeed8540fc").build();
                SumUpAPI.openLoginActivity(MainActivity.this, sumupLogin, REQUEST_CODE_LOGIN);
            }
        });
    }
}



full code:
MainActivity.java:
package com.sumup.sdksampleapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.sumup.merchant.reader.models.TransactionInfo;
import com.sumup.merchant.reader.api.SumUpAPI;
import com.sumup.merchant.reader.api.SumUpLogin;
import com.sumup.merchant.reader.api.SumUpPayment;

import java.math.BigDecimal;
import java.util.UUID;

public class MainActivity extends Activity {

    private static final int REQUEST_CODE_LOGIN = 1;
    private static final int REQUEST_CODE_PAYMENT = 2;
    private static final int REQUEST_CODE_CARD_READER_PAGE = 4;

    private TextView mResultCode;
    private TextView mResultMessage;
    private TextView mTxCode;
    private TextView mReceiptSent;
    private TextView mTxInfo;

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

        findViews();

        Button login = (Button) findViewById(R.id.button_login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Please go to https://me.sumup.com/developers to get your Affiliate Key by entering the application ID of your app. (e.g. com.sumup.sdksampleapp)
                SumUpLogin sumupLogin = SumUpLogin.builder("7ca84f17-84a5-4140-8df6-6ebeed8540fc").build();
                SumUpAPI.openLoginActivity(MainActivity.this, sumupLogin, REQUEST_CODE_LOGIN);
            }
        });

        Button logMerchant = (Button) findViewById(R.id.button_log_merchant);
        logMerchant.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!SumUpAPI.isLoggedIn()) {
                    mResultCode.setText("Result code: " + SumUpAPI.Response.ResultCode.ERROR_NOT_LOGGED_IN);
                    mResultMessage.setText("Message: Not logged in");
                } else {
                    mResultCode.setText("Result code: " + SumUpAPI.Response.ResultCode.SUCCESSFUL);
                    mResultMessage.setText(
                            String.format("Currency: %s, Merchant Code: %s", SumUpAPI.getCurrentMerchant().getCurrency().getIsoCode(),
                                    SumUpAPI.getCurrentMerchant().getMerchantCode()));
                }
            }
        });

        Button btnCharge = (Button) findViewById(R.id.button_charge);
        btnCharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SumUpPayment payment = SumUpPayment.builder()
                        // mandatory parameters
                        .total(new BigDecimal("1.12")) // minimum 1.00
                        .currency(SumUpPayment.Currency.EUR)
                        // optional: add details
                        .title("Taxi Ride")
                        .receiptEmail("customer@mail.com")
                        .receiptSMS("+3531234567890")
                        // optional: Add metadata
                        .addAdditionalInfo("AccountId", "taxi0334")
                        .addAdditionalInfo("From", "Paris")
                        .addAdditionalInfo("To", "Berlin")
                        // optional: foreign transaction ID, must be unique!
                        .foreignTransactionId(UUID.randomUUID().toString()) // can not exceed 128 chars
                        .build();

                SumUpAPI.checkout(MainActivity.this, payment, REQUEST_CODE_PAYMENT);
            }
        });

        Button cardReaderPage = (Button) findViewById(R.id.button_card_reader_page);
        cardReaderPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SumUpAPI.openCardReaderPage(MainActivity.this, REQUEST_CODE_CARD_READER_PAGE);
            }
        });

        Button prepareCardTerminal = (Button) findViewById(R.id.button_prepare_card_terminal);
        prepareCardTerminal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SumUpAPI.prepareForCheckout();
            }
        });

        Button btnLogout = (Button) findViewById(R.id.button_logout);
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SumUpAPI.logout();
            }
        });
    }

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

        switch (requestCode) {
            case REQUEST_CODE_LOGIN:
            case REQUEST_CODE_CARD_READER_PAGE:
                setResultCodeAndMessage(data);
                break;

            case REQUEST_CODE_PAYMENT:
                if (data != null) {
                    Bundle extra = data.getExtras();

                    mResultCode.setText("Result code: " + extra.getInt(SumUpAPI.Response.RESULT_CODE));
                    mResultMessage.setText("Message: " + extra.getString(SumUpAPI.Response.MESSAGE));

                    String txCode = extra.getString(SumUpAPI.Response.TX_CODE);
                    mTxCode.setText(txCode == null ? "" : "Transaction Code: " + txCode);

                    boolean receiptSent = extra.getBoolean(SumUpAPI.Response.RECEIPT_SENT);
                    mReceiptSent.setText("Receipt sent: " + receiptSent);

                    TransactionInfo transactionInfo = extra.getParcelable(SumUpAPI.Response.TX_INFO);
                    mTxInfo.setText(transactionInfo == null ? "" : "Transaction Info : " + transactionInfo);
                }
                break;

            default:
                break;
        }
    }

    private void setResultCodeAndMessage(Intent data) {
        if (data != null) {
            Bundle extra = data.getExtras();
            mResultCode.setText("Result code: " + extra.getInt(SumUpAPI.Response.RESULT_CODE));
            mResultMessage.setText("Message: " + extra.getString(SumUpAPI.Response.MESSAGE));
        }
    }

    private void resetViews() {
        mResultCode.setText("");
        mResultMessage.setText("");
        mTxCode.setText("");
        mReceiptSent.setText("");
        mTxInfo.setText("");
    }

    private void findViews() {
        mResultCode = (TextView) findViewById(R.id.result);
        mResultMessage = (TextView) findViewById(R.id.result_msg);
        mTxCode = (TextView) findViewById(R.id.tx_code);
        mReceiptSent = (TextView) findViewById(R.id.receipt_sent);
        mTxInfo = (TextView) findViewById(R.id.tx_info);
    }
}
 

drgottjr

Expert
Licensed User
Longtime User
where are these?
com.sumup.merchant.reader.models.TransactionInfo;
com.sumup.merchant.reader.api.SumUpAPI;
com.sumup.merchant.reader.api.SumUpLogin;
com.sumup.merchant.reader.api.SumUpPayment;
you can't do anything without them
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
where are these?
com.sumup.merchant.reader.models.TransactionInfo;
com.sumup.merchant.reader.api.SumUpAPI;
com.sumup.merchant.reader.api.SumUpLogin;
com.sumup.merchant.reader.api.SumUpPayment;
you can't do anything without them
I have the aars that are already being imported


B4X:
#Extends: android.support.v7.app.AppCompatActivity

#AdditionalJar: com.google.android.gms:play-services-base
#AdditionalJar: com.google.android.gms:play-services-basement
#AdditionalJar: com.google.android.gms:play-services-location
#AdditionalJar: com.google.android.gms:play-services-places-placereport
#AdditionalJar: com.google.android.gms:play-services-tasks

#MultiDex: true
#AdditionalJar: com.google.android.material:material

#AdditionalJar: com_sumup_designlib_circuit-ui_2.6.6_circuit-ui-2.6.6.aar
#AdditionalJar: com_sumup_designlib_solo-ui_2.6.6_solo-ui-2.6.6.aar
#AdditionalJar: com_sumup_analyticskit_core_0.0.18_core-0.0.18.aar
#AdditionalJar: com_sumup_android_logging_2.2.4_logging-2.2.4.aar
#AdditionalJar: com_sumup_base-analytics_4.3.0_base-analytics-4.3.0.aar
#AdditionalJar: com_sumup_base-common_4.3.0_base-common-4.3.0.aar
#AdditionalJar: com_sumup_base-network_4.3.0_base-network-4.3.0.aar
#AdditionalJar: com_sumup_identity_4.0.3_identity-4.0.3.aar
#AdditionalJar: com_sumup_merchant-sdk_4.3.0_merchant-sdk-4.3.0.aar
#AdditionalJar: com_sumup_mvp-base_4.3.0_mvp-base-4.3.0.aar
#AdditionalJar: com_sumup_reader-core_4.3.0_reader-core-4.3.0.aar
#AdditionalJar: com_sumup_receipts-core_4.3.0_receipts-core-4.3.0.aar



 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
declare a global javaobject:
dim jo as javaobject

inititialize it:
jo.initializecontext

declare and initialize your login button (or whatever you want to call it)
in its click sub, do your sumup setup
B4X:
sub login_click
    jo.RunMethod("setup", Array(jo))
end sub

as inline java, put this:
B4X:
#if Java
    import android.content.Context;
    import android.app.Activity;
    import com.sumup.merchant.reader.models.TransactionInfo;
    import com.sumup.merchant.reader.api.SumUpAPI;
    import com.sumup.merchant.reader.api.SumUpLogin;
    import com.sumup.merchant.reader.api.SumUpPayment;
    
    public void setup( Context cxt) {
        SumUpLogin sumupLogin = SumUpLogin.builder("7ca84f17-84a5-4140-8df6-6ebeed8540fc").build();
        SumUpAPI.openLoginActivity(cxt, sumupLogin, 1);
    }

#End If

remove (temporarily) the inline java stuff you currently have.
see if this simple test runs. MainActivity.this is related to context. there are a number of threads regarding how to pass BA's
properties. this is one way. see what happens. i expect it either to crash or to run silently since we don't know what to expect from openLoginActivity.
if some value were returned, we could ask to see what its value was. without that, either you crash or success is assumed. normally, you would expect
some kind of handle back that you would use for the transactions, but i don't see that here.
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
When trying to compile the code, it says that the types are incompatible, it seems that it expects the activity and is receiving the context (I don't really understand what the context is, is it as if it were the class that is executing?)

1700560356764.png
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
call it Activity instead.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
so now you can give me the 50 euros you wouldn't give to the other guy:)
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
telling me that you're "doing it" means nothing. exactly what happens after you do it? what's the exception? almost all the threads here relating to passing an activity to inline java involve initializing a javaobject and passing it. did you change public void setup( Context cxt) to public void setup( Activity cxt)? you can leave the "cxt". it's just a name. or change it to "act" or something if it's confusing.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Note that any additional activities must be listed inside the apps manifest.
EVEN (and especially) the Activities which are inside one or more of these AARs.

NONE of them are added automatically to the b4x manifest. It happens automatically in Android Studio but not in B4X.

It´s the library author which needs to add them manually.
I don´t have the mood to check the com.sumup.merchant jars/aar for them as i´m not interested in sumup
 
Upvote 0
Top