Clearing purchases from iOS in-app purchase sandbox for a test user

IphoneIosIn App-Purchase

Iphone Problem Overview


Does anyone have any ideas on how to reset and/or clear the iOS in-app purchase sandbox?

I have an app that I'm testing with the sandbox, and I'd like to test new purchases without having to create a new test user every time I purchase something.

If I don't do this, then I (of course) always get a message that the in-app purchase item has already been purchased when I click on my app's buy button.

Iphone Solutions


Solution 1 - Iphone

IMO there are 3 things you can do to make testing non-consumables bearable:

  1. You can have many test accounts associated to one email. Gmail for example lets you add a "plus" string to the email to create aliases for an address: so [email protected] and [email protected] both really just go to [email protected]. Probably other email hosts do the same. When you create a test account you need to introduce: first name, last name, email address, password, secret question, secret answer, date of birth, and iTunes store country. You can put exactly the same data (including password) for [email protected] and [email protected] and you will have two test accounts. Finally, in your [email protected] inbox you will receive two verification emails from Apple to confirm both test accounts.

  2. Say that you have a non-consumable with product ID @"Extra_Levels". Instead of writing @"Extra_Levels" in all methods (requestProduct, purchaseProduct, ...), just write PRODUCT_ID1 and at some header file put #define PRODUCT_ID1 @"Extra_Levels" (with no semicolon!), then the preprocessor will search PRODUCT_ID1 and substitute it for @"Extra_Levels". Then creating a new non-consumable called @"Extra_Levels_01" and changing the #define will be as good as resetting the purchases for all your test users.

  3. As appsmatics pointed out, you can test the correct behavior of your code when you buy a non-consumable IAP by first using a consumable IAP (so that test user can make as many purchases as needed) to get rid of some bugs. Of course, you should also test the code with the real non-consumable IAP after that.

Solution 2 - Iphone

You can't do this, as far as I know. The sandbox backend works like a real account-- once it's purchased, it's purchased (and thus you can test restore). You should do most of your development with the store stuff shimmed out, and then when you get to testing it for real, just expect to create several test accounts.

Solution 3 - Iphone

I have 2 in app purchase items. 1 for production. and the other for testing. when I need to "clear" I delete the in app item and create new one (15 seconds in itunes connect and 1 second to change the product id in code)

if i dont need to test "new user", i use the production in app item.

Solution 4 - Iphone

Deleting your app and reinstalling works also for sandbox testing. Depends on the app obviously, but I'm testing a subscription based app that only purchases during sign up at the moment so it's been the easiest solution.

Solution 5 - Iphone

Well, technically you don't need that.

If you get SKPaymentTransactionStateRestored, it is 100% equivalent to the app store verifying the user and granting him the purchase. I have a switch like:

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
  for( SKPaymentTransaction *purch in transactions )
  {
    switch( purch.transactionState )
    {
      case SKPaymentTransactionStateRestored:
        info( "PURCHASE RESTORE" ) ;
        // fall thru
      case SKPaymentTransactionStatePurchased:
        [[SKPaymentQueue defaultQueue] finishTransaction:purch];
        // Do regular changes to app state for this purchase,
        // register in keychain, etc.
        break ;

       //.. other cases
     }
  }
}

The question of having your app logic / take back the purchase is simple: if you're caching purchases in keychain, delete your keychain. If you're doing it some other how, just change your local app state to pretend like the user never purchased it before. The request to purchase dialog is still exactly the same, the only difference is when you punch YES, it gives you SKPaymentTransactionStateRestored instead of SKPaymentTransactionStatePurchased.

Solution 6 - Iphone

You can clear the purchase history for a tester so that you can continue to use the same sandbox Apple ID for ongoing testing. Clearing purchase history will delete all past auto-renewable subscriptions and non-consumables purchased by the selected testers in the sandbox environment. In-app purchases made by customers on the App Store are not affected.

To clear tester purchase history:

From Users and Access, under Sandbox, click Testers.

Click Edit.

enter image description here

Select the checkbox for each tester you want to modify and click Clear Purchase History.

enter image description here

Click Clear Purchase History in the dialog that appears.

enter image description here

