busuu/ios-receipts-api

1.1.2 2017-06-19 13:14 UTC

README

A library to get information on auto-renewing subscriptions from the App Store receipts API, as described in the IOS documentation.

This is useful for:

  • maintaining on your server an up-to-date database of your IOS users' auto-renewing subscriptions
  • giving cross-platform access to users based on their IOS purchases
  • knowing the type of plans a user has subscribed to, when is the plan expiration date, has the user requested a cancellation of the subscription

To retrieve information about a user's purchases from the App store, a receipt for this user must be sent to the App store. This receipt is stored as a file on the user's device. This receipt must then be sent from the IOS device to your server and stored by you, so that it can be used to retrieve the user's purchases afterward.

Features

  • Abstracts the complexity of interacting with IOS receipts
  • Extensively unit tested and used in production at Busuu, a language learning platform with 60M users
  • Follows PSR-4 conventions and coding standard: autoload friendly

Requirements

  • PHP >= 5.5
  • Guzzle library,
  • (optional) PHPUnit to run tests.

Installation

We recommend using composer to install this library.

Run this commands to install composer if you don't already have it:

$ curl -sS https://getcomposer.org/installer | php

Then install this library with:

$ php composer.phar require busuu/ios-receipts-api

Or edit composer.json and add:

{
    "require": {
        "busuu/ios-receipts-api": "~1.0"
    }
}

Basic usage

<?php

// This file is generated by Composer
require_once 'vendor/autoload.php';

$client = \Busuu\IosReceiptsApi\Factory\ReceiptServiceFactory::createReceiptService('your_apple_shared_secret');
$client->setReceiptData('receipt_data');
$purchase = $client->getLastPurchase();
echo $purchase->getExpiresDate();

You can generate your apple shared secret from iTunes Connect, see official documentation. In this example, "receipt_data" is the encoded receipt data that was sent by your app. This string should be base64-encoded by the app before being sent to the server. For more information read the following paragraph.

About the receipt data

Here's an excerpt from the official IOS documentation relative to sending the receipt data from the app to your server (source).

Read the Receipt Data

To retrieve the receipt data, use the appStoreReceiptURL method of NSBundle to locate the app’s receipt, and then read the entire file. If the appStoreReceiptURL method is not available, you can fall back to the value of a transaction's transactionReceipt property for backward compatibility. Then send this data to your server—as with all interactions with your server, the details are your responsibility.

// Load the receipt from the app bundle.
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
if (!receipt) { /* No local receipt -- handle the error. */ }

/* ... Send the receipt data to your server ... */```

Possible issues

This method of validating IOS purchases relies on the API returning information about payments more recent than the generation date of receipt that was sent to the API. This API feature has been deprecated since IOS 7 and it's unclear if Apple will go forward with removing it entirely or if they will provide a replacement API for it. As far as we know, some major businesses rely on this feature to provide cross-platform subscriptions, so it seems unlikely to sudenly disappear. See relevant discussion.