我使用以下函数来验证收据。如您所见,我使用了沙盒 URL,因为我在将接收对象提交到商店之前对其进行了测试。我想学习的是:在提交之前,我应该在storeUrl部分写什么?如何访问或找到该 URL?根据我的研究,我需要输入我的服务器地址或其他东西,但我想提交的产品是非消耗品,所以我没有任何服务器。
func verifyReciept (transaction : SKPaymentTransaction?) {
let recieptURL = Bundle.main.appStoreReceiptURL!
if let reciept = NSData(contentsOf: recieptURL){
let requestContents = ["receipt-data" : reciept.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue:0))]
do {
let requestData = try JSONSerialization.data(withJSONObject: requestContents, options: JSONSerialization.WritingOptions(rawValue : 0))
let storeURL = NSURL(string: "https:/sandbox.itunes.apple.com/verifyReceipt")
let request = NSMutableURLRequest(url: storeURL! as URL)
request.httpMethod = "Post"
request.httpBody = requestData
let session = URLSession.shared
let task = session.dataTask(with: request as URLRequest, completionHandler: { (responseData: Data?, response : URLResponse?, error : Error?) -> Void in
do {
let json = try JSONSerialization.jsonObject(with: responseData!, options: .mutableLeaves) as! NSDictionary
print(json)
if(json.object(forKey: "status") as! NSNumber) == 0 {
let receipt_dict = json["receipt"] as! NSDictionary
if let purchases = receipt_dict["in_app"] as? NSArray {
self.validatePurchaseArray(purchases: purchases)
}
if transaction != nil {
SKPaymentQueue.default().finishTransaction(transaction!)
}
}
else {
print(json.object(forKey: "status") as! NSNumber)
}
}
catch{
print(error)
}
})
task.resume()
} catch {
print(error)
}
}
else {
print("No Reciept")
}
}
您应该将其替换为用于验证收据的产品 URL:
"https://buy.itunes.apple.com/verifyReceipt"
您正在谈论的涉及服务器的事情是您可以采取的额外措施。对于服务器端验证,您需要将收据发送到服务器,然后从服务器运行相同的功能。