【发布时间】:2015-11-24 08:01:23
【问题描述】:
我一直在尝试在我的 spritekit 游戏中实现收据验证。我一直在关注各种教程,基本上以这段代码结束
enum RequestURL: String {
case production = "https://buy.itunes.apple.com/verifyReceipt"
case sandbox = "https://sandbox.itunes.apple.com/verifyReceipt"
case myServer = "my server address"
}
enum ReceiptStatusCode: Int {
// Not decodable status
case unknown = -2
// No status returned
case none = -1
// valid status
case valid = 0
// The App Store could not read the JSON object you provided.
case JSONNotReadable = 21000
// The data in the receipt-data property was malformed or missing.
case malformedOrMissingData = 21002
// The receipt could not be authenticated.
case receiptCouldNotBeAuthenticated = 21003
// The shared secret you provided does not match the shared secret on file for your account.
// Only returned for iOS 6 style transaction receipts for auto-renewable subscriptions.
case sharedSecretNotMatching = 21004
// The receipt server is currently not available.
case receiptServerUnavailable = 21005
// This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response.
// Only returned for iOS 6 style transaction receipts for auto-renewable subscriptions.
case subscriptionExpired = 21006
// This receipt is from the test environment, but it was sent to the production environment for verification. Send it to the test environment instead.
case testReceipt = 21007
// This receipt is from the production environment, but it was sent to the test environment for verification. Send it to the production environment instead.
case productionEnvironment = 21008
}
func validateReceipt(forTransaction transaction: SKPaymentTransaction) {
guard let receiptURL = NSBundle.mainBundle().appStoreReceiptURL else { return }
guard let receipt = NSData(contentsOfURL: receiptURL) else { return }
let receiptData = receipt.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
let payload = ["receipt-data": receiptData]
var receiptPayloadData: NSData?
do {
receiptPayloadData = try NSJSONSerialization.dataWithJSONObject(payload, options: NSJSONWritingOptions(rawValue: 0))
}
catch let error as NSError {
print(error.localizedDescription)
return
}
guard let payloadData = receiptPayloadData else { return }
guard let requestURL = NSURL(string: RequestURL.sandbox.rawValue) else { return }
let request = NSMutableURLRequest(URL: requestURL)
request.HTTPMethod = "POST"
request.HTTPBody = payloadData
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in
if let error = error {
print(error.localizedDescription)
return
}
guard let data = data else { return }
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves) as? NSDictionary
guard let json = jsonData else { return }
// Correct ?
guard let status = json["status"] as? Int where status == ReceiptStatusCode.valid.rawValue else { return }
// Unlock product here?
// Other checks needed?
}
catch let error as NSError {
print(error.localizedDescription)
return
}
}
task.resume()
}
它是一个漂亮的样板代码,可以按预期工作。我现在的问题是我不知道如何在最后一步(标记线)实际验证收据。 我相信我现在必须携带 5 张左右的支票来验证收据。我只是不知道他们中的大多数人将如何迅速完成。大多数教程要么是旧的,不包括这一步,要么不是用 swift 编写的。
如果有人成功使用收据验证可以帮助我朝着正确的方向前进,我们将不胜感激。非常感谢
更新:
在得到 JSA986I 和 cbartel 的出色回答后,我将其变成了 github 上的助手。非常感谢您的帮助
【问题讨论】:
-
你能得到这个工作吗?如果是这样,您介意分享您完成的代码吗?我现在正在解决同样的问题。非常感谢!
-
嘿,很遗憾我现在已经放弃了收据验证。上面的代码是正确的代码减去我为 swift 2 所做的一些更改(使用保护语句)。我只是不明白最后一点验证,关于这个的教程很少。
-
感谢您的回复。我已经取得了一些进展,如果我能够取得更多进展,我会在这里分享代码。
-
@crashoverride777 - 感谢您创建了一个很棒的帮助文件。助手工作出色。但是,沙盒测试失败,错误代码为 21002。经过调试和花费大量时间。知道这是由于“付费应用”协议状态不是“活动”而发生的。如果您可以在使用该实用程序之前记录检查协议状态,一定会有所帮助!
标签: swift validation in-app-purchase receipt