Skip to content

Instantly share code, notes, and snippets.

@ren6
Last active Dec 16, 2020
Embed
What would you like to do?
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()
}
@linktoshubham
Copy link

linktoshubham commented Jul 5, 2020

Hi @ren6
My app got rejected with the below message.

Guideline 5.6 - Developer Code of Conduct

We noticed your app attempts to manipulate customers into making unwanted in-app purchases. Specifically, users are required to log into their iTunes account to make an in-app purchase at login
Scenario-
The app is enabled with Auto renewable subscription with 7 days free trial period. To check the eligibility of the user's free trial period I need to send a receipt to the server before displaying the free trial period message to the user. As mentioned here
To do so when I'm going to access the receipt from appStoreReceiptURL which returns nil because the user is not login to their iTunes account or sandbox account initially. So when appStoreReceiptURL returns nil, I use to refresh the receipt from SKReceiptRefreshRequest which opens the iTunes Account credentials popup without intimating the user.

So I guess this was the reason for rejection of the app.

How do I come to know that a user is eligible for a free trial period?

Is this the right way of implementation?

Please help me to resolve this issue.

Loading

@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.

Loading

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