Skip to content

Instantly share code, notes, and snippets.

@rubixhacker
Last active August 29, 2015 14:27
Show Gist options
  • Save rubixhacker/456dac7ae6880729a8b3 to your computer and use it in GitHub Desktop.
Save rubixhacker/456dac7ae6880729a8b3 to your computer and use it in GitHub Desktop.
Simplify Android SDK 2.0.0 Alpha Documentation

To pull the alpha sdk inculde the following depenency in your build.gradle

compile 'com.simplify:sdk-android:2.0.0-alpha2@aar'

For Android Pay/Google Wallet include:

compile 'com.google.android.gms:play-services-wallet:7.8.0'

To use Android Pay add the Android Pay button to you view

// Check if WalletFragment already exists

SupportWalletFragment walletFragment = (SupportWalletFragment) getSupportFragmentManager()
    .findFragmentByTag(WALLET_FRAGMENT_ID);


if (walletFragment != null) {
  return;
}

// Define fragment style

WalletFragmentStyle fragmentStyle = new WalletFragmentStyle()
            .setBuyButtonText(BuyButtonText.BUY_NOW)
            .setBuyButtonAppearance(BuyButtonAppearance.CLASSIC)
            .setBuyButtonWidth(Dimension.MATCH_PARENT);

// Define fragment options

WalletFragmentOptions fragmentOptions = WalletFragmentOptions.newBuilder()
        .setEnvironment(WalletConstants.ENVIRONMENT_SANDBOX)
        .setFragmentStyle(fragmentStyle)
        .setTheme(WalletConstants.THEME_HOLO_LIGHT)
        .setMode(WalletFragmentMode.BUY_BUTTON)
        .build();

// Create a new instance of WalletFragment
    
walletFragment = SupportWalletFragment.newInstance(fragmentOptions);

// Initialize the fragment with start params
// Note: If using the provided helper method Simplify.handleAndroidPayResult(int, int, Intent),
//       you MUST set the request code to Simplify.REQUEST_CODE_MASKED_WALLET

WalletFragmentInitParams.Builder startParamsBuilder = WalletFragmentInitParams.newBuilder()
        .setMaskedWalletRequest(getMaskedWalletRequest())
        .setMaskedWalletRequestCode(Simplify.REQUEST_CODE_MASKED_WALLET);

walletFragment.initialize(startParamsBuilder.build());

// Add Wallet fragment to the U

getSupportFragmentManager().beginTransaction()
        .replace(R.id.buy_button_holder, walletFragment, WALLET_FRAGMENT_ID)
        .commit();

Create a MaskedWalletRequest

private MaskedWalletRequest getMaskedWalletRequest() {

    return MaskedWalletRequest.newBuilder()
            .setMerchantName("MasterCard labs")
            .setPhoneNumberRequired(true)
            .setShippingAddressRequired(true)
            .setCurrencyCode(CURRENCY_CODE_USD)
            .setCart(Cart.newBuilder()
                    .setCurrencyCode(CURRENCY_CODE_USD)
                    .setTotalPrice("5.00")
                    .addLineItem(LineItem.newBuilder()
                            .setCurrencyCode(CURRENCY_CODE_USD)
                            .setDescription("Mc labs")
                            .setQuantity("1")
                            .setUnitPrice("5.00")
                            .setTotalPrice("5.00")
                            .build())
                    .build())
            .setEstimatedTotalPrice("5.00")
            .build();
}

Add the Simplify Android Pay callbacks to your Activity/Fragment

public class MainActivity extends AppCompatActivity implements Simplify.AndroidPayCallback {

@Override
protected void onStart() {
    super.onStart();

    // register Android Pay callback
    Simplify.addAndroidPayCallback(this);
}

@Override
protected void onStop() {
    // remove Android Pay callback
    Simplify.removeAndroidPayCallback(this);

    super.onStop();
}

In your onActivityResult passed the returned data to Simplify

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // to get back MaskedWallet using call back method.
    if (Simplify.handleAndroidPayResult(requestCode, resultCode, data)) {
        return;
    }

