Repository navigation
feat(core): in-app purchases through StoreKit 2 - #406
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ownDisplayPrice(never a hardcoded price), type, and a subscription's group, period, introductory and promotional offers.Purchase(product, PurchaseOptions{AccountToken, Quantity, Offer})answers theTransaction, whoseSignedis the StoreKit JWS for server verification, or aPurchaseError:PurchaseCancelledandPurchasePending(Ask to Buy) apart fromPurchaseFailed, which wraps the store's words.OnPurchaseUpdatehearsTransaction.updates(renewals, approvals, other devices, refunds, unfinished at launch), kept for the first handler asOnNotificationPresskeeps its press.FinishTransaction,CurrentEntitlementsandSyncPurchases(AppStore.sync()) complete it.Env.CanPurchase,AccessPurchases, and the gate test list the six methods.cmd/nsgo/storekit/StoreKit.swift(about 250 lines) exports two@_cdeclfunctions,nsgo_storekit_start(callback)andnsgo_storekit_call(id, json), and only moves JSON;internal/platform/apple/purchases.goloads it from the bundle's frameworks through purego. The tool compiles it withxcrun swiftc, cached like the widgets' library, only whennsgo.yamlsetsapple.purchases:NSGoStoreKit.frameworkon iOS/tvOS andlibNSGoStoreKit.dylibon macOS (built for macOS 12; on macOS 11 it does not load andCanPurchaseis false). App Clips never get it; watchOS leaves it out with a note.com.apple.developer.in-app-paymentsis Apple Pay's merchant entitlement. In-App Purchase is an App ID capability that is on by default and needs nothing in the signature.Purchasesmodule (available,products,purchaseresolving{status: "purchased" | "pending" | "cancelled"},finish,currentEntitlements,sync,on("update")) behind thepurchasescapability, with typings and the sandbox surface.nsgotest:Products,PurchaseRequests,PurchaseAnswer,Unfinished,Entitlements,Finished,SyncAnswer,UpdatePurchase, and theUnfinishedTransactionsoption for updates at launch.apple.purchases: true.Targets
core,base,ui,tsx, thensgotool)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 checkandgo tool nsgo check -target allpassed.go tool nsgo testpassed for./core,./nsgotest,./tsx,./tsx/pkg,./internal/driver,./internal/platform/apple,./internal/moduletest,./examples/galleryand./cmd/nsgo.TestStoreKitCompilecompiles 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'sSKTestSessionfrom Go, with dialogs disabled and XCTest found through-env DYLD_FRAMEWORK_PATH=<iPhoneSimulator platform>/Developer/Library/Frameworks. Through this PR's library and backend:XcodeFinishTransactionsucceeded, then answered not foundCurrentEntitlementslisted bothapproveAskToBuyTransactiondelivered it throughOnPurchaseUpdateOn 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,
SyncPurchasesand launch-time unfinished delivery against StoreKit. These are covered only innsgotest; with dialogs off there is no sheet to cancel, andsimulatedError(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
fix(linux): keep the caret after a paste; a squash merge uses it as the commit subject.CHANGELOG.md'sUnreleasedhas a line for a capability users can see.