use of com.android.billingclient.api.AcknowledgePurchaseResponseListener in project CodenameOne by codenameone.
the class BillingSupport method handlePurchase.
private void handlePurchase(final Purchase purchase) {
if (handlingPurchase.contains(purchase.getPurchaseToken())) {
return;
}
handlingPurchase.add(purchase.getPurchaseToken());
final PurchaseCallback pc = getPurchaseCallback();
if (!verifyDeveloperPayload(purchase)) {
if (pc != null && pc instanceof PendingPurchaseCallback) {
final PendingPurchaseCallback ppc = (PendingPurchaseCallback) pc;
CN.callSerially(new Runnable() {
public void run() {
for (String sku : purchase.getSkus()) {
ppc.itemPurchaseError(sku, "Invalid developer payload");
}
}
});
}
handlingPurchase.remove(purchase.getPurchaseToken());
return;
}
if (purchase.getPurchaseState() != Purchase.PurchaseState.PURCHASED) {
// This will be called again when the purchase completes.
if (purchase.getPurchaseState() == Purchase.PurchaseState.PENDING && pc != null && pc instanceof PendingPurchaseCallback) {
final PendingPurchaseCallback ppc = (PendingPurchaseCallback) pc;
CN.callSerially(new Runnable() {
@Override
public void run() {
ppc.itemPurchasePending(purchase.getSkus().iterator().next());
}
});
}
handlingPurchase.remove(purchase.getPurchaseToken());
return;
}
final String sku = purchase.getSkus().iterator().next();
final Runnable onPurchaseAcknowledged = new Runnable() {
public void run() {
if (pc != null) {
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
// Sandbox transactions have no order ID, so we'll make a dummy transaction ID
// in this case.
String transactionId = (purchase.getOrderId() == null || purchase.getOrderId().isEmpty()) ? "play-sandbox-" + UUID.randomUUID().toString() : purchase.getOrderId();
String purchaseJsonStr = purchase.getOriginalJson();
try {
// In order to verify receipts, we'll need both the order data and the signature
// so we'll pack it all into a single JSON string.
JSONObject purchaseJson = new JSONObject(purchaseJsonStr);
JSONObject rootJson = new JSONObject();
rootJson.put("data", purchaseJson);
rootJson.put("signature", purchase.getSignature());
purchaseJsonStr = rootJson.toString();
} catch (JSONException ex) {
Logger.getLogger(CodenameOneActivity.class.getName()).log(Level.SEVERE, null, ex);
}
com.codename1.payment.Purchase.postReceipt(Receipt.STORE_CODE_PLAY, sku, transactionId, purchase.getPurchaseTime(), purchaseJsonStr);
pc.itemPurchased(sku);
}
});
inventory.add(sku, purchase);
// This is a temp hack to get the last purchase raw data
// The IAP API needs to be modified to support this on all platforms
Display.getInstance().setProperty("lastPurchaseData", purchase.getOriginalJson());
}
}
};
if (!isConsumable(sku)) {
if (!purchase.isAcknowledged()) {
runWithConnection(new Runnable() {
public void run() {
billingClient.acknowledgePurchase(AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build(), new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(final BillingResult billingResult) {
handlingPurchase.remove(purchase.getPurchaseToken());
if (isFailure(billingResult)) {
final PurchaseCallback pc = getPurchaseCallback();
if (pc != null) {
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
pc.itemPurchaseError(sku, billingResult.getDebugMessage());
}
});
}
} else {
onPurchaseAcknowledged.run();
}
}
});
}
});
} else {
handlingPurchase.remove(purchase.getPurchaseToken());
}
return;
}
final ConsumeParams consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
final ConsumeResponseListener listener = new ConsumeResponseListener() {
@Override
public void onConsumeResponse(final BillingResult billingResult, String purchaseToken) {
if (purchase != null)
handlingPurchase.remove(purchase.getPurchaseToken());
if (isFailure(billingResult)) {
final PurchaseCallback pc = getPurchaseCallback();
if (pc != null) {
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
pc.itemPurchaseError(sku, billingResult.getDebugMessage());
}
});
}
} else {
onPurchaseAcknowledged.run();
}
if (purchase != null) {
inventory.erasePurchase(sku);
}
}
};
if (!purchase.isAcknowledged()) {
runWithConnection(new Runnable() {
public void run() {
billingClient.consumeAsync(consumeParams, listener);
}
}).except(new SuccessCallback<Throwable>() {
public void onSucess(final Throwable t) {
if (purchase != null)
handlingPurchase.remove(purchase.getPurchaseToken());
final PurchaseCallback pc = getPurchaseCallback();
if (pc != null) {
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
pc.itemPurchaseError(sku, t.getMessage());
}
});
}
if (purchase != null) {
inventory.erasePurchase(sku);
}
}
});
} else {
handlingPurchase.remove(purchase.getPurchaseToken());
}
}
use of com.android.billingclient.api.AcknowledgePurchaseResponseListener in project sms-backup-plus by jberkel.
the class DonationActivity method acknowledgePurchase.
// https://developer.android.com/google/play/billing/billing_library_overview#acknowledge
private void acknowledgePurchase(final Purchase purchase) {
if (purchase.getPurchaseState() == PURCHASED && !purchase.isAcknowledged() && billingClient != null) {
AcknowledgePurchaseParams params = AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
billingClient.acknowledgePurchase(params, new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
log("onAcknowledgePurchaseResponse(" + billingResult + ")");
if (billingResult.getResponseCode() != OK) {
Log.w(TAG, "not acknowledged purchase " + purchase + ":" + billingResult);
}
}
});
}
}
use of com.android.billingclient.api.AcknowledgePurchaseResponseListener in project Android-InAppBilling by LiteKite.
the class BillingManager method acknowledgeNonConsumablePurchasesAsync.
/**
* If you do not acknowledge a purchase, the Google Play Store will provide a refund to the
* users within a few days of the transaction. Therefore you have to implement
* [BillingClient.acknowledgePurchaseAsync] inside your app.
*
* @param purchase list of Purchase Details returned from the queries.
*/
private void acknowledgeNonConsumablePurchasesAsync(Purchase purchase) {
final AcknowledgePurchaseParams params = AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
final AcknowledgePurchaseResponseListener listener = billingResult -> {
if (billingResult.getResponseCode() == BillingResponseCode.OK) {
MonetizeApp.printLog(TAG, "onAcknowledgePurchaseResponse: " + BillingResponseCode.OK);
} else {
MonetizeApp.printLog(TAG, "onAcknowledgePurchaseResponse: " + billingResult.getDebugMessage());
}
};
executeServiceRequest(() -> myBillingClient.acknowledgePurchase(params, listener));
}
use of com.android.billingclient.api.AcknowledgePurchaseResponseListener in project Osmand by osmandapp.
the class BillingManager method handlePurchase.
/**
* Handles the purchase
* <p>Note: Notice that for each purchase, we check if signature is valid on the client.
* It's recommended to move this check into your backend.
* See {@link Security#verifyPurchase(String, String, String)}
* </p>
*
* @param purchase Purchase to be handled
*/
private void handlePurchase(final Purchase purchase) {
if (!verifyValidSignature(purchase.getOriginalJson(), purchase.getSignature())) {
LOG.info("Got a purchase: " + purchase + ", but signature is bad. Skipping...");
return;
}
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
// Acknowledge the purchase if it hasn't already been acknowledged.
if (!purchase.isAcknowledged()) {
AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
if (billingResult.getResponseCode() != BillingResponseCode.OK) {
LOG.info("Acknowledge a purchase: " + purchase + " failed (" + billingResult.getResponseCode() + "). " + billingResult.getDebugMessage());
}
}
});
}
} else if (purchase.getPurchaseState() == Purchase.PurchaseState.PENDING) {
LOG.info("Got a purchase: " + purchase + ", but purchase state is pending. Skipping...");
return;
} else {
LOG.info("Got a purchase: " + purchase + ", but purchase state is " + purchase.getPurchaseState() + ". Skipping...");
return;
}
LOG.debug("Got a verified purchase: " + purchase);
mPurchases.add(purchase);
}
Aggregations