Created
September 16, 2015 19:35
-
-
Save mhomol/85881b6ac9c0d6714796 to your computer and use it in GitHub Desktop.
Bag Labs Post - In-App Purchases - Getting the Receipt Section
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func validateReceipt() | |
{ | |
//Get the Path to the receipt | |
var receiptUrl = NSBundle.mainBundle().appStoreReceiptURL | |
//Check if it's actually there | |
if NSFileManager.defaultManager().fileExistsAtPath(receiptUrl!.path!) | |
{ | |
//Now lets do some validating | |
self.validateReceiptAtURL(receiptUrl!) | |
} | |
else | |
{ | |
//Try and refresh the receipt since we couldn't find one | |
//Make sure to conform to SKRequestDelegate | |
var refreshRequest = SKReceiptRefreshRequest() | |
refreshRequest.delegate = self | |
refreshRequest.start() | |
println("Did not find a receipt, refreshing receipt") | |
} | |
} | |
func validateReceiptAtURL(receiptUrl:NSURL) | |
{ | |
var response: NSURLResponse? | |
var err: NSError? | |
var receipt: NSData = NSData(contentsOfURL:receiptUrl, options: nil, error: nil)! | |
var receiptdata: String = receipt.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)) | |
var requestData:[String: AnyObject] = ["receipt-data" : receiptdata] | |
var jsonRequest = NSJSONSerialization.dataWithJSONObject(requestData, options: nil, error: &err) | |
//NOTE: Make sure you change out the URL for production!!! | |
//Production URL: https://buy.itunes.apple.com/verifyReceipt | |
var request = NSMutableURLRequest(URL: NSURL(string: "https://sandbox.itunes.apple.com/verifyReceipt")!) | |
var session = NSURLSession.sharedSession() | |
request.HTTPMethod = "POST" | |
request.HTTPBody = jsonRequest! | |
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in | |
var err: NSError? | |
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary | |
if(err != nil) | |
{ | |
println(err!.localizedDescription) | |
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding) | |
println("Error could not parse JSON: '\(jsonStr)'") | |
} | |
else | |
{ | |
if let parseJSON = json | |
{ | |
//Start parsing the JSON | |
} | |
else | |
{ | |
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding) | |
println("Receipt Error: \(jsonStr)") | |
} | |
} | |
}) | |
task.resume() | |
} | |
//SKRequestDelegate Methods | |
func requestDidFinish(request: SKRequest!) | |
{ | |
//Get the Path to the receipt | |
var receiptUrl = NSBundle.mainBundle().appStoreReceiptURL | |
//Check if it's actually there | |
if NSFileManager.defaultManager().fileExistsAtPath(receiptUrl!.path!) | |
{ | |
println("Just refreshed and now we have a receipt") | |
} | |
else | |
{ | |
println("Still did not find a receipt") | |
} | |
} | |
func request(request: SKRequest!, didFailWithError error: NSError!) | |
{ | |
println(error) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment