# Making a purchase

The process of making a purchase will typically go like this.

- Provide the user with an option to make the purchases e.g. a screen with buttons on showing all the different products available.
- Attempt to purchase the product.
- Wait until the purchase process has completed.
- Determine whether the purchase succeeded or failed.
- If appropriate supply the user with the product.

The key commands involved in this process are -

- InAppPurchaseActivate - this takes an ID number that corresponds to the product IDs set up by InAppPurchaseAddProductID.
- GetInAppPurchaseState - determine the current state when a product has been activated. A value of 0 indicates that the process is taking place. A value of 1 means the process has finished. This only relates to the state of attempting a purchase that may have succeeded or failed.
- GetInAppPurchaseAvailable - used to check whether a product is available after GetInAppPurchaseState has finished.

Code to handle a purchase could look like this.

    InAppPurchaseActivate ( 0 )

    do
    	if GetInAppPurchaseState ( ) = 1
    		exit
    	endif

    	sync ( )
    loop

    if GetInAppPurchaseAvailable ( 0 ) = 1
    	// provide the content
    endif

The call to InAppPurchaseActivate starts the process of purchasing the product with ID 0. In our case this is "productID1". After calling this command it is necessary to wait and check the state of the in app purchase.

The do loop checks the state of the purchase by calling GetInAppPurchaseState. Inside the do loop is a call to sync. This is important - you cannot simply wait in a loop without passing control back to AppGameKit, so ensure you call sync while waiting. Bear in mind that there may be a small delay after calling InAppPurchaseActivate prior to any dialogs appear on screen, therefore at this point it would be a good idea to display something on screen indicating to the user that an attempt is being made to purchase the product.

The final stage is a call to GetInAppPurchaseAvailable. If this returns 1 then the purchase was a success and our product can be presented to the user. If this returns 0 then something went wrong e.g. the user decided to cancel at some point in the process or the payment failed to go through, in which case we must not let the user have the product.