InApp Subscription Setup and Testing

April 23, 2018 0 Comments

Step 1:


public class SubscriptionActivity extends AppCompatActivity implements View.OnClickListener, PurchasesUpdatedListener, BillingClientStateListener {

TextView btn_select_asker, btn_select_creator;
public static final String LOG_TAG = "InAppPurchase:";

List<String> ownedSkuList = new ArrayList<>();
List<String> ownedSignatureList = new ArrayList<>();
List<SkuDetails> availableItems = new ArrayList<>();
private BillingClient mBillingClient;

private boolean isConnected = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subscription_activity);
initView();
subScriptionInit();
}

public void subScriptionInit() {
mBillingClient = BillingClient.newBuilder(this).setListener(this).build();
mBillingClient.startConnection(this); // For Connection...

btn_type_1 = (TextView) findViewById(R.id.btn_type_1);
btn_type_2 = (TextView) findViewById(R.id.btn_type_2);

btn_type_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getAvailItems();
// String skuId = "<app_name>";
// String skuType = BillingClient.SkuType.INAPP;
String skuType = BillingClient.SkuType.SUBS;

purchase("<your_subscription_name_type_1>", skuType);

}
});



btn_type_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// getAvailItems();
// String skuId = "<app_name>";
// String skuType = BillingClient.SkuType.INAPP;
String skuType = BillingClient.SkuType.SUBS;
purchase("<your_subscription_name_type_2>", skuType);

}
});

}

@Override
public void onClick(View view) {
switch (view.getId()) {

case R.id.txt_inf_settings_change_subscription:
getAvailItems();
String skuId = "<app_name>";
// String skuType = BillingClient.SkuType.INAPP; //It's for InApp purchase
String skuType = BillingClient.SkuType.SUBS; //It's for InApp subscriptio
purchase(skuId, skuType);

Toast.makeText(activity, "Change subscription.", Toast.LENGTH_SHORT).show();
break;

case R.id.txt_inf_settings_cancel_subscription:
Toast.makeText(activity, "Cancel Subscripton.", Toast.LENGTH_SHORT).show();
break;
}
}

// ------- SUBSCRIPTION CODE Paste on Activity --------------


@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
if (responseCode == BillingClient.BillingResponse.OK) {

// Manage list of retrieved items...
if (purchases != null) {
for (int i = 0; i < purchases.size(); i++) {
Log.v(LOG_TAG, i + ": " + purchases.get(i)); // Get Single Item...

// Storing purchased items...
ownedSkuList.add(purchases.get(i).getSku());
ownedSignatureList.add(purchases.get(i).getSignature());
}
} else {
Toast.makeText(this, "You have not purchased any thing yet.", Toast.LENGTH_SHORT).show();
}

} else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
Toast.makeText(this, "You have cancelled retrieving list.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Something went wrong. Please Try Again Later..", Toast.LENGTH_SHORT).show();
}
}


@Override
public void onBillingSetupFinished(int responseCode) {
if (responseCode == BillingClient.BillingResponse.OK) {
// The billing client is ready. You can query purchases here.
isConnected = true;
}
}

@Override
public void onBillingServiceDisconnected() {
isConnected = false;
Toast.makeText(this, "Connection could not Established. Please Try Again Later..", Toast.LENGTH_LONG).show();
}

private void getAvailItems() {
if (isConnected) {
List<String> skuList = new ArrayList<>();
skuList.add("<your_subscription_name_type_1>");
skuList.add("<your_subscription_name_type_2>");
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
mBillingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
if (responseCode == BillingClient.BillingResponse.OK) {

// Getting Available Items for Purchase...
availableItems = skuDetailsList;
if (availableItems != null && skuDetailsList != null && availableItems.size() > 0) {
Log.e("detailed", "=>" + availableItems.size() + "=>" + availableItems.get(0).getDescription());
Log.e("detailed", "=>" + availableItems.get(0).getFreeTrialPeriod() + "=>" + availableItems.get(0).getPrice());
}

// Check and display if items already purchased...
}
}
});
}
}

/**
* purchase Item...
*/

private void purchase(String skuId, String skuType) {
// BillingFlowParams flowParams = BillingFlowParams.newBuilder().setSku(skuId).setType(skuType).build();
BillingFlowParams flowParams = BillingFlowParams.newBuilder().setSku(skuId).setType(skuType).build();

int responseCode = mBillingClient.launchBillingFlow(this, flowParams);

if (responseCode == BillingClient.BillingResponse.OK) {
checkPurchased();
} else {
Toast.makeText(this, "Cannot Purchase Item. Please Try Again.", Toast.LENGTH_SHORT).show();
}
}


private void checkPurchased() {
mBillingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP, new PurchaseHistoryResponseListener() {
@Override
public void onPurchaseHistoryResponse(@BillingClient.BillingResponse int responseCode, List<Purchase> purchasesList) {
if (responseCode == BillingClient.BillingResponse.OK && purchasesList != null) {
for (Purchase purchase : purchasesList) {
// Process the result.
Log.v(LOG_TAG, "new purchased item" + purchase.getSku());
}
}
}
});

}


}

Step 2: Create Developer Merchant Account


Step 3: Selecte App > Store Presence > In-app Product


Step 4: See the Screen Shot :


Contributer

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Popular Posts