Sell Products
There are two types of products you can sell, Consumables and Non-Consumables. Consumable in-app purchases are depleted as they are used and Can be purchased multiple times, for example, lives or gems in a game, or a boosts in a dating app. Non-consumable in-app purchases are purchased once and Can only be purchased once, for example, additional filters in a photo app, or cosmetic items in a game.
Create products
To offer in-app purchases inside your app, you need to add their information in App Store Connect.
- Go to App Store Connect.
- From My App, select your app.
- In the sidebar, under Features, select In-App Purchases.
- Click the add button (+).
- Select Consumable or Non-Consumable.
- Fill the form by adding the reference name and product ID.
- Click the Create button.
Validate receipts
An App Store receipt is a binary encrypted file signed with an Apple certificate. Your mobile application should receive the receipt once a successful purchase is made, and you should validate the receipt on your server to ensure that the purchase is valid.
You can verify the receipt using the Product
Facade as follows:
use Imdhemy\Purchases\Facades\Product;
use Exception;
// Verify the receipt on App Store
$receiptResponse = Product::appStore()->receiptData($receipt)->verifyReceipt();
// Check the values of the response to verify transactions as needed.
$receipt = $receiptResponse->getReceipt();
$inAppList = $receipt->getInApp(); // contains all purchased products
// Depending on your business, you can loop over all products, or just the first one.
$productPurchased = false;
foreach ($inAppList as $inApp) {
if ($inApp->getProductId() === $productId) {
$productPurchased = true;
break;
}
}
if ($productPurchased) {
// Do something with the product
}
throw new Exception('Product not purchased');
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.