1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
| package com.test.plannine.ui.purchase;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
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.PurchasesUpdatedListener;
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsParams;
import com.android.billingclient.api.SkuDetailsResponseListener;
import com.sarinoty.plannine.PlanDBHelper;
import com.sarinoty.plannine.Profil;
import com.sarinoty.plannine.R;
import com.sarinoty.plannine.Variables;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
* Use the {@link PurchaseFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class PurchaseFragment extends Fragment implements PurchasesUpdatedListener {
Button btnPurchase;
private BillingClient billingClient;
private List skuList = new ArrayList();
private String sku = "versionfull";
private SkuDetails mSkuDetails;
private PlanDBHelper mDatabase;
private Profil profil;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public PurchaseFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment PurchaseFragment.
*/
// TODO: Rename and change types and number of parameters
public static PurchaseFragment newInstance(String param1, String param2) {
PurchaseFragment fragment = new PurchaseFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root;
if (Variables.full) // Quand l'achet est reconnu comme effectué, la variable "full" (booleen) de ma classe "Variables" est définie sur "true".
root = inflater.inflate(R.layout.fragment_purchase, container, false); // Ce xml ne contient qu'un textview qui dit "Vous possédez déjà la version complête"
else {
// Ce xml contient des textviews qui expliquent pourquoi acheter la version complète et un bouton qui est défini comme non enable à la base
root = inflater.inflate(R.layout.fragment_purchase2, container, false);
btnPurchase = (Button) root.findViewById(R.id.btnPurchase);
btnPurchase.setEnabled(false);
mDatabase = new PlanDBHelper(getActivity());
skuList.add(sku);
setupBillingClient();
}
// Inflate the layout for this fragment
return root;
}
private void setupBillingClient() {
billingClient = BillingClient.newBuilder(getContext()).enablePendingPurchases().setListener(this).build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
System.out.println(billingResult.getResponseCode());
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
System.out.println("******** Connecté ********"); // Ce message apparaît dans le logcat donc j'en déduis que la connexion avec le service Google est bien établie
loadAllSKUs();
}
else
System.out.println("****** Quelques chose ne va pas ******");
}
@Override
public void onBillingServiceDisconnected() {
System.out.println("******** Non connecté ********");
}
});
}
private void loadAllSKUs() {
if (billingClient.isReady()) {
SkuDetailsParams params = SkuDetailsParams.newBuilder()
.setSkusList(skuList)
.setType(BillingClient.SkuType.INAPP)
.build();
billingClient.querySkuDetailsAsync(params, new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(@NonNull BillingResult billingResult, @Nullable List<SkuDetails> skuDetailsList) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && !skuDetailsList.isEmpty()) {
for (Object skuDetailsObject : skuDetailsList) {
final SkuDetails skuDetails = (SkuDetails) skuDetailsObject;
if (skuDetails.getSku().equals(sku)) {
System.out.println("****** On est entrés ici ******"); // Ce message apparaît bien dans le logcat
Purchase.PurchasesResult result = billingClient.queryPurchases(BillingClient.SkuType.INAPP);
List<Purchase> purchases = result.getPurchasesList();
System.out.println(purchases.size()); // Ici la valeur retournée est "0". Je ne comprends pas pourquoi.
boolean isOwned = false;
for (Purchase purchase : purchases) {
System.out.println("On passe par ici ? ******"); // Ce message et les suivants n'apparaîssent pas dans le logcat
String thisSKU = purchase.getSku();
if (thisSKU.equals(sku)) {
System.out.println("****** Est-ce que c'est là qu'on est entrés ? ******");
isOwned = true;
Toast.makeText(getActivity(), "Vous possédez la version complète, veuillez relancer l'application.", Toast.LENGTH_LONG).show();
btnPurchase.setVisibility(View.INVISIBLE);
manipProfil();
break;
}
if (!isOwned) {
System.out.println("****** Et on est entrés là ? ******");
mSkuDetails = skuDetails;
String price = skuDetails.getPrice();
btnPurchase.setText("Acheter pour " + price);
btnPurchase.setEnabled(true);
btnPurchase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BillingFlowParams billingFlowParams = BillingFlowParams
.newBuilder()
.setSkuDetails(skuDetails)
.build();
billingClient.launchBillingFlow(getActivity(), billingFlowParams);
}
});
}
}
}
}
}
}
});
}
else {
Toast.makeText(getContext(), "billing client not ready, attendre...", Toast.LENGTH_LONG).show();
}
}
@Override
public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> purchases) {
int responseCode = billingResult.getResponseCode();
if (responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
for (Purchase purchase : purchases) {
handlePurchase(purchase);
}
}
}
private void manipProfil() { // Cette méthode modifie un objet de type Profil dans ma base de données. C'est avec cela que je modifie le comportement de l'application si l'achat a été fait ou non.
profil = mDatabase.leprofilou();
if (profil != null)
mDatabase.addFullToProfil();
else {
Profil fantome = new Profil(0, 0, 0, 0, 0, "", 0, 0, 0, 20000101, 0, 0, 0d, 0d, 0d, 0d, 0d, 0d, 1, 0, 0, " ", " ", " ");
mDatabase.addProfil(fantome);
}
}
private void handlePurchase(Purchase purchase) {
if (purchase.getSku().equals(sku) && purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
if (!purchase.isAcknowledged()) {
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) {
Toast.makeText(getActivity(), "Purchase Acknowledged", Toast.LENGTH_LONG).show();
}
}
});
}
Toast.makeText(getContext(), "Purchase done.", Toast.LENGTH_LONG).show();
manipProfil();
}
}
} |
Partager