gpt4 book ai didi

javascript - 我如何使用 Swift 将收据数据发送到 Node.js 服务器

转载 作者:行者123 更新时间:2023-11-28 13:53:11 28 4
gpt4 key购买 nike

我目前正在尝试验证 iOS 中用户订阅的 IAP 收据。首先,我发出一个发布请求,从当前存储在 Bundle.main.appStoreReceiptURL 中的收据中获取收据数据。我能够在我的服务器中使用此收据数据来验证收据是否运作良好。但是,此收据数据字符串在特定于特定用户的 Node 中进行了硬编码。根据要验证的任何用户,我如何才能将此收据字符串发送到我的服务器以获取任何收据。这是我的 Swift 和服务器代码。苹果在文档中说

To retrieve the receipt data, use the appStoreReceiptURL method of NSBundle to locate the app’s receipt, and then read the entire file. Send this data to your server—as with all interactions with your server, the details are your responsibility.

我想获取收据数据并将其发送到我的服务器。

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);
});

最佳答案

如果您打算使用您的服务器来验证收据(您应该这样做),那么 validatePurchaseReceipt 方法应该调用您的服务器,而不是 /verifyReceipt 端点.收据数据被传递到您的服务器,就像您使用 requestReceiptDict 一样。

此外,secretKey 应该在您的服务器上进行硬编码 - 而不是在客户端的任何地方。

这是一个类似的问题:https://stackoverflow.com/a/54261816/3166209

关于javascript - 我如何使用 Swift 将收据数据发送到 Node.js 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54288892/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com