title: Validating auto-renewable subscription receipt and updating Parse using Cloud Code
body: <p>I am developing an iOS app that uses Parse as the backend and users subscribe using Apple's auto-renewable subscription IAP. During the Subscribe and Restore Purchases functions, I pass the receipt as a parameter in a Cloud Code function and then POST that to Apple's server via Parse, as described in <a href="https://stackoverflow.com/a/28553342/4139760">this SO answer</a>.</p>
  
  <p>I would like to parse the HTTPResponse and update the user object in Parse with the <code>expires_date</code> property of the JSON receipt. I think my Cloud Code function must be off a bit, because my console shows <code>Result: TypeError: Cannot read property 'status' of undefined</code>, meaning that HTTPResponse.data is undefined(?). My function is below, followed by the console log. </p>
  
  <p>Also, I'm not 100% clear on whether I should be using <code>receipt</code>, <code>latest_receipt</code>, or <code>latest_receipt_info</code> for accessing the <code>expires_date</code> property, so I have some extra variables in there just to test what comes out.</p>
  
  <p>(Thanks for any help as Cloud Code is the only JavaScript that I've ever written.)</p>
  
  <pre><code>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 &gt; 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);
      }
  })
  
  });
  </code></pre>
  
  <p><strong>Console Log</strong></p>
  
  <pre><code>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.&lt;anonymous&gt; (&lt;anonymous&gt;:575:17)
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p><code>console.log(JSON.stringify(httpResponse, null, 4));</code> gave me</p>
  
  <pre><code>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)
  </code></pre>