Last active
May 1, 2020 23:35
-
-
Save lukecurtis93/07bc3f3eddca1830a0b2ea3472431601 to your computer and use it in GitHub Desktop.
Validate a iOS Receipt
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; | |
use Illuminate\Support\Arr; | |
use Illuminate\Http\Request; | |
use App\Http\Controllers\Controller; | |
use ReceiptValidator\iTunes\Validator as iOSValidator; | |
class iOSReceiptValidationController extends Controller | |
{ | |
protected $validator; | |
public function __construct(iOSValidator $iOSValidator) | |
{ | |
$this->validator = $iOSValidator; | |
if (config('app.env') == 'local') { | |
$this->validator->setEndpoint(iOSValidator::ENDPOINT_SANDBOX); | |
} | |
} | |
/** | |
* Handle the incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function __invoke(Request $request) | |
{ | |
try { | |
$response = $this->validator | |
->setSharedSecret(config('services.apple.shared_secret')) | |
->setReceiptData($request->receipt_data) | |
->validate(); | |
$receipt = $response->getRawData(); | |
} catch (Exception $e) { | |
return response()->noContent(422); | |
} | |
if ($response->isValid()) { | |
$latest_receipt = Arr::last($receipt['latest_receipt_info']); | |
if ($latest_receipt && $latest_receipt['product_id'] == config('services.apple.product_name')) { | |
$subscription = [ | |
'status' => 'valid', | |
'expires_date' => $latest_receipt['expires_date'] ?? null, | |
]; | |
} else { | |
$subscription['status'] = 'latest_receipt_info_not_found'; | |
} | |
return $subscription; | |
} | |
return response()->noContent(401); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment