Skip to content

Instantly share code, notes, and snippets.

@ren6
Last active December 16, 2020 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ren6/2332351efc12a8140f25071be6f8d39f to your computer and use it in GitHub Desktop.
Save ren6/2332351efc12a8140f25071be6f8d39f to your computer and use it in GitHub Desktop.
Determining eligibility for introductory offer
func isEligibleForIntroductory(callback: @escaping (Bool) -> Void){
guard let receiptUrl = Bundle.main.appStoreReceiptURL else {
callback(true)
return
}
#if DEBUG
let urlString = "https://sandbox.itunes.apple.com/verifyReceipt"
#else
let urlString = "https://buy.itunes.apple.com/verifyReceipt"
#endif
let receiptData = try? Data(contentsOf: receiptUrl).base64EncodedString()
let shared_secret = "YOUR_SHARED_SECRET"
let requestData = ["receipt-data" : receiptData ?? "", "password" : shared_secret, "exclude-old-transactions" : false] as [String : Any]
var request = URLRequest(url: URL(string: urlString)!)
request.httpMethod = "POST"
request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
let httpBody = try? JSONSerialization.data(withJSONObject: requestData, options: [])
request.httpBody = httpBody
URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String : AnyHashable], let receipts_array = json["latest_receipt_info"] as? [[String : AnyHashable]] else {
callback(true)
return
}
var latestExpiresDate = Date(timeIntervalSince1970: 0)
let formatter = DateFormatter()
for receipt in receipts_array {
let used_trial : Bool = receipt["is_trial_period"] as? Bool ?? false || (receipt["is_trial_period"] as? NSString)?.boolValue ?? false
let used_intro : Bool = receipt["is_in_intro_offer_period"] as? Bool ?? false || (receipt["is_in_intro_offer_period"] as? NSString)?.boolValue ?? false
if used_trial || used_intro {
callback(false)
return
}
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
if let expiresDateString = receipt["expires_date"] as? String, let date = formatter.date(from: expiresDateString) {
if date > latestExpiresDate {
latestExpiresDate = date
}
}
}
if latestExpiresDate > Date() {
callback(false)
} else {
callback(true)
}
}.resume()
}
@KiGi
Copy link

KiGi commented Dec 16, 2020

@linktoshubham - in the Sandbox it seems like receipts are no present until a purchase is made, but in production after an app is installed there should be a receipt. As you saw, calling SKReceiptRefreshRequest prompts for a user password, so generally do not use that unless you are at a point where the user was prompted for an iTunes password already. If there is no receipt, you just have to assume the user is not valid for an introductory offer and display the normal price - if the offer actually applied to them, they would be charged the offer price instead. But, as I said there should be a receipt normally before any purchase is made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment