Search Unity

IOS Native InApp Purchasing with Playmaker

Discussion in 'Assets and Asset Store' started by stanislav-osipov, Sep 24, 2014.

  1. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    Hello, I want to share, how you can implement purchasing with IOS Native using Playmaker.

    The guide will cover the way to implement:
    • Consumable items
    • Non-Consumable items
    • Restoring purchases
    I hope this tutorial will be useful not for only IOS Native user, but for everyone who want’s to implement In Apps in the IOS Game.

    To get started you need:

    First of all you need to setup your IOS app, the guide can be found here.

    Now we can get started on In Apps implementation.

    Note: I haven't used any global variable or events for this example. But you can feel free to use it on your own implementation to make it more elegant.

    Note: Inside IOS Native The playmaker actions scripts can be found in the zip archive at: Assets/Extensions/IOSNative/Addons/PlayMakerActions.zip And the example scene with we will make in this tutorial can be founded at: Assets/Extensions/IOSNative/xExample/Scenes/PlayMaker/BillingWithPlaymakerExample

    Here is how our scene will looks like.



    And here is scene hierarchy view




    Let’s start from the Connection implementation.




    At the FSM start we will use INS_BillingInit action, with will fire the Connected and Failed actions for as.

    Let also add some messages to Connected and Failed states using ISN_MessagePopUpAction action. At the beginning of connection let’s also use ISN_ShowPreloader action, and ISN_HidePreloader when connection is finished.

    Here is view of init and Connected states.





    Now let’s implement the our purchase buttons. We need to be able to purchase red sphere, green sphere and 100 coins pack. So let’s make 3 buttons for it.





    We going to use ISN_BillingEventsListener action to listener for the billing connect event. As soon as we will have this event fired, it means that billing is ready to use and we can go to Purchase Button state, with will draw the button.

    When user clicks the buttons, we will go to Purchase state with ISN_PurchaseAction.



    We need to specify what product we will purchase by clicking on our button. In this case we will set our non-consumable red sphere product id.

    Make sure you have setupped products in plugin settings. If you haven’t done this yet, go to Windows → IOS Native → Edit Settings and add your products ids that you have created earlier in ITunes connect



    For this tutorial I have two non-consumable items:
    • ios.native.green.sphere.product
    • ios.native.red.sphere.product
    and one consumable
    • small_coins_bag
    Also make sure the “Send Fake Actions In Editor” option is enabled. It will give us ability to test our implementation right in the editor.


    One more important thing, that on the Purchase state we not actually unlocking the product or adding the coins. In my case I will simply show the pop up:




    This is made due to security reasons, for example if for some reason our app will be terminated during purchase flow, we will not provide our player with the item event he is purchased it.

    So for preventing this we will create one manager with will manage all our products.

    Do the same for the rest of our product buttons.

    We will also implement Restore purchases button using ISN_RestorePurchases action.



    The button is implemented similar to all our purchase buttons.



    With ISN_RestorePurchases you can also listen for the Item Restored Event (with will be fired for each restored item) but in this example we do not need this.


    So now we going to the most important part. The purchase manager. here is how it looks like:



    Starting from Load Products States action we loading all our product states from the PlayerPrefs.



    Variables red_sphere_item_id, green_sphere_item_id and coins_item_id are equals to iTunes ids of our products.

    When our products states loaded in our local variables we can update the UI (or any other game play logic) according to our products states.

    So for example here how we check and unlock greensphere product:

    Check For Green Sphere State:



    So we simply checking if our GreenSphereVal variable (which was loaded in Load Products States state is equals 1), and it it is, it means that our sphere is purchased and we will go to Unlock Green Sphere state.

    For coins it’s a bit different, our coins amount was already loaded in ,so we need simply convert it to String and update the UI:





    So far we have:
    • Connecting
    • Runing purchase and restore action
    • Loading produc states
    • Update Game UI according to the product state
    The only thing is left is to listened for purchase/restore events and update PlayerPrefs depending on product id.

    That’s why when our product update finished we going to Listen For Billing Events state.

    In this state we will use already known ISN_BillingEventsListener action, but for this time we going to litner for the Transaction Complete, and Restore Complete Actions.

    When Transaction Complete it means that some product was just purchased so we need to update its state in the PlayerPrefs.

    First of all we will go to Check If Coins Purchased state and check if that product was coins. We cheking it because we save our consumable item state in a but different way, point is we need to update coins amount instead of just saving purchased flag for our red and green sphere products.




    So we checking current BoughtProductId with coins_item_id and if it’s equals we going to add 100 for our current coins amount and save it to PlayerPrefs:




    And then we will run our product stated update cycle by returning to Load Products States states.

    For Green and Red Sphere products we have same logic we just saving 1 to PlayerPrefs with the item id key:



    And returning to Load Products States as well.


    That’s it Consumable, Non-Consumable products with Restore Purchases ability was just implemented in our game.


    Note: In the example scene I also added Clean PlayerPrefs And Restart sate, with will clean up all our PlayerPrefs and restart the scene. As imitation new device for Restore Purchases Testing.


    Note: If you will test in Editor ISN_RestorePurchases will restore all products listed in plugin setting, event consumable and no matter was it earlier purchased or not. You should understand that it’s firing those events for testing purpose only.

    On real IOS device only non-consumable items with was earlier purchased by current user will be restored. And do not forget to use iTunes test user when you testing your purchases implementation on real device.
     
    Last edited: Sep 24, 2014
    XCO likes this.
  2. XCO

    XCO

    Joined:
    Nov 17, 2012
    Posts:
    380
    Question ? In order to test this on the iPad but with my non consumable items what should I do ?

    Should I just place my product ID and game APP ID where yours is ?

    For instance I have to AVATARS for sell. Should I just place those instead of red sphere and green sphere ?

    Cause it seems like the coin logic is tied into it ? How do I test what you created with just the 2 non consumable items I have without the coins ?

    Also when I click on the restore purchase button after only having bought (in the editor) the RED SPHERE it unlocks BOTH ?
     
    Last edited: Sep 25, 2014
  3. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    Here is line form the guide:
    Note: If you will test in Editor ISN_RestorePurchases will restore all products listed in plugin setting, event consumable and no matter was it earlier purchased or not. You should understand that it’s firing those events for testing purpose only.


    1) Set up your products id in the plugin settings:
    Windows → IOS Native → Edit Settings




    Update this variables values with your non-consumable product ids


    Variables red_sphere_item_id, green_sphere_item_id and coins_item_id are equals to iTunes ids of our products.



    If you not going to test consumable item, simply forget about the coins.
    You can remove it from Billing Manager logic, or simply live it like in example, it will not work, if you will try to purchase coins it will say that such product wasn't founded.
     
    XCO likes this.
  4. XCO

    XCO

    Joined:
    Nov 17, 2012
    Posts:
    380
    Thanx bro. Ill try this today :| I'm sorry for all the hassle. Everything else was so easy... :| (Wwhhhhhhhhhyy Cruel World!!)
     
    Last edited: Sep 25, 2014
  5. XCO

    XCO

    Joined:
    Nov 17, 2012
    Posts:
    380
    Confused again want to make sure I do it right...

    For the RED BALL for example I notice in the IOS settings that you use the /// ios.native.red.sphere.product ////

    But in the variable you want me to change the //// red_sphere_item_id /// part <<< and leave the ((( ios.native.red.sphere.product ))) alone ?
     
  6. XCO

    XCO

    Joined:
    Nov 17, 2012
    Posts:
    380
    10470174_1525730331004738_775007749091969502_o.jpg OK I got it to work, Sent it in FINGERS CROSSED STAN :-O 10470174_1525730331004738_775007749091969502_o.jpg
     
    Last edited: Sep 26, 2014
  7. XCO

    XCO

    Joined:
    Nov 17, 2012
    Posts:
    380
    Hey STAN just wanted to let you know everything is working fine and my APP got approved!!

    On behalf of the PlayMaker community Thank you so much for all of this, all of your hard work and Time I appreciate it so much BRO!! :D


    (A link to the game with the "restore Button")

    https://itunes.apple.com/us/app/super-flight/id910229614?mt=8
     
  8. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    I glad you made it! I will play your game shortly :)
    Cheers!
     
    XCO likes this.
  9. munaeem

    munaeem

    Joined:
    Jul 6, 2012
    Posts:
    13
    When I test the example scene for playmaker in unity for in-app purchase (billing) on my phone it goes to purchase failed. I put the product id on the editor setting with my App Id as well but it does not work. Keep sending me failed purchase. What should i do?
     
  10. stanislav-osipov

    stanislav-osipov

    Joined:
    May 30, 2012
    Posts:
    1,790
    Can I get your device log?
    Cheers!
     
  11. darkinyourass

    darkinyourass

    Joined:
    Sep 24, 2015
    Posts:
    28
    Contact me via email - support@stansassets.com and i will send you the package with the fix.
     
  12. tomislav_

    tomislav_

    Joined:
    Sep 22, 2013
    Posts:
    4
    How can I on restore purchase with Playmaker Action (when I have more then one product) find which product are bought so that I can set playerprefs.
     
  13. Lab618

    Lab618

    Joined:
    Jan 12, 2015
    Posts:
    36
    Hello,

    I'm currently trying to set up a 'restore purchases' button but having a bit of trouble. The ISN_Restore Purchases action always gives a success event, even on a device where there are no purchases to restore, is this correct? I was expecting it to fail if there are no previous purchases to restore.

    I'm also unsure of where the ISN_Restore Purchases action stores the information that it retrieves.

    Sorry if I'm being a bit dim but any help would be greatly appreciated. I only need to restore a single item.

    Thanks
    Steve
     
  14. Lab618

    Lab618

    Joined:
    Jan 12, 2015
    Posts:
    36
    OK, so I've taken a look a the code for the ISN_Restore Purchases action and it appears to be internally storing the data in an array, but I still don't understand how I access this once it has been retrieved.

    Please help.

    Cheers
     
  15. Litow

    Litow

    Joined:
    Jan 8, 2015
    Posts:
    17
    Hello Lab618. Having the same trouble. Were you able to resolve this?
     
  16. Litow

    Litow

    Joined:
    Jan 8, 2015
    Posts:
    17
    In case anyone else gets here with the same problem that canceling a restore call always trigger a successful result. :)
    I "think" I resolved this by collecting the "currentRestoredItem" under the ISN_Restore_Purchase and comparing that against the product ID I registered for purchase.
    Will need to do more testing though.
    It is a shame that the developer has not clarified this process better, given that it is a requirement for publishing an app with iap.
     
  17. eonyanov

    eonyanov

    Joined:
    Oct 6, 2014
    Posts:
    21
    Hi there, did you solve this problem with restoring the purchase? Can you describe in more detail.