Consumable In App Purchases on Android

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


Andrew,

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
  else
    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
  mobileStoreEnablePurchaseUpdates
  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
      else
        //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

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

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



Do you see anything wonky in my code?

--Dan

From: use-livecode <use-livecode-bounces at lists.runrev.com> on behalf of Andrew at MidWest Coast Media via use-livecode <use-livecode at lists.runrev.com>
Date: Tuesday, August 8, 2023 at 10:57 AM
To: use-livecode at lists.runrev.com <use-livecode at lists.runrev.com>
Cc: Andrew at MidWest Coast Media <andrew at midwestcoastmedia.com>
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

      mobileStoreEnablePurchaseUpdates
      ## 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 https://lessons.livecode.com/m/4069/l/184481-how-do-i-implement-in-app-purchases-in-livecode-google-play-store <https://lessons.livecode.com/m/4069/l/184481-how-do-i-implement-in-app-purchases-in-livecode-google-play-store> has a good example of this.

—Andrew Bell


More information about the use-livecode mailing list