diff --git a/src/Requests/AbstractRequest.php b/src/Requests/AbstractRequest.php index 178b7a2..eebc8f8 100644 --- a/src/Requests/AbstractRequest.php +++ b/src/Requests/AbstractRequest.php @@ -95,6 +95,19 @@ protected function putRequest($path, \stdClass $data ): ?ResponseInterface return $this->doRequest( 'PUT', $path, $data ); } + /** + * Do a DELETE request on the Pax8 API + * + * @param $path + * @param \stdClass $data + * @return ResponseInterface|null + * @throws \GuzzleHttp\Exception\GuzzleException + */ + protected function deleteRequest($path, \stdClass $data ): ?ResponseInterface + { + return $this->doRequest( 'DELETE', $path, $data ); + } + /** * Handle PUT or POST request on the Pax8 API * @@ -121,8 +134,8 @@ protected function doRequest(string $method, $path, \stdClass $data ): ?Response RequestOptions::JSON => $data ]); - // If returned status is successful (or not equal 401/Unauthorized), don't retry - if( $response->getStatusCode() != 401 ) + // If returned status is successful (or not equal 401/Unauthorized or 404/Not found), don't retry + if( in_array( $response->getStatusCode(), [200,204,401,404] ) ) break; $this->pax8->renew(); @@ -140,7 +153,7 @@ protected function doRequest(string $method, $path, \stdClass $data ): ?Response */ private function handleErrors(ResponseInterface &$response ): ?ResponseInterface { - if( $response->getStatusCode() !== 200 ) { + if( ! in_array( $response->getStatusCode(), [200,204] ) ) { $this->errors = null; $data = json_decode($response->getBody()); diff --git a/src/Requests/SubscriptionRequest.php b/src/Requests/SubscriptionRequest.php index e24df91..b2f2737 100644 --- a/src/Requests/SubscriptionRequest.php +++ b/src/Requests/SubscriptionRequest.php @@ -2,6 +2,7 @@ namespace Mvdgeijn\Pax8\Requests; +use GuzzleHttp\Exception\GuzzleException; use Mvdgeijn\Pax8\Collections\PaginatedCollection; use Mvdgeijn\Pax8\Responses\Subscription; @@ -39,13 +40,20 @@ public function get(string $subscriptionId ): ?Subscription { $response = $this->getRequest('/v1/subscriptions/' . $subscriptionId ); - if ($response->getStatusCode() == 200) - return Subscription::parse(json_decode( $response->getBody() ) ); - else + if ($response->getStatusCode() == 200) { + return Subscription::parse(json_decode($response->getBody())); + } else { return null; + } } - public function update( string $subscriptionId, int $quantity ): ?Subscription + /** + * @param string $subscriptionId + * @param int $quantity + * @return Subscription|null + * @throws GuzzleException + */ + public function update(string $subscriptionId, int $quantity ): ?Subscription { $data = new \stdClass(); $data->quantity = $quantity; @@ -59,4 +67,24 @@ public function update( string $subscriptionId, int $quantity ): ?Subscription else return null; } + + /** + * @param string $subscriptionId + * @param string $cancelDate + * @return bool + * @throws GuzzleException + */ + public function delete(string $subscriptionId, string $cancelDate ): bool + { + $data = new \stdClass(); + $data->canceldate = $cancelDate; + + $response = $this->deleteRequest( '/v1/subscriptions/' . $subscriptionId, $data ); + + if( $response->getStatusCode() == 204 ) { + return true; + } + + return false; + } }