Skip to main content

Sell Subscriptions

There are two types of subscriptions, Auto-renewable and Non-renewable subscriptions. The former provides ongoing access to the content users purchase, it allows Family sharing, and it can be automatically renewed. The latter provides access to the content for a limited period of time, and it cannot be renewed. So people need to purchase a new subscription to continue enjoying the content.

Receipt Validation

An App Store receipt is a binary encrypted file singed with an Apple certificate. Your mobile application should receive the receipt once a successful purchase is made. The receipt should be validated against the Apple App Store on your server, and here LIAP comes into play.

You can verify the receipt using the Subscription Facade as follows:

use Imdhemy\Purchases\Facades\Subscription;

// Verifiy the receipt on App Store servers.
// For non-renewable subscriptions, you can use the `verifyReceipt` method.
// Subscription::appStore()->receiptData($receipt)->verifyReceipt();
$receiptResponse = Subscription::appStore()->receiptData($receipt)->verify();

// Get the receipt status
$receiptStatus = $receiptResponse->getStatus();

if($receiptStatus->isValid()) {
$latestReceiptInfo = $receiptResponse->getLatestReceiptInfo();
// You can loop all of them or either get the first one (recently purchased).
$receiptInfo = $latestReceiptInfo[0];

$productId = $receiptInfo->getProductId();
$transactionId = $receiptInfo->getTransactionId();
$originalTransactionId = $receiptInfo->getOriginalTransactionId();
$expiresDate = $receiptInfo->getExpiresDate();
// And so on...
} else {
// The receipt is invalid
}

The getLatestReceiptInfo() method returns an array of objects containing all the transactions for the subscription, including the initial purchase, and subsequent renewals. You can use the values to determine if the subscription expired or not, Just use the getter methods to get the values.

caution

Code samples above are for guidance only. You should maintain your own business logic.

Available Methods

The returned object from the validation is an instance of \Imdhemy\AppStore\Receipts\ReceiptResponse, which represents the response body returned from App Store.

For any property, describe in the App Store documentation, there is a getter method that returns its value. The getter method names are the same as the property names, except that are prefixed with get and are camelCase.

For example, the getEnvironment() method returns the environment property.

If the property is an array/JSON object, the returned value will be an instance of a class that represents that object. The same idea of getter methods applies to the returned class.