Unity In-App Purchase Local Validation is slow - ios

I'm currently changing our game's in-app purchasing model from an older add-in to unity's built-in service. Up until now there have been no issues testing payments and validating receipts, both live and using sandbox accounts.
Following the Unity guides I have developed a store, and can successfully make new payments with a sandbox account, however local validation takes around 2 minutes on an iPad Air 2. Is this normal? Our previous add-in validated receipt data using the App Store, and took only a few seconds.
private bool CheckReceipt()
{
#if UNITY_EDITOR
Debug.Log("IAP: Default for editor, receipt valid.");
return true;
#elif UNITY_ANDROID || UNITY_IOS
try
{
CrossPlatformValidator validator = new CrossPlatformValidator(GooglePlayTangle.Data(),
AppleTangle.Data(), Application.bundleIdentifier);
Product product = controller.products.WithID(ProductID);
string receipt = product.receipt;
if (receipt == null)
{
Debug.Log("IAP: No receipt.");
return false;
}
Debug.Log("IAP: Validating receipt...");
IPurchaseReceipt[] result = validator.Validate(receipt);
return result[0].productID == ProductID;
}
catch (IAPSecurityException e)
{
Debug.Log("IAP: Invalid receipt, not unlocking content");
return false;
}
#endif
}

In mode "Sandbox" this time for validation very real, when you change paying to "Battle" mode all stand normal

Related

How to get the original_application_version (the first purchased version number) out of iOS Receipt in iOS 11?