    super.onActivityResult(requestCode, resultCode, data);
}

Pass the returned MaskedWallet to your ConfirmationActivty

@Override
public void onReceivedMaskedWallet(MaskedWallet maskedWallet) {
    // launch confirmation activity
    Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class);
    intent.putExtra(WalletConstants.EXTRA_MASKED_WALLET, maskedWallet);
    startActivity(intent);
}

Get the MaskedWallet from the Intent and get the GoogleApiClient

mGoogleApiClient = getGoogleApiClient();
mMaskedWallet = getIntent().getParcelableExtra(WalletConstants.EXTRA_MASKED_WALLET);

GoogleApiClient getGoogleApiClient() {

    return new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Wallet.API, new Wallet.WalletOptions.Builder()
                    .setEnvironment(WalletConstants.ENVIRONMENT_SANDBOX)
                    .setTheme(WalletConstants.THEME_HOLO_LIGHT)
                    .build())
            .build();
}

Add the SupportWalletFragment to your confirmation activity

void showConfirmationScreen(MaskedWallet maskedWallet) {

    //fragment style for confirmation screen
    WalletFragmentStyle walletFragmentStyle = new WalletFragmentStyle()
            .setMaskedWalletDetailsBackgroundColor(
                    getResources().getColor(android.R.color.white))
            .setMaskedWalletDetailsButtonBackgroundResource(
                    android.R.color.holo_orange_dark)
            .setMaskedWalletDetailsLogoTextColor(
                    getResources().getColor(android.R.color.black));

    WalletFragmentOptions walletFragmentOptions = WalletFragmentOptions.newBuilder()
            .setEnvironment(WalletConstants.ENVIRONMENT_SANDBOX)
            .setFragmentStyle(walletFragmentStyle)
            .setTheme(WalletConstants.THEME_HOLO_LIGHT)
            .setMode(WalletFragmentMode.SELECTION_DETAILS)
            .build();

    SupportWalletFragment walletFragment = SupportWalletFragment.newInstance(walletFragmentOptions);

    WalletFragmentInitParams.Builder startParamsBuilder = WalletFragmentInitParams.newBuilder()
            .setMaskedWallet(maskedWallet)
            .setMaskedWalletRequestCode(REQUEST_CODE_FULL_WALLET);

    walletFragment.initialize(startParamsBuilder.build());

    // add Wallet fragment to the UI
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.confirm_wallet_holder, walletFragment)
            .commit();
}

Add the Google Client and Simplify Callbacks to your Activity/Fragment

public class ConfirmationActivity extends AppCompatActivity implements
    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, Simplify.AndroidPayCallback {

    @Override
    protected void onStart() {
        super.onStart();

        Simplify.addAndroidPayCallback(this);
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        mGoogleApiClient.disconnect();
        Simplify.removeAndroidPayCallback(this);

        super.onStop();
    }

When the FullWallet is returned from Google pass it to Simplify to recieve a Simplify CardToken

@Override
public void onReceivedFullWallet(FullWallet fullWallet) {
    // Use fullwallet object to create token
    if(fullWallet != null) {

        Simplify.createCardToken(fullWallet, new CardToken.Callback() {
            @Override
            public void onSuccess(CardToken cardToken) {
                Log.i(TAG, "Card token created");
            }

            @Override
            public void onError(Throwable throwable) {
                Log.e(TAG, "Error Creating Token: " + throwable.getMessage());
            }
        });
    }
}

All callbacks are also availiable as RxJava Observables.

For example:

Simplify.createCardToken(fullWallet).subscribe(new Action1<CardToken>() {
            @Override
            public void call(CardToken cardToken) {
                Log.i(TAG, "Card token created");
            }
        });

Make sure you include RxAndroid in your project

compile 'io.reactivex:rxandroid:1.0.1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment