migrando proyecto de android studio billing_version = "3.0.0" a billing_version = "4.0.0"

  • Autor Autor venezuelaapps21
  • Fecha de inicio Fecha de inicio
venezuelaapps21

venezuelaapps21

Alfa
¡Usuario con pocos negocios! ¡Utiliza siempre saldo de Forobeta!
buenas tardes a todos estoy migrando un proyecto y no logro actualizar el código hay muy poca información para este tema acá les dejo el código y el error que genera
_________________________________________codigo_________________________________________________________
package com.liberty.appsindustry.studio.vpnfree.billing;

import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.billingclient.api.AcknowledgePurchaseParams;
import com.android.billingclient.api.AcknowledgePurchaseResponseListener;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesResponseListener;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsParams;
import com.android.billingclient.api.SkuDetailsResponseListener;
import com.liberty.appsindustry.studio.vpnfree.AppSettings;

import java.util.ArrayList;
import java.util.List;

public class BillingClass implements PurchasesUpdatedListener, BillingClientStateListener {

private BillingClient billingClient;
private List<String> skuListSubscriptionsList;
private List<SkuDetails> skuListFromStore;

//others
private boolean isAvailable = false;
private boolean isListGot = false;
private Activity refActivity;

private BillingErrorHandler mCallback;
private SkuDetailsListener mDetailsCallback;

//step-1 init
public BillingClass(Activity activity) {

refActivity = activity;

billingClient = BillingClient.newBuilder(activity)
.setListener(this)
.enablePendingPurchases()
.build();

skuListSubscriptionsList = new ArrayList<>();

//add all products here (subscriptions)
skuListSubscriptionsList.add(AppSettings.Companion.getOne_month_subscription_id());
skuListSubscriptionsList.add(AppSettings.Companion.getThree_month_subscription_id());
skuListSubscriptionsList.add(AppSettings.Companion.getOne_year_subscription_id());
}

//step-2 make connection to store
public void startConnection() {
billingClient.startConnection(this);
}

public void setmCallback(BillingErrorHandler mCallback, SkuDetailsListener skuDetailsListener) {
if (this.mCallback == null) {
this.mCallback = mCallback;
}
if (this.mDetailsCallback == null) {
this.mDetailsCallback = skuDetailsListener;
}
}

//purchase update listener
//step-5
@Override
public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> list) {

if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& list != null) {
for (Purchase purchase : list) {
handlePurchase(purchase);
}
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
// Handle an error caused by a user cancelling the purchase flow.

} else {
// Handle any other error codes.
}
}

//step-3
//state listener
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResult) {

if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {

mCallback.displayErrorMessage("done");

//proceed, Client is ready
isAvailable = true;

SkuDetailsParams.Builder subscriptionParams = SkuDetailsParams.newBuilder();
SkuDetailsParams.Builder oneTimeProductsParams = SkuDetailsParams.newBuilder();

subscriptionParams.setSkusList(skuListSubscriptionsList).setType(BillingClient.SkuType.SUBS);

if (!skuListSubscriptionsList.isEmpty()) {
billingClient.querySkuDetailsAsync(subscriptionParams.build(), new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(@NonNull BillingResult billingResult, @Nullable List<SkuDetails> list) {

//subscription list
skuListFromStore = list;
isListGot = true;
mDetailsCallback.subscriptionsDetailList(list);
}
});
}

} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.SERVICE_UNAVAILABLE || billingResult.getResponseCode() == BillingClient.BillingResponseCode.ERROR || billingResult.getResponseCode() == BillingClient.BillingResponseCode.BILLING_UNAVAILABLE) {
mCallback.displayErrorMessage("error");
}
}

@Override
public void onBillingServiceDisconnected() {
//restart, No connection to billing service
isAvailable = false;
}

public boolean isAvailable() {
return isAvailable;
}

public boolean isListGot() {
return isListGot;
}

//step-4
public String purchaseSubscriptionItemByPos(int itemIndex) {

int index = itemIndex;

for (int i = 0; i < skuListFromStore.size(); i++) {
if (skuListFromStore.get(i).getSku().equals(skuListSubscriptionsList.get(itemIndex))) {
index = i;
}
}

if (billingClient.isReady()) {

BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuListFromStore.get(index))
.build();

BillingResult responseCode = billingClient.launchBillingFlow(refActivity, billingFlowParams);

switch (responseCode.getResponseCode()) {

case BillingClient.BillingResponseCode.BILLING_UNAVAILABLE:
mCallback.displayErrorMessage("Billing not supported for type of request");
return "Billing not supported for type of request";


case BillingClient.BillingResponseCode.ITEM_NOT_OWNED:
case BillingClient.BillingResponseCode.DEVELOPER_ERROR:
return "";

case BillingClient.BillingResponseCode.ERROR:
mCallback.displayErrorMessage("Error completing request");
return "Error completing request";

case BillingClient.BillingResponseCode.FEATURE_NOT_SUPPORTED:
return "Error processing request.";

case BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED:
return "Selected item is already owned";

case BillingClient.BillingResponseCode.ITEM_UNAVAILABLE:
return "Item not available";

case BillingClient.BillingResponseCode.SERVICE_DISCONNECTED:
return "Play Store service is not connected now";

case BillingClient.BillingResponseCode.SERVICE_TIMEOUT:
return "Timeout";

case BillingClient.BillingResponseCode.SERVICE_UNAVAILABLE:
mCallback.displayErrorMessage("Network error.");
return "Network Connection down";

case BillingClient.BillingResponseCode.USER_CANCELED:
mCallback.displayErrorMessage("Request Canceled");
return "Request Canceled";

case BillingClient.BillingResponseCode.OK:
return "Subscribed Successfully";
}
}

