This repository was archived by the owner on Oct 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStoreKitTypesBridge.swift
More file actions
530 lines (483 loc) · 22.3 KB
/
StoreKitTypesBridge.swift
File metadata and controls
530 lines (483 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import Foundation
import StoreKit
@available(iOS 15.0, macOS 14.0, *)
enum StoreKitTypesBridge {
static func product(from product: StoreKit.Product) async -> Product {
.productIos(await productIOS(from: product))
}
static func productSubscription(from product: StoreKit.Product) async -> ProductSubscription? {
guard let subscription = await productSubscriptionIOS(from: product) else { return nil }
return .productSubscriptionIos(subscription)
}
static func productIOS(from product: StoreKit.Product) async -> ProductIOS {
ProductIOS(
currency: product.priceFormatStyle.currencyCode,
debugDescription: product.description,
description: product.description,
displayName: product.displayName,
displayNameIOS: product.displayName,
displayPrice: product.displayPrice,
id: product.id,
isFamilyShareableIOS: product.isFamilyShareable,
jsonRepresentationIOS: String(data: product.jsonRepresentation, encoding: .utf8) ?? "",
platform: .ios,
price: NSDecimalNumber(decimal: product.price).doubleValue,
subscriptionInfoIOS: makeSubscriptionInfo(from: product.subscription),
title: product.displayName,
type: productType(from: product.type),
typeIOS: productTypeIOS(from: product.type)
)
}
static func productSubscriptionIOS(from product: StoreKit.Product) async -> ProductSubscriptionIOS? {
guard let subscription = product.subscription else { return nil }
return ProductSubscriptionIOS(
currency: product.priceFormatStyle.currencyCode,
debugDescription: product.description,
description: product.description,
discountsIOS: makeDiscounts(from: subscription),
displayName: product.displayName,
displayNameIOS: product.displayName,
displayPrice: product.displayPrice,
id: product.id,
introductoryPriceAsAmountIOS: introductoryPriceAmount(from: subscription.introductoryOffer),
introductoryPriceIOS: subscription.introductoryOffer?.displayPrice,
introductoryPriceNumberOfPeriodsIOS: introductoryPeriods(from: subscription.introductoryOffer),
introductoryPricePaymentModeIOS: subscription.introductoryOffer?.paymentMode.paymentModeIOS,
introductoryPriceSubscriptionPeriodIOS: subscription.introductoryOffer?.period.unit.subscriptionPeriodIOS,
isFamilyShareableIOS: product.isFamilyShareable,
jsonRepresentationIOS: String(data: product.jsonRepresentation, encoding: .utf8) ?? "",
platform: .ios,
price: NSDecimalNumber(decimal: product.price).doubleValue,
subscriptionInfoIOS: makeSubscriptionInfo(from: product.subscription),
subscriptionPeriodNumberIOS: String(subscription.subscriptionPeriod.value),
subscriptionPeriodUnitIOS: subscription.subscriptionPeriod.unit.subscriptionPeriodIOS,
title: product.displayName,
type: .subs,
typeIOS: productTypeIOS(from: product.type)
)
}
static func purchase(from transaction: StoreKit.Transaction, jwsRepresentation: String?) async -> Purchase {
.purchaseIos(await purchaseIOS(from: transaction, jwsRepresentation: jwsRepresentation))
}
static func purchaseIOS(from transaction: StoreKit.Transaction, jwsRepresentation: String?) async -> PurchaseIOS {
let transactionId = String(transaction.id)
let purchaseState: PurchaseState = .purchased
let expirationDate = transaction.expirationDate?.milliseconds
let revocationDate = transaction.revocationDate?.milliseconds
let renewalInfoIOS = await subscriptionRenewalInfoIOS(for: transaction)
// Default to false if renewalInfo unavailable - safer to underreport than falsely claim auto-renewal
let autoRenewing = renewalInfoIOS?.willAutoRenew ?? false
let environment: String?
if #available(iOS 16.0, *) {
environment = transaction.environment.rawValue
} else {
environment = nil
}
let offerInfo: PurchaseOfferIOS?
if #available(iOS 17.2, macOS 14.2, *) {
offerInfo = makePurchaseOffer(from: transaction.offer)
} else {
offerInfo = nil
}
let ownershipDescription = ownershipTypeDescription(from: transaction.ownershipType)
let reasonDetails = transactionReasonDetails(from: transaction)
return PurchaseIOS(
appAccountToken: transaction.appAccountToken?.uuidString,
appBundleIdIOS: transaction.appBundleID,
countryCodeIOS: {
if #available(iOS 17.0, *) {
transaction.storefront.countryCode
} else {
transaction.storefrontCountryCode
}
}(),
currencyCodeIOS: nil,
currencySymbolIOS: nil,
environmentIOS: environment,
expirationDateIOS: expirationDate,
id: transactionId,
ids: nil,
isAutoRenewing: autoRenewing,
isUpgradedIOS: transaction.isUpgraded,
offerIOS: offerInfo,
originalTransactionDateIOS: transaction.originalPurchaseDate.milliseconds,
originalTransactionIdentifierIOS: transaction.originalID != 0 ? String(transaction.originalID) : nil,
ownershipTypeIOS: ownershipDescription,
platform: .ios,
productId: transaction.productID,
purchaseState: purchaseState,
purchaseToken: jwsRepresentation ?? transactionId,
quantity: transaction.purchasedQuantity,
quantityIOS: transaction.purchasedQuantity,
reasonIOS: reasonDetails.lowercased,
reasonStringRepresentationIOS: reasonDetails.string,
renewalInfoIOS: renewalInfoIOS,
revocationDateIOS: revocationDate,
revocationReasonIOS: transaction.revocationReason?.rawValue.description,
storefrontCountryCodeIOS: {
if #available(iOS 17.0, *) {
transaction.storefront.countryCode
} else {
transaction.storefrontCountryCode
}
}(),
subscriptionGroupIdIOS: transaction.subscriptionGroupID,
transactionDate: transaction.purchaseDate.milliseconds,
transactionId: transactionId,
transactionReasonIOS: reasonDetails.uppercased,
webOrderLineItemIdIOS: transaction.webOrderLineItemID.map { String($0) }
)
}
private static func determineAutoRenewStatus(for transaction: StoreKit.Transaction) async -> Bool {
guard transaction.productType == .autoRenewable else { return false }
if let resolved = await subscriptionAutoRenewState(for: transaction) {
return resolved
}
return true
}
private static func subscriptionAutoRenewState(for transaction: StoreKit.Transaction) async -> Bool? {
guard let groupId = transaction.subscriptionGroupID else { return nil }
do {
let statuses = try await StoreKit.Product.SubscriptionInfo.status(for: groupId)
for status in statuses {
guard case .verified(let statusTransaction) = status.transaction else { continue }
guard statusTransaction.productID == transaction.productID else { continue }
switch status.renewalInfo {
case .verified(let info):
return info.willAutoRenew
case .unverified(let info, _):
return info.willAutoRenew
}
}
} catch {
return nil
}
return nil
}
private static func subscriptionRenewalInfoIOS(for transaction: StoreKit.Transaction) async -> RenewalInfoIOS? {
guard transaction.productType == .autoRenewable else {
return nil
}
guard let groupId = transaction.subscriptionGroupID else {
return nil
}
do {
let statuses = try await StoreKit.Product.SubscriptionInfo.status(for: groupId)
for status in statuses {
guard case .verified(let statusTransaction) = status.transaction else { continue }
guard statusTransaction.productID == transaction.productID else { continue }
switch status.renewalInfo {
case .verified(let info):
let pendingProductId = (info.autoRenewPreference != transaction.productID) ? info.autoRenewPreference : nil
let offerInfo: (id: String?, type: String?)?
#if swift(>=6.1)
if #available(iOS 18.0, macOS 15.0, *) {
// Map type only when present to avoid "nil" literal strings
let offerTypeString = info.offer.map { String(describing: $0.type) }
offerInfo = (id: info.offer?.id, type: offerTypeString)
} else {
#endif
// Fallback to deprecated properties
#if compiler(>=5.9)
let offerTypeString = info.offerType.map { String(describing: $0) }
offerInfo = (id: info.offerID, type: offerTypeString)
#else
offerInfo = nil
#endif
#if swift(>=6.1)
}
#endif
// priceIncreaseStatus only available on iOS 15.0+
let priceIncrease: String? = {
if #available(iOS 15.0, macOS 12.0, *) {
return String(describing: info.priceIncreaseStatus)
}
return nil
}()
let renewalInfo = RenewalInfoIOS(
autoRenewPreference: info.autoRenewPreference,
expirationReason: info.expirationReason?.rawValue.description,
gracePeriodExpirationDate: info.gracePeriodExpirationDate?.milliseconds,
isInBillingRetry: nil, // Not available in RenewalInfo, available in Status
jsonRepresentation: nil,
pendingUpgradeProductId: pendingProductId,
priceIncreaseStatus: priceIncrease,
renewalDate: info.renewalDate?.milliseconds,
renewalOfferId: offerInfo?.id,
renewalOfferType: offerInfo?.type,
willAutoRenew: info.willAutoRenew
)
return renewalInfo
case .unverified(let info, _):
let pendingProductId = (info.autoRenewPreference != transaction.productID) ? info.autoRenewPreference : nil
let offerInfo: (id: String?, type: String?)?
#if swift(>=6.1)
if #available(iOS 18.0, macOS 15.0, *) {
// Map type only when present to avoid "nil" literal strings
let offerTypeString = info.offer.map { String(describing: $0.type) }
offerInfo = (id: info.offer?.id, type: offerTypeString)
} else {
#endif
// Fallback to deprecated properties
#if compiler(>=5.9)
let offerTypeString = info.offerType.map { String(describing: $0) }
offerInfo = (id: info.offerID, type: offerTypeString)
#else
offerInfo = nil
#endif
#if swift(>=6.1)
}
#endif
// priceIncreaseStatus only available on iOS 15.0+
let priceIncrease: String? = {
if #available(iOS 15.0, macOS 12.0, *) {
return String(describing: info.priceIncreaseStatus)
}
return nil
}()
let renewalInfo = RenewalInfoIOS(
autoRenewPreference: info.autoRenewPreference,
expirationReason: info.expirationReason?.rawValue.description,
gracePeriodExpirationDate: info.gracePeriodExpirationDate?.milliseconds,
isInBillingRetry: nil, // Not available in RenewalInfo, available in Status
jsonRepresentation: nil,
pendingUpgradeProductId: pendingProductId,
priceIncreaseStatus: priceIncrease,
renewalDate: info.renewalDate?.milliseconds,
renewalOfferId: offerInfo?.id,
renewalOfferType: offerInfo?.type,
willAutoRenew: info.willAutoRenew
)
return renewalInfo
}
}
} catch {
OpenIapLog.debug("⚠️ Failed to fetch renewalInfo: \(error.localizedDescription)")
return nil
}
return nil
}
static func purchaseOptions(from props: RequestPurchaseIosProps) throws -> Set<StoreKit.Product.PurchaseOption> {
var options: Set<StoreKit.Product.PurchaseOption> = []
if let quantity = props.quantity, quantity > 1 {
options.insert(.quantity(quantity))
}
if let token = props.appAccountToken, let uuid = UUID(uuidString: token) {
options.insert(.appAccountToken(uuid))
}
if let offerInput = props.withOffer {
guard let option = promotionalOffer(from: offerInput) else {
throw PurchaseError.make(
code: .developerError,
productId: props.sku,
message: "Invalid promotional offer: nonce must be valid UUID and signature must be base64 encoded"
)
}
options.insert(option)
}
return options
}
}
@available(iOS 15.0, macOS 14.0, *)
private extension StoreKitTypesBridge {
static func makeSubscriptionInfo(from info: StoreKit.Product.SubscriptionInfo?) -> SubscriptionInfoIOS? {
guard let info else { return nil }
let intro = info.introductoryOffer.map { makeSubscriptionOffer(from: $0, type: .introductory) }
let promos = makeSubscriptionOffers(from: info.promotionalOffers, type: .promotional)
return SubscriptionInfoIOS(
introductoryOffer: intro,
promotionalOffers: promos.isEmpty ? nil : promos,
subscriptionGroupId: info.subscriptionGroupID,
subscriptionPeriod: SubscriptionPeriodValueIOS(
unit: info.subscriptionPeriod.unit.subscriptionPeriodIOS,
value: info.subscriptionPeriod.value
)
)
}
static func makeSubscriptionOffers(from offers: [StoreKit.Product.SubscriptionOffer], type: SubscriptionOfferTypeIOS) -> [SubscriptionOfferIOS] {
offers.map { makeSubscriptionOffer(from: $0, type: type) }
}
static func makeSubscriptionOffer(from offer: StoreKit.Product.SubscriptionOffer, type: SubscriptionOfferTypeIOS) -> SubscriptionOfferIOS {
SubscriptionOfferIOS(
displayPrice: offer.displayPrice,
id: offer.id ?? "",
paymentMode: offer.paymentMode.paymentModeIOS,
period: SubscriptionPeriodValueIOS(
unit: offer.period.unit.subscriptionPeriodIOS,
value: offer.period.value
),
periodCount: offer.periodCount,
price: NSDecimalNumber(decimal: offer.price).doubleValue,
type: type
)
}
static func makeDiscounts(from subscription: StoreKit.Product.SubscriptionInfo) -> [DiscountIOS]? {
var discounts: [DiscountIOS] = []
if let intro = subscription.introductoryOffer {
discounts.append(makeDiscount(from: intro, type: "introductory"))
}
let promotional = subscription.promotionalOffers.map { makeDiscount(from: $0, type: "promotional") }
discounts.append(contentsOf: promotional)
return discounts.isEmpty ? nil : discounts
}
static func makeDiscount(from offer: StoreKit.Product.SubscriptionOffer, type: String) -> DiscountIOS {
DiscountIOS(
identifier: offer.id ?? "",
localizedPrice: offer.displayPrice,
numberOfPeriods: offer.periodCount,
paymentMode: offer.paymentMode.paymentModeIOS,
price: offer.displayPrice,
priceAmount: NSDecimalNumber(decimal: offer.price).doubleValue,
subscriptionPeriod: offer.period.iso8601,
type: type
)
}
static func introductoryPriceAmount(from offer: StoreKit.Product.SubscriptionOffer?) -> String? {
guard let price = offer?.price else { return nil }
return String(NSDecimalNumber(decimal: price).doubleValue)
}
static func introductoryPeriods(from offer: StoreKit.Product.SubscriptionOffer?) -> String? {
guard let periodCount = offer?.periodCount else { return nil }
return String(periodCount)
}
static func productType(from type: StoreKit.Product.ProductType) -> ProductType {
switch type {
case .autoRenewable, .nonRenewable:
return .subs
case .consumable, .nonConsumable:
return .inApp
default:
return .inApp
}
}
static func productTypeIOS(from type: StoreKit.Product.ProductType) -> ProductTypeIOS {
switch type {
case .consumable:
return .consumable
case .nonConsumable:
return .nonConsumable
case .autoRenewable:
return .autoRenewableSubscription
case .nonRenewable:
return .nonRenewingSubscription
default:
return .consumable
}
}
static func promotionalOffer(from offer: DiscountOfferInputIOS) -> StoreKit.Product.PurchaseOption? {
guard let nonce = UUID(uuidString: offer.nonce) else {
OpenIapLog.error("❌ Invalid nonce format: \(offer.nonce)")
return nil
}
guard let signature = Data(base64Encoded: offer.signature) else {
OpenIapLog.error("❌ Invalid signature format (must be base64): \(offer.signature)")
return nil
}
let timestamp = Int(offer.timestamp)
OpenIapLog.debug("✅ Creating promotional offer - ID: \(offer.identifier), KeyID: \(offer.keyIdentifier), Timestamp: \(timestamp)")
return .promotionalOffer(
offerID: offer.identifier,
keyID: offer.keyIdentifier,
nonce: nonce,
signature: signature,
timestamp: timestamp
)
}
@available(iOS 17.2, macOS 14.2, *)
static func makePurchaseOffer(from offer: StoreKit.Transaction.Offer?) -> PurchaseOfferIOS? {
guard let offer else { return nil }
return PurchaseOfferIOS(
id: offer.id ?? "",
paymentMode: offer.paymentMode?.rawValue ?? "",
type: String(describing: offer.type)
)
}
static func ownershipTypeDescription(from ownership: StoreKit.Transaction.OwnershipType) -> String {
switch ownership {
case .purchased:
return "purchased"
case .familyShared:
return "family_shared" // Maintain backward compatibility
default:
return "purchased" // Default to purchased for compatibility
}
}
struct TransactionReason {
let lowercased: String
let string: String
let uppercased: String
}
struct JSONTransactionReason: Codable {
let transactionReason: String
}
static func transactionReasonDetails(from transaction: StoreKit.Transaction) -> TransactionReason {
if let revocation = transaction.revocationReason {
// Map revocation reasons to expected strings
let reasonString: String
switch revocation {
case .developerIssue:
reasonString = "developer_issue"
case .other:
reasonString = "other"
default:
reasonString = "unknown"
}
return TransactionReason(
lowercased: reasonString,
string: reasonString,
uppercased: reasonString.uppercased()
)
}
if transaction.isUpgraded {
return TransactionReason(lowercased: "upgrade", string: "upgrade", uppercased: "UPGRADE")
}
// Try to infer renewal for iOS <17
if let decodedReason = try? JSONDecoder().decode(JSONTransactionReason.self, from: transaction.jsonRepresentation),
decodedReason.transactionReason == "RENEWAL" {
return TransactionReason(lowercased: "renewal", string: "renewal", uppercased: "RENEWAL")
}
return TransactionReason(lowercased: "purchase", string: "purchase", uppercased: "PURCHASE")
}
}
@available(iOS 15.0, macOS 14.0, *)
private extension StoreKit.Product.SubscriptionOffer.PaymentMode {
var paymentModeIOS: PaymentModeIOS {
switch self {
case .freeTrial: return .freeTrial
case .payAsYouGo: return .payAsYouGo
case .payUpFront: return .payUpFront
default: return .empty
}
}
}
@available(iOS 15.0, macOS 14.0, *)
private extension StoreKit.Product.SubscriptionPeriod.Unit {
var subscriptionPeriodIOS: SubscriptionPeriodIOS {
switch self {
case .day: return .day
case .week: return .week
case .month: return .month
case .year: return .year
default: return .empty
}
}
}
@available(iOS 15.0, macOS 14.0, *)
private extension StoreKit.Product.SubscriptionPeriod {
var iso8601: String { "P\(value)\(unit.isoComponent)" }
}
@available(iOS 15.0, macOS 14.0, *)
private extension StoreKit.Product.SubscriptionPeriod.Unit {
var isoComponent: String {
switch self {
case .day: return "D"
case .week: return "W"
case .month: return "M"
case .year: return "Y"
default: return "D"
}
}
}
extension Date {
var milliseconds: Double { timeIntervalSince1970 * 1000 }
}