Blog Detail

25

Feb
Validate & Handle Products & Subscription Receipt in Laravel cover image

arrow_back Validate & Handle Products & Subscription Receipt in Laravel

Google Play and App Store provide the In-App Purchase (IAP) services. IAP can be used to sell a variety of content, including subscriptions, new features, and services. The purchase event and the payment process occur on and are handled by the mobile application (iOS and Android), then your backend needs to be informed about this purchase event to deliver the purchased product or update the user’s subscription state.

Laravel In-App purchase comes to help you to parse and validate the purchased products and handle the different states of a subscription, like New subscription, auto-renew, cancellation, expiration and etc.

Installation

You can install this package via composer:

composer require imdhemy/laravel-purchases

Next, you have to publish the config file:

php artisan vendor:publish --provider="Imdhemy\Purchases\PurchaseServiceProvider" --tag="config"

Sell Products

Google Products

You can use the \Imdhemy\Purchases\Facades\Product facade to acknowledge or to get the receipt data from Google Play as follows:

use \Imdhemy\Purchases\Facades\Product;
use \Imdhemy\GooglePlay\Products\ProductPurchase;

$itemId = 'product_id';
$token = 'purchase_token';

Product::googlePlay()->id($itemId)->token($token)->acknowledge();
// You can optionally submit a developer payload
Product::googlePlay()->id($itemId)->token($token)->acknowledge("your_developer_payload");

/** @var ProductPurchase $productReceipt */
$productReceipt = Product::googlePlay()->id($itemId)->token($token)->get();

Each key has a getter method prefixed with get, for example: getKind() to get the kind value. For more information check:

App Store Products

You can use the \Imdhemy\Purchases\Facades\Product to send a verifyReceipt request to the App Store. as follows:

use Imdhemy\AppStore\Receipts\ReceiptResponse;
use \Imdhemy\Purchases\Facades\Product;

$receiptData = 'the_base64_encoded_receipt_data';
/** @var ReceiptResponse $receiptResponse */
$receiptResponse = Product::appStore()->receiptData($receiptData)->verifyReceipt();

As usual, each key has a getter method. For more information check:

Sell Subscriptions

Google Play Subscriptions

You can use the \Imdhemy\Purchases\Facades\Subscription facade to acknowledge or to get the receipt data from Google Play as follows:

use Imdhemy\GooglePlay\Subscriptions\SubscriptionPurchase;
use Imdhemy\Purchases\Facades\Subscription;

$itemId = 'product_id';
$token = 'purchase_token';

Subscription::googlePlay()->id($itemId)->token($token)->acknowledge();
// You can optionally submit a developer payload
Subscription::googlePlay()->id($itemId)->token($token)->acknowledge("your_developer_payload");

/** @var SubscriptionPurchase $subscriptionReceipt */
$subscriptionReceipt = Subscription::googlePlay()->id($itemId)->token($token)->get();
// You can optionally override the package name
Subscription::googlePlay()->packageName('com.example.name')->id($itemId)->token($token)->get();

The SubscriptionPurchase resource indicates the status of a user’s in-app product purchase. This is its JSON Representation:

For more information check:

App Store Subscriptions

You can use the \Imdhemy\Purchases\Facades\Subscription to send a verifyReceipt request to the App Store. as follows:

use Imdhemy\Purchases\Facades\Subscription;
// To verify a subscription receipt
$receiptData = 'the_base64_encoded_receipt_data';
$receiptResponse = Subscription::appStore()->receiptData($receiptData)->verifyReceipt();

// If the subscription is an auto-renewable one, 
//call the renewable() method before the trigger method verifyReceipt()
$receiptResponse = Subscription::appStore()->receiptData($receiptData)->renewable()->verifyReceipt();

// or you can omit the renewable() method and use the verifyRenewable() method instead
$receiptResponse = Subscription::appStore()->receiptData($receiptData)->verifyRenewable();

For more information check:

This package has a lot more features, If you want to dig more you can visit its complete documentation on Github.

Published at : 25-02-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project