I have been trying to test In App purchases (Auto Renewable Subscriptions) to be specific and I always see "The receipt is not valid"
In this regard, as soon as the purchase completes I would like to write true
to a bool value called premium
I have first tried to check whether the user is already subscribed at app launch and do the same logic if the user is subscribed (unlock premium)
Here is my code for the same
application didFinishlaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// Init other stuff
Purchases.configure(withAPIKey: Config.REVENUE_CAT_API_KEY)
checkAllPurchases()
}
checkAllPurchases()
func checkAllPurchases(){
Purchases.shared.purchaserInfo { (purchaseInfo, err) in
print("Purchase info :", purchaseInfo?.entitlements.all)
if(err != nil){
if purchaseInfo?.entitlements["allaccess"]?.isActive == true {
UserDefaults.standard.setValue(true, forKey: "premium")
}
}
else{
self.purchaseError = err?.localizedDescription ?? ""
//print(err?.localizedDescription)
}
}
}
purchase()
This gets called when the buy button is clicked
func purchase (productId : String?){
guard productId != nil else {
return
}
var skProduct : SKProduct?
Purchases.shared.products([productId!]) { (skProducts) in
if !skProducts.isEmpty{
skProduct = skProducts[0]
print("SKProduct:", skProducts[0].productIdentifier)
Purchases.shared.purchaseProduct(skProduct!) { (transaction, purchaseInfo, error, userCancelled) in
// If successfull purchase
if (error == nil && !userCancelled){
UserDefaults.standard.setValue(true, forKey: "premium")
}
else if (error != nil && !userCancelled){
self.purchaseError = error?.localizedDescription ?? ""
if let err = error as NSError? {
print("Error: \(err.userInfo)")
print("Message: \(err.localizedDescription)")
print("Underlying Error: \(String(describing: err.userInfo[NSUnderlyingErrorKey]))")
}
}
}
}
}
}
There is an open issue Here but seems like it is only the case with StoreKit file and not the physical sandbox testing I have this issue for both the cases and now I don't know how to test my in app purchases