In App Billing (IAB) SDK

Neurophobic Animal edited this page May 18, 2018 · 9 revisions

Getting Started

To integrate the In App Billing (IAB) SDK, you only need an instance of the AppCoinsSdk interface. In the sample code, we create an instance of the IAB SDK in the Application class.

  public static AppCoinsIab appCoinsSdk;

  private final String developerAddress = "0x4fbcc5ce88493c3d9903701c143af65f54481119";

  @Override public void onCreate() {
    super.onCreate();

    appCoinsSdk = new AppCoinsIabBuilder(developerAddress).withSkus(buildSkus())
        .withDebug(true)
        .createAppCoinsIab();
  }

  private List<SKU> buildSkus() {
    List<SKU> skus = new LinkedList<>();

    skus.add(new SKU(Skus.SKU_GAS_LABEL, Skus.SKU_GAS_ID, BigDecimal.valueOf(1)));
    skus.add(new SKU(Skus.SKU_PREMIUM_LABEL, Skus.SKU_PREMIUM_ID, BigDecimal.valueOf(2)));

    return skus;
  }

Here we use a convenient builder to create our AppCoinsSDK instance with a list consisting of two products. The debug flag will set the IAB SDK to use the testnet (Ropsten) instead of the mainnet. If the SDK uses the testnet, APPC and ETH spent using the SDK do not have any monetary value and are only used for testing purposes.

Given the Android architecture, you will have to let the SDK know each time a Purchase Flow is finished. For that, just inform the SDK:

  AppCoinSdk  appCoinsSdk = AppCoinsSdkSingleton.getAppCoinsSdk();

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

AppCoinsSdk's onActivityResult will return true if handled, and false otherwise. This may be useful if you want to include further logic at this point.

To start the purchase flow, you have to pass one of the previously defined SKU_IDs, and the activity that will be used to call the Wallet (please notice, since the sdk is using RxJava, you have to subscribe the Completable returned by the sdk):

appCoinsSdk.buy(SKU_GAS, this).subscribe(() -> {
      // In this case the buy process was triggered as expected.
}, throwable -> {
      // There was an error triggering the buy process.
});

And finally, we want to react to the purchase, so we can reflect that change in our App's state.

Below we provide a good pattern to follow:

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

    if (appCoinsSdk.onActivityResult(requestCode, requestCode, data)) {
      appCoinsSdk.getCurrentPayment()
          .subscribe(paymentDetails -> runOnUiThread(() -> {
            if (paymentDetails.getPaymentStatus() == PaymentStatus.SUCCESS) {
              String skuId = paymentDetails.getSkuId();
              // Now we tell the sdk to consume the skuId.
              Application.appCoinsSdk.consume(skuId);

              // Purchase successfully done. Release the prize.
            }
          }));
    }
  }

First we check if it was the IAB SDK who made us leave the current activity. If so, we will react to the purchase result. In order to complete the purchase flow, we must consume the purchase.

Clone this wiki locally
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.