Fragments

James Goodwin edited this page Mar 12, 2017 · 9 revisions

Fragments provide more flexibility in how you use the judo SDK, allowing you to customize the toolbar or handle behavior around events (e.g. the payment is declined or a connection error occurs).

Showing a PaymentFragment in your Activity

In your Activity's onCreate() method, create a new instance of a PaymentFragment and provide a Judo instance:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    if(savedInstanceState == null) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(Judo.JUDO_OPTIONS, new Judo.Builder()
                .setApiToken("<TOKEN>")
                .setApiSecret("<SECRET>")
                .setJudoId("<JUDO_ID>")
                .setEnvironment(Judo.SANDBOX)
                .setAmount("2.00")
                .setCurrency(Currency.GBP)
                .setConsumerReference("<CONSUMER_REFERENCE>")
                .build());

        PaymentFragment fragment = new PaymentFragment();
        fragment.setArguments(bundle);
    }
}

Next, we'll use the Android FragmentManager to show the PaymentFragment we've just created. Use the FragmentManager to start a transaction, then add the fragment to a layout in your activity:

getFragmentManager()
        .beginTransaction()
        .add(R.id.fragment_layout, fragment)
        .commit();

That is all you need to show a payment form in your Activity. Next, we'll add a callback to listen for events from the payment form, such as when a payment is successful or declined.

Handling the payment response

In your Activity, override the onActivityResult() method and check the resultCode parameter:

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

    if(requestCode == Judo.JUDO_REQUEST) {
        switch (resultCode) {
            case Judo.RESULT_SUCCESS:
                // handle success
                break;
                
            case Judo.RESULT_DECLINED:
                // handle declined
                break;
        }
    }
}

In addition to handling successful and declined payments, you should also handle Judo.RESULT_ERROR, Judo.RESULT_CONNECTION_ERROR and Judo.RESULT_TOKEN_EXPIRED to show a dialog message to the user instructing them what to do next.

Customizing the toolbar

The benefit of using Fragments is that you can easily customize the appearance of the toolbar shown above the fragment. Let's add a logo and change the toolbar text to match our company branding.

In the onCreate() method of your Activity, above the creation of the PaymentFragment, add this code to change the logo and text:

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setIcon(R.drawable.ic_launcher);
    actionBar.setTitle(R.string.app_name);
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.