• Stars
    star
    633
  • Rank 70,793 (Top 2 %)
  • Language
    PHP
  • License
    Apache License 2.0
  • Created almost 11 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PHP receipt validator for Apple iTunes, Google Play and Amazon App Store

store-receipt-validator

Latest Stable Version Composer Downloads Build Status Code Coverage Scrutinizer Code Quality StyleCI License

PHP receipt validator for Apple iTunes, Google Play and Amazon App Store

Requirements

  • PHP >= 7.3

Installation

composer require aporat/store-receipt-validator

Quick Example

iTunes

use ReceiptValidator\iTunes\Validator as iTunesValidator;

$validator = new iTunesValidator(iTunesValidator::ENDPOINT_PRODUCTION); // Or iTunesValidator::ENDPOINT_SANDBOX if sandbox testing

$receiptBase64Data = 'ewoJInNpZ25hdHVyZSIgPSAiQXBNVUJDODZBbHpOaWtWNVl0clpBTWlKUWJLOEVkZVhrNjNrV0JBWHpsQzhkWEd1anE0N1puSVlLb0ZFMW9OL0ZTOGNYbEZmcDlZWHQ5aU1CZEwyNTBsUlJtaU5HYnloaXRyeVlWQVFvcmkzMlc5YVIwVDhML2FZVkJkZlcrT3kvUXlQWkVtb05LeGhudDJXTlNVRG9VaFo4Wis0cFA3MHBlNWtVUWxiZElWaEFBQURWekNDQTFNd2dnSTdvQU1DQVFJQ0NHVVVrVTNaV0FTMU1BMEdDU3FHU0liM0RRRUJCUVVBTUg4eEN6QUpCZ05WQkFZVEFsVlRNUk13RVFZRFZRUUtEQXBCY0hCc1pTQkpibU11TVNZd0pBWURWUVFMREIxQmNIQnNaU0JEWlhKMGFXWnBZMkYwYVc5dUlFRjFkR2h2Y21sMGVURXpNREVHQTFVRUF3d3FRWEJ3YkdVZ2FWUjFibVZ6SUZOMGIzSmxJRU5sY25ScFptbGpZWFJwYjI0Z1FYVjBhRzl5YVhSNU1CNFhEVEE1TURZeE5USXlNRFUxTmxvWERURTBNRFl4TkRJeU1EVTFObG93WkRFak1DRUdBMVVFQXd3YVVIVnlZMmhoYzJWU1pX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U0FpTWpBeE1pMHdOQzB6TUNBeE5Ub3dOVG8xTlNCRmRHTXZSMDFVSWpzS2ZRPT0iOwoJImVudmlyb25tZW50IiA9ICJTYW5kYm94IjsKCSJwb2QiID0gIjEwMCI7Cgkic2lnbmluZy1zdGF0dXMiID0gIjAiOwp9';

try {
  $response = $validator->setReceiptData($receiptBase64Data)->validate();
  // $sharedSecret = '1234...'; // Generated in iTunes Connect's In-App Purchase menu
  // $response = $validator->setSharedSecret($sharedSecret)->setReceiptData($receiptBase64Data)->validate(); // use setSharedSecret() if for recurring subscriptions
} catch (Exception $e) {
  echo 'got error = ' . $e->getMessage() . PHP_EOL;
}

if ($response->isValid()) {
  echo 'Receipt is valid.' . PHP_EOL;
  echo 'Receipt data = ' . print_r($response->getReceipt()) . PHP_EOL;
  
  foreach ($response->getPurchases() as $purchase) {
    echo 'getProductId: ' . $purchase->getProductId() . PHP_EOL;
    echo 'getTransactionId: ' . $purchase->getTransactionId() . PHP_EOL;

    if ($purchase->getPurchaseDate() != null) {
      echo 'getPurchaseDate: ' . $purchase->getPurchaseDate()->toIso8601String() . PHP_EOL;
    }
  }
} else {
  echo 'Receipt is not valid.' . PHP_EOL;
  echo 'Receipt result code = ' . $response->getResultCode() . PHP_EOL;
}

Play Store

Get the refresh token from OAuth2 flow.

use ReceiptValidator\GooglePlay\Validator as PlayValidator;

$client = new \Google_Client();
$client->setApplicationName('...');
$client->setAuthConfig('...');
$client->setScopes('...');

$validator = new PlayValidator(new \Google\Service\AndroidPublisher($client));

try {
  $response = $validator->setPackageName('PACKAGE_NAME')
    ->setProductId('PRODUCT_ID')
    ->setPurchaseToken('PURCHASE_TOKEN')
    ->validatePurchase();
} catch (Exception $e){
  var_dump($e->getMessage());
  // example message: Error calling GET ....: (404) Product not found for this application.
}
// success

Or Using a service account

Create service account Service Account flow and guide

$googleClient = new \Google_Client();
$googleClient->setScopes([\Google\Service\AndroidPublisher::ANDROIDPUBLISHER]);
$googleClient->setApplicationName('Your_Purchase_Validator_Name');
$googleClient->setAuthConfig($pathToServiceAccountJsonFile);

$googleAndroidPublisher = new \Google\Service\AndroidPublisher($googleClient);
$validator = new \ReceiptValidator\GooglePlay\Validator($googleAndroidPublisher);

try {
  $response = $validator->setPackageName('PACKAGE_NAME')
      ->setProductId('PRODUCT_ID')
      ->setPurchaseToken('PURCHASE_TOKEN')
      ->validateSubscription();
} catch (\Exception $e){
  var_dump($e->getMessage());
  // example message: Error calling GET ....: (404) Product not found for this application.
}
// success

Reduce the size of the google sdk

To reduce the size of the google sdk you can follow thoses steps on the google documentation

{
    "scripts": {
        "post-install-cmd": [
            "Google_Task_Composer::cleanup"
        ],
        "post-update-cmd": [
            "Google_Task_Composer::cleanup"
        ]
    },
    "extra": {
        "google/apiclient-services": [
            "AndroidPublisher"
        ]
    }
}

IMPORTANT: If you add any services back in composer.json, you will need to remove the vendor/google/apiclient-services directory explicity for the change you made to have effect:

rm -r vendor/google/apiclient-services
composer update

Amazon App Store

use ReceiptValidator\Amazon\Validator as AmazonValidator;
use ReceiptValidator\Amazon\Response as ValidatorResponse;

$validator = new AmazonValidator;

$response = null;
try {
  $response = $validator->setDeveloperSecret("DEVELOPER_SECRET")->setReceiptId("RECEIPT_ID")->setUserId("USER_ID")->validate();

} catch (Exception $e) {
  echo 'got error = ' . $e->getMessage() . PHP_EOL;
}

if ($response instanceof ValidatorResponse && $response->isValid()) {

  echo 'Receipt is valid.' . PHP_EOL;


  foreach ($response->getPurchases() as $purchase) {
    echo 'getProductId: ' . $purchase->getProductId() . PHP_EOL;

    if ($purchase->getPurchaseDate() != null) {
      echo 'getPurchaseDate: ' . $purchase->getPurchaseDate()->toIso8601String() . PHP_EOL;
    }
  }
} else {
  echo 'Receipt is not valid.' . PHP_EOL;
  echo 'Receipt result code = ' . $response->getResultCode() . PHP_EOL;
}

More Repositories

1

KKPasscodeLock

KKPasscodeLock is an iOS toolkit for adding a passcode view controller and passcode settings in ios apps.
Objective-C
290
star
2

iTellAFriend

iTellAFriend is an iOS toolkit for displaying a preconfigued mail composer with a "Tell a Friend" template in ios apps.
Objective-C
145
star
3

KKProgressToolbar

KKProgressToolbar is an iOS drop-in class that displays a translucent UIToolbar with a progress indicator and some optional labels while work is being done in a background thread
Swift
61
star
4

cordova-plugin-fetch

HTTP networking plugin that brings the fetch() standard to Cordova
Objective-C
41
star
5

opsworks-php-cookbooks

Cookbooks for AWS OpsWorks & Vagrant with support for PHP 7+ apps
Ruby
33
star
6

xcode-libmms

Modified version of libMMS library which designed to run on ios devices
C
27
star
7

three20lite

A lite stripped down version of Facebook's three20 framework
Objective-C
25
star
8

AFSignedHTTPRequestOperationManager

Automatically sign AFNetworking api requests with SHA-256 hash signature and timestamp
Swift
17
star
9

zend-framework-rest-project-example

Example project on using RESTful API with Zend Framework 1.11
PHP
13
star
10

PHP-GearmanDaemons

The PHP Gearman Daemons Manager library allows running of gearman workers as deamons using supervisord or using a built it php process manager. The library is framework independent and can be easily integrated into any php project.
PHP
9
star
11

php55-cookbook

PHP 5.5 Chef Cookbook for Ubuntu, Centos and Amazon Linux AMI
Ruby
9
star
12

NSData-APNSToken

Category for NSData that converts device tokens into NSString Objects
Swift
8
star
13

rate-limiter-middleware

Rate Limit Middleware for PHP
PHP
6
star
14

zend-framework-base-project

A lightweight zend framework project with built in RESTful API and user management
PHP
4
star
15

KKTableViewEmptyState

Customizable UIView for Handling Empty Table State
Objective-C
4
star
16

php-development-box

Easy to install virtual box powered by Vagrant and Chef
Ruby
3
star
17

aporat.github.com

2
star
18

jquery-menu-widget

jQuery simple dropdown menu
JavaScript
2
star
19

laravel-rate-limiter

Request and actions rate limiter middleware for Laravel & Lumen
PHP
2
star
20

laravel-filter-var

Laravel package for filtering and sanitizing request variables
PHP
1
star
21

SugarRecord

Swift
1
star
22

AlamofireSwiftSoup

Alamofire extension for serialize SwiftSoup HTML documents'
Swift
1
star