@tenthbilling/expo 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +41 -2
- package/android/build.gradle +23 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/tenthbilling/expo/TenthStoreModule.kt +669 -0
- package/app.plugin.js +3 -0
- package/dist/api/client.d.ts +107 -0
- package/dist/api/client.js +139 -0
- package/dist/api/types.d.ts +17 -0
- package/dist/api/types.js +1 -0
- package/dist/cache.d.ts +23 -0
- package/dist/cache.js +40 -0
- package/dist/errors.d.ts +18 -0
- package/dist/errors.js +42 -0
- package/dist/generated/openapi.d.ts +3208 -0
- package/dist/generated/openapi.js +1 -0
- package/dist/identity.d.ts +35 -0
- package/dist/identity.js +92 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +10 -0
- package/dist/keys.d.ts +8 -0
- package/dist/keys.js +14 -0
- package/dist/native/module.d.ts +12 -0
- package/dist/native/module.js +14 -0
- package/dist/native/spec.d.ts +166 -0
- package/dist/native/spec.js +1 -0
- package/dist/products.d.ts +32 -0
- package/dist/products.js +213 -0
- package/dist/react/Paywall.d.ts +28 -0
- package/dist/react/Paywall.js +19 -0
- package/dist/react/TenthProvider.d.ts +11 -0
- package/dist/react/TenthProvider.js +23 -0
- package/dist/react/context.d.ts +4 -0
- package/dist/react/context.js +8 -0
- package/dist/react/hooks.d.ts +48 -0
- package/dist/react/hooks.js +68 -0
- package/dist/react/present.d.ts +15 -0
- package/dist/react/present.js +47 -0
- package/dist/react/usePaywall.d.ts +12 -0
- package/dist/react/usePaywall.js +109 -0
- package/dist/storage/storage.d.ts +14 -0
- package/dist/storage/storage.js +63 -0
- package/dist/storage/storage.web.d.ts +4 -0
- package/dist/storage/storage.web.js +35 -0
- package/dist/store/apple.d.ts +19 -0
- package/dist/store/apple.js +127 -0
- package/dist/store/context.d.ts +46 -0
- package/dist/store/context.js +21 -0
- package/dist/store/google.d.ts +22 -0
- package/dist/store/google.js +145 -0
- package/dist/store/preview.d.ts +16 -0
- package/dist/store/preview.js +68 -0
- package/dist/stripe/appearance.d.ts +47 -0
- package/dist/stripe/appearance.js +44 -0
- package/dist/stripe/flows.d.ts +23 -0
- package/dist/stripe/flows.js +145 -0
- package/dist/stripe/payment-sheet.d.ts +1 -0
- package/dist/stripe/payment-sheet.js +24 -0
- package/dist/stripe/sheet-registry.d.ts +16 -0
- package/dist/stripe/sheet-registry.js +7 -0
- package/dist/stripe/web-step.d.ts +10 -0
- package/dist/stripe/web-step.js +4 -0
- package/dist/stripe/web-step.web.d.ts +4 -0
- package/dist/stripe/web-step.web.js +55 -0
- package/dist/tenth.d.ts +125 -0
- package/dist/tenth.js +329 -0
- package/dist/updates.d.ts +15 -0
- package/dist/updates.js +30 -0
- package/expo-module.config.json +9 -0
- package/ios/CountryCodes.swift +52 -0
- package/ios/TenthBilling.podspec +31 -0
- package/ios/TenthStoreExceptions.swift +88 -0
- package/ios/TenthStoreMapping.swift +108 -0
- package/ios/TenthStoreModule.swift +213 -0
- package/ios/TenthStoreRecords.swift +105 -0
- package/package.json +94 -3
- package/plugin/withTenth.js +249 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Errors the module rejects with. Each has a stable code the JS side switches on and a short
|
|
2
|
+
// reason. None carries a payload: no product data, transaction, JWS, token or underlying error
|
|
3
|
+
// text, so nothing sensitive can reach JS error reporting or logs.
|
|
4
|
+
|
|
5
|
+
import ExpoModulesCore
|
|
6
|
+
import StoreKit
|
|
7
|
+
|
|
8
|
+
enum TenthStoreErrors {
|
|
9
|
+
/// Input from JS failed validation.
|
|
10
|
+
static func invalidArgument(_ field: String) -> Exception {
|
|
11
|
+
return Exception(name: "TenthInvalidArgument", description: "Invalid \(field)", code: "invalid_argument")
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/// The App Store doesn't offer this product (unknown ID, or not sold in this storefront).
|
|
15
|
+
static func productUnavailable() -> Exception {
|
|
16
|
+
return Exception(name: "TenthProductUnavailable", description: "The product is not available", code: "product_unavailable")
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/// Purchases are disabled on this device or account (parental controls, MDM).
|
|
20
|
+
static func purchasesUnavailable() -> Exception {
|
|
21
|
+
return Exception(name: "TenthPurchasesUnavailable", description: "Purchases are not allowed", code: "purchases_unavailable")
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/// The promotional offer was refused by StoreKit (ineligible, bad signature, missing fields).
|
|
25
|
+
static func offerInvalid() -> Exception {
|
|
26
|
+
return Exception(name: "TenthOfferInvalid", description: "The promotional offer was refused", code: "offer_invalid")
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// The customer dismissed a sheet that has no "cancelled" result of its own (AppStore.sync).
|
|
30
|
+
static func userCancelled() -> Exception {
|
|
31
|
+
return Exception(name: "TenthUserCancelled", description: "The customer cancelled", code: "user_cancelled")
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/// The App Store couldn't be reached.
|
|
35
|
+
static func storeUnavailable() -> Exception {
|
|
36
|
+
return Exception(name: "TenthStoreUnavailable", description: "The App Store is not reachable", code: "store_unavailable")
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/// No foreground window scene to present StoreKit UI in.
|
|
40
|
+
static func uiUnavailable() -> Exception {
|
|
41
|
+
return Exception(name: "TenthUiUnavailable", description: "No active window to present in", code: "ui_unavailable")
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// Anything else StoreKit reported. `kind` is a fixed identifier from this file, never error text.
|
|
45
|
+
static func storeError(_ kind: String) -> Exception {
|
|
46
|
+
return Exception(name: "TenthStoreError", description: "StoreKit error (\(kind))", code: "store_error")
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/// Maps any error StoreKit throws to one of the coded errors above.
|
|
50
|
+
static func from(_ error: Error) -> Exception {
|
|
51
|
+
if let exception = error as? Exception {
|
|
52
|
+
return exception
|
|
53
|
+
}
|
|
54
|
+
if let storeKitError = error as? StoreKitError {
|
|
55
|
+
switch storeKitError {
|
|
56
|
+
case .userCancelled:
|
|
57
|
+
return userCancelled()
|
|
58
|
+
case .networkError:
|
|
59
|
+
return storeUnavailable()
|
|
60
|
+
case .notAvailableInStorefront:
|
|
61
|
+
return productUnavailable()
|
|
62
|
+
case .notEntitled:
|
|
63
|
+
return storeError("not_entitled")
|
|
64
|
+
case .systemError:
|
|
65
|
+
return storeError("system_error")
|
|
66
|
+
case .unknown:
|
|
67
|
+
return storeError("unknown")
|
|
68
|
+
default:
|
|
69
|
+
return storeError("other")
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if let purchaseError = error as? Product.PurchaseError {
|
|
73
|
+
switch purchaseError {
|
|
74
|
+
case .productUnavailable:
|
|
75
|
+
return productUnavailable()
|
|
76
|
+
case .purchaseNotAllowed:
|
|
77
|
+
return purchasesUnavailable()
|
|
78
|
+
case .ineligibleForOffer, .invalidOfferIdentifier, .invalidOfferPrice, .invalidOfferSignature, .missingOfferParameters:
|
|
79
|
+
return offerInvalid()
|
|
80
|
+
case .invalidQuantity:
|
|
81
|
+
return storeError("invalid_quantity")
|
|
82
|
+
default:
|
|
83
|
+
return storeError("purchase_error")
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return storeError("unexpected")
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// Maps StoreKit 2 values to the plain dictionaries spec.ts describes (IosProduct, IosTransaction).
|
|
2
|
+
// Nulls are NSNull so expo-modules-core turns them into JS null, not undefined.
|
|
3
|
+
|
|
4
|
+
import ExpoModulesCore
|
|
5
|
+
import Foundation
|
|
6
|
+
import StoreKit
|
|
7
|
+
|
|
8
|
+
enum TenthStoreMapping {
|
|
9
|
+
private static let posix = Locale(identifier: "en_US_POSIX")
|
|
10
|
+
|
|
11
|
+
/// Decimal → "59.99": NSDecimalNumber's description in the POSIX locale, so the separator is
|
|
12
|
+
/// always "." and there's no grouping, whatever the device's region.
|
|
13
|
+
static func decimalString(_ value: Decimal) -> String {
|
|
14
|
+
return NSDecimalNumber(decimal: value).description(withLocale: posix)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static func period(_ period: Product.SubscriptionPeriod) -> [String: Any]? {
|
|
18
|
+
let unit: String
|
|
19
|
+
switch period.unit {
|
|
20
|
+
case .day: unit = "day"
|
|
21
|
+
case .week: unit = "week"
|
|
22
|
+
case .month: unit = "month"
|
|
23
|
+
case .year: unit = "year"
|
|
24
|
+
@unknown default: return nil
|
|
25
|
+
}
|
|
26
|
+
return ["unit": unit, "value": period.value]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static func productType(_ type: Product.ProductType) -> String? {
|
|
30
|
+
switch type {
|
|
31
|
+
case .autoRenewable: return "autoRenewable"
|
|
32
|
+
case .nonRenewable: return "nonRenewable"
|
|
33
|
+
case .consumable: return "consumable"
|
|
34
|
+
case .nonConsumable: return "nonConsumable"
|
|
35
|
+
default: return nil
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static func paymentMode(_ mode: Product.SubscriptionOffer.PaymentMode) -> String? {
|
|
40
|
+
switch mode {
|
|
41
|
+
case .freeTrial: return "freeTrial"
|
|
42
|
+
case .payAsYouGo: return "payAsYouGo"
|
|
43
|
+
case .payUpFront: return "payUpFront"
|
|
44
|
+
default: return nil
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/// IosProduct. Returns nil for a product type spec.ts doesn't know, which is then left out.
|
|
49
|
+
static func product(_ product: Product) async -> [String: Any]? {
|
|
50
|
+
guard let type = productType(product.type) else { return nil }
|
|
51
|
+
|
|
52
|
+
var subscriptionPeriod: Any = NSNull()
|
|
53
|
+
var subscriptionGroupId: Any = NSNull()
|
|
54
|
+
var introductoryOffer: Any = NSNull()
|
|
55
|
+
|
|
56
|
+
if let subscription = product.subscription {
|
|
57
|
+
subscriptionPeriod = period(subscription.subscriptionPeriod) ?? NSNull()
|
|
58
|
+
subscriptionGroupId = subscription.subscriptionGroupID
|
|
59
|
+
// Only offer the intro price to a customer StoreKit says can still get it.
|
|
60
|
+
if let offer = subscription.introductoryOffer,
|
|
61
|
+
await subscription.isEligibleForIntroOffer,
|
|
62
|
+
let mode = paymentMode(offer.paymentMode),
|
|
63
|
+
let offerPeriod = period(offer.period)
|
|
64
|
+
{
|
|
65
|
+
introductoryOffer = [
|
|
66
|
+
"paymentMode": mode,
|
|
67
|
+
"period": offerPeriod,
|
|
68
|
+
"periodCount": offer.periodCount,
|
|
69
|
+
"displayPrice": offer.displayPrice,
|
|
70
|
+
"price": decimalString(offer.price),
|
|
71
|
+
] as [String: Any]
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return [
|
|
76
|
+
"id": product.id,
|
|
77
|
+
"type": type,
|
|
78
|
+
"displayName": product.displayName,
|
|
79
|
+
"displayPrice": product.displayPrice,
|
|
80
|
+
"price": decimalString(product.price),
|
|
81
|
+
"currencyCode": product.priceFormatStyle.currencyCode,
|
|
82
|
+
"subscriptionPeriod": subscriptionPeriod,
|
|
83
|
+
"subscriptionGroupId": subscriptionGroupId,
|
|
84
|
+
"introductoryOffer": introductoryOffer,
|
|
85
|
+
]
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/// IosTransaction. The JWS is passed on whether or not StoreKit could verify it: Tenth's server
|
|
89
|
+
/// verifies every JWS against Apple's root and decides. Nothing here grants access.
|
|
90
|
+
static func transaction(_ verification: VerificationResult<Transaction>) -> [String: Any] {
|
|
91
|
+
let transaction = verification.unsafePayloadValue
|
|
92
|
+
return [
|
|
93
|
+
"jws": verification.jwsRepresentation,
|
|
94
|
+
"transactionId": String(transaction.id),
|
|
95
|
+
"productId": transaction.productID,
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/// The same, typed for Module.sendEvent.
|
|
100
|
+
static func transactionEvent(_ verification: VerificationResult<Transaction>) -> [String: Any?] {
|
|
101
|
+
let transaction = verification.unsafePayloadValue
|
|
102
|
+
return [
|
|
103
|
+
"jws": verification.jwsRepresentation,
|
|
104
|
+
"transactionId": String(transaction.id),
|
|
105
|
+
"productId": transaction.productID,
|
|
106
|
+
]
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// Tenth's StoreKit 2 bridge. The JS side's contract is IosStoreModule and IosStoreEvents.
|
|
2
|
+
//
|
|
3
|
+
// The module only talks to the App Store. It never verifies a transaction, never grants access,
|
|
4
|
+
// and never finishes a transaction on its own: every signed transaction goes to Tenth's server
|
|
5
|
+
// as is (its JWS), and JS calls finishTransaction only after Tenth has recorded it. Unverified
|
|
6
|
+
// transactions are passed on too, because the server is the one that verifies.
|
|
7
|
+
//
|
|
8
|
+
// Nothing here logs. In particular, no JWS, transaction, appAccountToken or offer signature is
|
|
9
|
+
// ever printed, and errors carry only stable codes (TenthStoreExceptions.swift).
|
|
10
|
+
|
|
11
|
+
import ExpoModulesCore
|
|
12
|
+
import StoreKit
|
|
13
|
+
import UIKit
|
|
14
|
+
|
|
15
|
+
public final class TenthStoreModule: Module {
|
|
16
|
+
private let observerLock = NSLock()
|
|
17
|
+
private var updatesTask: Task<Void, Never>?
|
|
18
|
+
|
|
19
|
+
public func definition() -> ModuleDefinition {
|
|
20
|
+
Name("TenthStore")
|
|
21
|
+
|
|
22
|
+
Events("onTransactionUpdate")
|
|
23
|
+
|
|
24
|
+
OnDestroy {
|
|
25
|
+
self.stopObserving()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
Function("canMakePayments") { () -> Bool in
|
|
29
|
+
return AppStore.canMakePayments
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
AsyncFunction("storefrontCountry") { () async -> String? in
|
|
33
|
+
guard let storefront = await Storefront.current else {
|
|
34
|
+
return nil
|
|
35
|
+
}
|
|
36
|
+
return CountryCodes.alpha2(fromAlpha3: storefront.countryCode)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
AsyncFunction("getProducts") { (productIds: [String]) async throws -> [[String: Any]] in
|
|
40
|
+
try TenthValidate.productIds(productIds)
|
|
41
|
+
if productIds.isEmpty {
|
|
42
|
+
return []
|
|
43
|
+
}
|
|
44
|
+
let products: [Product]
|
|
45
|
+
do {
|
|
46
|
+
products = try await Product.products(for: Set(productIds))
|
|
47
|
+
} catch {
|
|
48
|
+
throw TenthStoreErrors.from(error)
|
|
49
|
+
}
|
|
50
|
+
var result: [[String: Any]] = []
|
|
51
|
+
for product in products {
|
|
52
|
+
if let mapped = await TenthStoreMapping.product(product) {
|
|
53
|
+
result.append(mapped)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return result
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
AsyncFunction("purchase") { (productId: String, appAccountToken: String, promotionalOffer: PromotionalOfferInput?) async throws -> [String: Any] in
|
|
60
|
+
try TenthValidate.productId(productId)
|
|
61
|
+
let accountToken = try TenthValidate.uuid(appAccountToken, field: "appAccountToken")
|
|
62
|
+
let offer = try promotionalOffer.map { try TenthValidate.offer($0) }
|
|
63
|
+
|
|
64
|
+
let product: Product
|
|
65
|
+
do {
|
|
66
|
+
guard let found = try await Product.products(for: [productId]).first else {
|
|
67
|
+
throw TenthStoreErrors.productUnavailable()
|
|
68
|
+
}
|
|
69
|
+
product = found
|
|
70
|
+
} catch {
|
|
71
|
+
throw TenthStoreErrors.from(error)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Every purchase carries the customer's appAccountToken, which is how Tenth tells whose it
|
|
75
|
+
// is.
|
|
76
|
+
var options: Set<Product.PurchaseOption> = [.appAccountToken(accountToken)]
|
|
77
|
+
if let offer {
|
|
78
|
+
options.insert(
|
|
79
|
+
.promotionalOffer(
|
|
80
|
+
offerID: offer.offerId,
|
|
81
|
+
keyID: offer.keyId,
|
|
82
|
+
nonce: offer.nonce,
|
|
83
|
+
signature: offer.signature,
|
|
84
|
+
timestamp: offer.timestamp
|
|
85
|
+
)
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let result: Product.PurchaseResult
|
|
90
|
+
do {
|
|
91
|
+
result = try await TenthStoreModule.runPurchase(product, options: options)
|
|
92
|
+
} catch {
|
|
93
|
+
throw TenthStoreErrors.from(error)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
switch result {
|
|
97
|
+
case .success(let verification):
|
|
98
|
+
// Not finished here: JS finishes it once Tenth has accepted it.
|
|
99
|
+
return ["status": "success", "transaction": TenthStoreMapping.transaction(verification)]
|
|
100
|
+
case .userCancelled:
|
|
101
|
+
return ["status": "userCancelled"]
|
|
102
|
+
case .pending:
|
|
103
|
+
return ["status": "pending"]
|
|
104
|
+
@unknown default:
|
|
105
|
+
throw TenthStoreErrors.storeError("unknown_purchase_result")
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
AsyncFunction("finishTransaction") { (transactionId: String) async throws in
|
|
110
|
+
let id = try TenthValidate.transactionId(transactionId)
|
|
111
|
+
// Only unfinished transactions need finishing; one that isn't there is already finished
|
|
112
|
+
// (or never existed), which is a no-op.
|
|
113
|
+
for await verification in Transaction.unfinished {
|
|
114
|
+
let transaction = verification.unsafePayloadValue
|
|
115
|
+
if transaction.id == id {
|
|
116
|
+
await transaction.finish()
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
AsyncFunction("currentEntitlements") { () async -> [[String: Any]] in
|
|
123
|
+
var result: [[String: Any]] = []
|
|
124
|
+
for await verification in Transaction.currentEntitlements {
|
|
125
|
+
result.append(TenthStoreMapping.transaction(verification))
|
|
126
|
+
}
|
|
127
|
+
return result
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
AsyncFunction("unfinishedTransactions") { () async -> [[String: Any]] in
|
|
131
|
+
var result: [[String: Any]] = []
|
|
132
|
+
for await verification in Transaction.unfinished {
|
|
133
|
+
result.append(TenthStoreMapping.transaction(verification))
|
|
134
|
+
}
|
|
135
|
+
return result
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
AsyncFunction("sync") { () async throws in
|
|
139
|
+
do {
|
|
140
|
+
try await AppStore.sync()
|
|
141
|
+
} catch {
|
|
142
|
+
throw TenthStoreErrors.from(error)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
AsyncFunction("showManageSubscriptions") { () async throws in
|
|
147
|
+
do {
|
|
148
|
+
try await TenthStoreModule.presentManageSubscriptions()
|
|
149
|
+
} catch {
|
|
150
|
+
throw TenthStoreErrors.from(error)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
Function("startObserving") {
|
|
155
|
+
self.startObserving()
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// MARK: - Transaction.updates
|
|
160
|
+
|
|
161
|
+
/// One listener for the module's lifetime. Calling it again while it runs does nothing.
|
|
162
|
+
private func startObserving() {
|
|
163
|
+
observerLock.lock()
|
|
164
|
+
defer { observerLock.unlock() }
|
|
165
|
+
guard updatesTask == nil else {
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
updatesTask = Task.detached(priority: .utility) { [weak self] in
|
|
169
|
+
for await verification in Transaction.updates {
|
|
170
|
+
if Task.isCancelled {
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
guard let self else {
|
|
174
|
+
return
|
|
175
|
+
}
|
|
176
|
+
// Renewals, Ask to Buy approvals, refunds and purchases made elsewhere. JS sends each to
|
|
177
|
+
// Tenth and finishes it once accepted.
|
|
178
|
+
self.sendEvent("onTransactionUpdate", TenthStoreMapping.transactionEvent(verification))
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private func stopObserving() {
|
|
184
|
+
observerLock.lock()
|
|
185
|
+
let task = updatesTask
|
|
186
|
+
updatesTask = nil
|
|
187
|
+
observerLock.unlock()
|
|
188
|
+
task?.cancel()
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// MARK: - UI on the main actor
|
|
192
|
+
|
|
193
|
+
/// Product.purchase, confirmed in the foreground scene where the API allows (iOS 17+), so the
|
|
194
|
+
/// payment sheet shows in the window the customer is looking at.
|
|
195
|
+
@MainActor
|
|
196
|
+
private static func runPurchase(
|
|
197
|
+
_ product: Product,
|
|
198
|
+
options: Set<Product.PurchaseOption>
|
|
199
|
+
) async throws -> Product.PurchaseResult {
|
|
200
|
+
if #available(iOS 17.0, *), let scene = Utilities.keyWindow()?.windowScene {
|
|
201
|
+
return try await product.purchase(confirmIn: scene, options: options)
|
|
202
|
+
}
|
|
203
|
+
return try await product.purchase(options: options)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
@MainActor
|
|
207
|
+
private static func presentManageSubscriptions() async throws {
|
|
208
|
+
guard let scene = Utilities.keyWindow()?.windowScene else {
|
|
209
|
+
throw TenthStoreErrors.uiUnavailable()
|
|
210
|
+
}
|
|
211
|
+
try await AppStore.showManageSubscriptions(in: scene)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Argument records and input validation for TenthStoreModule. Everything from JS is untrusted:
|
|
2
|
+
// it is checked for type, length and character set before it reaches StoreKit, and a bad value
|
|
3
|
+
// is refused (never rewritten).
|
|
4
|
+
|
|
5
|
+
import ExpoModulesCore
|
|
6
|
+
import Foundation
|
|
7
|
+
|
|
8
|
+
/// IosPromotionalOffer in spec.ts: a promotional offer signed by Tenth's server.
|
|
9
|
+
struct PromotionalOfferInput: Record {
|
|
10
|
+
@Field(.required) var offerId: String = ""
|
|
11
|
+
@Field(.required) var keyId: String = ""
|
|
12
|
+
@Field(.required) var nonce: String = ""
|
|
13
|
+
@Field(.required) var signature: String = ""
|
|
14
|
+
@Field(.required) var timestamp: Double = 0
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
enum TenthValidate {
|
|
18
|
+
/// App Store product IDs: letters, digits, underscores and periods (we also allow hyphens).
|
|
19
|
+
private static let productIdCharacters = CharacterSet(
|
|
20
|
+
charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-"
|
|
21
|
+
)
|
|
22
|
+
private static let keyIdCharacters = CharacterSet(
|
|
23
|
+
charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
24
|
+
)
|
|
25
|
+
private static let base64Characters = CharacterSet(
|
|
26
|
+
charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/="
|
|
27
|
+
)
|
|
28
|
+
private static let digits = CharacterSet(charactersIn: "0123456789")
|
|
29
|
+
|
|
30
|
+
static let maxProducts = 100
|
|
31
|
+
|
|
32
|
+
static func isToken(_ value: String, maxLength: Int, allowed: CharacterSet) -> Bool {
|
|
33
|
+
guard !value.isEmpty, value.utf8.count <= maxLength else { return false }
|
|
34
|
+
return value.unicodeScalars.allSatisfy { allowed.contains($0) }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static func productId(_ value: String) throws {
|
|
38
|
+
guard isToken(value, maxLength: 255, allowed: productIdCharacters) else {
|
|
39
|
+
throw TenthStoreErrors.invalidArgument("productId")
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static func productIds(_ values: [String]) throws {
|
|
44
|
+
guard values.count <= maxProducts else {
|
|
45
|
+
throw TenthStoreErrors.invalidArgument("productIds")
|
|
46
|
+
}
|
|
47
|
+
for value in values {
|
|
48
|
+
try productId(value)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// A UUID in its canonical 36-character form. UUID(uuidString:) accepts either case.
|
|
53
|
+
static func uuid(_ value: String, field: String) throws -> UUID {
|
|
54
|
+
guard value.utf8.count == 36, let uuid = UUID(uuidString: value) else {
|
|
55
|
+
throw TenthStoreErrors.invalidArgument(field)
|
|
56
|
+
}
|
|
57
|
+
return uuid
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// StoreKit transaction IDs are UInt64 values, sent to JS as decimal strings.
|
|
61
|
+
static func transactionId(_ value: String) throws -> UInt64 {
|
|
62
|
+
guard isToken(value, maxLength: 20, allowed: digits), let id = UInt64(value) else {
|
|
63
|
+
throw TenthStoreErrors.invalidArgument("transactionId")
|
|
64
|
+
}
|
|
65
|
+
return id
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
struct ValidOffer {
|
|
69
|
+
let offerId: String
|
|
70
|
+
let keyId: String
|
|
71
|
+
let nonce: UUID
|
|
72
|
+
let signature: Data
|
|
73
|
+
let timestamp: Int
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static func offer(_ input: PromotionalOfferInput) throws -> ValidOffer {
|
|
77
|
+
guard isToken(input.offerId, maxLength: 255, allowed: productIdCharacters) else {
|
|
78
|
+
throw TenthStoreErrors.invalidArgument("promotionalOffer.offerId")
|
|
79
|
+
}
|
|
80
|
+
guard isToken(input.keyId, maxLength: 64, allowed: keyIdCharacters) else {
|
|
81
|
+
throw TenthStoreErrors.invalidArgument("promotionalOffer.keyId")
|
|
82
|
+
}
|
|
83
|
+
let nonce = try uuid(input.nonce, field: "promotionalOffer.nonce")
|
|
84
|
+
guard isToken(input.signature, maxLength: 1024, allowed: base64Characters),
|
|
85
|
+
let signature = Data(base64Encoded: input.signature),
|
|
86
|
+
!signature.isEmpty
|
|
87
|
+
else {
|
|
88
|
+
throw TenthStoreErrors.invalidArgument("promotionalOffer.signature")
|
|
89
|
+
}
|
|
90
|
+
// Milliseconds since the epoch: a positive whole number that fits exactly in a Double.
|
|
91
|
+
let timestamp = input.timestamp
|
|
92
|
+
guard timestamp.isFinite, timestamp > 0, timestamp <= 9_007_199_254_740_991,
|
|
93
|
+
timestamp.rounded(.towardZero) == timestamp
|
|
94
|
+
else {
|
|
95
|
+
throw TenthStoreErrors.invalidArgument("promotionalOffer.timestamp")
|
|
96
|
+
}
|
|
97
|
+
return ValidOffer(
|
|
98
|
+
offerId: input.offerId,
|
|
99
|
+
keyId: input.keyId,
|
|
100
|
+
nonce: nonce,
|
|
101
|
+
signature: signature,
|
|
102
|
+
timestamp: Int(timestamp)
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,97 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tenthbilling/expo",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Tenth for Expo: StoreKit 2 and Play Billing module, config plugin, hooks and the drop-in paywall",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Tenth",
|
|
8
|
+
"homepage": "https://tenthbilling.com/docs/expo",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"tenth",
|
|
11
|
+
"expo",
|
|
12
|
+
"react-native",
|
|
13
|
+
"in-app-purchases",
|
|
14
|
+
"subscriptions",
|
|
15
|
+
"storekit",
|
|
16
|
+
"google-play-billing",
|
|
17
|
+
"stripe",
|
|
18
|
+
"paywall",
|
|
19
|
+
"entitlements"
|
|
20
|
+
],
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./payment-sheet": {
|
|
28
|
+
"types": "./dist/stripe/payment-sheet.d.ts",
|
|
29
|
+
"default": "./dist/stripe/payment-sheet.js"
|
|
30
|
+
},
|
|
31
|
+
"./app.plugin.js": "./app.plugin.js",
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@stripe/react-stripe-js": "^7.0.0",
|
|
39
|
+
"@stripe/stripe-js": "^10.0.0",
|
|
40
|
+
"@tenthbilling/design": "0.1.1",
|
|
41
|
+
"@tenthbilling/paywall": "0.1.1"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@stripe/stripe-react-native": ">=0.81.0",
|
|
45
|
+
"expo": ">=58.0.0",
|
|
46
|
+
"expo-constants": ">=58.0.0",
|
|
47
|
+
"expo-crypto": ">=58.0.0",
|
|
48
|
+
"expo-modules-core": ">=58.0.0",
|
|
49
|
+
"expo-secure-store": ">=58.0.0",
|
|
50
|
+
"expo-web-browser": ">=58.0.0",
|
|
51
|
+
"lucide-react-native": "^1.55.0",
|
|
52
|
+
"react": "^19.3.0",
|
|
53
|
+
"react-native": ">=0.88.0",
|
|
54
|
+
"react-native-svg": "^15.16.0"
|
|
55
|
+
},
|
|
56
|
+
"peerDependenciesMeta": {
|
|
57
|
+
"@stripe/stripe-react-native": {
|
|
58
|
+
"optional": true
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@expo/config-plugins": "58.0.7",
|
|
63
|
+
"@stripe/stripe-react-native": "0.81.0",
|
|
64
|
+
"@types/react": "~19.3.0",
|
|
65
|
+
"expo": "~58.0.19",
|
|
66
|
+
"expo-constants": "~58.0.13",
|
|
67
|
+
"expo-crypto": "~58.0.7",
|
|
68
|
+
"expo-modules-core": "~58.0.26",
|
|
69
|
+
"expo-secure-store": "~58.0.2",
|
|
70
|
+
"expo-web-browser": "~58.0.6",
|
|
71
|
+
"lucide-react-native": "1.55.0",
|
|
72
|
+
"react": "19.3.0",
|
|
73
|
+
"react-dom": "19.3.0",
|
|
74
|
+
"react-native": "0.88.0",
|
|
75
|
+
"react-native-svg": "15.16.0",
|
|
76
|
+
"react-native-web": "^0.21.4"
|
|
77
|
+
},
|
|
78
|
+
"files": [
|
|
79
|
+
"dist",
|
|
80
|
+
"plugin/withTenth.js",
|
|
81
|
+
"ios",
|
|
82
|
+
"!ios/build",
|
|
83
|
+
"android",
|
|
84
|
+
"!android/build",
|
|
85
|
+
"!android/.gradle",
|
|
86
|
+
"app.plugin.js",
|
|
87
|
+
"expo-module.config.json",
|
|
88
|
+
"README.md"
|
|
89
|
+
],
|
|
90
|
+
"scripts": {
|
|
91
|
+
"build": "node ../../scripts/packages/clean-dist.mjs && tsc -p tsconfig.build.json && tsc -p tsconfig.build.json --declaration --emitDeclarationOnly --removeComments false",
|
|
92
|
+
"lint": "eslint src plugin app.plugin.js",
|
|
93
|
+
"typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.test.json",
|
|
94
|
+
"test": "vitest run"
|
|
95
|
+
},
|
|
96
|
+
"types": "./dist/index.d.ts"
|
|
6
97
|
}
|