Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Pagamentos {
- Activity activity;
- public Pagamentos(Activity activity){
- this.activity = activity;
- billingSetup();
- }
- private BillingClient billingClient;
- private ProductDetails productDetails;
- private Purchase purchase;
- static final String TAG = "InAppPurchaseTag";
- private void billingSetup() {
- billingClient = BillingClient.newBuilder(activity)
- .setListener(purchasesUpdatedListener)
- .enablePendingPurchases()
- .build();
- billingClient.startConnection(new BillingClientStateListener() {
- @Override
- public void onBillingSetupFinished(
- @NonNull BillingResult billingResult) {
- if (billingResult.getResponseCode() ==
- BillingClient.BillingResponseCode.OK) {
- Log.e(TAG, "OnBillingSetupFinish connected");
- queryProduct();
- } else {
- Log.e(TAG, "OnBillingSetupFinish failed");
- }
- }
- @Override
- public void onBillingServiceDisconnected() {
- Log.e(TAG, "OnBillingSetupFinish connection lost");
- }
- });
- }
- private void queryProduct() {
- QueryProductDetailsParams queryProductDetailsParams =
- QueryProductDetailsParams.newBuilder()
- .setProductList(
- ImmutableList.of(
- QueryProductDetailsParams.Product.newBuilder()
- .setProductId("testeproduto")
- .setProductType(
- BillingClient.ProductType.INAPP)
- .build()))
- .build();
- billingClient.queryProductDetailsAsync(
- queryProductDetailsParams,
- new ProductDetailsResponseListener() {
- public void onProductDetailsResponse(
- @NonNull BillingResult billingResult,
- @NonNull List<ProductDetails> productDetailsList) {
- if (!productDetailsList.isEmpty()) {
- productDetails = productDetailsList.get(0);
- Log.e(TAG, "onProductDetailsResponse: " + productDetails.getName());
- makePurchase();
- } else {
- Log.e(TAG, "onProductDetailsResponse: No products");
- }
- }
- }
- );
- }
- private final PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener() {
- @Override
- public void onPurchasesUpdated(BillingResult billingResult,
- List<Purchase> purchases) {
- if (billingResult.getResponseCode() ==
- BillingClient.BillingResponseCode.OK
- && purchases != null) {
- for (Purchase pur : purchases) {
- completePurchase(pur);
- }
- } else if (billingResult.getResponseCode() ==
- BillingClient.BillingResponseCode.USER_CANCELED) {
- Log.e(TAG, "onPurchasesUpdated: Purchase Canceled");
- } else {
- Log.e(TAG, "onPurchasesUpdated: Error " + billingResult.getResponseCode() + billingResult.getDebugMessage());
- }
- }
- };
- public void makePurchase() {
- BillingResult isProductSupported = (billingClient.isFeatureSupported(BillingClient.FeatureType.PRODUCT_DETAILS));
- Log.e("rainbow", "suporte " + isProductSupported.getResponseCode() + " " + isProductSupported.getDebugMessage());
- BillingFlowParams billingFlowParams =
- BillingFlowParams.newBuilder()
- .setProductDetailsParamsList(
- ImmutableList.of(
- BillingFlowParams.ProductDetailsParams.newBuilder()
- .setProductDetails(productDetails)
- .build()
- )
- )
- .build();
- billingClient.launchBillingFlow(activity, billingFlowParams);
- }
- private void completePurchase(Purchase item) {
- purchase = item;
- if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED)
- Log.e("rainbow", "complete");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement