我正在开发一个iOS应用程序,使用Parse作为后端,用户使用苹果的自动续订订阅IAP进行订阅。在订阅和恢复购买功能期间,我将收据作为参数传递给Cloud Code函数,然后通过Parse将其POST到苹果服务器,如此SO答案所述。
我想解析HTTPResponse并使用JSON收据的
此外,我不确定是否应该使用
(感谢任何帮助,因为Cloud Code是我写过的唯一JavaScript。)
我想解析HTTPResponse并使用JSON收据的
expires_date
属性更新Parse中的用户对象。我认为我的Cloud Code函数可能有点问题,因为我的控制台显示Result: TypeError: Cannot read property 'status' of undefined
,这意味着HTTPResponse.data未定义(?)。我的函数如下,后面是控制台日志。此外,我不确定是否应该使用
receipt
,latest_receipt
还是latest_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)