return "";
}

void handlePurchase(Purchase purchase) {
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(@NonNull BillingResult billingResult) {

}
});
}
}


public boolean isSubscribedToSubscriptionItem(String sku) {
if (skuListSubscriptionsList != null) {
Purchase.PurchasesResult result = billingClient.queryPurchases(BillingClient.SkuType.SUBS);

if (result.getResponseCode() == BillingClient.BillingResponseCode.OK && result.getPurchasesList() != null) {
for (Purchase purchase :
result.getPurchasesList()) {

if (purchase.getSku().equals(sku)) {
return true;
} else {
return false;
}
}
}
}

return false;
}

//message interface
public interface BillingErrorHandler {
void displayErrorMessage(String message);
}

public interface SkuDetailsListener {
void subscriptionsDetailList(List<SkuDetails> skuDetailsList);
}
}
___________________________________________fin codigo__________________________________________

_______________________________________________error al complilar_________________________________
if (purchase.getSku().equals(sku)) {
^
symbol: method getSku()
location: variable purchase of type Purchase
________________________________________________________________________________________
ESPERO ALGUNO PUEDA AYUDARME YO TRABAJO CON ANDROID STUDIO
 
Hola, ¿Que tal @venezuelaapps21 ? getSku() fue reemplazado por getSkus()
  • Added Purchase#getSkus() and PurchaseHistoryRecord#getSkus(). These replace Purchase#getSku and PurchaseHistoryRecord#getSku which have been removed.
Puedes leerlo aqui en las notas de cambios. https://developer.android.com/google/play/billing/release-notes#4-0


Prueba cambiando la sentencia if que te da problemas, por éste codigo.

Java:
if (purchase.getSkus().contains(sku)) {
return true;
} else {
return false;
}
 
Última edición:
Hola, ¿Que tal @venezuelaapps21 ? getSku() fue reemplazado por getSkus()
  • Added Purchase#getSkus() and PurchaseHistoryRecord#getSkus(). These replace Purchase#getSku and PurchaseHistoryRecord#getSku which have been removed.
Puedes leerlo aqui en las notas de cambios. https://developer.android.com/google/play/billing/release-notes#4-0


Prueba cambiando la sentencia if que te da problemas, por éste codigo.

Java:
if (purchase.getSkus().contains(sku)) {
return true;
} else {
return false;
}
gracias por tu respuesta amigo si el error se quita pero la app no pasa de la pantalla de entrada
 
No problem bro.

Quizás se deba a otra cosa. ¿El log te da alguna información?
if (purchase.getSku().equals(sku)) {
^
symbol: method getSku()
location: variable purchase of type Purchase

este es el error amigo
 
if (purchase.getSku().equals(sku)) {
^
symbol: method getSku()
location: variable purchase of type Purchase

este es el error amigo
Es el mismo error que menciona usted al principio del post.

Esa sentencia if, tiene que cambiarla por el código que le dejé en el post anterior.
 
Es el mismo error que menciona usted al principio del post.

Esa sentencia if, tiene que cambiarla por el código que le dejé en el post anterior.
cambio el código compila pero la app no pasa de la pantalla de bienvenida
 
cambio el código compila pero la app no pasa de la pantalla de bienvenida
¿Y cuando está en la pantalla de bienvenida le da alguna información en el log?
Puede ser por otra cosa diferente a la libreria de compras que está usando.
 
¿Y cuando está en la pantalla de bienvenida le da alguna información en el log?
Puede ser por otra cosa diferente a la libreria de compras que está usando.
2022-09-03 20:28:41.561 26192-26348/ E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
2022-09-03 20:28:41.612 26192-26348/ E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
 
Última edición:
2022-09-03 20:28:41.561 26192-26348/com.xxxxx.xxxxx.xxxx.xxxx E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
2022-09-03 20:28:41.612 26192-26348/com.xxxxx.xxxxx.xxxx.xxxx E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
Habría que hacer un debug más a profundidad para ver que es lo que esta causando el problema. ¿Estas usando un webview?
 
Habría que hacer un debug más a profundidad para ver que es lo que esta causando el problema. ¿Estas usando un webview?
si en una parte es una app de vpn
 

billing_version = "3.0.0" a billing_version = "4.0.0" el problema esta aqui cuando actualizo a la version 4 que me lo pide playstore con la version 3 funciona todo perfecto​

 
Atrás
Arriba