| 259 | } |
| 260 | |
| 261 | private void purchase(String productIdOrJson, CallbackContext callbackContext) { |
| 262 | try { |
| 263 | if (productIdOrJson == null || productIdOrJson.trim().isEmpty()) { |
| 264 | callbackContext.error("Product ID cannot be null or empty"); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | final String productId = productIdOrJson; |
| 269 | |
| 270 | List<QueryProductDetailsParams.Product> productList = new ArrayList<>(); |
| 271 | productList.add( |
| 272 | QueryProductDetailsParams.Product.newBuilder() |
| 273 | .setProductId(productId) |
| 274 | .setProductType(BillingClient.ProductType.INAPP) |
| 275 | .build() |
| 276 | ); |
| 277 | QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder() |
| 278 | .setProductList(productList) |
| 279 | .build(); |
| 280 | |
| 281 | billingClient.queryProductDetailsAsync( |
| 282 | params, |
| 283 | new ProductDetailsResponseListener() { |
| 284 | public void onProductDetailsResponse( |
| 285 | BillingResult billingResult, |
| 286 | QueryProductDetailsResult queryProductDetailsResult |
| 287 | ) { |
| 288 | if (billingResult.getResponseCode() == BillingResponseCode.OK) { |
| 289 | List<ProductDetails> productDetailsList = queryProductDetailsResult.getProductDetailsList(); |
| 290 | if (!productDetailsList.isEmpty()) { |
| 291 | ProductDetails productDetails = productDetailsList.get(0); |
| 292 | BillingResult result = billingClient.launchBillingFlow( |
| 293 | activityRef.get(), |
| 294 | BillingFlowParams.newBuilder().setProductDetailsParamsList( |
| 295 | Arrays.asList( |
| 296 | BillingFlowParams.ProductDetailsParams.newBuilder() |
| 297 | .setProductDetails(productDetails) |
| 298 | .build() |
| 299 | ) |
| 300 | ).build() |
| 301 | ); |
| 302 | int responseCode = result.getResponseCode(); |
| 303 | if (responseCode == BillingResponseCode.OK) { |
| 304 | callbackContext.success(); |
| 305 | } else { |
| 306 | callbackContext.error(responseCode); |
| 307 | } |
| 308 | } else { |
| 309 | callbackContext.error("No product details found for: " + productId); |
| 310 | } |
| 311 | } else { |
| 312 | callbackContext.error(billingResult.getResponseCode()); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | ); |
| 317 | } catch (Exception e) { |
| 318 | callbackContext.error("Purchase error: " + e.getMessage()); |