diff --git a/src/Endpoints/Insurance.php b/src/Endpoints/Insurance.php index e8d9f2ea..d7c85a6b 100644 --- a/src/Endpoints/Insurance.php +++ b/src/Endpoints/Insurance.php @@ -5,6 +5,7 @@ use Alma\API\Entities\Insurance\Contract; use Alma\API\Entities\Insurance\File; use Alma\API\Entities\Insurance\Subscription; +use Alma\API\Exceptions\InsuranceCancelPendingException; use Alma\API\Exceptions\MissingKeyException; use Alma\API\Exceptions\ParametersException; use Alma\API\Exceptions\RequestException; @@ -317,6 +318,7 @@ public function addCustomerSessionToRequest($request, $customerSessionId = null, /** * @param string $subscriptionId * @return void + * @throws InsuranceCancelPendingException * @throws ParametersException * @throws RequestError * @throws RequestException @@ -329,6 +331,9 @@ public function cancelSubscription($subscriptionId) $request = $this->request(self::INSURANCE_PATH . 'subscriptions/' . $subscriptionId . '/void'); $response = $request->post(); + if ($response->responseCode === 410) { + throw new InsuranceCancelPendingException('Subscription can not be cancelled at this time'); + } if ($response->isError()) { throw new RequestException($response->errorMessage, $request, $response); } diff --git a/src/Exceptions/InsuranceCancelPendingException.php b/src/Exceptions/InsuranceCancelPendingException.php new file mode 100644 index 00000000..644732b5 --- /dev/null +++ b/src/Exceptions/InsuranceCancelPendingException.php @@ -0,0 +1,8 @@ +expectException(RequestException::class); $subscriptionCancelPayload = 'subscriptionId1'; + $this->responseMock->shouldReceive('isError')->once()->andReturn(true); $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); $this->insuranceMock->shouldReceive('request') @@ -914,6 +917,26 @@ public function testCancelSubscriptionCallRequestWithSubscriptionArrayPayloadAnd $this->insuranceMock->cancelSubscription($subscriptionCancelPayload); } + /** + * @return void + * @throws InsuranceCancelPendingException + * @throws ParametersException + * @throws RequestError + * @throws RequestException + */ + public function testCancelSubscriptionCallRequestWithSubscriptionArrayPayloadAndThrowInsuranceCancelPendingExceptionForResponse410() + { + $this->expectException(InsuranceCancelPendingException::class); + $subscriptionCancelPayload = 'subscriptionId1'; + $this->responseMock->responseCode = 410; + $this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock); + $this->insuranceMock->shouldReceive('request') + ->with(self::INSURANCE_SUBSCRIPTIONS_PATH . '/subscriptionId1/void') + ->once() + ->andReturn($this->requestObject); + $this->insuranceMock->cancelSubscription($subscriptionCancelPayload); + } + /** * @dataProvider cancelSubscriptionErrorPayloadDataProvider * @param $payload @@ -926,6 +949,9 @@ public function testCheckSubscriptionIdFormatThrowParamsErrorForBadPayload($payl $this->insuranceMock->checkSubscriptionIdFormat($payload); } + /** + * @return array + */ public function cancelSubscriptionErrorPayloadDataProvider() { return [