import SwiftyStoreKit
import SwiftyJSON
class CDIAPManager: NSObject {
class func getPurchaseProductList(retur:@escaping (_ productArr:[CDAppBuyInfo]) ->Void){
SwiftyStoreKit.retrieveProductsInfo(["xxx","xxx","xxx"]) { result in
var arr:[NKAppBuyInfo] = []
if result.retrievedProducts.count > 0 {
print("app 内购获取列表成功")
for product in result.retrievedProducts {
let info = CDAppBuyInfo()
info.productIdentifier = product.productIdentifier
info.price = product.localizedPrice!
info.title = product.localizedTitle
retur(newArr)
}else{
print("app 内购获取列表失败 = \(String(describing: result.error?.localizedDescription))")
}
}
}
class func purchaseProduct(productIDs:String,complete:@escaping (Bool)->Void){
SwiftyStoreKit.purchaseProduct(productIDs, quantity: 1, atomically: true) { result in
switch result {
case .success(let purchase):
print("Purchase Success: \(purchase.productId)")
verifyReceipt(service: .production, isRestor: false, product_id: purchase.productId)
complete(true)
case .error(let error):
complete(false)
print("Purchase Faile: \(error.code)")
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")
case .privacyAcknowledgementRequired: break
case .unauthorizedRequestData:break
case .invalidOfferIdentifier:break
case .invalidSignature:break
case .missingOfferParams:break
case .invalidOfferPrice:break
case .overlayCancelled:break
case .overlayInvalidConfiguration:break
case .overlayTimeout:break
case .ineligibleForOffer:break
case .unsupportedPlatform:break
case .overlayPresentedInBackgroundScene:break
@unknown default:break
}
}
}
}
class func restoreProduct(){
SwiftyStoreKit.restorePurchases(atomically: true, applicationUsername: "") { result in
if result.restoreFailedPurchases.count > 0 {
//恢复失败
print("restore Failed:\(result.restoredPurchases)")
}else if result.restoredPurchases.count > 0 {
print("restore Success")
verifyReceipt(service: .production, isRestor: true, product_id: "")
}else{
print("Nothing to Restore")
}
}
}
class func verifyReceipt(service:AppleReceiptValidator.VerifyReceiptURLType,isRestor:Bool,product_id:String){
let receipt = AppleReceiptValidator(service: service, sharedSecret: IAPPubKey)
SwiftyStoreKit.verifyReceipt(using: receipt) { result in
switch result {
case .success(let receipt):
let json = JSON(receipt)
let status = json["status"].intValue
if status == 0{
print("订单验证成功receipt = \(receipt)")
//验证是否过期
DispatchQueue.main.async {
let in_apps = json["latest_receipt_info"].arrayValue
for appJson in in_apps {
let expiresTime = appJson["expires_date_ms"].intValue
let productId = appJson["product_id"].stringValue
let current = GetTimestamp(nil) * 1000
if expiresTime > current {
print("订单验证OK,过期时间:\(expiresTime),当前时间:\(current)")
if isRestor {
// Restor success do something
}
}else{
// purchase success do something
}
}
}
}
}else if status == 21007 {
print("订单验证是沙盒模式-继续验证")
self.verifyReceipt(service: .sandbox, isRestor: isRestor, product_id: product_id)
}else if status == 21006{
print("收据是有效的,但订阅服务已经过期")
}else if status == 21003{
print("receipt无法通过验证")
}
case .error(let error):
print("订单验证失败:\(error.localizedDescription)")
}
}
}
}