Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : add error handler for addProduct and addBillingPlan from subscription helper #623

Merged
merged 11 commits into from
Mar 3, 2024
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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method name AdapterCreateSubscriptionHelpersTest::it_throws_exception_when_get_error_for_creating_a_billing_plan is not in camel caps format

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srmklive should I rename it to camelCase format ? I see other code using snake_case

{
$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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method name AdapterCreateSubscriptionHelpersTest::it_throws_exception_when_get_error_for_creating_a_product is not in camel caps format

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srmklive should I rename it to camelCase format ? I see other code using snake_case

{
$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);
}
}
Loading