Android Studio获取购买的计费项目的参考

时间:2019-06-04 05:08:08

标签: android billing

如何获取用户使用Google结算购买的产品/商品的参考信息?然后还可以参考商品说明

包含付款部分的活动

public class ponerDineroActivity extends AppCompatActivity implements PurchasesUpdatedListener {

    TextView dineroactual;
    Button loadProduct;
    BillingClient billingClient;
    RecyclerView recyclerProduct;
    String currentusermoney;
    FirebaseDatabase firebaseDatabase;
    SkuDetails skuDetails;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poner_dinero);
        dineroactual = findViewById(R.id.dineroactual);


        loadProduct = findViewById(R.id.aceptarpago);
        recyclerProduct = findViewById(R.id.recycler_precios);
        firebaseDatabase.getInstance().getReference();
        currentusermoney  = Prevalent.currentOnlineUser.getDinero();
        dineroactual.setText("Usted Tiene " + currentusermoney + " ARS");
        recyclerProduct.setHasFixedSize(true);
        recyclerProduct.setLayoutManager(new LinearLayoutManager(this));

        loadProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (billingClient.isReady())
                {
                    SkuDetailsParams  params = SkuDetailsParams.newBuilder()
                            .setSkusList(Arrays.asList("carga_100_pesos" , "carga_200_pesos" , "carga_300_pesos" ))
                            .setType(BillingClient.SkuType.INAPP) // INAPP / SUB
                            .build();

                    billingClient.querySkuDetailsAsync(params, new SkuDetailsResponseListener() {
                        @Override
                        public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
                            if (responseCode == BillingClient.BillingResponse.OK)
                            {
                                loadProductToRecyclerView(skuDetailsList);
                            }
                            else
                            {
                                Toast.makeText(ponerDineroActivity.this, "No logro cargarse la lista  :(", Toast.LENGTH_SHORT).show();
                            }
                        }

                    });
                }
                else
                {
                    Toast.makeText(ponerDineroActivity.this, "Billing client no esta listo", Toast.LENGTH_SHORT).show();
                }
            }
        });

        setupBillingClient();


    }

    private void setupBillingClient() {
        billingClient = BillingClient.newBuilder(this).setListener(this).build();
        billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(int responseCode) {
                if (responseCode == BillingClient.BillingResponse.OK)
                {
                    Toast.makeText(ponerDineroActivity.this, "EXITO", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(ponerDineroActivity.this, "Hubo un Error", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onBillingServiceDisconnected() {
                Toast.makeText(ponerDineroActivity.this, "Desconectado de Billing", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void loadProductToRecyclerView(List<SkuDetails> skuDetailsList) {
        MyProductAdapter adapter  = new MyProductAdapter(ponerDineroActivity.this, skuDetailsList,billingClient);
        recyclerProduct.setAdapter(adapter);

    }
    @Override
    public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
        //Here,  if user click to TAP-BUY, we will retrieve data here

//        Toast.makeText(this, "Purchase  item:" + purchases.size(), Toast.LENGTH_SHORT).show();
        if (responseCode == BillingClient.BillingResponse.OK)
        {
            skuDetails.getSku();
            String dineroconseguido = skuDetails.getDescription();




            int myNum = 0;
            int myNum2 = 0;
            try {
                myNum = Integer.parseInt(dineroconseguido);
            } catch(NumberFormatException nfe) {
            }
                try {
                    myNum2 = Integer.parseInt(currentusermoney);
                } catch(NumberFormatException nfe) {
            }

           int total = myNum + myNum2;

            String dinerorecibido = Integer.toString(total);

            Toast.makeText(this, dinerorecibido, Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(this, "Hubo  un Error", Toast.LENGTH_SHORT).show();
        }

    }


}

产品适配器类

public class MyProductAdapter extends RecyclerView.Adapter<MyProductAdapter.MyViewHolder> {

    ponerDineroActivity ponerDineroActivity;
    List<SkuDetails>  skuDetailsList;
    BillingClient billingClient;

    public MyProductAdapter(com.aplicacion.vivaluganoapp.ar.ponerDineroActivity ponerDineroActivity, List<SkuDetails> skuDetailsList, BillingClient billingClient) {
        this.ponerDineroActivity = ponerDineroActivity;
        this.skuDetailsList = skuDetailsList;
        this.billingClient = billingClient;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(ponerDineroActivity.getBaseContext())
                .inflate(R.layout.layout_product_item, viewGroup, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
        myViewHolder.txt_product.setText(skuDetailsList.get(i).getTitle());

        //Product Click
        myViewHolder.setiProductClickListener(new IProductClickListener() {
            @Override
            public void onProductClickListener(View view, int position) {
                //Launch Billing  follow
                BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                        .setSkuDetails(skuDetailsList.get(i))
                        .build();
                billingClient.launchBillingFlow(ponerDineroActivity,billingFlowParams);
            }
        });
    }

    @Override
    public int getItemCount() {
        return skuDetailsList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView txt_product;

        IProductClickListener iProductClickListener;

        public void setiProductClickListener(IProductClickListener iProductClickListener) {
            this.iProductClickListener = iProductClickListener;
        }

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            txt_product = (TextView) itemView.findViewById(R.id.txt_product_name);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            iProductClickListener.onProductClickListener(v,getAdapterPosition());
        }
    }
}

我需要的是能够识别用户购买了哪些产品/商品,然后获取产品说明,并将其添加到用户的当前积分(currentusermoney)中

编辑:添加了所有的课堂活动及其适配器

0 个答案:

没有答案