SKPaymentTransactionState is purchased before I buy a subscription with new sandbox user

I am trying to add a monthly auto renewable subscription to my app.


I do not understad why the following happens:


I just create a new sandbox tester and add it to my iPhone device in settings > iTunes & App Store > Sandbox Account


I delete the app from the device and in XCode I do Product > Clean & build folder and then install the app in the device.


When the app is launched I start observing the Payment Queue by adding:


SKPaymentQueue.default().add(self)


Then the following function is called:

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])


At this point in the debug console:


(lldb) po transactions.count


return 9


(lldb) po transactions[0].payment.productIdentifier


return the product identifier of my monthly auto renewable subscription


And the SKPaymentTransactionState of the transaction is purchased before I buy a subscription with this new sandbox user.


Why is this happening?


Is there any way to remove for all sandbox users all previous purchases?

Accepted Answer

You seem to have unfinished transactions in your queue. You need to call finishTransaction on all transactions or they will remain in the queue and keep hitting updatedTransactions whenever you add a tranactionObserver. The app thinks it is still logged into the last test user's account - the one with the unfinished transactions.

Thanks for your anwer PBK,


Do you mean to do it after validate the App Receipt againts ITunes in all cases, I mean indepently if the receipt is valid or not?

"Just do it"


When is up to you. Apple's 'best practice' is leaving it until you complete doing whatever you plan to do for the transaction so that if you get interrupted the system will recall updatedTransactions and restart your process. I think 'even better practice' is to call finishTransactions while still in updatedTransactions because of my difficult experience with "the endless loop".


If the receipt is invalid there most likely is no transaction in the queue. The real problem is what happens if your process of validating the receipt is interrupted or your code doesn't handle an edge case.

Thank you very much, have a nice day.

Speaking of best practices, see 'Suggested Testing Steps' for various scenarios for testing your implementation and TN2413: In-App Purchase FAQ to troubleshoot your issues.

SKPaymentTransactionState is purchased before I buy a subscription with new sandbox user
 
 
Q