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 pathSubscriptionFlowScreen.swift
More file actions
529 lines (467 loc) · 19.7 KB
/
SubscriptionFlowScreen.swift
File metadata and controls
529 lines (467 loc) · 19.7 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
import SwiftUI
import OpenIAP
@available(iOS 15.0, *)
struct SubscriptionFlowScreen: View {
@StateObject private var iapStore = OpenIapStore()
// UI State
@State private var showError = false
@State private var errorMessage = ""
@State private var recentPurchase: OpenIapPurchase?
@State private var selectedPurchase: OpenIapPurchase?
@State private var isInitialLoading = true
// Product IDs for subscription testing
// Ordered from lowest to highest tier for upgrade scenarios
private let subscriptionIds: [String] = [
"dev.hyo.martie.premium", // Monthly subscription (lower tier)
"dev.hyo.martie.premium_year" // Yearly subscription (higher tier)
]
var body: some View {
ScrollView(.vertical, showsIndicators: true) {
VStack(spacing: 20) {
VStack(alignment: .leading, spacing: 16) {
HStack {
Image(systemName: "repeat.circle.fill")
.font(.largeTitle)
.foregroundColor(AppColors.secondary)
VStack(alignment: .leading, spacing: 4) {
Text("Subscription Management")
.font(.headline)
Text("iOS")
.font(.caption)
.padding(.horizontal, 8)
.padding(.vertical, 2)
.background(AppColors.secondary.opacity(0.2))
.cornerRadius(4)
}
Spacer()
}
Text("Manage your premium subscriptions and auto-renewable purchases.")
.font(.subheadline)
.foregroundColor(.secondary)
}
.padding()
.background(AppColors.cardBackground)
.cornerRadius(12)
.shadow(radius: 2)
.padding(.horizontal)
if isInitialLoading {
LoadingCard(text: "Loading subscriptions...")
} else {
if let purchase = recentPurchase {
purchaseResultCard(for: purchase)
}
let productIds = subscriptionProductIds
if productIds.isEmpty {
EmptyStateCard(
icon: "repeat.circle",
title: "No subscriptions available",
subtitle: "Configure subscription products in App Store Connect"
)
} else {
ForEach(productIds, id: \.self) { productId in
let product = product(for: productId)
let currentSubscription = getCurrentSubscription()
let upgradeInfo = getUpgradeInfo(from: currentSubscription, to: productId)
SubscriptionCard(
productId: productId,
product: product,
purchase: purchase(for: productId),
isSubscribed: isSubscribed(productId: productId),
isCancelled: isCancelled(productId: productId),
isLoading: iapStore.status.isPurchasing(productId),
upgradeInfo: upgradeInfo,
onSubscribe: {
let subscribed = isSubscribed(productId: productId)
if subscribed {
Task {
await manageSubscriptions()
}
} else if upgradeInfo.canUpgrade {
// Handle upgrade scenario
if let product = product {
Task {
await upgradeSubscription(from: currentSubscription, to: product)
}
}
} else {
if let product = product {
purchaseProduct(product)
}
}
},
onManage: {
Task {
await manageSubscriptions()
}
}
)
}
}
}
VStack(alignment: .leading, spacing: 12) {
Text("Notes")
.font(.headline)
VStack(alignment: .leading, spacing: 8) {
Text("• Subscriptions may take a moment to reflect")
Text("• Use Sandbox account for testing")
Text("• Restore purchases to sync status")
}
.font(.caption)
}
.padding()
.background(AppColors.secondary.opacity(0.05))
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(AppColors.secondary.opacity(0.3), lineWidth: 1)
)
.cornerRadius(8)
.padding(.horizontal)
Button(action: {
Task {
await restorePurchases()
}
}) {
HStack {
Image(systemName: "arrow.clockwise.circle")
Text("Restore Purchases")
Spacer()
Image(systemName: "arrow.up.forward.app")
}
.padding()
.background(AppColors.secondary)
.foregroundColor(.white)
.cornerRadius(8)
}
.padding(.horizontal)
Spacer(minLength: 20)
}
.padding(.vertical)
}
.background(AppColors.background)
.navigationTitle("Subscriptions")
.navigationBarTitleDisplayMode(.large)
.navigationBarItems(trailing:
Button {
Task { await loadProducts() }
} label: {
Image(systemName: "arrow.clockwise")
}
.disabled(isInitialLoading || iapStore.status.isLoading)
)
.sheet(isPresented: Binding(
get: { selectedPurchase != nil },
set: { if !$0 { selectedPurchase = nil } }
)) {
if let purchase = selectedPurchase {
PurchaseDetailSheet(purchase: purchase)
}
}
.alert("Error", isPresented: $showError) {
Button("OK") {}
} message: {
Text(errorMessage)
}
.onAppear {
isInitialLoading = true
setupIapProvider()
}
.onDisappear {
iapStore.resetEphemeralState()
teardownConnection()
recentPurchase = nil
selectedPurchase = nil
}
}
// MARK: - OpenIapStore Setup
private func setupIapProvider() {
print("🔷 [SubscriptionFlow] Setting up OpenIapStore...")
// Setup callbacks
iapStore.onPurchaseSuccess = { purchase in
if let iosPurchase = purchase.asIOS() {
Task { @MainActor in
self.handlePurchaseSuccess(iosPurchase)
}
}
}
iapStore.onPurchaseError = { error in
Task { @MainActor in
self.handlePurchaseError(error)
}
}
Task {
do {
try await iapStore.initConnection()
print("✅ [SubscriptionFlow] Connection initialized")
await loadProducts()
await MainActor.run { isInitialLoading = false }
await loadPurchases()
} catch {
await MainActor.run {
errorMessage = "Failed to initialize connection: \(error.localizedDescription)"
showError = true
isInitialLoading = false
}
}
}
}
private func teardownConnection() {
print("🔷 [SubscriptionFlow] Tearing down connection...")
Task {
try await iapStore.endConnection()
print("✅ [SubscriptionFlow] Connection ended")
}
}
// MARK: - Product and Purchase Loading
private func loadProducts() async {
do {
try await iapStore.fetchProducts(skus: subscriptionIds, type: .all)
await MainActor.run {
let ids = subscriptionProductIds
if ids.isEmpty {
errorMessage = "No subscription products found. Please check your App Store Connect configuration."
showError = true
}
print("✅ [SubscriptionFlow] Loaded subscriptions: \(ids.joined(separator: ", "))")
}
} catch {
await MainActor.run {
errorMessage = "Failed to load products: \(error.localizedDescription)"
showError = true
}
}
}
private func loadPurchases() async {
do {
// Only use activeSubscriptions - demonstrates it contains all necessary info
try await iapStore.getActiveSubscriptions()
} catch {
await MainActor.run {
errorMessage = "Failed to load purchases: \(error.localizedDescription)"
showError = true
}
}
}
// MARK: - Purchase Flow
private func purchaseProduct(_ product: OpenIapProduct) {
print("🔄 [SubscriptionFlow] Starting subscription purchase for: \(product.id)")
Task {
do {
_ = try await iapStore.requestPurchase(sku: product.id, type: .subs, autoFinish: true)
} catch {
// Error is already handled by OpenIapStore internally
print("❌ [SubscriptionFlow] Purchase failed: \(error.localizedDescription)")
}
}
}
// MARK: - Subscription Upgrade Flow
private func upgradeSubscription(from currentSubscription: ActiveSubscription?, to product: OpenIapProduct) async {
print("⬆️ [SubscriptionFlow] Starting subscription upgrade")
print(" From: \(currentSubscription?.productId ?? "none")")
print(" To: \(product.id)")
print(" iOS will automatically prorate the subscription")
do {
// Request the upgrade purchase
// iOS handles proration automatically when upgrading within the same subscription group
_ = try await iapStore.requestPurchase(
sku: product.id,
type: .subs,
autoFinish: true
)
print("✅ [SubscriptionFlow] Upgrade successful to: \(product.id)")
// Reload purchases to update UI
await loadPurchases()
} catch {
print("❌ [SubscriptionFlow] Upgrade failed: \(error.localizedDescription)")
await MainActor.run {
errorMessage = "Failed to upgrade subscription: \(error.localizedDescription)"
showError = true
}
}
}
// Get current active subscription
private func getCurrentSubscription() -> ActiveSubscription? {
// Use activeSubscriptions from store (includes renewalInfo)
let activeSubs = iapStore.activeSubscriptions.filter { $0.isActive }
// Return the subscription with the highest tier (yearly over monthly)
return activeSubs.first { $0.productId.contains("year") } ?? activeSubs.first
}
// Determine upgrade possibilities
private func getUpgradeInfo(from currentSubscription: ActiveSubscription?, to targetProductId: String) -> UpgradeInfo {
guard let current = currentSubscription else {
return UpgradeInfo(canUpgrade: false, isDowngrade: false, currentTier: nil)
}
// Check renewalInfo for pending upgrade
if let renewalInfo = current.renewalInfoIOS,
let pendingUpgrade = renewalInfo.pendingUpgradeProductId {
if pendingUpgrade == targetProductId {
return UpgradeInfo(
canUpgrade: false,
isDowngrade: false,
currentTier: current.productId,
message: "This upgrade will activate on your next renewal date",
isPending: true
)
}
}
// Don't show upgrade for the same product
if current.productId == targetProductId {
return UpgradeInfo(canUpgrade: false, isDowngrade: false, currentTier: current.productId)
}
// Determine tier based on product ID
let currentTier = getSubscriptionTier(current.productId)
let targetTier = getSubscriptionTier(targetProductId)
let canUpgrade = targetTier > currentTier
let isDowngrade = targetTier < currentTier
return UpgradeInfo(
canUpgrade: canUpgrade,
isDowngrade: isDowngrade,
currentTier: current.productId,
message: canUpgrade ? "Upgrade available" : (isDowngrade ? "Downgrade option" : nil)
)
}
// Get subscription tier level (higher number = higher tier)
private func getSubscriptionTier(_ productId: String) -> Int {
if productId.contains("year") || productId.contains("annual") {
return 2 // Yearly is higher tier
} else if productId.contains("month") || productId.contains("premium") {
return 1 // Monthly is lower tier
}
return 0 // Unknown tier
}
private func restorePurchases() async {
do {
try await iapStore.refreshPurchases(forceSync: true)
try await iapStore.getActiveSubscriptions()
await MainActor.run {
print("✅ [SubscriptionFlow] Restored \(iapStore.activeSubscriptions.count) active subscriptions")
}
} catch {
await MainActor.run {
errorMessage = "Failed to restore purchases: \(error.localizedDescription)"
showError = true
}
}
}
private func manageSubscriptions() async {
do {
_ = try await iapStore.showManageSubscriptionsIOS()
} catch {
await MainActor.run {
errorMessage = "Failed to open subscription management: \(error.localizedDescription)"
showError = true
}
}
}
// MARK: - Event Handlers
private func handlePurchaseSuccess(_ purchase: OpenIapPurchase) {
print("✅ [SubscriptionFlow] Subscription successful: \(purchase.productId)")
// Reload purchases to update UI
Task {
await loadPurchases()
}
recentPurchase = purchase
}
private func handlePurchaseError(_ error: OpenIapError) {
print("❌ [SubscriptionFlow] Subscription error: \(error.message)")
// Error status is already handled internally by OpenIapStore
}
}
@available(iOS 15.0, *)
@MainActor
private extension SubscriptionFlowScreen {
var subscriptionProductIds: [String] {
var orderedIds: [String] = []
func appendIfNeeded(_ id: String) {
guard orderedIds.contains(id) == false else { return }
orderedIds.append(id)
}
subscriptionIds.forEach { appendIfNeeded($0) }
iapStore.iosProducts.filter { $0.type == .subs }.forEach { appendIfNeeded($0.id) }
iapStore.activeSubscriptions.forEach { appendIfNeeded($0.productId) }
return orderedIds
}
func product(for id: String) -> OpenIapProduct? {
iapStore.iosProducts.first { $0.id == id }
}
func purchase(for productId: String) -> OpenIapPurchase? {
iapStore.iosAvailablePurchases.first { $0.productId == productId }
}
func isSubscribed(productId: String) -> Bool {
// Check activeSubscriptions first (more accurate)
if let subscription = iapStore.activeSubscriptions.first(where: { $0.productId == productId }) {
return subscription.isActive
}
return false
}
func isCancelled(productId: String) -> Bool {
// Check if subscription is active but won't auto-renew (cancelled)
if let subscription = iapStore.activeSubscriptions.first(where: { $0.productId == productId }) {
return subscription.isActive && subscription.renewalInfoIOS?.willAutoRenew == false
}
return false
}
}
// MARK: - Upgrade Info Model
struct UpgradeInfo {
let canUpgrade: Bool
let isDowngrade: Bool
let currentTier: String?
let message: String?
let isPending: Bool // True if upgrade is pending (already scheduled)
init(canUpgrade: Bool = false, isDowngrade: Bool = false, currentTier: String? = nil, message: String? = nil, isPending: Bool = false) {
self.canUpgrade = canUpgrade
self.isDowngrade = isDowngrade
self.currentTier = currentTier
self.message = message
self.isPending = isPending
}
}
@available(iOS 15.0, *)
@MainActor
private extension SubscriptionFlowScreen {
func purchaseResultCard(for purchase: OpenIapPurchase) -> some View {
let transactionDate = Date(timeIntervalSince1970: purchase.transactionDate / 1000)
let formattedDate = DateFormatter.localizedString(from: transactionDate, dateStyle: .short, timeStyle: .short)
let message = """
✅ Subscription successful
Product: \(purchase.productId)
Transaction ID: \(purchase.id)
Date: \(formattedDate)
"""
return VStack(alignment: .leading, spacing: 12) {
HStack {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(AppColors.success)
Text("Latest Subscription")
.font(.headline)
Spacer()
Button("Dismiss") {
recentPurchase = nil
}
.font(.caption)
.foregroundColor(AppColors.primary)
}
Button {
selectedPurchase = purchase
} label: {
HStack(alignment: .top, spacing: 8) {
Image(systemName: "chevron.right.circle.fill")
.foregroundColor(AppColors.primary)
Text(message)
.font(.system(.caption, design: .monospaced))
.foregroundColor(.primary)
.frame(maxWidth: .infinity, alignment: .leading)
}
.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(8)
}
.buttonStyle(.plain)
}
.padding()
.background(AppColors.cardBackground)
.cornerRadius(12)
.shadow(radius: 2)
.padding(.horizontal)
}
}