IPhone inApp Purchase Queue will not eliminate

I have an InApp purchase setting in my application. However, I have strange behavior. Every time I launch the application, I call

[[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 

to set the initial observer. However, this immediately causes

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

with a full array of each transaction. I tried just calling

 [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 

in each of these transactions, and then restart the application again, but paymentQueue still starts as soon as I call addTransactionObserver. My main goal right now is just to clear the transaction queue and start cleaning. I do not know how I got into this state, and how to get out of it.

+6
source share
5 answers

Make sure you implement this method:

 -(void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions; 
+5
source

Put this somewhere to clear the queue (maybe your boot method, but delete it in your final application):

 for (SKPaymentTransaction* transaction in [[SKPaymentQueue defaultQueue] transactions]) { [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; } 

When finishTransaction is called, each of them will call the observer removeTransaction method.

I had 30 in my queue and I received 30 calls to this method.

+3
source

You must be sure that you complete the transaction every time you make a purchase or restore a product. If you run into this problem, you should clear the queue and then design the logic correctly. You can get a quick cleanup by doing something like this in swift 3. (which is the same as the previous answer). But this should not be in your real application.

 func cleanUp() { for transaction in SKPaymentQueue.default().transactions { SKPaymentQueue.default().finishTransaction(transaction) } } 

You must also add and remove your observer in appDelegate. This is a recommendation and best to avoid problems.

+1
source

From SKPaymentTransactionObserver:PaymentQueue.. call:

 SKPaymentQueue.default().finishTransaction(transaction) 

Please note that you cannot call for all types. Calling finishTransaction for .purchasing will fail with an error. Thus, for through all transactions is not a complete solution.

When you call finishTransaction for .purchasing :

  *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot finish a purchasing transaction' 
0
source

I think your problem is with Sandbox User accounts in iTunes Connect. If you don't have sandbox users, this guide was really useful for setting them up:
https://support.magplus.com/hc/en-us/articles/203809008-iOS-How-to-Test-In-App-Purchases-in-Your-App

If you have Sandbox users:

You can try calling finishTransaction: but you need to make sure transaction SKPaymentTransactionState valid.

In the finishTransaction documentation:

 // Asynchronous. // Remove a finished (ie failed or completed) transaction from the queue. // Attempting to finish a purchasing transaction will throw an exception. 

So, to safely delete completed transactions:

 for transaction in SKPaymentQueue.default().transactions { guard transaction.transactionState != .purchasing, transaction.transactionState != .deferred else { //Optionally provide user feedback for pending or processing transactions return } //Transaction can now be safely finished SKPaymentQueue.default().finishTransaction(transaction) } 

The documentation for .purchasing and .deferred pretty vague:

 case purchasing // Transaction is being added to the server queue. case deferred // The transaction is in the queue, but its final status is pending external action. 

From what I understand, processing pending and / or processing transactions should be fairly passive. The application has completed everything necessary and is awaiting a response from the iTunes Store server or some other dependency (i.e. payment authorization).

paymentQueue: updatedTransactions: will be called into the SKPaymentTransactionObserver when the transaction is updated.

As far as your transaction queue is stuck in limbo, I bet that all the transactions in your queue are in .purchasing state. This is most likely an iTunes Connect / Sandbox / iTunes Account Production user error. Others, including me, also had this question. There is an error report for this. Try to recreate / change the password of your sandbox user or create a new Sandbox user for testing.

More details here: https://forums.developer.apple.com/thread/70418

0
source

Source: https://habr.com/ru/post/888580/


All Articles