CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

PurchaseStacks 1.0.1

PurchaseStacks 1.0.1

Maintained by Rifat Monzur.



  • By
  • Purchase Stacks

PurchaseStacks

PurchaseStacks is a framework for in app purchase. It works with both Swift and Objective-C. API reference is available at ios-sdk.purchasestacks.com

PurchaseStacks

Requirements

PurchaseFly requires minimum targets iOS 12.0

Installation

PurchaseStacks SDK can be installed via CocoaPods or manually.

Install via CocoaPods

Add the following line to your Podfile:

pod 'PurchaseStacks', '~> 1.0.0'

And then run in the Terminal:

pod install

Manual Installation

Copy PurchaseStacks.framework to your project.

Intialization

import PurchaseStacks

PSPurchase.config(withAPIKey: "API_KEY", withAPISecret: "API_SECRET")

If you have user info, it's recommended to track directly in config

PSPurchase.config(withAPIKey: "API_KEY", withAPISecret: "API_SECRET", withUserID: "USER_ID", withEmail: "[email protected]")

Only one instance of PSPurchase should be instantiated at a time! Use the configure method to let the framework handle the singleton instance for you.

SDK Ready Notification

After config is called to initialize the SDK, when SDK becomes ready it sends notification, if it is subscribed to

NotificationCenter.default.addObserver(self, selector: #selector(sdkReady(notification:)),
                name: Notification.Name("PS_SDK_READY"), object: nil)
@objc func sdkReady(notification: Notification) {
    // SDK is ready!
}

Track user info

PSPurchase.shared.trackUserID("USER_ID", email: "[email protected]", withUsername: "USERNAME")

Get Groups

You can organize the groups in PurchaseStacks Dashboard and access them here.

This returns a dictionary of groups with names as keys

PSPurchase.shared.group { (groupsByName, error) in
    if let groupsByName = groupsByName {
        let group = groupsByName["PREMIUM"]
    }
}

This returns all groups array, active groups array and additonal information

PSPurchase.shared.groups { (groupsInfo, error) in
    // groupsInfo contains all groups information
}

Get Package & Product

if let group = group {
    let monthlyPackage = group.packageByName["Monthly"]
    if let monthlyPackage = monthlyPackage {
        let monthlyProduct = monthlyPackage.activeProduct()
    }

    let yearlyPackage = group.packageByName["Yearly"]
    if let yearlyPackage = yearlyPackage {
        let yearlyProduct = yearlyPackage.activeProduct()
    }
}

Make Purchase

Purchase with product

PFPurchase.shared.make(with: monthlyProduct) { (transaction, groupsByName, error, cancelled) in
    if let groupsByName = groupsByName {
        let group = groupsByName["PREMIUM"]
        if (group.isActive) {
            // Purchase is successful
        }
    }
}

Purchase with package

PFPurchase.shared.make(with: monthlyPackage) { (transaction, groupsByName, error, cancelled) in
    if let groupsByName = groupsByName {
        let group = groupsByName["PREMIUM"]
        if (group.isActive) {
            // Purchase is successful
        }
    }
}

Restore purchase

PSPurchase.shared.restore { (groupsByName, error) in
    if let groupsByName = groupsByName {
        // check for the group if it is active and
        // provide user with that subscription
    }
}

Get Consumable products

PSPurchase.shared.getAllConsumableProducts { (products, error) in
    if let products = products {
        // These are only consumable products
    }
}

Get associated user info

PSPurchase.shared.userInfo { (user, error) in
    if let user = user {
        // Tracked user
    }
}

Delegate

You can setup delegate to be notified whenver any changes to the groups happen

PSPurchase.shared.delegate = self

This method will be called when any change to groups happen or any change to subscriptions take place

func purchases(_ purchase: PSPurchase, didReceiveUpdatedGroupsInfo groupsByName: [String: PSGroup], withGroups groups: PSGroupsInfo) {
    // Check groupByName or groups
}

Discount

There's two kinds of discounts that can be set and check against. Introductory pricing and promotional pricing. You have to configure them first at appstore connect.

Check for discount eligibility

PSPurchase.shared.checkDiscountEligibility(monthlyProduct.productId, with: .promo) { (status, error) in
    // Check if user is eligible for promo type discount
}

Apply Promo discount and make purchase

let discounts = monthlyProduct.productDiscounts()
if let promo = discounts.first {
    if promo.discountType == PSDiscountType.promo {
        PSPurchase.shared.make(with: monthlyProduct, with: promo) { (transaction, groupByName, error, cancelled) in
            // check transaction?.group.discount
        }
    }
}