Skip to content

Commit

Permalink
added support for Amazon RVS service
Browse files Browse the repository at this point in the history
  • Loading branch information
Adar Porat committed Apr 19, 2015
1 parent 9a3044c commit 2a60d53
Show file tree
Hide file tree
Showing 2 changed files with 235 additions and 0 deletions.
112 changes: 112 additions & 0 deletions src/ReceiptValidator/Amazon/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
namespace ReceiptValidator\Amazon;

use ReceiptValidator\RunTimeException;

class Response
{

/**
* Response Codes
*
* @var int
*/
const RESULT_OK = 200;

// Amazon RVS Error: Invalid receiptID
const RESULT_INVALID_RECEIPT = 400;

// Amazon RVS Error: Invalid developerSecret
const RESULT_INVALID_DEVELOPER_SECRET = 496;

// Amazon RVS Error: Invalid userId
const RESULT_INVALID_USER_ID = 497;

// Amazon RVS Error: Internal Server Error
const RESULT_INTERNAL_ERROR = 500;


/**
* Result Code
*
* @var int
*/
protected $_code;


/**
* receipt info
*
* @var array
*/
protected $_receipt = [];



/**
* Constructor
*
* @param int $httpStatusCode
* @param array $jsonResponse
* @return Response
*/
public function __construct($httpStatusCode = 200, $jsonResponse = null)
{
$this->_code = $httpStatusCode;

if ($jsonResponse !== null) {
$this->parseJsonResponse($jsonResponse);
}
}

/**
* Get Result Code
*
* @return int
*/
public function getResultCode()
{
return $this->_code;
}

/**
* Get receipt info
*
* @return array
*/
public function getReceipt()
{
return $this->_receipt;
}

/**
* returns if the receipt is valid or not
*
* @return boolean
*/
public function isValid()
{
if ($this->_code == self::RESULT_OK) {
return true;
}

return false;
}

/**
* Parse JSON Response
*
* @param string $jsonResponse
* @return Message
*/
public function parseJsonResponse($jsonResponse = null)
{
if (!is_array($jsonResponse)) {
throw new RuntimeException('Response must be a scalar value');
}

$this->_receipt = $jsonResponse;

return $this;
}
}
123 changes: 123 additions & 0 deletions src/ReceiptValidator/Amazon/Validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
namespace ReceiptValidator\Amazon;

use Aws\CloudFront\Exception\Exception;
use Guzzle\Http\Client as GuzzleClient;
use ReceiptValidator\RunTimeException as RunTimeException;

class Validator
{

const ENDPOINT_SANDBOX = 'http://localhost:8080/RVSSandbox/';

const ENDPOINT_PRODUCTION = 'https://appstore-sdk.amazon.com/version/1.0/verifyReceiptId/';

/**
* endpoint url
*
* @var string
*/
protected $_endpoint;

/**
* Guzzle http client
*
* @var \Guzzle\Http\Client
*/
protected $_client = null;


/**
* @var string
*/
protected $_userId = null;

/**
* @var string
*/
protected $_receiptId = null;

/**
* @var string
*/
protected $_developerSecret = null;

/**
* @var string
*/
protected $_product_id = null;

public function __construct($endpoint = self::ENDPOINT_PRODUCTION)
{
if ($endpoint != self::ENDPOINT_PRODUCTION && $endpoint != self::ENDPOINT_SANDBOX) {
throw new RunTimeException("Invalid endpoint '{$endpoint}'");
}

$this->_endpoint = $endpoint;
}


/**
*
* @param string $userId
* @return \ReceiptValidator\Amazon\Validator
*/
public function setUserId($userId)
{
$this->_userId = $userId;

return $this;
}

/**
*
* @param string $receiptId
* @return \ReceiptValidator\Amazon\Validator
*/
public function setReceiptId($receiptId)
{
$this->_receiptId = $receiptId;

return $this;
}

/**
*
* @param int $developerSecret
* @return \ReceiptValidator\Amazon\Validator
*/
public function setDeveloperSecret($developerSecret)
{
$this->_developerSecret = $developerSecret;

return $this;
}


/**
* returns the Guzzle client
*
* @return \Guzzle\Http\Client
*/
protected function getClient()
{
if ($this->_client == null) {
$this->_client = new GuzzleClient($this->_endpoint);
}

return $this->_client;
}

/**
* validate the receipt data
*
* @return Response
*/
public function validate()
{

$httpResponse = $this->getClient()->get(sprintf("developer/%s/user/%s/receiptId/%s", $this->_developerSecret, $this->_userId, $this->_receiptId), null, ['exceptions' => false])->send();

return new Response($httpResponse->getStatusCode(), $httpResponse->json());
}
}

0 comments on commit 2a60d53

Please sign in to comment.