kullanıcı satın almayı gerçekleştirdikten sonra itunes verifyReceipt sunucusuna bağlanarak gerçekleşen transaction'u alabilir ve kendi veritabanınızda bu işlemle ilgili bir kayıt tutabilirsiniz.
sandbox'da: https://sandbox.itunes.apple.com/verifyReceipt
production'da: https://buy.itunes.apple.com/verifyReceipt
sunucu adreslerini kullanıyoruz.
puschase işlemini kontrol ettiğimiz delegate metodu olan
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!)
içerisinde SKPaymentTransactionState.Purchased veya SKPaymentTransactionState.Restored olarak gerçekleştiğinde bu validate işlemini yapmalıyız.
kendi yazdığım ReceiptValidation class'ını aşağıda paylaşıyorum
class ReceiptValidation:NSObject {
private class func getReceiptInJsonFormat() -> NSData! {
var jsonData:NSData! = nil
if let receiptURL:NSURL = NSBundle.mainBundle().appStoreReceiptURL {
if let receipt:NSData = NSData(contentsOfURL: receiptURL) {
let encodedReceipt:NSString = receipt.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
let jsonStructure:NSDictionary = ["receipt-data":encodedReceipt]
jsonData = NSJSONSerialization.dataWithJSONObject(jsonStructure, options: NSJSONWritingOptions.allZeros, error: nil)!
}
}
return jsonData
}
class func validateReceipt(completionHandler:(data: NSDictionary!) -> Void) {
if let receiptValidationData:NSData = getReceiptInJsonFormat() {
let baseURL:NSURL = NSURL(string:"https://sandbox.itunes.apple.com/verifyReceipt")!
// let baseURL:NSURL = NSURL(string:"https://buy.itunes.apple.com/verifyReceipt")! // ********* change receipt validation address when app in production *********************
let request:NSMutableURLRequest = NSMutableURLRequest(URL: baseURL)
request.HTTPMethod = "POST"
request.HTTPBody = receiptValidationData
let operation:AFHTTPRequestOperation = AFHTTPRequestOperation(request: request)
operation.responseSerializer = AFHTTPResponseSerializer() as AFHTTPResponseSerializer
operation.setCompletionBlockWithSuccess({ (operation:AFHTTPRequestOperation!, data:AnyObject!) -> Void in
let convertedData:NSData = data as! NSData
var receivedData:NSDictionary = NSJSONSerialization.JSONObjectWithData(convertedData, options: NSJSONReadingOptions.MutableLeaves, error: nil) as! NSDictionary
completionHandler(data: receivedData)
}, failure: { (operation:AFHTTPRequestOperation!, error:NSError!) -> Void in
completionHandler(data: nil)
})
operation.start()
}
}
}
aşağıdaki örnekte'de func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) içerisinde bu class'ın nasıl handle edildiğini görebilirsiniz.
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
for transaction in transactions as! [SKPaymentTransaction] {
switch transaction.transactionState {
case SKPaymentTransactionState.Purchased:
println("purchase complete")
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
SKPaymentQueue.defaultQueue().removeTransactionObserver(self)
purchased(false)
break
case SKPaymentTransactionState.Failed:
println("purchase failed with error:\(transaction.error.localizedDescription)")
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
SKPaymentQueue.defaultQueue().removeTransactionObserver(self)
break
case SKPaymentTransactionState.Restored:
println("restore complete")
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
purchased(true)
break
case SKPaymentTransactionState.Purchasing:
println("purchasing in progress")
break
default:
break
}
}
}
func purchased(isRestored:Bool) {
println("purchase validation has started")
ReceiptValidation.validateReceipt { (data) -> Void in
if data != nil {
if let status:Int = data["status"] as? Int {
if status == 0 {
let receiptString:String = String(format: "%@", data) //aldığımız validation string'i. Bunu sunucumuza gönderebiliriz.
if isRestored {
println("restore action validated")
} else {
println("purchase action validated")
}
} else {
println("purchase validation error:\(status)")
}
} else {
println("purchase validation error")
}
} else {
println("purchase validation error")
}
}
}