iOS in-app purchase sandbox mode testing logic - ios

In my app there is a monthly auto-renewing subscription. We are doing server side validation based on expires_date from latest_receipt_info, then calculating and sending to app with daysLeft using this if daysLeft > 0, and then unlocking subscription features.
if(_backendDaysLeft) {
if(_backendDaysLeft.integerValue > 0) {
NSLog(#"Subs :ACTIVE");
status = SubscriptionStatusActive;
}
else {
NSLog(#"Subs :EXPIRED");
status = SubscriptionStatusExpired;
}
}
But in sandbox mode always daysLeft will be 0 in our tests, and so is always expired. In sandbox mode
If I change the condition to be greater than zero (>0) to >=0 for sandbox mode testing and when I submit the app if I revert back to >0, will the Apple review team also use sandbox mode to unlock it?
How do I tackle this situation?

Not really sure but looks like your server-end calculation of days-left could be wrong. In Sandbox mode, a monthly Auto-Renewing subscription would expire in just 5 mins and a renew would trigger. This goes on for some 5 or so times, so in any case, your subscription gets canceled after some 30 mins or so.
If you are calculating days-left, seems to me, that this calculation could be flawed as 30 mins are too less than 24 hrs.
A better approach would be to return the exact date-time the subscription expires and use that in your app to enable/disable features.

Sandbox subscriptions expire in ~5mins so "days left" is always 0.
Send back the expires_date and server timestamp to do the calculation on the client of time remaining.
Don't send back just an expires_date since the device time could be incorrect.

Related

In app purchase sometimes returns status code 21002

Always return status code 21002. But if you send 10 more requests, one of them can return status code 0. Anybody see the same? This is apple mistake?
Try this in postman.
https://sandbox.itunes.apple.com/verifyReceipt
{
"receipt-data":"MIIVnAYJKoZIhvcNAQcCoIIVjTCCFYkCAQExCzAJBgUrDgMCGgUAMIIFPQYJKoZIhvcNAQcBoIIFLgSCBSoxggUmMAoCAQgCAQEEAhYAMAoCARQCAQEEAgwAMAsCAQECAQEEAwIBADALAgELAgEBBAMCAQAwCwIBDwIBAQQDAgEAMAsCARACAQEEAwIBADALAgEZAgEBBAMCAQMwDAIBCgIBAQQEFgI0KzAMAgEOAgEBBAQCAgDPMA0CAQMCAQEEBQwDMTA0MA0CAQ0CAQEEBQIDAnIsMA0CARMCAQEEBQwDMS4wMA4CAQkCAQEEBgIEU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K1drk/NAJBzewdXUhMYIByzCCAccCAQEwgaMwgZYxCzAJBgNVBAYTAlVTMRMwEQYDVQQKDApBcHBsZSBJbmMuMSwwKgYDVQQLDCNBcHBsZSBXb3JsZHdpZGUgRGV2ZWxvcGVyIFJlbGF0aW9uczFEMEIGA1UEAww7QXBwbGUgV29ybGR3aWRlIERldmVsb3BlciBSZWxhdGlvbnMgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkCCA7rV4fnngmNMAkGBSsOAwIaBQAwDQYJKoZIhvcNAQEBBQAEggEAmMTux8H4vhuHQt8fJZ3BV+P8ad/olc8MC/bFT8Hm6ula6ATGa/tpJ1hckOel0pE2xnW/UOLQdrATB1aHXFYGEnYmSKigD20b22HgKGFqZzGpR2FqCUMtlEtbulbhZGTvwxyVNpi4CoViVUFAtSE37fSlGFcOQ29mAyM2KjB/HZN8zHDeReZtNXS2vgrsRvWE7poMKhgZ0ETxyLGtp48MK7XIYXadVl7EFwvOMTebw7RHWVYU6Xz3pAzUMOPlTEfzNl1z1zCh0ZHgFYVNOnnOeNhdPe97aS75+7MreqPzOZyCH6+kzXgms894ENbcDsRmBGBcMxxr+PHPylkOWYn3QQ==", "password":"eb08b930750e4a798fcf993814ad29f1"
}
The same in RevenueCat validation https://www-origin.revenuecat.com/app-store-receipt-validation/
There's an ongoing outage with App Store Receipt Validation:
https://twitter.com/RevenueCat/status/1623069933016948736
Same here since 2 days, so annoying.. Even the developer support is not aware about it.

ios subsciptions don't auto renew in sandbox

I have implemented auto renewing subscription in my app. As per documentation, this should auto renew every few minutes up to six times a day. But it's not happening for my app.
I look for the subscription expiration date in the app receipt, and this works the first time, but it doesn't work afterwards. Is the app receipt not being automatically updated in the sandbox environment? It was my understanding that it should.
For anyone dealing with this problem in 2019 or later.
I also experienced subscriptions not being automatically renewed.
In my case auto renewals stopped working after buying 6 times within 8 hours.
Details:
A one month subscription in the Sandbox lasts 5 minutes and is automatically renewed 6 times (the entire purchase expires in 30 minutes). After that you have to buy again and the same process starts over.
For automatic renewals there is however a limit of buying 6 times for every 8 hour period.
This limit is per test user per 8 hours, so you can simply use a new test users to get around this.
Under
Settings -> iTunes & App Store
there is now a new option for Sandbox Account where you can log out and log in for a new test user. This took me a long time to figure out.
The following can be used to get the receipt from apple
static func getReceipt() -> String? {
guard let url = Bundle.main.appStoreReceiptURL,
let _ = try? Data(contentsOf: url) else {
print("no receipt exists")
return nil
}
do {
let receipt = try Data(contentsOf: url)
print("receipt-data: \(receipt.base64EncodedString(options:[]))")
return receipt.base64EncodedString(options: [])
}
catch {
print("catch error")
return nil
}
}
Instead of attempting to 'Build and Run' your App each time, try to reopen the App by reopening it on the device directly. I found this way, I can refresh the subscription receipts, based upon the prior sandbox receipt.
It appears that each time you 'Build and Run' via Xcode that any pending subscription renewals are reset. Potentially rebuilding your App is the reason that the subscription auto-renew is reset.

Handling IAP Fullfilment results for Windows 10 Store

When providing consumable In App Purchases on the Windows 10 Store, there are FullfillmentResults when ReportConsumableFullfillmentAsync is called.
The user of my app has had their IAP fullfilled by the time I get this result. This means they have their Coins/Gems/Potatoes.
But if I receive FulfillmentResult.PurchaseReverted, then what happened? How did the user just revert the purchase? Am I meant to withdraw their Coins/Gems/Potatoes?
What are scenarios behind the other error messages?
Note: I'm working with using Windows.ApplicationModel.Store
But if I receive FulfillmentResult.PurchaseReverted, then what
happened? How did the user just revert the purchase? Am I meant to
withdraw their Coins/Gems/Potatoes?
The value PurchaseReverted means the transaction is canceled on the backend and users get their money back. So you should disable the user's access to the cosumable content (withdraw the Coins/Gems/Potatoes) as necessary.
What are scenarios behind the other error messages?
NothingToFulfill : The transaction id has been fulfilled or is otherwise complete
PurchasePending: The purchase is not complete. At this point it is still possible for the transaction to be reversed due to provider failures and/or risk checks. It means the purchase has not yet cleared and could still be revoked.
ServerError: There was an issue receiving fulfillment status. It might be the problem from the Store.
Succeed: The fulfillment is complete and your Coins/Gems/Potatoes can be offered again.
Here is the documentation about FulfillmentResult Enum

How to detect if user cancel auto-renewable subscriptions during the free trial period?

Apple's document does not seem to mention this. So if user cancels an auto-renewable subscription purchased during the free trial period, how do we detect?
In appstore receipt JSON there is this field: is_trial_period. But I think this is for indication of whether the free trial period is over.
The only thing I can think of is this NSBundle.mainBundle().appStoreReceiptURL?.path and if this is nil than that will indicate the user has not subscribed or cancel within the free trial period. But for sandbox testing, there is no way to do a cancel during free trial period to test this scenario.
Does anyone have a solid knowledge of this?
In order to support auto-renewing subscriptions, your app needs to periodically submit the app receipt obtained from NSBundle.mainBundle().appStoreReceiptURL?.path to Apple's receipt validation service.
Contained in the JSON response from this service is the latest_receipt_info array.
By examining this array you will be able to determine the currently active subscription(s).
If a user turns off auto-renewal before the expiration of the free trial then latest_receipt_info won't contain a purchase with an expires_date after the free trial end date
This means, that strictly speaking, you can't "detect a cancellation" as there is no "cancellation"; there just isn't a renewal at the end of the free trial period.
This is possible with the web hook feature in iTunes Connect.
When you set a path for the Subscription Status URL of your app the App Store server will call that URL when the subscription status changes.
Currently the following key events will trigger the call:
INITIAL_BUY Initial purchase of the subscription.
CANCEL Subscription was canceled by Apple customer support.
RENEWAL Automatic renewal was successful for an expired subscription.
INTERACTIVE_RENEWAL Customer renewed a subscription interactively after it lapsed, either by using your app’s interface or on the App
Store in account settings.
DID_CHANGE_RENEWAL_PREFERENCE Customer changed the plan that takes affect at the next subscription renewal.
DID_CHANGE_RENEWAL_STATUS Subscription has expired and customer resubscribed to the same or another plan.
More can be found here and here.
The correct way to do this is to check the auto-renew preference on the receipt. If you want to get notified of this even if the user doesn't open your app (or deletes it) you'll need to store and refresh the receipt on your server. There are 3 fields that you should be concerned with to detect a cancellation.
Expiration Date (lets you know if subscription is still active)
Auto-renew status (lets you know if the user "cancelled")
Cancellation Date (tells you why subscription cancelled by support)
You should check for receipts that are not expired, not cancelled and have an auto-renew status of "0". These will be users that are in a free trial, but have auto-renew turned off. Unfortunately, the App Store Connect Subscription Status Notifications don't report this to you.
Here's a good blog post that goes over a little more of the the details: iOS Subscriptions are Hard
Here is how I got the json data about receipt:
let sharedSecret = "..." //you can find this string on AppStoreConnect-IAP
if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
FileManager.default.fileExists(atPath: appStoreReceiptURL.path) {
do {
let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)
let receiptString = receiptData.base64EncodedString(options: [])
AF.request("https://sandbox.itunes.apple.com/verifyReceipt", method: .post, parameters: ["receipt-data":receiptString, "password": sharedSecret], encoding: JSONEncoding.default, headers: nil)
.responseJSON(completionHandler: { (response) -> Void in
print(response)
})
}
catch {
print("Couldn't read receipt data with error: " + error.localizedDescription) }
}
If you want a production URL, not the Sandbox, change the url to https://buy.itunes.apple.com/verifyReceipt
In the response you will find all data you need.
Note that you need to include this in the Podfile : pod 'Alamofire', '~> 5.2'

Auto renewable IAP subscription user flow & refreshing receipts

I am using the library RMStore - here's what I have currently.
1) Purchase auto renewable subscription & verify the returned receipt.
[[RMStore defaultStore]addPayment:[Environment environment].premiumProductIAPId success:^(SKPaymentTransaction *transaction) {
[[RMStore defaultStore].receiptVerificator verifyTransaction:transaction success:^{
//enable premium service
} failure:^(NSError *error) {
}];
} failure:^(SKPaymentTransaction *transaction, NSError *error) {
}];
2) On each app launch check the subscription is active for the date and enable the premium service if it is
RMAppReceipt *appReceipt = [RMAppReceipt bundleReceipt];
if (appReceipt){
NSInteger isActive = [appReceipt containsActiveAutoRenewableSubscriptionOfProductIdentifier:[Environment environment].premiumProductIAPId forDate:[NSDate date]];
//enable premium service if active
}
3) If user launches app on another device allow them to restore purchases by refreshing the receipt if it exists and checking if there is an active subscription in the purchases.
"In most cases, all your app needs to do is refresh its receipt and deliver the products in its receipt."
- That's from the guide. Here's the code:
[[RMStore defaultStore]refreshReceiptOnSuccess:^{
if ([receipt containsActiveAutoRenewableSubscriptionOfProductIdentifier:[Environment environment].premiumProductIAPId forDate:[NSDate date]]){
//enable
}else{
//no longer active
}
} failure:^(NSError *error) {
}];
My questions:
When RMStore checks if the subscription is active it can return no, I look in the receipt and it is correct and I am assuming it hasn't been auto renewed. When I go to purchase another subscription I get a message from itunes saying I'm already subscribed. On subsequent launch I see the new receipt. This indicates the receipt needed to be refreshed on launch, but I don't want to refresh it as it brings up the username & password pop up which is unnecessary. What is the best practice here?
Am I restoring the subscriptions for another device the right way? It seems to sometimes take more than one attempt to restore the subscriptions.
Is there any need apart from record keeping to store the subscriptions on my server?
I'm going to try and answer my question.
There may be a renewal which is not detected first thing on launch hence the subscription appears inactive.
I added an observer to listen for finished transactions (RMStore extends this StoreKit functionality).
Each time I receive this notification I check the (now updated) receipt for an active subscription and enable the premium service if there is one.
- (void)storePaymentTransactionFinished:(NSNotification*)notification
{
BOOL isActive = [[RMAppReceipt bundleReceipt] containsActiveAutoRenewableSubscriptionOfProductIdentifier:[Environment environment].premiumProductIAPId forDate:[NSDate date]];
if (isActive){
//enable premium
}
}
This seems to be working. If anyone has any other suggestions let me know.

Resources