purchase

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package purchase sells in-app products through the platform store.

purchase.OnUpdate(app, func(t purchase.Transaction) { // before Run
    if t.State == purchase.Purchased || t.State == purchase.Restored {
        unlock(t.ProductID)
        purchase.Finish(app, t)
    }
})
ps, invalid, err := purchase.Products(app, "pro", "coins100")
t, err := purchase.Buy(app, "pro")

iOS: StoreKit (SKProductsRequest and SKPaymentQueue — the Objective-C API; StoreKit 2 is Swift-only). The transaction observer is installed at launch, so transactions left over from an earlier run (an interrupted purchase, Ask to Buy approvals, renewals) arrive as updates after Started. Purchased and restored transactions stay in the queue until Finish: deliver the content first. Failed transactions are finished automatically. Products must exist in App Store Connect (or a StoreKit configuration in Xcode); otherwise they come back as invalid.

Android: Google Play Billing needs the Play Billing library, which Tango does not bundle (the host has no third-party dependencies). Every call returns ErrUnsupported; see docs/mobile.md ("应用内购买") for wiring it in.

Calls that wait for the store block; on Android call them from a goroutine (see docs/mobile.md). Desktop: ErrUnsupported.

Index

Constants

View Source
const (
	Consumable    = platform.ProductConsumable
	NonConsumable = platform.ProductNonConsumable
	Subscription  = platform.ProductSubscription

	Purchasing = platform.TxPurchasing
	Purchased  = platform.TxPurchased
	Failed     = platform.TxFailed
	Restored   = platform.TxRestored
	Deferred   = platform.TxDeferred
)

Variables

View Source
var (
	ErrUnsupported  = platform.ErrUnsupported
	ErrCanceled     = platform.ErrCanceled
	ErrNotAvailable = platform.ErrNotAvailable // payments disabled (parental controls, MDM)
	ErrNotFound     = platform.ErrNotFound     // unknown product / no receipt
)

Functions

func CanMakePayments

func CanMakePayments(a *tango.App) bool

CanMakePayments reports whether the user may buy (false when restricted or unsupported).

func Finish

func Finish(a *tango.App, t Transaction) error

Finish tells the store the app delivered t's content. Until then the store reports t again on every launch.

func OnUpdate

func OnUpdate(a *tango.App, fn func(Transaction))

OnUpdate calls fn on the main thread for every transaction update: the app's own purchases (each state), restores, and transactions the store delivers by itself. Register it before Run. Several functions may be registered; each sees every update. It does nothing where the backend has no store.

func Receipt

func Receipt(a *tango.App) ([]byte, error)

Receipt returns the store receipt (iOS: the App Store receipt, PKCS #7) to validate purchases on your server.

func Restore

func Restore(a *tango.App) error

Restore asks the store to re-deliver completed purchases of non-consumables and subscriptions; they arrive through OnUpdate with State Restored. It returns when the store is done.

Types

type Product

type Product = platform.Product

func Products

func Products(a *tango.App, ids ...string) (products []Product, invalid []string, err error)

Products looks products up by id. invalid lists the ids the store does not know.

type ProductKind

type ProductKind = platform.ProductKind

type State

type State = platform.TransactionState

type Transaction

type Transaction = platform.Transaction

func Buy

func Buy(a *tango.App, productID string) (Transaction, error)

Buy purchases one unit of a product returned by Products and waits for the outcome: Purchased, or Deferred (awaiting approval; the final state arrives through OnUpdate). ErrCanceled when the user canceled.

func BuyQuantity

func BuyQuantity(a *tango.App, productID string, n int) (Transaction, error)

BuyQuantity is Buy for n units of a consumable.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL