Advertisement
Guest User

google billing

a guest
May 19th, 2024
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | Source Code | 0 0
  1.  
  2. public class Pagamentos {
  3.  
  4.     Activity activity;
  5.  
  6.     public Pagamentos(Activity activity){
  7.         this.activity = activity;
  8.  
  9.         billingSetup();
  10.     }
  11.  
  12.     private BillingClient billingClient;
  13.     private ProductDetails productDetails;
  14.     private Purchase purchase;
  15.  
  16.     static final String TAG = "InAppPurchaseTag";
  17.  
  18.     private void billingSetup() {
  19.  
  20.         billingClient = BillingClient.newBuilder(activity)
  21.                 .setListener(purchasesUpdatedListener)
  22.                 .enablePendingPurchases()
  23.                 .build();
  24.  
  25.         billingClient.startConnection(new BillingClientStateListener() {
  26.  
  27.             @Override
  28.             public void onBillingSetupFinished(
  29.                     @NonNull BillingResult billingResult) {
  30.  
  31.                 if (billingResult.getResponseCode() ==
  32.                         BillingClient.BillingResponseCode.OK) {
  33.                     Log.e(TAG, "OnBillingSetupFinish connected");
  34.                     queryProduct();
  35.                 } else {
  36.                     Log.e(TAG, "OnBillingSetupFinish failed");
  37.                 }
  38.             }
  39.  
  40.             @Override
  41.             public void onBillingServiceDisconnected() {
  42.                 Log.e(TAG, "OnBillingSetupFinish connection lost");
  43.             }
  44.         });
  45.     }
  46.  
  47.     private void queryProduct() {
  48.  
  49.         QueryProductDetailsParams queryProductDetailsParams =
  50.                 QueryProductDetailsParams.newBuilder()
  51.                         .setProductList(
  52.                                 ImmutableList.of(
  53.                                         QueryProductDetailsParams.Product.newBuilder()
  54.                                                 .setProductId("testeproduto")
  55.                                                 .setProductType(
  56.                                                         BillingClient.ProductType.INAPP)
  57.                                                 .build()))
  58.                         .build();
  59.  
  60.         billingClient.queryProductDetailsAsync(
  61.                 queryProductDetailsParams,
  62.                 new ProductDetailsResponseListener() {
  63.                     public void onProductDetailsResponse(
  64.                             @NonNull BillingResult billingResult,
  65.                             @NonNull List<ProductDetails> productDetailsList) {
  66.  
  67.                         if (!productDetailsList.isEmpty()) {
  68.                             productDetails = productDetailsList.get(0);
  69.                                 Log.e(TAG, "onProductDetailsResponse: " + productDetails.getName());
  70.                                 makePurchase();
  71.  
  72.                         } else {
  73.                             Log.e(TAG, "onProductDetailsResponse: No products");
  74.                         }
  75.                     }
  76.                 }
  77.         );
  78.     }
  79.     private final PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener() {
  80.         @Override
  81.         public void onPurchasesUpdated(BillingResult billingResult,
  82.                                        List<Purchase> purchases) {
  83.  
  84.             if (billingResult.getResponseCode() ==
  85.                     BillingClient.BillingResponseCode.OK
  86.                     && purchases != null) {
  87.                 for (Purchase pur : purchases) {
  88.                     completePurchase(pur);
  89.                 }
  90.             } else if (billingResult.getResponseCode() ==
  91.                     BillingClient.BillingResponseCode.USER_CANCELED) {
  92.                 Log.e(TAG, "onPurchasesUpdated: Purchase Canceled");
  93.             } else {
  94.                 Log.e(TAG, "onPurchasesUpdated: Error " + billingResult.getResponseCode() + billingResult.getDebugMessage());
  95.             }
  96.         }
  97.     };
  98.     public void makePurchase() {
  99.  
  100.         BillingResult isProductSupported = (billingClient.isFeatureSupported(BillingClient.FeatureType.PRODUCT_DETAILS));
  101.  
  102.         Log.e("rainbow", "suporte " + isProductSupported.getResponseCode() + " " + isProductSupported.getDebugMessage());
  103.  
  104.         BillingFlowParams billingFlowParams =
  105.                 BillingFlowParams.newBuilder()
  106.                         .setProductDetailsParamsList(
  107.                                 ImmutableList.of(
  108.                                         BillingFlowParams.ProductDetailsParams.newBuilder()
  109.                                                 .setProductDetails(productDetails)
  110.                                                 .build()
  111.                                 )
  112.                         )
  113.                         .build();
  114.  
  115.         billingClient.launchBillingFlow(activity, billingFlowParams);
  116.     }
  117.  
  118.     private void completePurchase(Purchase item) {
  119.  
  120.         purchase = item;
  121.  
  122.         if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED)
  123.                 Log.e("rainbow", "complete");
  124.     }
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement