我目前正在尝试验证IAP
在iOS中收到的用户订阅。首先,我发出了一个发布请求,该请求从当前存储在Bundle.main.appStoreReceiptURL
中的收据中获取收据数据。我可以在我的服务器中使用该收据数据来验证收据是否正常运行。但是,此收据数据字符串以硬编码在Node
中,特定于特定用户。我如何能够将此收据字符串发送到我的服务器以获取任何收据,具体取决于要验证的任何用户。这是我的Swift代码和服务器代码。苹果在文档中说
要检索收据数据,请使用NSBundle的appStoreReceiptURL方法找到应用程序的收据,然后读取整个文件。将这些数据发送到您的服务器-与与服务器的所有交互一样,详细信息是您的责任。
我想获取收据数据并将其发送到我的服务器。
func getReceipt() {
if let receiptUrl = receiptUrl {
do {
let purchaseReceipt = try Data(contentsOf: receiptUrl)
self.validatePurchaseReceipt(pReceiptData: purchaseReceipt)
} catch {
let receiptRefreshRequest = SKReceiptRefreshRequest(receiptProperties: nil)
receiptRefreshRequest.delegate = self
receiptRefreshRequest.start()
}
}
}
func validatePurchaseReceipt(pReceiptData: Data) {
let base64encodedReceipt = pReceiptData.base64EncodedString()
let secretKey = "myAppstoreConnectSecretKey"
let requestReceiptDict = ["receipt-data": base64encodedReceipt, "password": secretKey]
guard JSONSerialization.isValidJSONObject(requestReceiptDict) else { return }
do {
let data = try JSONSerialization.data(withJSONObject: requestReceiptDict)
let validationString = "https://sandbox.itunes.apple.com/verifyReceipt"
guard let validationUrl = URL(string: validationString) else { return }
let session = URLSession(configuration: .default)
var request = URLRequest(url: validationUrl, cachePolicy: .reloadIgnoringLocalCacheData)
request.httpMethod = "POST"
let task = session.uploadTask(with:request, from: data) { (data, response, error) in
guard let data = data, error == nil else { return }
do {
let purchaseReceiptJSON = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
print("Success retrieved json:\(purchaseReceiptJSON)")
} catch let error {
print(error)
}
}
task.resume()
} catch let error {
print(error)
}
}
这是我的服务器代码
const express = require('express');
const requirePromise = require('request-promise');
const app = express();
let verificationURL = 'https://sandbox.itunes.apple.com/verifyReceipt';
let secretKey = 'myAppstoreConnectSecretKey';
let receiptData = 'MIIntgYJKoZIhvcNAQcCoIInpzCCJ6MCAQExCzAJBgUrDgMCGgUAMIIXVwYJKoZIhvcNAQcBoIIXSASCFetc'
const options = {
url: verificationURL,
method: 'POST',
headers: {
'User-Agent': 'Request-Promise',
'Content-Type': 'application/x-www-form-urlencoded',
},
json: true
};
options.form = JSON.stringify({
'receipt-data': receiptData,
'password': secretKey
});
requirePromise(options).then((resData) => {
console.log(resData);
return resData;
}).catch(function (error) {
console.log(error);
});
答案 0 :(得分:1)
如果要使用服务器来验证收据(应该这样做),则validatePurchaseReceipt
方法应该调用服务器,而不是/verifyReceipt
端点。收据数据将完全像使用requestReceiptDict
一样传递到您的服务器。
此外,secretKey
应该在您的服务器上进行硬编码,而不是在客户端上的任何地方。