PurchaseStacks is a framework for in app purchase. It works with both Swift and Objective-C. API reference is available at SDK Reference
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:
Copy pod 'PurchaseStacks', '~> 1.0.0'
And then run in the Terminal:
Manual Installation# Copy PurchaseStacks.framework to your project.
Intialization# Copy import PurchaseStacks
PSPurchase.config(withAPIKey: "API_KEY", withAPISecret: "API_SECRET")
If you have user info, it's recommended to track directly in config
Copy PSPurchase.config(withAPIKey: "API_KEY", withAPISecret: "API_SECRET", withUserID: "USER_ID", withEmail: "USER@email.com")
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
Copy NotificationCenter.default.addObserver(self, selector: #selector(sdkReady(notification:)),
name: Notification.Name("PS_SDK_READY"), object: nil)
Copy @objc func sdkReady(notification: Notification) {
// SDK is ready!
}
Track user info# Copy PSPurchase.shared.trackUserID("USER_ID", email: "USER@Email.com", 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
Copy 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
Copy PSPurchase.shared.groups { (groupsInfo, error) in
// groupsInfo contains all groups information
}
Get Package & Product# Copy 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# Copy 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# Copy 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# Copy 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# Copy PSPurchase.shared.getAllConsumableProducts { (products, error) in
if let products = products {
// These are only consumable products
}
}
Get associated user info# Copy 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
Copy PSPurchase.shared.delegate = self
This method will be called when any change to groups happen or any change to subscriptions take place
Copy 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
Copy 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
Copy 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
}
}
}