Sandbox Apple IDs with a high number of purchases may take longer to clear. This action cannot be reversed.

Solution 7 - Iphone

Check out SimStoreKit. It's a "simulated version of the iPhone's StoreKit, for testing store UIs on the iPhone Simulator, or even on device without having to set up IAP in Connect."

SimStoreKit stores purchases in the user defaults under the key ILSimSKTransactions. So to clear all purchases you can do:

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"ILSimSKTransactions"]

On the simulator, you can simply remove your app and install it again.

I've successfully used SimStoreKit to debug my app's store front before testing with the sandbox. The beauty of this library is that it can be set-up to use the same class names as the real StoreKit framework (by doing #define ILSimReplaceRealStoreKit 1 before doing #include <ILSimStoreKit.h>).

In source files where I need to access StoreKit, I include this header file:

#import <TargetConditionals.h>

#if TARGET_IPHONE_SIMULATOR
    #define kILSimAllowSimulatedStoreKit 1
    #define ILSimReplaceRealStoreKit 1
    #import <ILSimStoreKit.h>
#else
    #import <StoreKit/StoreKit.h>
#endif

This has the effect of using SimStoreKit when I run on the simulator and the real StoreKit when I run on the device.

Solution 8 - Iphone

You can clear the purchase history for a sandbox Apple ID in App Store Connect.

To clear tester purchase history:

  1. From Users and Access, under Sandbox, click Testers.

  2. Click Edit.

  3. Select the checkbox for each tester you want to modify and click Clear Purchase History.

  4. Click Clear Purchase History in the dialog that appears.

see Documentation <https://help.apple.com/app-store-connect/#/dev7e89e149d>

Solution 9 - Iphone

Just keep using the same test account, restoring purchases as opposed to completing new ones. After all, whether you start a new purchase or restore an old one, YOUR APP will do the same thing (at least initially, maybe the user interface will update differently upon completion). Apple are the folks handling things differently in those different situations - don't worry about it.

Place your delivery logic in the SKPaymentTransactionStateRestored case within this method's implementation for testing:

- (void)paymentQueue:(SKPaymentQueue *)queue
 updatedTransactions:(NSArray *)transactions;

Then be sure to put that delivery logic into the SKPaymentTransactionStatePurchased case.

At the end, because most of us are obsessive-compulsive to varying degrees, do a final test with a fresh account (not a big deal to make a second one for absolute certainty).

The final thing to note: consider apple's position. If there was a problem with developers having to waste time creating tens or hundreds of accounts to test IAP thoroughly, they would have solved the problem. There is no problem.

Solution 10 - Iphone

This is now possible for subscriptions purchases through Reset Eligibility

  1. On the test iOS device, open Settings > Apple ID > Media & Purchases (or iTunes &/or App Store for iOS 13 and earlier). Under the Sandbox Account section, tap your highlighted Sandbox Apple ID then tap Manage to open the sandbox Subscription Management page.
  2. If the subscription has expired, tap one of the options to resubscribe.
  3. Once you resubscribe or when you have a subscription going on, you can use the Reset Eligibility button to reset and redeem another introductory offer (trial or discounted price).

Solution 11 - Iphone

alternatively to create multiple test user solution you can create multiple test in app purchases in iTunes connect then you don't need to change a user account.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRandomIOSDeveloperView Question on Stackoverflow
Solution 1 - IphoneRoberto CanogarView Answer on Stackoverflow
Solution 2 - IphoneBen ZottoView Answer on Stackoverflow
Solution 3 - Iphoneuser1105951View Answer on Stackoverflow
Solution 4 - IphoneChristopher LarsenView Answer on Stackoverflow
Solution 5 - IphoneboboboboView Answer on Stackoverflow
Solution 6 - Iphonekiril kiroskiView Answer on Stackoverflow
Solution 7 - IphoneEmile CormierView Answer on Stackoverflow
Solution 8 - IphoneAydinView Answer on Stackoverflow
Solution 9 - IphoneArthurVonBabylonView Answer on Stackoverflow
Solution 10 - IphoneZerosAndOnesView Answer on Stackoverflow
Solution 11 - IphoneBlazej SLEBODAView Answer on Stackoverflow