Skip to content

Instantly share code, notes, and snippets.

@maxhis
Created October 9, 2017 09:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxhis/15015545a5fa95397f0beedf0c6873a4 to your computer and use it in GitHub Desktop.
Save maxhis/15015545a5fa95397f0beedf0c6873a4 to your computer and use it in GitHub Desktop.
[iOS In App Purchase Helper] #swift #in_app_purchase
//
// IAPHelper.swift
// Bandwagon
//
// Created by Andy on 2017/9/27.
// Copyright © 2017年 Doraemon. All rights reserved.
//
import Foundation
import SwiftyStoreKit
import SwiftyJSON
/// 应用内购买工具类
class IAPHelper {
static let KEY_PURCHASED_APP = "purchased_app"
static let KEY_PURCHASED_IN_APP = "purchased_in_app"
/// 一般在应用启动时调用
static func setup() {
SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
for purchase in purchases {
if purchase.transaction.transactionState == .purchased || purchase.transaction.transactionState == .restored {
if purchase.needsFinishTransaction {
// Deliver content from server, then:
SwiftyStoreKit.finishTransaction(purchase.transaction)
}
log.warning("purchased: \(purchase)")
if purchase.productId == Constants.iapProductId {
markPurchasedInApp(purchased: true)
}
}
}
}
SwiftyStoreKit.shouldAddStorePaymentHandler = { payment, product in
// return true if the content can be delivered by your app
// return false otherwise
return true
}
if IAPHelper.isAppPurchased() == false {
let appleValidator = AppleReceiptValidator(service: .production)
SwiftyStoreKit.verifyReceipt(using: appleValidator, password: Constants.iapSharedSecret, forceRefresh: false) { result in
switch result {
case .success(let receipt):
log.info("Verify receipt Success: \(receipt)")
let receiptJSON = JSON(receipt)
// 是否之前付费下载
if let orignalVersion = receiptJSON["receipt"]["original_application_version"].string,
Int(orignalVersion) ?? 0 < Constants.iapStartVersion {
IAPHelper.markPurchasedApp(purchased: true)
log.info("Already purchased the App")
} else {
IAPHelper.markPurchasedApp(purchased: false)
}
// 是否应用内购买
if let inAppReceipts = receiptJSON["receipt"]["in_app"].array,
inAppReceipts.count > 0 {
IAPHelper.markPurchasedInApp(purchased: true)
log.info("Already in-app purchased")
} else {
IAPHelper.markPurchasedInApp(purchased: false)
}
case .error(let error):
log.error("Verify receipt Failed: \(error)")
}
}
}
}
/// 是否已购买App
static func isAppPurchased() -> Bool {
return UserDefaults.standard.bool(forKey: KEY_PURCHASED_APP)
}
/// 标记是否已购买App
static func markPurchasedApp(purchased: Bool) {
UserDefaults.standard.setValue(purchased, forKey: KEY_PURCHASED_APP)
}
/// 是否已应用内购买
static func isInAppPurchased() -> Bool {
return UserDefaults.standard.bool(forKey: KEY_PURCHASED_IN_APP)
}
/// 标记是否已应用内购买
static func markPurchasedInApp(purchased: Bool) {
UserDefaults.standard.setValue(purchased, forKey: KEY_PURCHASED_IN_APP)
}
/// 是否需要应用内购买
static func isIAPRequried() -> Bool {
if isAppPurchased() || isInAppPurchased() {
return false
}
return true
}
/// 应用内购买
static func purchaseProduct() {
SwiftyStoreKit.purchaseProduct(Constants.iapProductId, quantity: 1, atomically: true) { result in
switch result {
case .success(let purchase):
log.info("Purchase Success: \(purchase.productId)")
if purchase.productId == Constants.iapProductId {
markPurchasedInApp(purchased: true)
}
case .error(let error):
switch error.code {
case .unknown: log.warning("Unknown error. Please contact support")
case .clientInvalid: log.warning("Not allowed to make the payment")
case .paymentCancelled: break
case .paymentInvalid: log.warning("The purchase identifier was invalid")
case .paymentNotAllowed: log.warning("The device is not allowed to make the payment")
case .storeProductNotAvailable: log.warning("The product is not available in the current storefront")
case .cloudServicePermissionDenied: log.warning("Access to cloud service information is not allowed")
case .cloudServiceNetworkConnectionFailed: log.warning("Could not connect to the network")
case .cloudServiceRevoked: log.warning("User has revoked permission to use this cloud service")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment