Skip to content

Commit

Permalink
refactor amazon response
Browse files Browse the repository at this point in the history
  • Loading branch information
Adar Porat committed Jan 25, 2017
1 parent d466afe commit 52c6eb8
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 19 deletions.
39 changes: 26 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,34 @@ try {
### Amazon App Store ###

```php
use ReceiptValidator\GooglePlay\Validator as AmazonValidator;
use ReceiptValidator\Amazon\Validator as AmazonValidator;
use ReceiptValidator\Amazon\Response as ValidatorResponse;

$validator = new AmazonValidator();
$validator = new AmazonValidator;

$response = null;
try {
$receiptResponse = $validator->setDeveloperSecret('DEVELOPER_SECRET')
->setReceiptId('ORDER_ID')
->setUserId('AMAZON_USER_ID')
->validate();
} catch (\Exception $e) {
var_dump($e->getMessage());
}
$response = $validator->setDeveloperSecret("DEVELOPER_SECRET")->setReceiptId("RECEIPT_ID")->setUserId("USER_ID")->validate();

if ($receiptResponse->isValid()) {
$product_id = $receiptResponse->getReceipt()['productId'];
} else {
// Not valid receipt
} 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;
}

```
42 changes: 42 additions & 0 deletions examples/test_amazon_receipt.php
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;
}
133 changes: 133 additions & 0 deletions src/Amazon/PurchaseItem.php
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;
}
}
19 changes: 17 additions & 2 deletions src/Amazon/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ class Response
*/
protected $_receipt = [];


/**
* purchases info
* @var PurchaseItem[]
*/
protected $_purchases = [];

/**
* Constructor
Expand Down Expand Up @@ -77,6 +81,16 @@ public function getReceipt()
return $this->_receipt;
}

/**
* Get purchases info
*
* @return PurchaseItem[]
*/
public function getPurchases()
{
return $this->_purchases;
}

/**
* returns if the receipt is valid or not
*
Expand All @@ -102,11 +116,12 @@ public function isValid()
public function parseJsonResponse($jsonResponse = null)
{
if (!is_array($jsonResponse)) {

throw new RuntimeException('Response must be a scalar value');
}

$this->_receipt = $jsonResponse;
$this->_purchases = [];
$this->_purchases[] = new PurchaseItem($jsonResponse);

return $this;
}
Expand Down
4 changes: 0 additions & 4 deletions tests/iTunes/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public function testInvalidReceipt()

$this->assertFalse($response->isValid(), 'receipt must be invalid');
$this->assertEquals(Response::RESULT_DATA_MALFORMED, $response->getResultCode(), 'receipt result code must match');

$response = new Response(array('status' => Response::RESULT_OK));

$this->assertFalse($response->isValid(), 'receipt must be invalid');
}

public function testReceiptSentToWrongEndpoint()
Expand Down

0 comments on commit 52c6eb8

Please sign in to comment.