Reputation: 523
My issue, i really need help with, is that i can't get the logic working which renders the 'Restore Purchase' button conditionally.
I have built my IOS app with an in-app purchase button that allows the user to pay once to unlock extra features - of type "Non-Consumable Product". The purchase feature works fine in TestFlight using my regular home user Apple IDs and some other Test Apple IDs (not sandbox testers). I don't have a Mac and so can't use a Sandbox environment. When the trial period is up i display the Purchase button, this invokes the CN1 Purchase API and PurchaseCallback, and i then use Storage to store the flag that its been purchased.
purchaseApp.addActionListener(e -> {
if (Purchase.getInAppPurchase().wasPurchased("full_product")) {
ToastBar.showMessage("You have already previously purchased this product.", FontImage.MATERIAL_INFO);
...
} else {
Purchase p = Purchase.getInAppPurchase();
p.purchase("full_product");
}
});
(I am not doing anything with receipts or receipt_stores.)
But to comply with Apple i need a 'Restore Purchase' button, so users don't pay again on new devices. The difficulty is getting the logic right, as i only want the Restore button to display IF they have paid for it before, else i just want the Purchase button displayed.
So i created the userHasPreviousPurchased() method below to test for the button to show, and added RestoreCallback interface to my main class as per CN1 documentation. But the method below never returns true on my IPhone using a TestFlight build, so my Restore button never shows. It is that p.getReceipts() always returns an empty List, when i expect 1 row with my SKU in it.
private boolean userHasPreviousPurchased() {
Purchase p = Purchase.getInAppPurchase();
if (p.isRestoreSupported()) {
p.synchronizeReceipts();
List<Receipt> receipts = p.getReceipts();
for (Receipt receipt : receipts) {
if (receipt.getSku().equalsIgnoreCase("full_product")) {
return true;
}
}
}
return false;
}
Please can you point me in the right direction. Using CN1 API's how do you test for a previous purchase using this type of IAP?
I tried creating a sandbox test user and, after signing out on my device, purchasing the IAP using the user (which was accepted). I have debug code in my app with tests the above method and that still didn't return a receipt (i fail to see how it could have as i wasn't signed in as the sandbox user).
Many thanks in advance
Upvotes: 2
Views: 266
Reputation: 4716
If you want to keep track of who has purchased your products already, you need to have a server-side component to store this. The Receipts API depends on this, and requires you to implement a ReceiptStore. Check out these blog posts that cover this in detail.
Upvotes: 2