Created
May 8, 2020 05:58
-
-
Save lukecurtis93/d4eb8c17b662fc7a722b1a7b9f0bd1ba to your computer and use it in GitHub Desktop.
Validate a Android subscription with a token and subscription ID
This file contains 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 | |
namespace App\Http\Controllers\Api\v1; | |
use Illuminate\Http\Request; | |
use App\Http\Controllers\Controller; | |
use App\Services\ValidateAndroidSubscription; | |
use ReceiptValidator\GooglePlay\Validator as PlayValidator; | |
use \Google_Client as GoogleClient; | |
use \Google_Service_AndroidPublisher as AndroidPublisher; | |
class AndroidReceiptValidationController extends Controller | |
{ | |
protected $client; | |
protected $validator; | |
public function __construct(GoogleClient $client) | |
{ | |
$this->client = $client; | |
$this->client->setScopes([AndroidPublisher::ANDROIDPUBLISHER]); | |
$this->client->useApplicationDefaultCredentials(); | |
$this->client->setApplicationName(config('services.google_play.package_name')); | |
$this->client->setAuthConfig(config('services.google_play')); | |
$this->validator = new PlayValidator(new AndroidPublisher($this->client)); | |
} | |
/** | |
* Handle the incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function __invoke(Request $request) | |
{ | |
try { | |
$response = $this->validator | |
->setPackageName(config('services.google_play.package_name')) | |
->setProductId($request->subscription_id) | |
->setPurchaseToken($request->token) | |
->validateSubscription(); | |
if ($response->getExpiryTimeMillis()) { | |
return [ | |
'status' => 'valid', | |
'expires_date' => date('Y-m-d H:i:s', ($response->getExpiryTimeMillis()/1000)), | |
]; | |
} | |
return [ | |
'status' => 'invalid', | |
]; | |
} catch (Exception $e) { | |
return $e->getMessage(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment