SwiftyStoreKit Migration

This document describes how to migrate from SwiftyStoreKit to Glassfy. It assumes you have installed Glassfy following the Installation

App startup

SwiftyStoreKit app startup

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
	// see notes below for the meaning of Atomic / Non-Atomic
	SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
	    for purchase in purchases {
	        switch purchase.transaction.transactionState {
	        case .purchased, .restored:
	            if purchase.needsFinishTransaction {
	                // Deliver content from server, then:
	                SwiftyStoreKit.finishTransaction(purchase.transaction)
	            }
	            // Unlock content
	        case .failed, .purchasing, .deferred:
	            break // do nothing
	        }
	    }
	}
    return true
}

Glassfy app startup

There is no need in Glassfy to manually process the transaction: the SDK does that automatically. Simply replace all the SwiftyStoreKit code with Glassfy initialize.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    Glassfy.initialize(apiKey: "your_api_key", watcherMode: false)

    return true
}

Purchases

SwiftyStoreKit retrive purchases

SwiftyStoreKit.retrieveProductsInfo(["com.musevisions.purchase"]) { result in
    if let product = result.retrievedProducts.first {
        let priceString = product.localizedPrice!
        print("Product: \(product.localizedDescription), price: \(priceString)")
    }
    else if let invalidProductId = result.invalidProductIDs.first {
        print("Invalid product identifier: \(invalidProductId)")
    }
    else {
        print("Error: \(result.error)")
    }
}

Glassfy retrive purchases

In Glassfy you can organize the products in Offerings that will contains a list of SKUs. Once the offering is configured in Glassfy Dashboard{:target="_blank"} you
can remove the SwiftyStoreKit retrivedProduct with Glassfy.offerings api.

Glassfy.offerings { offerings, err in
    guard let premium = offerings?["my_premium_offering"] else {
        // handle error?
        return
    }

    for sku in premium.skus {
        print("SKU \(sku.product.localizedTitle) \(sku.product.price)")
    }
}

SwiftyStoreKit purchase a product

SwiftyStoreKit.purchaseProduct("com.musevisions.SwiftyStoreKit.Purchase1", quantity: 1, atomically: true) { result in
    switch result {
    case .success(let purchase):
        print("Purchase Success: \(purchase.productId)")
    case .error(let error):
        switch error.code {
        case .unknown: print("Unknown error. Please contact support")
        case .clientInvalid: print("Not allowed to make the payment")
        case .paymentCancelled: break
        case .paymentInvalid: print("The purchase identifier was invalid")
        case .paymentNotAllowed: print("The device is not allowed to make the payment")
        case .storeProductNotAvailable: print("The product is not available in the current storefront")
        case .cloudServicePermissionDenied: print("Access to cloud service information is not allowed")
        case .cloudServiceNetworkConnectionFailed: print("Could not connect to the network")
        case .cloudServiceRevoked: print("User has revoked permission to use this cloud service")
        default: print((error as NSError).localizedDescription)
        }
    }
}

Glassfy purchase a product

In SwiftyStoreKit the purchase can be done using the productid while in Glassfy the best way to purchase a product is using the offerings and skus created in the step above. To verify if the product has been purchased sucessfully you can verify the permission created here

Glassfy.purchase(sku: sku) { transaction, err in
    if let p = transaction?.permissions["my_premium_permission"], p.isValid{
        print("Purchase Success: \(purchase.productId)")
    }
}

Restore previous purchases

SwiftyStoreKit Restore previous purchases

SwiftyStoreKit.restorePurchases(atomically: true) { results in
    if results.restoreFailedPurchases.count > 0 {
        print("Restore Failed: \(results.restoreFailedPurchases)")
    }
    else if results.restoredPurchases.count > 0 {
        print("Restore Success: \(results.restoredPurchases)")
    }
    else {
        print("Nothing to Restore")
    }
}

Glassfy Restore previous purchases

Please replace SwiftyStoreKit.restorePurchases with Glassfy.restorePurchases and then check the permissions.

Glassfy.restorePurchases { permissions, err in
    if let p = permissions?["my_premium_permission"], p.isValid {
        print("Restore Success: \(p.permissionIdentifier) isValid")
    }
}

Receipt validation

Glassfy automatically verify the receipt and there is nothing to do in your application code.

Verifying purchases and subscriptions

SwiftyStoreKit verifying purchases and subscriptions

let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: "your-shared-secret")
SwiftyStoreKit.verifyReceipt(using: appleValidator) { result in
    switch result {
    case .success(let receipt):
        let productId = "com.musevisions.SwiftyStoreKit.Subscription"
        // Verify the purchase of a Subscription
        let purchaseResult = SwiftyStoreKit.verifySubscription(
            ofType: .autoRenewable, // or .nonRenewing (see below)
            productId: productId,
            inReceipt: receipt)

        switch purchaseResult {
        case .purchased(let expiryDate, let items):
            print("\(productId) is valid until \(expiryDate)\n\(items)\n")
        case .expired(let expiryDate, let items):
            print("\(productId) is expired since \(expiryDate)\n\(items)\n")
        case .notPurchased:
            print("The user has never purchased \(productId)")
        }

    case .error(let error):
        print("Receipt verification failed: \(error)")
    }
}

Glassfy verifying purchases and subscriptions

Replace the SwiftyStoreKit receipt validation code with Glassfy.permissions.
Glassfy verify the receipt on its infrastructure and always keep the subscriptions status updated and can easly fetched by the app using the Glassfy.permissions api.

Glassfy.permissions { permissions, err in
    if let p = permissions?["my_premium_permission"] {
        print("Is valid \(p.isValid)") // the user has "my_premium_permission" granted
        if let expireDate = p.expireDate {
            print("Expire date: \(expireDate )")
        }
    }
}