Skip to content

purchase ​

go
import "github.com/tangoui/tango/sys/purchase"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

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.

常量 ​

go
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
)

变量 ​

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

函数 ​

func CanMakePayments ​

go
func CanMakePayments(a *tango.App) bool

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

func Finish ​

go
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 ​

go
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 ​

go
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 ​

go
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.

类型 ​

Product ​

go
type Product = platform.Product

func Products ​

go
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.

ProductKind ​

go
type ProductKind = platform.ProductKind

State ​

go
type State = platform.TransactionState

Transaction ​

go
type Transaction = platform.Transaction

func Buy ​

go
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 ​

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

BuyQuantity is Buy for n units of a consumable.

以 MIT 许可发布