Created
June 11, 2015 04:02
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"error": { | |
"errors": [ | |
{ | |
"domain": "global", | |
"reason": "invalid", | |
"message": "Invalid Value" | |
} | |
], | |
"code": 400, | |
"message": "Invalid Value" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"kind": "androidpublisher#subscriptionPurchase", | |
"startTimeMillis": long, | |
"expiryTimeMillis": long, | |
"autoRenewing": boolean | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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