public class BuyPointActivity extends AppCompatActivity implements PurchasesUpdatedListener {
Context mContext;
// create new Person
private BillingClient mBillingClient;
SkuDetails skuDetails700, skuDetails2100;
String skuID700 = "point_700", skuID2100 = "point_2100"; //제품 ID
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buy_point);
mContext = BuyPointActivity.this;
...
mBillingClient = BillingClient.newBuilder(mContext).setListener(this).build();
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingResponse int billingResponseCode) {
if (billingResponseCode == BillingResponse.OK) {
// The billing client is ready. You can query purchases here.
List<String> skuList = new ArrayList<> ();
skuList.add(skuID700);
skuList.add(skuID2100);
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) {
// Process the result.
if (responseCode == BillingClient.BillingResponse.OK && skuDetailsList != null) {
for (SkuDetails skuDetails : skuDetailsList) {
String sku = skuDetails.getSku();
String price = skuDetails.getPrice();
if(skuID700.equals(sku)) {
skuDetails700 = skuDetails;
} else if(skuID2100.equals(sku)) {
skuDetails2100 = skuDetails;
}
}
}
}});
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
}
});
}
/**
* Handle a callback that purchases were updated from the Billing library
*/
@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
if (responseCode == BillingClient.BillingResponse.OK
&& purchases != null) {
for (Purchase purchase : purchases) {
handlePurchase(purchase);
}
} else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
// Handle an error caused by a user cancelling the purchase flow.
} else {
// Handle any other error codes.
}
}
private void doBillingFlow(SkuDetails skuDetails) {
BillingFlowParams flowParams;
int responseCode;
// Retrieve a value for "skuDetails" by calling querySkuDetailsAsync().
flowParams = BillingFlowParams.newBuilder().setSkuDetails(skuDetails).build();
responseCode = mBillingClient.launchBillingFlow(BuyPointActivity.this, flowParams);
/*if(responseCode == BillingClient.BillingResponse.ITEM_ALREADY_OWNED) {
Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.INAPP);
onPurchasesUpdated(BillingClient.BillingResponse.OK, purchasesResult.getPurchasesList());
}*/
}
private void handlePurchase(Purchase purchase) {
String purchaseToken;
purchaseToken = purchase.getPurchaseToken();
mBillingClient.consumeAsync(purchaseToken, consumeListener);
}
ConsumeResponseListener consumeListener = new ConsumeResponseListener() {
@Override
public void onConsumeResponse(@BillingClient.BillingResponse int responseCode, String outToken) {
if (responseCode == BillingClient.BillingResponse.OK) {
// Handle the success of the consume operation.
// For example, increase the number of coins inside the user's basket.
}
}
};
Button.OnClickListener mClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn700:
doBillingFlow(skuDetails700);
break;
case R.id.btn2100:
doBillingFlow(skuDetails2100);
break;
...
}
}
};
...
}