Consumable In App Purchases on Android

Dan Friedman dan at
Tue Aug 8 15:51:08 EDT 2023


Thank you for the input.   The code snippet you said you are using has mobileStoreConsumePurchase IMMEDIATELY following mobileStoreMakePurchase.  Is that what you are doing?   The LC example show that mobileStoreMakePurchase should be handled in the purchaseStateUpdate message after the purchase is paymentReceived”

If that’s true, then I feel like I’m doing exactly what you are suggesting.   The purchase is fine, but the consumption if not working for me.   Here’s what I’m doing:

on startInAppPurchase numCredits
  //setup purchase id
  if numCredits = 1 then
    put 1001 into productID
    put "$0.99" into productAmount
  else if numCredits = 2 then
    put 1002 into productID
    put "$1.99" into productAmount
  else if numCredits = 3 then
    put 1003 into productID
    put "$2.99" into productAmount
  else if numCredits = 11 then
    put 1011 into productID
    put "$9.99" into productAmount
  end if

  //confirm with user
  if numCredits = 1 then
    put "Are you sure you want to purchase 1 credit for" && productAmount & "?" into pString
    put "Are you sure you want to purchase" && numCredits && "credits for" && productAmount & "?" into pString
  end if
  if myAnswer(pString,"Cancel|Buy Now","Purchase Confirmation","|appBlue") = "Cancel" then //my custom answer dialog
    exit startInAppPurchase
  end if

  //initiate the purchase
  mobileStoreSetProductType productID,"inapp"
  mobileStoreMakePurchase productID,"1","testPayload - Android Only"
end startInAppPurchase

on purchaseStateUpdate pPurchaseID,pProductID,pState
  global user

  switch pState
    case "paymentReceived"
      put mobilePurchaseGet(pPurchaseID,"receipt") into rawReciptData

      //tell our server is was sucessful and log users credits to database
      if isIPhone() then
        if db_validateAppleReceipt(rawReciptData) then
          add (pProductID-1000) to user["credits"]
        end if
        //will add Android database call when ready
      end if

      mobileStoreConfirmPurchase pProductID //Inform the store that the purchase identifier productID was successfully delivered
      if isAndroid() then
        mobileStoreConsumePurchase pProductID. //  <--- DOES NOT SEEM TO CONSUME THE PRODUCT !!
      end if

      loadMeUp //refresh our display
    case "error"
      answer "Error occured during purchase handling:" & return & return & mobileStorePurchaseError(pPurchaseID)
    case "invalidSKU"
      answer "Invalid SKU."
    case "alreadyEntitled"
      answer "Already Owned."
    case "restored"
      answer "restored"
     offerPurchasedProduct pProductID
      mobileStoreConfirmPurchase pProductID

      loadMeUp //refresh our display
    case "canceled"
    case "cancelled"
      answer "Purchase Cancelled:" && pProductID
  end switch
end purchaseStateUpdate

Do you see anything wonky in my code?


From: use-livecode <use-livecode-bounces at> on behalf of Andrew at MidWest Coast Media via use-livecode <use-livecode at>
Date: Tuesday, August 8, 2023 at 10:57 AM
To: use-livecode at <use-livecode at>
Cc: Andrew at MidWest Coast Media <andrew at>
Subject: Re: Consumable In App Purchases on Android
If I’m consuming instantly, this is the code working in my Android/iOS project with comment lines explaining the values being passed in the Android build (note that the product ID values are more specific for Android than iOS):

        if tPlatform = "android" then
                 put "com.midwestcoastmedia.dj3pm." into tProductIDbase
         else if tPlatform = "iPhone" then
                 put EMPTY into tProductIDbase
        end if
        put tProductIDbase & pWhichProduct into tProductID

      ## mobileStoreSetProductType "com.midwestcoastmedia.dj3pm.tip01", "inapp"
      mobileStoreSetProductType tProductID, "inapp"
      ## mobileStoreMakePurchase "com.midwestcoastmedia.dj3pm.tip01", "1", "Thanks for the buck!"
      mobileStoreMakePurchase tProductID, "1", tMessage
      ## mobileStoreConsumePurchase "com.midwestcoastmedia.dj3pm.tip01"
      mobileStoreConsumePurchase tProductID
      put the result into tCatch

BUT you’ll also need to implement the on purchaseStateUpdate handler to completely execute the purchase cycle. The lesson is sometimes hard to follow when Android changes their branding/naming but <> has a good example of this.

—Andrew Bell

More information about the use-livecode mailing list