Skip to content

Commit

Permalink
Added subscription cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdgeijn committed May 17, 2023
1 parent 1d3b939 commit 02dc149
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
19 changes: 16 additions & 3 deletions src/Requests/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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();
Expand All @@ -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());
Expand Down
36 changes: 32 additions & 4 deletions src/Requests/SubscriptionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Mvdgeijn\Pax8\Requests;

use GuzzleHttp\Exception\GuzzleException;
use Mvdgeijn\Pax8\Collections\PaginatedCollection;
use Mvdgeijn\Pax8\Responses\Subscription;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}

0 comments on commit 02dc149

Please sign in to comment.