• Studio

  • Bots

  • Web API

  • Designer Resources

  • Host Resources

  • API

    Services

    Edit

    Payments

    The Payments service provides methods for creating and managing payments in your game. Check out the PaymentsError class for error codes that can be returned by the Payments service.

    Methods

    ServerOnly

    Get a list of purchases made by the current user.

    Payments.GetPurchases(player, nil, 100, nil, function(purchases, nextCursorId, getPurchasesErr)
      
      -- Handle error
      if getPurchasesErr ~= PaymentsError.None then
        error("(Server) Failed to get player purchases: " .. getPurchasesErr)
        return
      end
    
      print("(Server) Player purchases:")
      for _, purchase in ipairs(purchases) do
          print("Purchase ID: " .. tostring(purchase.id))
          print("Product ID: " .. tostring(purchase.product_id))
          print("User ID: " .. tostring(purchase.user_id))
          print("Purchase Date: " .. tostring(purchase.purchase_date))
      end
    end)
    

    Parameters

    player
    Player
    productId

    string | null

    limit

    integer

    cursorId

    string | null

    callback

    function

    Returns

    void

    ServerOnly

    Get a list of purchases made by the current user for a specific product.

    Parameters

    player
    Player
    productId

    string

    limit

    integer

    cursorId

    string | null

    callback

    function

    Returns

    void

    ServerOnly

    Get a product by its ID.

    Payments.GetProduct("eel", function(product, getProductErr)
      if getProductErr ~= PaymentsError.None then
        error("(Server) Failed to get product: " .. getProductErr)
        return
      end
    
      print("Product ID: " .. product.product_id)
      print("Product Name: " .. product.name)
      print("Product Price: " .. product.price)
      print("Product Description: " .. product.description)
    end)
    

    Parameters

    productId

    string

    callback

    function

    Returns

    void

    ServerOnly

    Get a list of In-World Purchases (IWP).

    Payments.GetProducts(100, nil, function(products, nextCursorId, getProductsErr)
      if getProductsErr ~= PaymentsError.None then
        error("(Server) Failed to get In-World Purchases: " .. err)
        return
      end
    
      print("(Server) In-World Purchases:")
      for _, product in ipairs(products) do
        print("Product ID: " .. product.product_id)
        print("Product Name: " .. product.name)
        print("Product Price: " .. product.price)
        print("Product Description: " .. product.description)
      end
    end)
    

    Parameters

    limit

    integer

    cursorId

    string | null

    callback

    function

    Returns

    void

    ServerOnly

    Acknowledge a purchase made by the current user.

    Payments.AcknowledgePurchase(purchase, true, function(ackErr: PaymentsError)
      if ackErr ~= PaymentsError.None then
        error("(Server) Something went wrong while acknowledging purchase: " .. ackErr)
        return
      end
    
      print("(Server) Purchase acknowledged")
    
      -- Implement your logic here to grant the user the product they purchased
    end)
    
    -- Or reject the purchase
    Payments.AcknowledgePurchase(purchase, false) -- Acknowledge purchase without a callback
    

    Parameters

    acknowledged

    boolean

    callback

    function

    Returns

    void

    ServerOnly

    A function that is called when a purchase is made.

    local HandlePayment = function(purchase: WorldProductPurchase, player: Player)
      -- Handle the purchase logic here and grant the user the product they purchased
      -- You must acknowledge the purchase after handling it by calling the "AcknowledgePurchase" method
    end
    
    Payments.PurchaseHandler = HandlePayment -- Set the purchase handler
    

    Parameters

    purchase
    WorldProduct
    player
    Player
    ClientOnly

    Prompt the current user to make a purchase.

    Payments:PromptPurchase("eel", function(paid)
      if paid then
        print("Purchase successful")
      else
        print("Purchase failed!" .. tostring(paid))
      end
    end)
    

    Parameters

    productId

    string

    callback

    function

    Returns

    void

    Updated 3 months ago

    PocketWorlds Icon

    © 2024 Pocket Worlds. All rights reserved.