Skip to content

Commit 67ffa1d

Browse files
authored
Merge pull request #22 from ionutcalara/master
Handle pagination via cursors
2 parents a7862d0 + 3f38b43 commit 67ffa1d

File tree

9 files changed

+591
-78
lines changed

9 files changed

+591
-78
lines changed

README.md

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ $merchants = $paylike->merchants();
6363
$merchants->create($args);
6464
$merchants->fetch($merchant_id);
6565
$merchants->update($merchant_id, $args);
66-
$all_merchants = $merchants->get($app_id,$limit,$before);
66+
$all_merchants = $merchants->find($app_id,$args);
67+
$some_merchants = $merchants->before($app_id,$before);
68+
$some_merchants = $merchants->after($app_id,$before);
6769

6870
$cards = $paylike->cards();
6971
$cards->create($merchant_id, $args);
@@ -75,28 +77,13 @@ $transactions->fetch($transaction_id);
7577
$transactions->capture($transaction_id, $args);
7678
$transactions->void($transaction_id, $args);
7779
$transactions->refund($transaction_id, $args);
78-
$all_transactions = $transactions->get($merchant_id,$limit,$before);
80+
$all_transactions = $transactions->find($merchant_id,$args);
81+
$some_transactions = $transactions->before($merchant_id,$before);
82+
$some_transactions = $transactions->after($merchant_id,$before);
7983
```
8084

8185
## Pagination
82-
The methods that allow fetching all transactions/merchants support pagination. By default they are limited to the last 10 items.
83-
An example of handling pagination to fetch all available transactions:
84-
85-
```php
86-
$transactions = array();
87-
$limit = 10;
88-
$before = null;
89-
90-
do {
91-
$api_transactions = $this->transactions->get($merchant_id, $limit, $before);
92-
if (count($api_transactions) < $limit) {
93-
$before = null;
94-
} else {
95-
$before = $api_transactions[$limit - 1]['id'];
96-
}
97-
$transactions = array_merge($transactions, $api_transactions);
98-
} while ($before);
99-
```
86+
The methods that return multiple merchants/transactions (find,after,before) use cursors, so you don't need to worry about pagination, you can access any index, or iterate all the items, this is handled in the background.
10087

10188
## Error handling
10289

changelog.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## Paylike client (PHP)
88

9+
## 1.0.2 - 2018-09-12
10+
### Changed
11+
- Transactions and Merchants now support find and return a filter
12+
13+
### Removed
14+
- Transactions and Merchants no longer have get method
15+
916
## 1.0.1 - 2018-09-04
1017
### Added
1118
- This CHANGELOG file

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "paylike/php-api",
33
"description": "PHP SDK to communicate with the Paylike HTTP api",
4-
"version": "1.0.1",
4+
"version": "1.0.2",
55
"license": "MIT",
66
"authors": [
77
{

src/Paylike.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class Paylike
3636
* Paylike constructor.
3737
*
3838
* @param $api_key
39-
* @param HttpClientInterface $client
39+
* @param HttpClientInterface $client
40+
* @throws Exception\ApiException
4041
*/
4142
public function __construct($api_key, HttpClientInterface $client = null)
4243
{

src/Resource/Merchants.php

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Paylike\Resource;
44

5+
use Paylike\Utils\Cursor;
6+
57
/**
68
* Class Merchants
79
*
@@ -56,20 +58,47 @@ public function update($merchant_id, $args)
5658
}
5759

5860
/**
59-
* https://github.com/paylike/api-docs#fetch-all-merchants
60-
* @param int $app_id
61-
* @param int $limit
62-
* @param null $before
63-
* @return array
61+
* @link https://github.com/paylike/api-docs#fetch-all-merchants
62+
*
63+
* @param $app_id
64+
* @param array $args
65+
* @return Cursor
66+
* @throws \Exception
6467
*/
65-
public function get($app_id, $limit = 10, $before = null)
68+
public function find($app_id, $args = array())
6669
{
67-
$url = 'identities/' . $app_id . '/merchants?limit=' . $limit;
68-
if ($before) {
69-
$url .= '&before=' . $before;
70+
$url = 'identities/' . $app_id . '/merchants';
71+
if (!isset($args['limit'])) {
72+
$args['limit'] = 10;
7073
}
71-
$api_response = $this->paylike->client->request('GET', $url);
74+
$api_response = $this->paylike->client->request('GET', $url, $args);
7275
$merchants = $api_response->json;
73-
return $merchants;
76+
return new Cursor($url, $args, $merchants, $this->paylike);
77+
}
78+
79+
/**
80+
* @link https://github.com/paylike/api-docs#fetch-all-merchants
81+
*
82+
* @param $app_id
83+
* @param $merchant_id
84+
* @return Cursor
85+
* @throws \Exception
86+
*/
87+
public function before($app_id, $merchant_id)
88+
{
89+
return $this->find($app_id, array('before' => $merchant_id));
90+
}
91+
92+
/**
93+
* @link https://github.com/paylike/api-docs#fetch-all-merchants
94+
*
95+
* @param $app_id
96+
* @param $merchant_id
97+
* @return Cursor
98+
* @throws \Exception
99+
*/
100+
public function after($app_id, $merchant_id)
101+
{
102+
return $this->find($app_id, array('after' => $merchant_id));
74103
}
75104
}

src/Resource/Transactions.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Paylike\Resource;
44

5+
use Paylike\Utils\Cursor;
6+
57
/**
68
* Class Transactions
79
*
@@ -97,16 +99,44 @@ public function refund($transaction_id, $args)
9799
* @link https://github.com/paylike/api-docs#fetch-all-transactions
98100
*
99101
* @param $merchant_id
100-
* @param int $limit
101-
* @param null $before
102+
* @param array $args
103+
* @return Cursor
104+
* @throws \Exception
102105
*/
103-
public function get($merchant_id, $limit = 10, $before = null){
104-
$url = 'merchants/' . $merchant_id . '/transactions?limit=' . $limit;
105-
if ($before) {
106-
$url .= '&before=' . $before;
106+
public function find($merchant_id, $args = array())
107+
{
108+
$url = 'merchants/' . $merchant_id . '/transactions';
109+
if (!isset($args['limit'])) {
110+
$args['limit'] = 10;
107111
}
108-
$api_response = $this->paylike->client->request('GET', $url);
109-
$merchants = $api_response->json;
110-
return $merchants;
112+
$api_response = $this->paylike->client->request('GET', $url, $args);
113+
$transactions = $api_response->json;
114+
return new Cursor($url, $args, $transactions, $this->paylike);
115+
}
116+
117+
/**
118+
* @link https://github.com/paylike/api-docs#fetch-all-transactions
119+
*
120+
* @param $merchant_id
121+
* @param $transaction_id
122+
* @return Cursor
123+
* @throws \Exception
124+
*/
125+
public function before($merchant_id, $transaction_id)
126+
{
127+
return $this->find($merchant_id, array('before' => $transaction_id));
128+
}
129+
130+
/**
131+
* @link https://github.com/paylike/api-docs#fetch-all-transactions
132+
*
133+
* @param $merchant_id
134+
* @param $transaction_id
135+
* @return Cursor
136+
* @throws \Exception
137+
*/
138+
public function after($merchant_id, $transaction_id)
139+
{
140+
return $this->find($merchant_id, array('after' => $transaction_id));
111141
}
112142
}

0 commit comments

Comments
 (0)