6

I'm trying to implement an application with auto-renewable subscription. Users should pay to access all functions of my application. I already use Parse as backend for my application. It provides some API methods for inAppPurchases but there is nothing said about auto-renewable type. The only thing I have found is some two years old threads in the blog it is said that receipt verification was implemented only for downloadable purchases.

I have tried to use as it called in docs "Simple purchase" and it works fine but I can't figure out how can I check if my user already bought subscription or not.

Does anybody know is there way to do it via Parse API or This should implemented in another way?

Vitalii Boiarskyi
  • 1,363
  • 2
  • 10
  • 25

1 Answers1

4

As mentioned, receipt validation is only built into the Parse SDK for downloadable content, but it is fairly simple to to create a Cloud Code function that POSTs the app receipt to the iTunes Store for validation. Here are the Apple docs for server side validation: Validating Receipts with the App Store

Here is a what a basic function would look like:

Parse.Cloud.define('validateReceipt', function (request, response) {
    var receiptAsBase64EncodedString = request.params.receiptData;
    var postData = {
        method: 'POST',
        url: 'http://buy.itunes.apple.com/verifyReceipt',
        body: { 'receipt-data': receiptAsBase64EncodedString,
                'password': SHARED_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
        var receiptJSON = json.receipt; // Receipt data as a JSON object

        // TODO: You'll need to check the IAP receipts to retrieve the
        //       expiration date of the auto-renewing subscription, and
        //       determine if it is expired yet.
        var subscriptionIsActive = true;

       if (subscriptionIsActive) {
           return response.success('Subscription Active');
       }
       else {
           return response.error('Subscription Expired');
       }
    });
});

See Receipt Fields for details on interpreting the receipt JSON. It's fairly straight forward for iOS 7+, but auto-renewing subscription receipts for iOS 6 and earlier are tedious.

shyam
  • 9,134
  • 4
  • 29
  • 44
gyratory circus
  • 437
  • 6
  • 15
  • The above code didn't quite work for me; not sure if this is due to something new in Parse? Or maybe on Apple's side? (this post is two years old, so who knows?) To make it work, I had to explicitly set the header in `postData` to `{'Content-Type': 'application/json'}` – Anna Dickinson Jan 21 '17 at 02:22
  • @AnnaDickinson My hosted parse.com server still appears to be verifying receipts without content-type set here, so I bet its a change in Parse, and specifically I'll also assume you're using Parse-Server, as the hosted Parse.com is shutting down in 5 days. Because you can use any npm module in Parse-Server, I'd highly recommend using a different http request module which will offer a lot more flexibility than `Parse.Cloud.httpRequest`. Axios is my personal recommendation since its promise-based api fits well into cloud code. – gyratory circus Jan 23 '17 at 21:09