Realtime Developer Notification Callback for Purchases

On this page an example in lua is given for implementing a realtime developer notification webhook to handle refunds for purchases.

On Nakama v3.36.0 the given code does not work. It results in the following:
attempt to call a non-function object

I believe this is because the register_purchase_notification_google does not exist in the Lua runtime.

Is this correct? If yes, is the documentation wrong? Is using Go the only option to handle these realtime notifications?

Can I use a custom rpc to implement this myself? For example:

local function handleGooglePurchaseNotification(context, payload)local logger = context.loggerlocal nk = context.nk

-- Parse the webhook payload from Google
local data = nk.json_decode(payload)

logger.info(("Google purchase notification received: %s"):format(nk.json_encode(data)))

-- Extract notification details
local notificationType = data.notification_type or data.notificationType
local purchaseToken = data.purchase_token or data.purchaseToken
local productId = data.product_id or data.productId

-- Validate the purchase with Google
local validation = nk.purchase_validate_google(context.user_id, purchaseToken, {})

if validation then
    logger.info(("Validated purchase: product=%s, user=%s"):format(
        validation.product_id,
        context.user_id
    ))
    
    -- Handle different notification types
    if notificationType == "PURCHASED" or notificationType == "purchased" then
        logger.info("Granting entitlement for product: " .. validation.product_id)
        -- Grant entitlement logic here
        
    elseif notificationType == "REFUNDED" or notificationType == "refunded" then
        logger.info("Revoking entitlement for refunded purchase")
        -- Revoke entitlement logic here
        
    elseif notificationType == "CANCELED" or notificationType == "canceled" then
        logger.info("Subscription canceled")
        -- Handle cancellation
        
    elseif notificationType == "RENEWED" or notificationType == "renewed" then
        logger.info("Subscription renewed")
        -- Handle renewal
    end
end

return nk.json_encode({success = true})

end

– Register as RPC endpointnk.register_rpc(handleGooglePurchaseNotification, “google_purchase_webhook”)

If yes, how can I provide the http_key to the google play console webhook url so it can send these webhooks. Is it just a matter of adding it as a query parameter?
https://my-nakama-server/v2/rpc/google_purchase_webhook?http_key=server-http-key

Thank you!

Hi @SirCez,

It looks like due to an oversight the functions to register the callbacks are missing from the Lua runtime.

You may be able to use a custom RPC, but you’d have to manually insert into the purchases/subscriptions tables or maintain dedicated storage objects yourself.

I suggest that for the time being you use the JS runtime to register these callbacks. We’ll be adding the missing Lua functions in a following release.

Best.

Thank you. I have implemented a custom RPC to receive the webhook at this time.