Skip to content

Chainless-Dev/CHPurchases

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CHPurchases

A comprehensive Swift package for subscription management built on RevenueCat 5.33.0, with integrated analytics and logging capabilities.

Features

  • 🚀 Easy-to-use subscription management
  • 📱 RevenueCat paywall integration
  • 📊 Built-in analytics tracking with CHAnalytics
  • 🔍 Comprehensive logging with CHLogger
  • 🎨 Configurable subscription tiers
  • ⚡ SwiftUI-first design
  • 🔄 Automatic subscription status updates

Installation

Add CHPurchases to your project using Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/your-username/CHPurchases.git", from: "1.0.0")
]

Quick Start

1. Define Your Subscription Tiers

import CHPurchases

enum MyAppSubscriptionTier: String, SubscriptionTier, CaseIterable {
    case none = "None"
    case premium = "Premium"
    
    var displayName: String {
        switch self {
        case .none: return "Free"
        case .premium: return "Premium"
        }
    }
    
    var hasUnlimitedAccess: Bool {
        self != .none
    }
    
    static var none: MyAppSubscriptionTier { .none }
}

2. Initialize CHPurchases

import SwiftUI
import CHPurchases

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    @StateObject private var purchases = CHPurchases<MyAppSubscriptionTier>(
        apiKey: "your_revenuecat_api_key"
    )
    
    var body: some View {
        VStack {
            Text("Current Plan: \\(purchases.currentSubscriptionStatus.type.displayName)")
            
            if !purchases.hasAccess() {
                Button("Upgrade to Premium") {
                    purchases.presentPaywall()
                }
            }
            
            Button("Restore Purchases") {
                Task {
                    try await purchases.restorePurchases()
                }
            }
        }
        .chPurchasesPaywall(purchases: purchases)
    }
}

3. Check Subscription Status

// Check if user has premium access
if purchases.hasAccess() {
    // Show premium features
}

// Get detailed subscription info
let status = purchases.checkSubscriptionStatus()
print("Tier: \\(status.type.displayName)")
print("Expires: \\(status.expirationDate?.description ?? "Never")")
print("In Trial: \\(status.isInTrialPeriod)")

Advanced Usage

Custom Paywall Delegate

class MyPaywallDelegate: PaywallManagerDelegate {
    func paywallDidAppear() {
        print("Paywall appeared")
    }
    
    func paywallDidDismiss() {
        print("Paywall dismissed")
    }
    
    func paywallDidCompletePurchase(customerInfo: CustomerInfo) {
        print("Purchase completed!")
    }
    
    func paywallDidFailPurchase(error: Error) {
        print("Purchase failed: \\(error)")
    }
}

// Set the delegate
purchases.paywallManager.delegate = myDelegate

Automatic Paywall Presentation

struct ContentView: View {
    @StateObject private var purchases = CHPurchases<MyAppSubscriptionTier>(
        apiKey: "your_api_key"
    )
    
    var body: some View {
        YourMainView()
            .chPurchasesPaywall(purchases: purchases)
            .onReceive(purchases.$shouldShowPaywall) { shouldShow in
                if shouldShow {
                    purchases.presentPaywall()
                }
            }
    }
}

Dependencies

  • RevenueCat: 5.33.0 (exact)
  • CHAnalytics: 1.0.0 (exact) - GitHub
  • CHLogger: main branch - GitHub

Platform Support

  • iOS 13.0+
  • macOS 10.15+
  • tvOS 13.0+
  • watchOS 6.0+
  • visionOS 1.0+

Architecture

CHPurchases is built with a clean, modular architecture:

  • CHPurchases: Main coordinator class
  • CHPurchaseService: Core subscription management
  • PaywallManager: Paywall presentation and handling
  • SubscriptionTier Protocol: Flexible subscription tier definition
  • RevenueCat Integration: Built-in RevenueCat paywall support

License

MIT License - see LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages