-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adar Porat
committed
Jan 25, 2017
1 parent
d466afe
commit 52c6eb8
Showing
5 changed files
with
218 additions
and
19 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
error_reporting(E_ALL | E_STRICT); | ||
ini_set('display_errors', 1); | ||
|
||
$root = realpath(dirname(dirname(__FILE__))); | ||
$library = "$root/library"; | ||
|
||
$path = array($library, get_include_path()); | ||
set_include_path(implode(PATH_SEPARATOR, $path)); | ||
|
||
require_once $root . '/vendor/autoload.php'; | ||
|
||
use ReceiptValidator\Amazon\Validator as AmazonValidator; | ||
use ReceiptValidator\Amazon\Response as ValidatorResponse; | ||
|
||
$validator = new AmazonValidator; | ||
|
||
$response = null; | ||
try { | ||
$response = $validator->setDeveloperSecret("DEVELOPER_SECRET")->setReceiptId("RECEIPT_ID")->setUserId("USER_ID")->validate(); | ||
|
||
} catch (Exception $e) { | ||
echo 'got error = ' . $e->getMessage() . PHP_EOL; | ||
} | ||
|
||
if ($response instanceof ValidatorResponse && $response->isValid()) { | ||
|
||
echo 'Receipt is valid.' . PHP_EOL; | ||
|
||
|
||
foreach ($response->getPurchases() as $purchase) { | ||
echo 'getProductId: ' . $purchase->getProductId() . PHP_EOL; | ||
|
||
if ($purchase->getPurchaseDate() != null) { | ||
echo 'getPurchaseDate: ' . $purchase->getPurchaseDate()->toIso8601String() . PHP_EOL; | ||
} | ||
} | ||
} else { | ||
echo 'Receipt is not valid.' . PHP_EOL; | ||
echo 'Receipt result code = ' . $response->getResultCode() . PHP_EOL; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
namespace ReceiptValidator\Amazon; | ||
|
||
use ReceiptValidator\RunTimeException; | ||
use Carbon\Carbon; | ||
|
||
class PurchaseItem | ||
{ | ||
|
||
/** | ||
* purchase item info | ||
* | ||
* @var array | ||
*/ | ||
protected $_response; | ||
|
||
/** | ||
* quantity | ||
* | ||
* @var int | ||
*/ | ||
protected $_quantity; | ||
|
||
/** | ||
* product_id | ||
* | ||
* @var string | ||
*/ | ||
protected $_product_id; | ||
|
||
/** | ||
* purchase_date | ||
* | ||
* @var Carbon | ||
*/ | ||
protected $_purchase_date; | ||
|
||
/** | ||
* cancellation_date | ||
* | ||
* @var Carbon | ||
*/ | ||
protected $_cancellation_date; | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getRawResponse() | ||
{ | ||
return $this->_response; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getQuantity() | ||
{ | ||
return $this->_quantity; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getProductId() | ||
{ | ||
return $this->_product_id; | ||
} | ||
|
||
/** | ||
* @return Carbon | ||
*/ | ||
public function getPurchaseDate() | ||
{ | ||
return $this->_purchase_date; | ||
} | ||
|
||
/** | ||
* @return Carbon | ||
*/ | ||
public function getCancellationDate() | ||
{ | ||
return $this->_cancellation_date; | ||
} | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param array $jsonResponse | ||
*/ | ||
public function __construct($jsonResponse = null) | ||
{ | ||
$this->_response = $jsonResponse; | ||
if ($this->_response !== null) { | ||
$this->parseJsonResponse(); | ||
} | ||
} | ||
|
||
/** | ||
* Parse JSON Response | ||
* | ||
* @return PurchaseItem | ||
* @throws RunTimeException | ||
*/ | ||
public function parseJsonResponse() | ||
{ | ||
$jsonResponse = $this->_response; | ||
if (!is_array($jsonResponse)) { | ||
throw new RuntimeException('Response must be a scalar value'); | ||
} | ||
|
||
if (array_key_exists('quantity', $jsonResponse)) { | ||
$this->_quantity = $jsonResponse['quantity']; | ||
} | ||
|
||
if (array_key_exists('receiptId', $jsonResponse)) { | ||
$this->_transaction_id = $jsonResponse['receiptId']; | ||
} | ||
|
||
if (array_key_exists('productId', $jsonResponse)) { | ||
$this->_product_id = $jsonResponse['productId']; | ||
} | ||
|
||
if (array_key_exists('purchaseDate', $jsonResponse) && !empty($jsonResponse['purchaseDate'])) { | ||
$this->_purchase_date = Carbon::createFromTimestampUTC(round($jsonResponse['purchaseDate'] / 1000)); | ||
} | ||
|
||
if (array_key_exists('cancelDate', $jsonResponse) && !empty($jsonResponse['cancelDate'])) { | ||
$this->_cancellation_date = Carbon::createFromTimestampUTC(round($jsonResponse['cancelDate'] / 1000)); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
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
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