Verifying Auto Subscription for Renewable Signatures and Parse Updates Using Cloud Code

I am developing an iOS app that uses Parse as the backend and users subscribe using Apple IAP auto-signup. During the Purchase Subscription and Restore functions, I pass the receipt as a parameter in the Cloud Code function and then POST to the Apple server via Analyze as described in this SO answer .

I would like to parse the HTTPResponse and update the user object in Parse with a expires_date

JSON receipt property . I think my Cloud Code function should be a little bit because my console is showing Result: TypeError: Cannot read property 'status' of undefined

what means that HTTPResponse.data is undefined (?). My function is below, then console log.

Also, I'm not 100% sure if I should use receipt

, latest_receipt

or latest_receipt_info

for access to the property expires_date

, so I have more variables to see what comes out.

(Thanks for any help, as Cloud Code is the only JavaScript I've ever written).

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

});

      

Console log

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)

      

Edit

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

gave me

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)

      

+3


source to share


1 answer


Try the following:

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

      



Solved the problem for me.

+1


source







All Articles