Skip to content

Commit

Permalink
Merge pull request #20 from Busuu/feature/error-handling
Browse files Browse the repository at this point in the history
Add error checking on invalid json
  • Loading branch information
adamtester authored Jan 24, 2020
2 parents 411260f + 99c4c30 commit d38331f
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/AppleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Busuu\IosReceiptsApi;

use Exception;
use GuzzleHttp\Client;

class AppleClient
Expand All @@ -22,23 +23,28 @@ public function __construct($password)
*
* @param $receiptData
* @param $endpoint
*
* @return array
* @throws Exception
*/
public function fetchReceipt($receiptData, $endpoint)
{
try {
$data = [
'password' => $this->password,
'receipt-data' => $receiptData
];

$response = $this->client->post($endpoint, ['body' => json_encode($data)]);

return json_decode($response->getBody(), true);
} catch (\Exception $e) {
throw new \InvalidArgumentException(
sprintf('Error in the communication with Apple - %s', $e->getMessage())
);
$data = [
'password' => $this->password,
'receipt-data' => $receiptData
];

$response = $this->client->post($endpoint, [
'body' => json_encode($data),
'timeout' => 10
]);

$jsonResponse = json_decode($response->getBody(), true);

if (null !== $jsonResponse) {
return $jsonResponse;
}

throw new Exception(sprintf('Invalid Response from Apple Server: %s', $response));
}
}

0 comments on commit d38331f

Please sign in to comment.