[Solved] How to use Unity IAP ConfirmPendingPurchase properly?

I use Unity IAP, and my own server to save data. Once I validated the receipt, the purchase will be in a state of pending and the productID and transactionID will be sent to server. When it’s saved, a local function will be called to confirm the pending purchase. But how do I do this properly in code?

I know I should call something like mycontroller.ConfirmPendingPurchase(product); but how do I make a product instance with the transactionID and productID (and other data if needed) properly to be used as the parameter here?

Discussing with the IAP team, the suggestion is to pass the product info to your server, then send it back to the application when complete. In this scenario, you would need to hook up your own callback, and then in this client callback, call ConfirmPendingPurchase with the product info that is passed back.

Hi Jeff, thanks a lot for your reply. That’s exactly what I thought. However could you give me an example code of how to get or make a Product instance with the right info please? I assume it’s the transactionID that all it needs?

I can get the product with id as:

var product = m_StoreController.products.WithID("diamond1");

but how to change the transactionID of the product?(it’s read only)

case A: For one productID there can be more than one transactionID for example: player bought a “diamond1”, while it’s still pending, the player bought another “diamond1”.

case B: Player bought “diamond1”, but unity IAP prevent him/her to buy more “diamond1” while it’s still pending(in this case what would happen if the game call InitiatePurchase with the same productID?). The product with this productID will always have the transactionID info while it’s still pending even if the app is restarted or re-installed.

Which case is right? If it’s case B, then I only have to send productID to my sever right?

Can you not pass the product to your server (that has the transactionID), then back again? Case A should not be possible.

I assume you’re not a programmer.

The communication through the client and server is via JSON string. The product saved locally is an instance of a Product class, that contains some data(including transactionID, productID, etc).

You can’t pass the “product”, rather than passing some data you extract from the product(the productID in case B), and when you want to use the product, use the data (productID) passed back to regenerate a new “product” with the same data.

What I want to know is the way I’m regenerating the new product(as in case B, actually it’s generated by unity IAP, I just find it with the productID) is legit and proper. Thanks!

@SunPengfei You can pass strongly typed objects, such as through a REST web service, through serialization. I have asked the IAP team for example code and they are looking into it. Hopefully we can provide the syntax for creating the required product object from a JSON string.

Thanks a lot, I’ve tried pickle in python, I guess it’s something similar.

However the way of passing only productID and/or transactionID would be much easier and more elegant, just my opinion :slight_smile: I’m waiting for your reply, please forward my idea to the IAP team, case B, which I summarize here in simple words:

1 When process a purchase, set the state to pending, and send its productID to server.
2 Process in server and return the productID.
3 Call the following function to get the product with the productID and confirm the pending purchase:

    void ConfirmPendingPurchase(string productID)
    {
        var p = m_StoreController.products.WithID(productID);
        m_StoreController.ConfirmPendingPurchase(p);
    }

I’m 90% sure this will work. However my app and IAP items are still in review in appstore so I can’t test it right now. I just want to make sure that in the code snippet the product “p” does has transactionID data properly even if I only passed and received productID to and from server, and even if the app is restarted or reinstalled.

4 Likes

confirmed, it worked

3 Likes