Skip to content

feat(core): in-app purchases through StoreKit 2 - #406

Merged
houko merged 1 commit into
mainfrom
feat/in-app-purchases
Nov 16, 2026
Merged

houko merged 1 commit into
mainfrom
feat/in-app-purchases

Conversation

@houko

@houko houko commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds in-app purchases on macOS, iOS and tvOS through StoreKit 2, the Apple half of the design in the issue's comment. Fixes #353.

  • core: Products(ids) answers the store's products with its own DisplayPrice (never a hardcoded price), type, and a subscription's group, period, introductory and promotional offers. Purchase(product, PurchaseOptions{AccountToken, Quantity, Offer}) answers the Transaction, whose Signed is the StoreKit JWS for server verification, or a PurchaseError: PurchaseCancelled and PurchasePending (Ask to Buy) apart from PurchaseFailed, which wraps the store's words. OnPurchaseUpdate hears Transaction.updates (renewals, approvals, other devices, refunds, unfinished at launch), kept for the first handler as OnNotificationPress keeps its press. FinishTransaction, CurrentEntitlements and SyncPurchases (AppStore.sync()) complete it. Env.CanPurchase, AccessPurchases, and the gate test list the six methods.
  • Apple: StoreKit 2 is Swift-only, so cmd/nsgo/storekit/StoreKit.swift (about 250 lines) exports two @_cdecl functions, nsgo_storekit_start(callback) and nsgo_storekit_call(id, json), and only moves JSON; internal/platform/apple/purchases.go loads it from the bundle's frameworks through purego. The tool compiles it with xcrun swiftc, cached like the widgets' library, only when nsgo.yaml sets apple.purchases: NSGoStoreKit.framework on iOS/tvOS and libNSGoStoreKit.dylib on macOS (built for macOS 12; on macOS 11 it does not load and CanPurchase is false). App Clips never get it; watchOS leaves it out with a note.
  • No entitlement is added: com.apple.developer.in-app-payments is Apple Pay's merchant entitlement. In-App Purchase is an App ID capability that is on by default and needs nothing in the signature.
  • TSX: Purchases module (available, products, purchase resolving {status: "purchased" | "pending" | "cancelled"}, finish, currentEntitlements, sync, on("update")) behind the purchases capability, with typings and the sandbox surface.
  • nsgotest: Products, PurchaseRequests, PurchaseAnswer, Unfinished, Entitlements, Finished, SyncAnswer, UpdatePurchase, and the UnfinishedTransactions option for updates at launch.
  • Gallery: System page "In-app purchases" card; the gallery sets apple.purchases: true.
  • Docs: architecture services/tool/tsx, public services/building/tsx (en and zh), skill references, AGENTS.md's Swift rule names the new library, CHANGELOG, and roadmap rows: device verification, managing subscriptions (not implemented here), Android Play Billing (tied to remote push open decision 2 and the Android SDK License question; not added), and watchOS/Windows/Linux/web/mini programs.

Targets

  • Shared code (core, base, ui, tsx, the nsgo tool)
  • macOS
  • iOS
  • tvOS
  • watchOS (unavailable, roadmap row)
  • Android (unavailable, roadmap row)
  • Windows (unavailable, roadmap row)
  • Linux (unavailable, roadmap row)
  • Web (unavailable, roadmap row)
  • Mini programs (unavailable, roadmap row)
  • Docs or website only

Screenshots

None captured. The only visible change is the gallery's new System card (three buttons, a caption and a result line, in the existing card layout).

Verification

Ran on a Mac Studio with Xcode 27.1:

  • go tool nsgo check and go tool nsgo check -target all passed.

  • go tool nsgo test passed for ./core, ./nsgotest, ./tsx, ./tsx/pkg, ./internal/driver, ./internal/platform/apple, ./internal/moduletest, ./examples/gallery and ./cmd/nsgo. TestStoreKitCompile compiles the Swift library for macOS and the iOS and tvOS simulators and checks both exported symbols.

  • In the iOS 27.0 simulator, with a StoreKit configuration file, a throwaway test app (not committed) ran under go tool nsgo test -target ios_simulator. It started StoreKitTest's SKTestSession from Go, with dialogs disabled and XCTest found through -env DYLD_FRAMEWORK_PATH=<iPhoneSimulator platform>/Developer/Library/Frameworks. Through this PR's library and backend:

    • products answered with the file's prices, types, period and free-trial offer
    • a non-consumable and an auto-renewable subscription were bought with the account token, a verified 3-part JWS and environment Xcode
    • FinishTransaction succeeded, then answered not found
    • CurrentEntitlements listed both
    • an Ask to Buy purchase answered pending, and approveAskToBuyTransaction delivered it through OnPurchaseUpdate
    • a refund arrived as a revoked update and dropped out of the entitlements

    On the simulator's first run, the first purchase showed an Apple Account sign-in sheet and timed out; the rerun went through.

  • Not run: cancel, failure, SyncPurchases and launch-time unfinished delivery against StoreKit. These are covered only in nsgotest; with dialogs off there is no sheet to cancel, and simulatedError(forAPI:) is Swift-only.

  • Not run: macOS and tvOS at runtime (built only), any physical device, and the App Store sandbox. A sandbox purchase on a device needs an app whose bundle ID has products in App Store Connect. The only such app on the account is the user's production app, which was not installed anywhere. The device check would be a sandbox tester buying a consumable and a subscription on an iPhone, verifying the JWS with the App Store Server API, finishing it, and seeing a renewal and an Ask to Buy approval arrive through OnPurchaseUpdate (roadmap row "In-app purchases on devices").

Checklist

  • The title is a Conventional Commit, such as fix(linux): keep the caret after a paste; a squash merge uses it as the commit subject.
  • Docs whose facts change are updated, and CHANGELOG.md's Unreleased has a line for a capability users can see.
  • New third-party code, assets and dependencies name their source and license. (None added.)

Products looks up the store's products with their localized prices, Purchase buys one with an account token and answers the signed transaction, or cancelled and pending apart from failed, OnPurchaseUpdate hears transactions made outside the app and those unfinished at launch, kept for the first handler, and FinishTransaction, CurrentEntitlements and SyncPurchases finish, list and restore them. macOS, iOS and tvOS reach StoreKit 2 through a small Swift library the tool compiles into apps that set apple.purchases, which the Apple backend loads through purego and talks to in JSON; other targets report Env.CanPurchase false, with roadmap rows for Android's Play Billing, watchOS and the rest. It is held to AccessPurchases, TSX gets the Purchases module behind the purchases capability, nsgotest models a store, and the gallery's System page gains a card.

Fixes #353
@github-actions github-actions Bot added size/XL documentation Improvements or additions to documentation macOS iOS core tsx cli enhancement New feature or request labels Nov 16, 2026
@houko
houko merged commit 7f2273b into main Nov 16, 2026
15 checks passed
@houko
houko deleted the feat/in-app-purchases branch November 16, 2026 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli core documentation Improvements or additions to documentation enhancement New feature or request iOS macOS size/XL tsx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] In-app purchases and subscriptions: StoreKit 2 and Google Play Billing

1 participant