Integrate Payment UI with Your Ethereum Dapp

M.A. Hasan Molla

Engineer, Samsung Developer Program

The Samsung Blockchain Platform SDK offers functionalities to develop decentralized applications (Dapp). Developers can easily integrate the ETH send functionality into their application by using the APIs provided by the SDK.

The Samsung Blockchain Platform SDK also offers a simple payment UI, which makes Ether transactions much easier. Developers can integrate the payment UI with their Dapp.

This article demonstrates a step-by-step process to integrate the payment UI with the Dapp. Developers can learn how to use the payment UI, followed by connecting the Dapp with a hardware wallet, creating a new account, and fetching the account balance. So, let’s get started by integrating the payment UI.

Initialize the Instance of Samsung Blockchain Platform SDK

Before creating an instance of the Samsung Blockchain Platform SDK, implement its dependencies and set up your device environment for using Samsung Blockchain Keystore as a hardware wallet. Detailed information about the dependencies and the environment settings are described on the Getting Started page.

Next, create an instance (SBlockchain) of the Samsung Blockchain Platform SDK to communicate with the SDK by using the following code segment:

try {
    SBlockchain mSblockchain = new SBlockchain();
    mSblockchain.initialize(context);
} catch (SsdkUnsupportedException e) {
    if (e.getErrorType() == VENDOR_NOT_SUPPORTED){
        Log.e("error", "Platform SDK does not support this device");
    }
}

Connect to a Hardware Wallet

Samsung Blockchain Keystore is an integrated hardware wallet within Samsung devices. Get the devices list from here.

Connect the application with the hardware wallet by using the following code:

HardwareWalletType mHardwareWalletType = HardwareWalletType.SAMSUNG;
HardwareWalletManager mHardwareWalletManager = mSblockchain.getHardwareWalletManager();

mHardwareWalletManager.connect(mHardwareWalletType, true)
        .setCallback(new ListenableFutureTask.Callback<HardwareWallet>() {
    @Override
    public void onSuccess(HardwareWallet hardwareWallet) {
        mHardwareWallet = hardwareWallet;
        Log.i(LOG_TAG, "Wallet connected successfully.");
    }

    @Override
    public void onFailure(@NotNull ExecutionException e) {
        Log.e(LOG_TAG, "Wallet connection failed.");
    }

    @Override
    public void onCancelled(@NotNull InterruptedException e) {
        Log.e(LOG_TAG, "Wallet connection canceled.");
    }
});

Retrieve Accounts

Samsung Blockchain Platform SDK provides APIs to generate, restore, and retrieve public accounts. Use the following code snippet to retrieve the accounts:

CoinType coinType = CoinType.ETH;
NetworkType networkType = EthereumNetworkType.GOERLI;
String rpc = "https://goerli.infura.io/v3/71cf9f88c5bxxxxxxxxx";
 
mCoinNetworkInfo = new CoinNetworkInfo(coinType, networkType, rpc);

private List<Account> accountList = mSblockchain.getAccountManager()
        .getAccounts(mHardwareWallet.getWalletId(), mCoinNetworkInfo.getCoinType(), mCoinNetworkInfo.getNetworkType());

The SDK does not allow generating a new account if an unused account already exists. An account is unused if there is no balance or transactions on the account.

mAccountManager.generateNewAccount(mHardwareWallet, mCoinNetworkInfo)
        .setCallback(new ListenableFutureTask.Callback<Account>() {
    @Override
    public void onSuccess(Account account) {
        Log.i(LOG_TAG, "Account: " + account.getAddress());
    }

    @Override
    public void onFailure(@NotNull ExecutionException e) {
        Log.e(LOG_TAG, " Account creation failed." + e.getMessage());
    }

    @Override
    public void onCancelled(@NotNull InterruptedException e) {
        Log.e(LOG_TAG, "Account creation canceled.");
    }
});

Get Account Balance

After creating a new account, get some test Ether from the Ethereum faucet. The Samsung Blockchain Platform SDK provides the getBalance API to fetch the balance of an account from the blockchain.

EthereumService mCoinService = (EthereumService) CoinServiceFactory.getCoinService(getApplicationContext(), mCoinNetworkInfo);

mCoinService.getBalance(mFirstAccount).setCallback(new ListenableFutureTask.Callback<BigInteger>() {
    @Override
    public void onSuccess(BigInteger bigInteger) {
        convertedAmount = EthereumUtils.convertWeiToEth(bigInteger);
        Log.i(LOG_TAG, "Balance is:" + convertedAmount.toString());
    }

    @Override
    public void onFailure(@NotNull ExecutionException e) {
        Log.e(LOG_TAG, "Fetching balance failed."); 
    }

    @Override
    public void onCancelled(@NotNull InterruptedException e) {
        Log.e(LOG_TAG, "Fetching balance canceled.");
    }
});

Launch Payment Sheet Intent

The Samsung Blockchain Platform SDK provides a payment UI to simplify implementing transactions for developers. This UI shows all information regarding the transaction. Users can also select the transaction speed (how much they are willing to pay as transaction cost), although it is not fully customized.

The createEthereumPaymentSheetActivityIntent API creates an intent of the Ethereum payment sheet activity. The API supports both the legacy and EIP1559 transaction types. The payment sheet activity intent triggers either a legacy or EIP1559 transaction by passing the ‘transactionType’ parameter as EthereumTransactionType.LEGACY or EthereumTransactionType.EIP1559.

try {
    Intent intent = ethereumService.createEthereumPaymentSheetActivityIntent(
            this,
            mHardwareWallet,
            EthereumTransactionType.EIP1559,
            ethereumAccount,
            toAddress,
            SendAmount,
            data = null,
            nonce = null
    );
    activityResultLauncher.launch(intent);
} catch (Exception e) {
    Log.e(LOG_TAG, "Error in sending: " + e);
}

Get Payment Sheet Results

After the completion of the payment, a transaction hash is generated. To get the transaction hash, use the “txid” key in the received intent parameter.

ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult(),
    new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result) {
            switch (result.getResultCode()) {
                case Activity.RESULT_OK:
                    Log.d(LOG_TAG, "Transaction Hash: " + result.getData().getStringExtra("txid"));

                    break;
                case Activity.RESULT_CANCELED:
                    Log.e(LOG_TAG, "Transaction Failed: " + result.getData().getStringExtra("error"));
                    break;
            }
        }
    }
);
undefined
undefined
undefined
undefined

Now you know how to integrate the payment sheet UI into your Dapp. This lets you reduce the development complexity and provide your user with an integrated UI to complete their transaction.

Additional Resources

  1. Download the SBP SDK Payment Sheet Transaction Application
  2. More information on the Samsung Blockchain Platform SDK

Related Articles

  1. Send Ether with Samsung Blockchain Platform SDK
  2. Take your Ethereum Dapp to Galaxy Devices Around the World

Additional resources on the Samsung Developers site

The Samsung Developers site has many resources for developers looking to build for and integrate with Samsung devices and services. Stay in touch with the latest news by creating a free account and subscribing to our monthly newsletter. Visit the Galaxy Store Games page for information on bringing your game to Galaxy Store and visit the Marketing Resources page for information on promoting and distributing your Android apps. Finally, our Developer Forum is an excellent way to stay up-to-date on all things related to the Galaxy ecosystem.