0

I tried looking at previous questions and answers but with no luck finding a real solution. I'm acknowledging purchases once they are processed although my users are still automatically refunded. Below is my whole purchase activity. It's a simple activity with a big button to purchase the app's full version. Anoter button to restore previous purchases. A third button to show some advice if users were not able to restore their purchase.

Any help would be appreciated!

public class BuyActivity extends AppCompatActivity {

    Button unlock_button;
    Button restore_purchase;

    static final String PRODUCT_ID = "full_version";

    private BillingClient billingClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_buy);

        unlock_button = findViewById(R.id.unlock_button);
        restore_purchase = findViewById(R.id.restore_purchase);

        billingClient = BillingClient.newBuilder(getApplicationContext())
                .setListener((billingResult, list) -> {
                    // responds to all purchases that were already made
                    if (billingResult.getResponseCode()==BillingClient.BillingResponseCode.OK && list!= null) {
                        for (Purchase purchase:list){
                            if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
                                
acknowledge_purchase(purchase);
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        features_unlocked();
                                    }
                                },500);
                            } else {
                                Toast.makeText(getApplicationContext(), "ERROR OCCURRED", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } else if (billingResult.getResponseCode()==BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED && list!=null) {
                        features_unlocked();
                    } else {
                        Toast.makeText(getApplicationContext(), "ERROR OCCURRED", Toast.LENGTH_SHORT).show();
                    }
                })
                .enablePendingPurchases()
                .build();

        connect_to_google();

        restore_purchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                restore_purchase();
            }
        });

        unlock_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //launch_purchase_flow(productDetailsList.get(0));
                Toast.makeText(getApplicationContext(), "CAN'T START THE PURCHASE PROCESS", Toast.LENGTH_SHORT).show();
            }
        });
    }

    void features_unlocked(){
            App.get().MyTheme = 1;
            Intent i = new Intent(BuyActivity.this, Unlocked_celebration.class);
            startActivity(i);
            finish();

    }

    void connect_to_google(){
        billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                if (billingResult.getResponseCode() ==  BillingClient.BillingResponseCode.OK) {
                    // The BillingClient is ready. You can query purchases here.
                    get_product_details(); // download all details to different variables as you want
                    Log.i( "appName", String.format( "billing setup response code  %s", billingResult.toString() ) );
                }
            }
            @Override
            public void onBillingServiceDisconnected() {
                connect_to_google();
                // Try to restart the connection on the next request to
                // Google Play by calling the startConnection() method.
            }
        });
    }


    //show products available to buy
    void get_product_details(){
        List<String> productIds = new ArrayList<>();
        productIds.add("full_version");
        SkuDetailsParams params = SkuDetailsParams
                .newBuilder()
                .setSkusList(productIds)
                .setType(BillingClient.SkuType.INAPP)
                .build();
        billingClient.querySkuDetailsAsync(
                params,
                new SkuDetailsResponseListener() {
                    @Override
                    public void onSkuDetailsResponse(@NonNull BillingResult billingResult, @Nullable List<SkuDetails> list) {
                        if (billingResult.getResponseCode()==BillingClient.BillingResponseCode.OK && list!=null){
                            if (list.size()>0) {
                                Log.println(Log.DEBUG,"TAG", "onSkuDetailsResponse: " + list.get(0).getPrice());

                                unlock_button.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        billingClient.launchBillingFlow(
                                                BuyActivity.this,
                                                BillingFlowParams.newBuilder().setSkuDetails(list.get(0)).build()
                                        );
                                    }
                                });
                            }
                        }
                    }
                }
        );
    }

    void acknowledge_purchase(Purchase purchase){
        AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams
                .newBuilder()
                .setPurchaseToken(purchase.getPurchaseToken())
                .build();
        billingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
            @Override
            public void onAcknowledgePurchaseResponse(@NonNull BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {

                }
            }
        });
    }

    void restore_purchase(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                connect_to_google();

                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        billingClient.queryPurchasesAsync(
                                BillingClient.SkuType.INAPP,
                                new PurchasesResponseListener() {
                                    @Override
                                    public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {
                                        if (billingResult.getResponseCode()==BillingClient.BillingResponseCode.OK) {
                                            for (Purchase purchase: list){
                                                if (purchase.getPurchaseState()==Purchase.PurchaseState.PURCHASED
                                                        && !purchase.isAcknowledged()) {
                                                    acknowledge_purchase(purchase);
                                                    features_unlocked();
                                                } else if (purchase.getPurchaseState()==Purchase.PurchaseState.PURCHASED) {
                                                    features_unlocked();
                                                }
                                            }
                                        } else if (billingResult.getResponseCode()==BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {
                                            features_unlocked();
                                        }
                                    }
                                }
                        );
                    }
                });
            }
        },300);
    }

    protected void onResume() {
        super.onResume();
        restore_purchase();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (billingClient != null) billingClient.endConnection();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onDestroy() {
        if (billingClient != null) billingClient.endConnection();
        billingClient=null;
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (billingClient != null) billingClient.endConnection();
    }

    public void cantRestore(View v){
        Intent i = new Intent(BuyActivity.this, RestorePurchase.class);
        startActivity(i);
    }
}
Bialy
  • 905
  • 2
  • 12
  • 22

0 Answers0