From 52c6eb863bb564106309314ce2e5c29becf80fdc Mon Sep 17 00:00:00 2001 From: Adar Porat Date: Wed, 25 Jan 2017 11:13:57 -0500 Subject: [PATCH] refactor amazon response --- README.md | 39 ++++++--- examples/test_amazon_receipt.php | 42 ++++++++++ src/Amazon/PurchaseItem.php | 133 +++++++++++++++++++++++++++++++ src/Amazon/Response.php | 19 ++++- tests/iTunes/ResponseTest.php | 4 - 5 files changed, 218 insertions(+), 19 deletions(-) create mode 100644 examples/test_amazon_receipt.php create mode 100644 src/Amazon/PurchaseItem.php diff --git a/README.md b/README.md index b613e21..e96a372 100755 --- a/README.md +++ b/README.md @@ -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; +} + ``` diff --git a/examples/test_amazon_receipt.php b/examples/test_amazon_receipt.php new file mode 100644 index 0000000..1a1465c --- /dev/null +++ b/examples/test_amazon_receipt.php @@ -0,0 +1,42 @@ +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; +} diff --git a/src/Amazon/PurchaseItem.php b/src/Amazon/PurchaseItem.php new file mode 100644 index 0000000..c89d9ec --- /dev/null +++ b/src/Amazon/PurchaseItem.php @@ -0,0 +1,133 @@ +_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; + } +} diff --git a/src/Amazon/Response.php b/src/Amazon/Response.php index 2af24d6..8011f3e 100644 --- a/src/Amazon/Response.php +++ b/src/Amazon/Response.php @@ -40,7 +40,11 @@ class Response */ protected $_receipt = []; - + /** + * purchases info + * @var PurchaseItem[] + */ + protected $_purchases = []; /** * Constructor @@ -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 * @@ -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; } diff --git a/tests/iTunes/ResponseTest.php b/tests/iTunes/ResponseTest.php index 9a3a513..be29ce9 100644 --- a/tests/iTunes/ResponseTest.php +++ b/tests/iTunes/ResponseTest.php @@ -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()