Skip to content

Commit 0ee952c

Browse files
authored
Merge pull request #14 from PaymentRails/balances
Added balances
2 parents 0ca1251 + b350591 commit 0ee952c

File tree

3 files changed

+166
-2
lines changed

3 files changed

+166
-2
lines changed

lib/PaymentRails/Balance.php

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
namespace PaymentRails;
3+
4+
/**
5+
* PaymentRails Balance module
6+
* PHP Version 5
7+
* Gets merchant balances
8+
*
9+
* @package PaymentRails
10+
*
11+
*/
12+
class Balance extends Base
13+
{
14+
/**
15+
* @access protected
16+
* @var array registry of customer data
17+
*/
18+
protected $_attributes = [
19+
"accountNumber",
20+
"amount",
21+
"amountCleared",
22+
"amountPending",
23+
"currency",
24+
"display",
25+
"pendingCredit",
26+
"pendingDebit",
27+
"primary",
28+
"provider",
29+
"providerAcct",
30+
"providerId",
31+
"type"
32+
];
33+
34+
/**
35+
* Show all balances
36+
*
37+
* @return Balance
38+
*/
39+
public static function all()
40+
{
41+
return Configuration::gateway()->balance()->search([]);
42+
}
43+
44+
/**
45+
* sets instance properties from an array of values
46+
*
47+
* @ignore
48+
* @access protected
49+
* @param array $transactionAttribs array of transaction data
50+
* @return void
51+
*/
52+
protected function _initialize($attributes) {
53+
$fields = [
54+
"accountNumber",
55+
"amount",
56+
"amountCleared",
57+
"amountPending",
58+
"currency",
59+
"display",
60+
"pendingCredit",
61+
"pendingDebit",
62+
"primary",
63+
"provider",
64+
"providerAcct",
65+
"providerId",
66+
"type"
67+
];
68+
69+
foreach ($fields as $field) {
70+
if (isset($attributes[$field])) {
71+
$this->_set($field, $attributes[$field]);
72+
}
73+
}
74+
}
75+
76+
/**
77+
* factory method: returns an instance of Transaction
78+
* to the requesting method, with populated properties
79+
*
80+
* @ignore
81+
* @return Transaction
82+
*/
83+
public static function factory($attributes)
84+
{
85+
$instance = new self();
86+
$instance->_initialize($attributes);
87+
return $instance;
88+
}
89+
}
90+
91+
class_alias('PaymentRails\Balance', 'PaymentRails_Balance');

lib/PaymentRails/BalanceGateway.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
namespace PaymentRails;
3+
4+
use InvalidArgumentException;
5+
6+
/**
7+
* PaymentRails Balance processor
8+
* Gets balances
9+
*
10+
* <b>== More information ==</b>
11+
*
12+
* For more detailed information on Balance, see {@link http://docs.paymentrails.com/#balances}
13+
*
14+
* @package PaymentRails
15+
* @category Resources
16+
*/
17+
class BalanceGateway
18+
{
19+
private $_gateway;
20+
private $_config;
21+
private $_http;
22+
23+
public function __construct($gateway)
24+
{
25+
$this->_gateway = $gateway;
26+
$this->_config = $gateway->config;
27+
$this->_config->assertHasAccessTokenOrKeys();
28+
$this->_http = new Http($gateway->config);
29+
}
30+
31+
/**
32+
* Returns a ResourceCollection of transactions matching the search query.
33+
*
34+
* If <b>query</b> is a string, the search will be a basic search.
35+
* If <b>query</b> is a hash, the search will be an advanced search.
36+
* For more detailed information and examples, see {@link http://docs.paymentrails.com/#balances}
37+
*
38+
* @param mixed $query search query
39+
* @param array $options options such as page number
40+
* @return ResourceCollection
41+
* @throws InvalidArgumentException
42+
*/
43+
public function search($query)
44+
{
45+
$response = $this->_http->get('/v1/balances', $query);
46+
47+
if ($response['ok']) {
48+
$pager = [
49+
'object' => $this,
50+
'method' => 'search',
51+
'methodArgs' => $query
52+
];
53+
54+
$items = array_map(function ($item) {
55+
return Balance::factory($item);
56+
}, $response['balances']);
57+
58+
return new ResourceCollection($response, $items, $pager);
59+
} else {
60+
throw new Exception\DownForMaintenance();
61+
}
62+
}
63+
}
64+
65+
class_alias('PaymentRails\BalanceGateway', 'PaymentRails_BalanceGateway');

lib/PaymentRails/Gateway.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ public function recipient()
3131
}
3232

3333
/**
34-
* @return RecipientGateway
34+
* @return RecipientAccountGateway
3535
*/
3636
public function recipientAccount()
3737
{
3838
return new RecipientAccountGateway($this);
3939
}
4040

4141
/**
42-
* @return RecipientGateway
42+
* @return BatchGateway
4343
*/
4444
public function batch()
4545
{
@@ -61,6 +61,14 @@ public function offlinePayments()
6161
{
6262
return new OfflinePaymentGateway($this);
6363
}
64+
65+
/**
66+
* @return BalanceGateway
67+
*/
68+
public function balance()
69+
{
70+
return new BalanceGateway($this);
71+
}
6472
}
6573

6674
class_alias('PaymentRails\Gateway', 'PaymentRails_Gateway');

0 commit comments

Comments
 (0)