From f9b3e6162f4b7ded0f53737101af5cf1ea9a02a2 Mon Sep 17 00:00:00 2001 From: Raza Mehdi Date: Thu, 7 Sep 2023 02:59:15 +0500 Subject: [PATCH] Add functions for pagination. --- src/Traits/PayPalAPI.php | 42 +++++++++++++++++++ src/Traits/PayPalAPI/BillingPlans.php | 10 +---- src/Traits/PayPalAPI/CatalogProducts.php | 10 +---- src/Traits/PayPalAPI/Disputes.php | 4 +- src/Traits/PayPalAPI/Invoices.php | 9 +--- src/Traits/PayPalAPI/InvoicesSearch.php | 10 +---- src/Traits/PayPalAPI/InvoicesTemplates.php | 6 +-- src/Traits/PayPalAPI/Reporting.php | 6 +-- src/Traits/PayPalRequest.php | 21 ++++++++++ .../AdapterBillingPlansPricingHelpersTest.php | 23 ++++++++++ tests/Feature/AdapterFeatureTest.php | 2 +- tests/Unit/Adapter/DisputesTest.php | 2 +- tests/Unit/Adapter/InvoicesTest.php | 2 +- 13 files changed, 103 insertions(+), 44 deletions(-) diff --git a/src/Traits/PayPalAPI.php b/src/Traits/PayPalAPI.php index 5c2ee8cc..9ba5b123 100644 --- a/src/Traits/PayPalAPI.php +++ b/src/Traits/PayPalAPI.php @@ -88,4 +88,46 @@ private function setPayPalAppId(array $response) $this->config['app_id'] = $app_id; } + + /** + * Set records per page for list resources API calls. + * + * @param int $size + * + * @return \Srmklive\PayPal\Services\PayPal + */ + public function setPageSize(int $size): \Srmklive\PayPal\Services\PayPal + { + $this->page_size = $size; + + return $this; + } + + /** + * Set the current page for list resources API calls. + * + * @param int $size + * + * @return \Srmklive\PayPal\Services\PayPal + */ + public function setCurrentPage(int $page): \Srmklive\PayPal\Services\PayPal + { + $this->current_page = $page; + + return $this; + } + + /** + * Toggle whether totals for list resources are returned after every API call. + * + * @param bool $totals + * + * @return \Srmklive\PayPal\Services\PayPal + */ + public function showTotals(bool $totals): \Srmklive\PayPal\Services\PayPal + { + $this->show_totals = $totals; + + return $this; + } } diff --git a/src/Traits/PayPalAPI/BillingPlans.php b/src/Traits/PayPalAPI/BillingPlans.php index bf10ffff..63a3ef25 100644 --- a/src/Traits/PayPalAPI/BillingPlans.php +++ b/src/Traits/PayPalAPI/BillingPlans.php @@ -31,21 +31,15 @@ public function createPlan(array $data) /** * List all billing plans. * - * @param int $page - * @param int $size - * @param bool $totals - * * @throws \Throwable * * @return array|\Psr\Http\Message\StreamInterface|string * * @see https://developer.paypal.com/docs/api/subscriptions/v1/#plans_list */ - public function listPlans(int $page = 1, int $size = 20, bool $totals = true) + public function listPlans() { - $totals = ($totals) ? 'true' : 'false'; - - $this->apiEndPoint = "v1/billing/plans?page={$page}&page_size={$size}&total_required={$totals}"; + $this->apiEndPoint = "v1/billing/plans?page={$this->current_page}&page_size={$this->page_size}&total_required={$this->show_totals}"; $this->verb = 'get'; diff --git a/src/Traits/PayPalAPI/CatalogProducts.php b/src/Traits/PayPalAPI/CatalogProducts.php index cc0478a7..f044b7a7 100644 --- a/src/Traits/PayPalAPI/CatalogProducts.php +++ b/src/Traits/PayPalAPI/CatalogProducts.php @@ -29,21 +29,15 @@ public function createProduct(array $data) /** * List products. * - * @param int $page - * @param int $size - * @param bool $totals - * * @throws \Throwable * * @return array|\Psr\Http\Message\StreamInterface|string * * @see https://developer.paypal.com/docs/api/catalog-products/v1/#products_list */ - public function listProducts(int $page = 1, int $size = 20, bool $totals = true) + public function listProducts() { - $totals = ($totals === true) ? 'true' : 'false'; - - $this->apiEndPoint = "v1/catalogs/products?page={$page}&page_size={$size}&total_required={$totals}"; + $this->apiEndPoint = "v1/catalogs/products?page={$this->current_page}&page_size={$this->page_size}&total_required={$this->show_totals}"; $this->verb = 'get'; diff --git a/src/Traits/PayPalAPI/Disputes.php b/src/Traits/PayPalAPI/Disputes.php index d4da543c..19fffbe4 100644 --- a/src/Traits/PayPalAPI/Disputes.php +++ b/src/Traits/PayPalAPI/Disputes.php @@ -25,8 +25,8 @@ public function listDisputes() /** * Update a dispute. * - * @param array $data * @param string $dispute_id + * @param array $data * * @throws \Throwable * @@ -34,7 +34,7 @@ public function listDisputes() * * @see https://developer.paypal.com/docs/api/customer-disputes/v1/#disputes_patch */ - public function updateDispute(array $data, string $dispute_id) + public function updateDispute(string $dispute_id, array $data) { $this->apiEndPoint = "v1/customer/disputes/{$dispute_id}"; diff --git a/src/Traits/PayPalAPI/Invoices.php b/src/Traits/PayPalAPI/Invoices.php index 05bd5eb0..7c8bbcfc 100644 --- a/src/Traits/PayPalAPI/Invoices.php +++ b/src/Traits/PayPalAPI/Invoices.php @@ -47,9 +47,6 @@ public function createInvoice(array $data) /** * Get list of invoices. * - * @param int $page - * @param int $size - * @param bool $totals * @param array $fields * * @throws \Throwable @@ -58,15 +55,13 @@ public function createInvoice(array $data) * * @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_list */ - public function listInvoices(int $page = 1, int $size = 20, bool $totals = true, array $fields = []) + public function listInvoices(array $fields = []) { - $totals = ($totals === true) ? 'true' : 'false'; - $fields_list = collect($fields); $fields = ($fields_list->count() > 0) ? "&fields={$fields_list->implode(',')}" : ''; - $this->apiEndPoint = "v2/invoicing/invoices?page={$page}&page_size={$size}&total_required={$totals}{$fields}"; + $this->apiEndPoint = "v2/invoicing/invoices?page={$this->current_page}&page_size={$this->page_size}&total_required={$this->show_totals}{$fields}"; $this->verb = 'get'; diff --git a/src/Traits/PayPalAPI/InvoicesSearch.php b/src/Traits/PayPalAPI/InvoicesSearch.php index ccb78fbe..213e3dac 100644 --- a/src/Traits/PayPalAPI/InvoicesSearch.php +++ b/src/Traits/PayPalAPI/InvoicesSearch.php @@ -11,27 +11,21 @@ trait InvoicesSearch /** * Search and return existing invoices. * - * @param int $page - * @param int $size - * @param bool $totals - * * @throws \Throwable * * @return array|\Psr\Http\Message\StreamInterface|string * * @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_list */ - public function searchInvoices(int $page = 1, int $size = 20, bool $totals = true) + public function searchInvoices() { - $totals = ($totals === true) ? 'true' : 'false'; - if (collect($this->invoice_search_filters)->count() < 1) { $this->invoice_search_filters = [ 'currency_code' => $this->getCurrency(), ]; } - $this->apiEndPoint = "v2/invoicing/search-invoices?page={$page}&page_size={$size}&total_required={$totals}"; + $this->apiEndPoint = "v2/invoicing/search-invoices?page={$this->current_page}&page_size={$this->page_size}&total_required={$this->show_totals}"; $this->options['json'] = $this->invoice_search_filters; diff --git a/src/Traits/PayPalAPI/InvoicesTemplates.php b/src/Traits/PayPalAPI/InvoicesTemplates.php index ec31e661..d50f5fd8 100644 --- a/src/Traits/PayPalAPI/InvoicesTemplates.php +++ b/src/Traits/PayPalAPI/InvoicesTemplates.php @@ -29,8 +29,6 @@ public function createInvoiceTemplate(array $data) /** * Get list of invoice templates. * - * @param int $page - * @param int $size * @param string $fields * * @throws \Throwable @@ -39,9 +37,9 @@ public function createInvoiceTemplate(array $data) * * @see https://developer.paypal.com/docs/api/invoicing/v2/#templates_list */ - public function listInvoiceTemplates(int $page = 1, int $size = 20, string $fields = 'all') + public function listInvoiceTemplates(string $fields = 'all') { - $this->apiEndPoint = "v2/invoicing/templates?page={$page}&page_size={$size}&fields={$fields}"; + $this->apiEndPoint = "v2/invoicing/templates?page={$this->current_page}&page_size={$this->page_size}&fields={$fields}"; $this->verb = 'get'; diff --git a/src/Traits/PayPalAPI/Reporting.php b/src/Traits/PayPalAPI/Reporting.php index a556e8d9..3ae76bc7 100644 --- a/src/Traits/PayPalAPI/Reporting.php +++ b/src/Traits/PayPalAPI/Reporting.php @@ -11,8 +11,6 @@ trait Reporting * * @param array $filters * @param string $fields - * @param int $page - * @param int $page_size * * @throws \Throwable * @@ -20,14 +18,14 @@ trait Reporting * * @see https://developer.paypal.com/docs/api/transaction-search/v1/#transactions_get */ - public function listTransactions(array $filters, string $fields = 'all', int $page = 1, int $page_size = 100) + public function listTransactions(array $filters, string $fields = 'all') { $filters_list = collect($filters)->isEmpty() ? '' : collect($filters)->map(function ($value, $key) { return "{$key}={$value}&"; })->implode(''); - $this->apiEndPoint = "v1/reporting/transactions?{$filters_list}fields={$fields}&page={$page}&page_size={$page_size}"; + $this->apiEndPoint = "v1/reporting/transactions?{$filters_list}fields={$fields}&page={$this->current_page}&page_size={$this->page_size}"; $this->verb = 'get'; diff --git a/src/Traits/PayPalRequest.php b/src/Traits/PayPalRequest.php index 9096a8d8..b2c3557e 100644 --- a/src/Traits/PayPalRequest.php +++ b/src/Traits/PayPalRequest.php @@ -44,6 +44,27 @@ trait PayPalRequest */ protected $options; + /** + * Set limit to total records per API call. + * + * @var int + */ + protected $page_size = 20; + + /** + * Set the current page for list resources API calls. + * + * @var bool + */ + protected $current_page = 1; + + /** + * Toggle whether totals for list resources are returned after every API call. + * + * @var bool + */ + protected $show_totals = true; + /** * Set PayPal API Credentials. * diff --git a/tests/Feature/AdapterBillingPlansPricingHelpersTest.php b/tests/Feature/AdapterBillingPlansPricingHelpersTest.php index add7ced9..bfd2967f 100644 --- a/tests/Feature/AdapterBillingPlansPricingHelpersTest.php +++ b/tests/Feature/AdapterBillingPlansPricingHelpersTest.php @@ -54,4 +54,27 @@ public function it_can_update_pricing_schemes_for_a_billing_plan() $this->assertEmpty($response); } + + /** @test */ + public function it_can_set_custom_limits_when_listing_billing_plans() + { + $this->client->setAccessToken([ + 'access_token' => self::$access_token, + 'token_type' => 'Bearer', + ]); + + $this->client = $this->client->setPageSize(30) + ->showTotals(true); + + $this->client->setClient( + $this->mock_http_client( + $this->mockListPlansResponse() + ) + ); + + $response = $this->client->setCurrentPage(1)->listPlans(); + + $this->assertNotEmpty($response); + $this->assertArrayHasKey('plans', $response); + } } diff --git a/tests/Feature/AdapterFeatureTest.php b/tests/Feature/AdapterFeatureTest.php index 20cf0673..511afd75 100644 --- a/tests/Feature/AdapterFeatureTest.php +++ b/tests/Feature/AdapterFeatureTest.php @@ -308,7 +308,7 @@ public function it_can_partially_update_a_dispute() $expectedParams = $this->updateDisputeParams(); - $response = $this->client->updateDispute($expectedParams, 'PP-D-27803'); + $response = $this->client->updateDispute('PP-D-27803', $expectedParams); $this->assertEmpty($response); } diff --git a/tests/Unit/Adapter/DisputesTest.php b/tests/Unit/Adapter/DisputesTest.php index ebd4b39f..b659fa0b 100644 --- a/tests/Unit/Adapter/DisputesTest.php +++ b/tests/Unit/Adapter/DisputesTest.php @@ -42,7 +42,7 @@ public function it_can_partially_update_a_dispute() $mockClient->setApiCredentials($this->getMockCredentials()); $mockClient->getAccessToken(); - $this->assertEquals($expectedResponse, $mockClient->{$expectedMethod}($expectedParams, 'PP-D-27803')); + $this->assertEquals($expectedResponse, $mockClient->{$expectedMethod}('PP-D-27803', $expectedParams)); } /** @test */ diff --git a/tests/Unit/Adapter/InvoicesTest.php b/tests/Unit/Adapter/InvoicesTest.php index c890d9a2..02171309 100644 --- a/tests/Unit/Adapter/InvoicesTest.php +++ b/tests/Unit/Adapter/InvoicesTest.php @@ -57,7 +57,7 @@ public function it_can_list_current_invoices() $mockClient->setApiCredentials($this->getMockCredentials()); $mockClient->getAccessToken(); - $this->assertEquals($expectedResponse, $mockClient->{$expectedMethod}(1, 2, true)); + $this->assertEquals($expectedResponse, $mockClient->{$expectedMethod}()); } /** @test */