问题
I am implementing in app purchase on iOS, where my server will provide access to certain content when the user purchases an item. I have everything working, including having the server able to verify the receipt from the purchase before providing access to the content.
However, if for some reason the app is not able to register the transaction with my server (due to a server outage, for example), I'd like to cancel the transaction so that the user isn't charged by iTunes. Is this possible?
To explain in further detail, the app contacts my server from within the paymentQueue:updatedTransactions function after the user clicks "buy." Currently, I call:
[_myQueue finishTransaction:transaction];
after contacting my server. However, if there was an error in contacting the server, is there a function I can call to cancel the transaction instead of finishing it?
I see from here that some people just don't call finishTransaction, so that the transaction gets restored the next time the user starts the app and the transaction observer is added (so the app can try to contact the server again) However, this is less than ideal because the user won't have access to the content until they restart the app and the server is available again, and yet the user will have already paid.
Thanks!
回答1:
I think the only elegant way to achieve this is to have your server implement an availability check and call it as the last thing before you actually make the purchase. Once the user has confirmed the purchase via the UIAlertView that is presented to him, the purchase is made and you must do all that you can so that he gets his money's worth.
回答2:
Since there is not a method to manually cancel a transaction, this is how I handle it: I do not mark the transaction finished, then I have the option of later manually calling the paymentQueue:updatedTransactions: like so:
if (SKPaymentQueue.defaultQueue.transactions.count > 0) {
//show a loading box to user
//...
//now manually call the observer method
[self paymentQueue:SKPaymentQueue.defaultQueue updatedTransactions:SKPaymentQueue.defaultQueue.transactions];
}
If my server call fails, then I present a UIAlertView with the option to Retry or Cancel. Retry calls the code above.
That way you do not have to wait until the app restarts, however if it does, that's ok too.
来源:https://stackoverflow.com/questions/9197231/how-to-cancel-in-app-purchase-if-my-server-fails-and-cannot-provide-content-io