Skip to content

Commit

Permalink
Add functions for pagination.
Browse files Browse the repository at this point in the history
  • Loading branch information
srmklive committed Sep 6, 2023
1 parent 07e5cc5 commit ef853fb
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 44 deletions.
42 changes: 42 additions & 0 deletions src/Traits/PayPalAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 2 additions & 8 deletions src/Traits/PayPalAPI/BillingPlans.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
10 changes: 2 additions & 8 deletions src/Traits/PayPalAPI/CatalogProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
4 changes: 2 additions & 2 deletions src/Traits/PayPalAPI/Disputes.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public function listDisputes()
/**
* Update a dispute.
*
* @param array $data
* @param string $dispute_id
* @param array $data
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @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}";

Expand Down
9 changes: 2 additions & 7 deletions src/Traits/PayPalAPI/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';

Expand Down
10 changes: 2 additions & 8 deletions src/Traits/PayPalAPI/InvoicesSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 2 additions & 4 deletions src/Traits/PayPalAPI/InvoicesTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';

Expand Down
6 changes: 2 additions & 4 deletions src/Traits/PayPalAPI/Reporting.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,21 @@ trait Reporting
*
* @param array $filters
* @param string $fields
* @param int $page
* @param int $page_size
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @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';

Expand Down
21 changes: 21 additions & 0 deletions src/Traits/PayPalRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/AdapterBillingPlansPricingHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/Feature/AdapterFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Adapter/DisputesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Adapter/InvoicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit ef853fb

Please sign in to comment.