I have a paid iOS App.
I need to get the original_application_version number (the first version purchased by the user) from the Apple AppStore Receipt.
To get the receipt, when my app loads, I use checkReceiptFromAppStore() function:
func checkReceiptFromAppStore() {
let receipt = self.getReceipt()
print("receipt Data is: \(receipt)") // prints this: receipt Data is: Optional(5141 bytes)
}
getReceipt() function is the following:
func getReceipt() -> Data? {
if Bundle.main.appStoreReceiptURL != nil {
print("app receipt: \(Bundle.main.appStoreReceiptURL)")
do {
let receiptData = try Data(contentsOf: Bundle.main.appStoreReceiptURL!)
return receiptData
} catch {
print("error converting receipt to Data: \(error.localizedDescription)")
}
}
return nil
}
I've watched WWDC 2017 Advanced StoreKit video about In App purchases and receipt validation and WWDC 2013 Video about using Receipts, read different
resources related to my problem (this, this, this, this, this, this, and this...), but I still don't understand what to do next to get the
"original_application_version" from the App Store Receipt. I need only this field and don't understand why is it so hard to get it.
I've read this too: https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html
I think that the receipt is not nil as long at when I run print("receipt Data is: (receipt)") it prints this: receipt Data is: Optional(5141 bytes)
I can suppose that I should parse the receipt to get that field. Can I do it using Decodable? Is there the easiest way to get this original_application_version field? Is it possible to do this without a receipt validation?
I need to get the original_application_version field only to detect the number of the first version bought by the user. If you know any other solutions to get the first purchased version number, I'd be glad to hear them.
I'm developing in Xcode 9, Swift 4, iOS 11
Any answers appreciated, thanks.
All receipt fields are in binary format within application receipt. You should use any kind of app receipt decoder in order to get original_application_version. It is always good thing to validate app receipt before using its contents. For example, you can use RMStore framework (RMStore). It contains receipt validator as well as decoder. Example Obj-C source:
RMAppReceipt *appReceipt = RMAppReceipt.bundleReceipt;
if (appReceipt != nil &&
[appReceipt.originalAppVersion length] > 0 &&
![appReceipt.originalAppVersion isEqualToString:#"1.0"]) {
//Process your original app version
} else {
//Probably it is sandbox mode or app install via Xcode
//Or maybe you should force app receipt refresh
}

Why does the latest_receipt object changing every time I request it from Apple?

There is one thing I don't quite understand when it comes to In-App Subscription purchase.
I obtain the receipt on iOS client like this:
private func loadReceipt() -> Data? {
guard let url = Bundle.main.appStoreReceiptURL else {
return nil
}
do {
let receipt = try Data(contentsOf: url)
return receipt
} catch {
print("Error loading receipt data: \(error.localizedDescription)")
return nil
}
}
And send it for verification to my server (written n Python).
def verify_receipt(self, receipt):
r = requests.post(config.APPLE_STORE_URL, json=receipt)
request_date_ms = DateUtils.generate_ms_from_current_time()
for item in r.json()['latest_receipt_info']:
expires_date_ms = int(item['expires_date_ms'])
if expires_date_ms > request_date_ms:
return True
return False
I'm not sure if this is the correct way of verifying if a subscription is still valid.
I get the expires_date_ms from latest_receipt_info, and if it's greater than the current time in milliseconds, then the subscription counts as still valid.
However what I noticed is that the separate latest_receipt, which is supposed to be equal to the one I have just sent in, is actually changing every time I call the API. But why? I haven't subscribed to anything new, why is the latest receipt changing?
According to the docs:
latest_receipt
Only returned for receipts containing auto-renewable subscriptions.
For iOS 6 style transaction receipts, this is the base-64 encoded
receipt for the most recent renewal. For iOS 7 style app receipts,
this is the latest base-64 encoded app receipt.
If this is against the sandbox then the subscription is auto-renewing on a predefined period. See:
https://help.apple.com/app-store-connect/#/dev7e89e149d

Gdx-Pay iOS store, can't find product info

I have a working implementation of gdx-pay for Android using the Google App Store. I did that following this: https://bitbucket.org/just4phil/gdxpayexample/src/.
I am now trying to get it working on iOS, but can't find any docs on how to do that. In the project linked above there is no "ios app store" or any changes made to the ios-project. Does anyone have a link to an example working with ios?
I tried some stuff out and got this far:
IOSResolver:
public class IOSResolver extends PlatformResolver {
public IOSResolver(MyGdxGame myGame) {
super(myGame);
PurchaseManagerConfig config = myGame.purchaseManagerConfig;
initializeIAP(null, myGame.purchaseObserver, config);
installIAP();
}
IOSLauncher:
#Override
protected IOSApplication createApplication() {
IOSApplicationConfiguration config = new IOSApplicationConfiguration();
config.orientationLandscape = false;
config.orientationPortrait = true;
config.useCompass = false;
config.useAccelerometer = false;
game = new MyGdxGame(new IOSPlatform());
return new IOSApplication(game, config);
}
#Override
public boolean didFinishLaunching(UIApplication application, UIApplicationLaunchOptions launchOptions) {
boolean finished = super.didFinishLaunching(application, launchOptions);
game.setPlatformResolver(new IOSResolver(game));
return finished;
}
Core:
// ---- IAP: define products ---------------------
purchaseManagerConfig = new PurchaseManagerConfig();
purchaseManagerConfig.addOffer(new Offer().setType(OfferType.ENTITLEMENT).setIdentifier(ios_productId));
This gives me the error:
[GdxPay/AppleIOS] Requesting product info for test.product.id
2017-02-20 16:58:58.089803 IOSLauncher[5611:2144625] [info] gdx-pay: calls purchasemanager.purchase()
[GdxPay/AppleIOS] Error purchasing product (wrong product info count returned: 0)!
com.badlogic.gdx.utils.GdxRuntimeException: java.lang.RuntimeException: Error purchasing product (wrong product info count returned: 0)!
at com.scene2d.space_camp.MyGdxGame$1.handlePurchaseError(MyGdxGame.java)
at com.badlogic.gdx.pay.ios.apple.PurchaseManageriOSApple$AppleProductsDelegatePurchase.didReceiveResponse(PurchaseManageriOSApple.java)
at com.badlogic.gdx.pay.ios.apple.PurchaseManageriOSApple$AppleProductsDelegatePurchase.$cb$productsRequest$didReceiveResponse$(PurchaseManageriOSApple.java)
at org.robovm.apple.uikit.UIApplication.main(UIApplication.java)
at org.robovm.apple.uikit.UIApplication.main(UIApplication.java)
at com.scene2d.tut.IOSLauncher.main(IOSLauncher.java)
Caused by: java.lang.RuntimeException: Error purchasing product (wrong product info count returned: 0)!
... 5 more
I registered the app on iTunes Connect and set up a product. However I did not post binaries, do I need to for testing IAP? It shows this message in iTunes Connect:
Your first In-App Purchase must be submitted with a new app version.
Select it from the app’s In-App Purchases section and click Submit.
Once your binary has been uploaded and your first In-App Purchase has
been submitted for review, additional In-App Purchases can be
submitted using the table below.
Is there no way to test without uploading the actual app?
I had this same issue and was seeing the same error message.
I think the two keys to getting your IAPs work are the following:
Use a real device for testing, not an emulated one.
Sign in with a sandbox account, not a real one.
Create your sandbox account on itunesconnect.
Build your app and install it onto your ipad/iphone using xcode.
On your device, go into settings and logout. This is important.
Run your app and make a purchase. You will be prompted to login. Use your sandbox account.
I did this and it worked.

Subscription Purchase in Unity iOS

My app contains subscription based functionality for purchase. So which way I require to move ahead into this?
When I search for this in Google, I always found Consumable and Non-Consumable product purchase related help.
I want to move ahead with Renewable Subscription purchase. At present I have Prime31 plugin with following source code written:
#if UNITY_ANDROID
var productId = GameConstants.SKU_PURCHASE_9999_COINS;
#elif UNITY_IOS
var productId = GameConstants.SKU_PURCHASE_9999_COINS;
#endif
IAP.purchaseConsumableProduct (productId, (didSucceed, error) => {
Debug.Log ("purchasing product " + productId + " result: " + didSucceed);
if (didSucceed) {
}
});
Whether using same code, Can I use for Subscription purchase?
What is alternative way for me to implement Subscription purchase?

Check if user has valid auto-renewable subscription with Parse iOS SDK

I'm trying to implement an application with auto-renewable subscription. Users should pay to access all functions of my application. I already use Parse as backend for my application. It provides some API methods for inAppPurchases but there is nothing said about auto-renewable type. The only thing I have found is some two years old threads in the blog it is said that receipt verification was implemented only for downloadable purchases.
I have tried to use as it called in docs "Simple purchase" and it works fine but I can't figure out how can I check if my user already bought subscription or not.
Does anybody know is there way to do it via Parse API or This should implemented in another way?
As mentioned, receipt validation is only built into the Parse SDK for downloadable content, but it is fairly simple to to create a Cloud Code function that POSTs the app receipt to the iTunes Store for validation. Here are the Apple docs for server side validation: Validating Receipts with the App Store
Here is a what a basic function would look like:
Parse.Cloud.define('validateReceipt', function (request, response) {
var receiptAsBase64EncodedString = request.params.receiptData;
var postData = {
method: 'POST',
url: 'http://buy.itunes.apple.com/verifyReceipt',
body: { 'receipt-data': receiptAsBase64EncodedString,
'password': SHARED_SECRET }
}
Parse.Cloud.httpRequest(postData).then(function (httpResponse) {
// httpResponse is a Parse.Cloud.HTTPResponse
var json = httpResponse.data; // Response body as a JavaScript object.
var validationStatus = json.status; // App Store validation status code
var receiptJSON = json.receipt; // Receipt data as a JSON object
// TODO: You'll need to check the IAP receipts to retrieve the
// expiration date of the auto-renewing subscription, and
// determine if it is expired yet.
var subscriptionIsActive = true;
if (subscriptionIsActive) {
return response.success('Subscription Active');
}
else {
return response.error('Subscription Expired');
}
});
});
See Receipt Fields for details on interpreting the receipt JSON. It's fairly straight forward for iOS 7+, but auto-renewing subscription receipts for iOS 6 and earlier are tedious.

Resources