Skip to content

Commit

Permalink
Error handler for addProduct and addBillingPlan for subscription help…
Browse files Browse the repository at this point in the history
…er (#623)
  • Loading branch information
otnansirk authored Mar 3, 2024
1 parent ec50129 commit cf2db31
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Traits/PayPalAPI/Subscriptions/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/Feature/AdapterCreateSubscriptionHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
27 changes: 27 additions & 0 deletions tests/Mocks/Responses/BillingPlans.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
29 changes: 29 additions & 0 deletions tests/Mocks/Responses/CatalogProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit cf2db31

Please sign in to comment.