Skip to content

Instantly share code, notes, and snippets.

@pauly4it
Last active November 10, 2020 06:44
Show Gist options
  • Save pauly4it/ec7e06d9e77588faba86 to your computer and use it in GitHub Desktop.
Save pauly4it/ec7e06d9e77588faba86 to your computer and use it in GitHub Desktop.
All code for Validating iOS In-App Purchases With Laravel blog post: http://blog.goforyt.com/validating-ios-app-purchases-laravel/
<?php
Array
(
[status] => 0
[environment] => Sandbox
[receipt] => Array
(
[receipt_type] => ProductionSandbox
[adam_id] => 0
[app_item_id] => 0
[bundle_id] => com.example.app
[application_version] => 2.3.1
[download_id] => 0
[version_external_identifier] => 0
[request_date] => 2014-11-21 07:06:39 Etc/GMT
[request_date_ms] => 1416553599857
[request_date_pst] => 2014-11-20 23:06:39 America/Los_Angeles
[original_purchase_date] => 2013-08-01 07:00:00 Etc/GMT
[original_purchase_date_ms] => 1375340400000
[original_purchase_date_pst] => 2013-08-01 00:00:00 America/Los_Angeles
[original_application_version] => 1.0
[in_app] => Array
(
[0] => Array
(
[quantity] => 1
[product_id] => com.example.app.product1
[transaction_id] => 1000000132544420
[original_transaction_id] => 1000000132544420
[purchase_date] => 2014-11-21 05:40:49 Etc/GMT
[purchase_date_ms] => 1416548449000
[purchase_date_pst] => 2014-11-20 21:40:49 America/Los_Angeles
[original_purchase_date] => 2014-11-21 05:40:45 Etc/GMT
[original_purchase_date_ms] => 1416548445000
[original_purchase_date_pst] => 2014-11-20 21:40:45 America/Los_Angeles
[is_trial_period] => false
)
)
)
)
<?php
private function buildJSONReceiptObject($receipt, $password = NULL)
{
$preObject['receipt-data'] = $receipt['appStoreReceipt'];
if ($password)
{
$preObject['password'] = $password;
}
return json_encode($preObject);
}
{
"status": "success",
"payload": {
"products": [
"com.example.product1",
"com.example.product2"
]
}
}
{
"receipt": {
"type": "ios-appstore",
"id": "1000000132544420",
"appStoreReceipt": "base64 encoded data goes here",
"transactionReceipt": "base64 encoded data goes here"
}
}
<?php
private function makeRequest($user_id, $endpoint, $receiptObject)
{
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => $receiptObject
],
];
// see: http://php.net/manual/en/function.stream-context-create.php
$context = stream_context_create($options);
// see: http://php.net/manual/en/function.file-get-contents.php
$result = file_get_contents($endpoint, FALSE, $context);
if ($result === FALSE)
{
throw new ServerErrorException('Error validating transaction.', 560);
}
// Decode json object (TRUE variable decodes as an associative array)
return json_decode($result, TRUE);
}
<?php
$this->transactionRepo->addReceiptBeforeValidation(
$user->id,
$platform,
$receipt['appStoreReceipt']
);
<?php
private function validateResponse($user_id, $decoded)
{
if (!isset($decoded['status']))
{
throw new ServerErrorException('Error validating transaction.', 560);
}
elseif ($decoded['status'] != 0)
{
// throw error depending on the status code received
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment