Skip to content

Instantly share code, notes, and snippets.

@pauly4it
Created June 11, 2015 04:02
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save pauly4it/4f66153ca5c5407a2005 to your computer and use it in GitHub Desktop.
Save pauly4it/4f66153ca5c5407a2005 to your computer and use it in GitHub Desktop.
All code for Validating Android In-App Purchases With Laravel blog post: http://blog.goforyt.com/validating-android-app-purchases-laravel/
{
"receipt": {
"type": "android-playstore",
"id": "12345678901234567890.1234567890123456",
"purchaseToken": "purchase token goes here",
"receipt": "{"orderId":"12345678901234567890.1234567890123456","packageName":"com.example.app","productId":"com.example.app.product","purchaseTime":1417113074914,"purchaseState":0,"purchaseToken":"purchase token goes here"}",
"signature": "signature data goes here"
}
}
<?php
try {
// create new Google Client
$client = new Google_Client();
// set Application Name to the name of the mobile app
$client->setApplicationName("Mobile_App_Name");
// get p12 key file
$key = file_get_contents($_ENV['GOOGLE_KEY_PATH']);
// create assertion credentials class and pass in:
// - service account email address
// - query scope as an array (which APIs the call will access)
// - the contents of the key file
$cred = new Google_Auth_AssertionCredentials(
'Service Account Email Address',
['https://www.googleapis.com/auth/androidpublisher'],
$key
);
// add the credentials to the client
$client->setAssertionCredentials($cred);
// create a new Android Publisher service class
$service = new Google_Service_AndroidPublisher($client);
// set the package name and subscription id
$packageName = "com.example.app";
$subscriptionId = "com.example.app.subscription";
// use the purchase token to make a call to Google to get the subscription info
$subscription = $service->purchases_subscriptions->get($packageName, $subscriptionId, $purchaseToken);
} catch (Google_Auth_Exception $e) {
// if the call to Google fails, throw an exception
throw new Exception('Error validating transaction', 500);
}
<?php
$rawReceipt = $command->getReceipt();
$receipt = json_decode($rawReceipt['receipt'], TRUE);
if (isset($receipt['purchaseToken'])) {
$this->transactionRepo->addReceiptBeforeValidation($user->id, $platform, $receipt['purchaseToken']);
} else {
throw new ValidationException('No receipt data found', 250);
}
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "Invalid Value"
}
],
"code": 400,
"message": "Invalid Value"
}
}
{
"kind": "androidpublisher#subscriptionPurchase",
"startTimeMillis": long,
"expiryTimeMillis": long,
"autoRenewing": boolean
}
<?php
if (is_null($subscription)) {
// query returned no data
throw new ServerErrorException('Error validating transaction.', 500);
} elseif (isset($subscription['error']['code'])) {
// query returned an error
throw new ServerErrorException('Error validating transaction.', 500);
} elseif (!isset($subscription['expiryTimeMillis'])) {
// query did not return a subscription expiration time
throw new ServerErrorException('Error validating transaction.', 500);
}
// convert expiration time milliseconds since Epoch to seconds since Epoch
$seconds = $subscription['expiryTimeMillis'] / 1000;
// format seconds as a datetime string and create a new UTC Carbon time object from the string
$date = date("d-m-Y H:i:s", $seconds);
$datetime = new Carbon($date);
// check if the expiration date is in the past
if (Carbon::now()->gt($datetime)) {
throw new ServerErrorException('Error validating transaction.', 500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment