Skip to content

Custom exceptions & minor fix #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions itunesReceiptValidator.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
<?php


// Custom exceptions
abstract class itunesReceiptException extends Exception { }
class itunesReceiptInvalidException extends itunesReceiptException { }
class itunesReceiptMalformedException extends itunesReceiptException { }

// itunes receipt validator
class itunesReceiptValidator {

const SANDBOX_URL = 'https://sandbox.itunes.apple.com/verifyReceipt';
const PRODUCTION_URL = 'https://buy.itunes.apple.com/verifyReceipt';

function __construct($endpoint, $receipt = NULL) {
function __construct($endpoint, $receipt = NULL, $password = NULL) {
$this->setEndPoint($endpoint);

if ($receipt) {
$this->setReceipt($receipt);
}
if ($password) {
$this->setPassword($password);
}
}

function getReceipt() {
return $this->receipt;
}

function setReceipt($receipt) {
if (strpos($receipt, '{') !== false) {
if (is_string($receipt) && strpos($receipt, '{') !== false) {
$this->receipt = base64_encode($receipt);
} else {
$this->receipt = $receipt;
}
}

function getPassword(){
return $this->password;
}

function setPassword($password){
$this->password = $password;
}

function getEndpoint() {
return $this->endpoint;
}
Expand All @@ -32,24 +51,32 @@ function setEndPoint($endpoint) {
$this->endpoint = $endpoint;
}

function validateReceipt() {
function validateReceipt($returnAll = false) {
$response = $this->makeRequest();

$decoded_response = $this->decodeResponse($response);

if (!isset($decoded_response->status) || $decoded_response->status != 0) {
throw new Exception('Invalid receipt. Status code: ' . (!empty($decoded_response->status) ? $decoded_response->status : 'N/A'));
throw new itunesReceiptInvalidException (
'Invalid receipt. Status code: ' . (!empty($decoded_response->status) ?
$decoded_response->status : 'N/A')
);
}

if (!is_object($decoded_response)) {
throw new Exception('Invalid response data');
}

return $decoded_response->receipt;
return $returnAll?$decoded_response:$decoded_response->receipt;
}

private function encodeRequest() {
return json_encode(array('receipt-data' => $this->getReceipt()));
$request_data = array('receipt-data' => $this->getReceipt());

if ($this->getPassword()) {
$request_data['password'] = $this->getPassword();
}

return json_encode($request_data);
}

private function decodeResponse($response) {
Expand Down