diff --git a/src/Traits/PayPalAPI/Subscriptions/Helpers.php b/src/Traits/PayPalAPI/Subscriptions/Helpers.php index 0b5333f9..054304cf 100644 --- a/src/Traits/PayPalAPI/Subscriptions/Helpers.php +++ b/src/Traits/PayPalAPI/Subscriptions/Helpers.php @@ -328,13 +328,19 @@ public function addProduct(string $name, string $description, string $type, stri $request_id = Str::random(); - $this->product = $this->createProduct([ + $product = $this->createProduct([ 'name' => $name, 'description' => $description, 'type' => $type, 'category' => $category, ], $request_id); + if ($error = data_get($product, 'error', false)) { + throw new \RuntimeException(data_get($error, 'details.0.description', 'Failed to add product')); + } + + $this->product = $product; + return $this; } @@ -398,7 +404,11 @@ protected function addBillingPlan(string $name, string $description, array $bill ], ]; - $this->billing_plan = $this->createPlan($plan_params, $request_id); + $billingPlan = $this->createPlan($plan_params, $request_id); + if ($error = data_get($billingPlan, 'error', false)) { + throw new \RuntimeException(data_get($error, 'details.0.description', 'Failed to add billing plan')); + } + $this->billing_plan = $billingPlan; } /** diff --git a/tests/Feature/AdapterCreateSubscriptionHelpersTest.php b/tests/Feature/AdapterCreateSubscriptionHelpersTest.php index bbe51211..550fa520 100644 --- a/tests/Feature/AdapterCreateSubscriptionHelpersTest.php +++ b/tests/Feature/AdapterCreateSubscriptionHelpersTest.php @@ -255,6 +255,52 @@ public function it_throws_exception_when_invalid_interval_is_provided_for_creati $this->client = $this->client->addCustomPlan('Demo Plan', 'Demo Plan', 100, 'MONTHLY', 3); } + /** @test */ + public function it_throws_exception_when_get_error_for_creating_a_billing_plan() + { + $this->client->setAccessToken([ + 'access_token' => self::$access_token, + 'token_type' => 'Bearer', + ]); + + $this->client->setClient( + $this->mock_http_client( + $this->mockCreateCatalogProductsResponse() + ) + ); + + $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE'); + + $this->client->setClient( + $this->mock_http_client( + $this->mockCreatePlansErrorResponse() + ) + ); + + $this->expectException(\RuntimeException::class); + + $this->client = $this->client->addMonthlyPlan('Demo Plan', 'Demo Plan', 100); + } + + /** @test */ + public function it_throws_exception_when_get_error_for_creating_a_product() + { + $this->client->setAccessToken([ + 'access_token' => self::$access_token, + 'token_type' => 'Bearer', + ]); + + $this->client->setClient( + $this->mock_http_client( + $this->mockGetCatalogProductsErrorResponse() + ) + ); + + $this->expectException(\RuntimeException::class); + + $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE'); + } + /** @test */ public function it_can_create_a_subscription_without_trial() { diff --git a/tests/Mocks/Responses/BillingPlans.php b/tests/Mocks/Responses/BillingPlans.php index 3cec7e41..02fb3eb4 100644 --- a/tests/Mocks/Responses/BillingPlans.php +++ b/tests/Mocks/Responses/BillingPlans.php @@ -274,6 +274,33 @@ private function mockGetPlansResponse(): array "method": "POST" } ] +}', true); + } + + /** + * @return array + */ + private function mockCreatePlansErrorResponse(): array + { + return Utils::jsonDecode('{ + "error": { + "name" : "UNPROCESSABLE_ENTITY", + "message" : "The requested action could not be performed, semantically incorrect, or failed business validation.", + "debug_id" : "7a944631e76bf", + "details" : [ + { + "issue" : "CURRENCY_NOT_SUPPORTED_FOR_RECEIVER", + "description" : "This currency cannot be accepted for this recipient\'s account." + } + ], + "links" : [ + { + "href" : "https://developer.paypal.com/docs/api/v1/billing/subscriptions#UNPROCESSABLE_ENTITY", + "rel" : "information_link", + "method" : "GET" + } + ] + } }', true); } } diff --git a/tests/Mocks/Responses/CatalogProducts.php b/tests/Mocks/Responses/CatalogProducts.php index cb0fa70e..45fbdf9f 100644 --- a/tests/Mocks/Responses/CatalogProducts.php +++ b/tests/Mocks/Responses/CatalogProducts.php @@ -119,6 +119,35 @@ private function mockGetCatalogProductsResponse(): array "method": "PATCH" } ] +}', true); + } + + /** + * @return array + */ + private function mockGetCatalogProductsErrorResponse(): array + { + return Utils::jsonDecode('{ + "error": { + "name": "INVALID_REQUEST", + "message": "Request is not well-formed, syntactically incorrect, or violates schema.", + "debug_id": "b2aaac7fe91d1", + "details": [ + { + "field": "\/type", + "location": "body", + "issue": "MISSING_REQUIRED_PARAMETER", + "description": "A required field is missing." + } + ], + "links": [ + { + "href": "https:\/\/developer.paypal.com\/docs\/api\/v1\/billing\/subscriptions#INVALID_REQUEST", + "rel": "information_link", + "method": "GET" + } + ] + } }', true); } }