gpt4 book ai didi

javascript - 使用 Cloud Code 验证自动续订订阅收据并更新 Parse

转载 作者:可可西里 更新时间:2023-11-01 06:05:46 26 4
gpt4 key购买 nike

我正在开发一个 iOS 应用程序,它使用 Parse 作为后端,用户使用 Apple 的自动续订订阅 IAP 进行订阅。在订阅和恢复购买功能期间,我将收据作为参数传递到 Cloud Code 函数中,然后通过 Parse 将其发布到 Apple 的服务器,如 this SO answer 中所述。 .

我想解析 HTTPResponse 并使用 JSON 收据的 expires_date 属性更新 Parse 中的用户对象。我认为我的 Cloud Code 函数一定有问题,因为我的控制台显示 Result: TypeError: Cannot read property 'status' of undefined,这意味着 HTTPResponse.data 未定义(?)。我的功能在下面,然后是控制台日志。

此外,我不是 100% 清楚我是否应该使用 receiptlatest_receiptlatest_receipt_info 来访问 expires_date 属性,所以我有一些额外的变量在那里只是为了测试结果。

(感谢您的帮助,因为 Cloud Code 是我编写过的唯一 JavaScript。)

Parse.Cloud.define('validateReceipt', function (request, response) {
var user = request.user;
var receiptAsBase64EncodedString = request.params.receiptData;

var postData = {
method: 'POST',
url: 'https://sandbox.itunes.apple.com/verifyReceipt',
body: { 'receipt-data': receiptAsBase64EncodedString,
'password': 'SECRET' }
}

Parse.Cloud.httpRequest(postData).then(function (httpResponse) {
// httpResponse is a Parse.Cloud.HTTPResponse

var json = httpResponse.data; // Response body as a JavaScript object.
var validationStatus = json.status; // App Store validation status code. If 0, the receipt is valid, but may be expired
var receiptJSON = json.receipt; // Receipt data as a JSON object
var latestReceipt = json.latest_receipt;
var latestReceiptInfo = json.latest_receipt_info;

console.log('JSON: ' + json);

console.log('Latest Receipt Info: ' + latestReceiptInfo);

console.log('Validation status: ' + validationStatus);

var now = new Date().getTime();
var expirationDate = latestReceiptInfo.expires_date;
console.log('Now: ' + now)
console.log('Expiration Date: ' + expirationDate)


if (expirationDate > now) { //updates expiration date
user.set("accountExpDate", expirationDate);
user.set("accountStatus", 1);
user.save();

return response.success('Subscription Active, expiration date: ' + expirationDate);

} else {
user.set("accountStatus", 2);
user.save();

return response.error('Subscription Expired, expiration date: ' + expirationDate);
}
})

});

控制台日志

E2015-06-28T22:48:51.919Z]v138 Ran cloud function validateReceipt for user pvoG72hGuw with:
Input: {"receiptData":"MII6bwYJKoZIhvcNAQcCoII6YDCCOlwCAQExCzAJBgUrDgMCGgUAMIIqIAYJKoZIhvcNAQcBoIIqEQSCKg0xgioJMAoCAQgCAQEEAhYAMAoCARQCAQEEAgwAMAsCAQECAQEEAwIBADALAgELAgEBBAMCAQAwCwIBDgIBAQQDAgFSMAsCAQ8CAQEEAwIBADALAgEQAgEBBAMCAQAwCwIBGQIBAQQDAgEDMAwCAQoCAQEEBBYCNCswDQIBAwIBAQQFDAMxLjAwDQIBDQIBAQQFAgMBOawwDQIBEwIBAQQFDAMxLjAwDgIBCQIBAQQGAgRQMjM0MBgCAQQCAQIEEB6oAGDFiz0t9OTppvGHMk4wGwIBAAIBAQQTDBFQcm9kdWN0aW9uU2FuZGJveDAcAgECAgEBBBQMEmNvLnByb3BlcmFwcHMuTG9mdDAcAgEFAgEBBBSFjiOc9pL+F1eDnK0S59Cv/fKyGDAeAgEMAgEBBBYWFDIwMTUtMDYtMjhUMjI6NDg6NDRaMB4CARICAQEEFhYUMjAxMy0wOC0wMVQwNzowMDowMFowRgIBBwIBAQQ+94xiArNdLTIUTMQebLCAxw5A6CJKxQPcT/kPPgN9TEatwcc63wvluCAiLWS9DM7CGrNUnGnPDou8MrAG3jkwRwIBBgIBAQQ/z23kkC0s7JavH... (truncated)
Result: TypeError: Cannot read property 'status' of undefined
at main.js:179:36
at e (Parse.js:3:8736)
at Parse.js:3:8185
at Array.forEach (native)
at Object.x.each.x.forEach [as _arrayEach] (Parse.js:1:661)
at c.extend.resolve (Parse.js:3:8136)
at Object.<anonymous> (<anonymous>:575:17)

编辑

console.log(JSON.stringify(httpResponse, null, 4)); 给了我

I2015-06-28T23:54:02.813Z]{
"uuid": "4ed0bfc8-b8c2-0815-e71c-a9b4c41e3a8d",
"status": 200,
"headers": {
"Date": "Sun, 28 Jun 2015 23:54:02 GMT",
"apple-timing-app": "6 ms",
"cache-control": "max-age=0",
"connection": "keep-alive",
"content-encoding": "gzip",
"content-length": "36",
"edge-control": "cache-maxage=0",
"expires": "Sun, 28 Jun 2015 23:54:02 GMT",
"itspod": "100",
"pod": "100",
"set-cookie": "mzf_dr=0; version=\"1\"; expires=Thu, 01-Jan-1970 00:00:00 GMT; path=/WebObjects; domain=.apple.com",
"x-apple-application-instance": "990263",
"x-apple-application-site": "SB",
"x-apple-jingle-correlation-key": "LTHOCJJYK6EOORRQRWZ6RNO6HU",
"x-apple-lokamai-no-cache": "true",
"x-apple-orig-url": "http://sandbox.itunes.apple.com/WebObjects/MZFinance.woa/wa/verifyReceipt",
"x-apple-translated-wo-url": "/WebObjects/MZFinance.woa/wa/verifyReceipt",
... (truncated)

最佳答案

试试这个:

var validationStatus = JSON.parse(httpResponse["text"])["status"];

为我解决了问题。

关于javascript - 使用 Cloud Code 验证自动续订订阅收据并更新 Parse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31105803/

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