Skip to content

Commit c9581b6

Browse files
authored
Merge pull request #240 from mwarzybok-sumoheavy/feature/SP-548
SP-548 Issue with PayoutBatch
2 parents 6555139 + 12a8b0e commit c9581b6

File tree

4 files changed

+69
-17
lines changed

4 files changed

+69
-17
lines changed

src/BitPaySDK/Env.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface Env
99
const TestUrl = "https://test.bitpay.com/";
1010
const ProdUrl = "https://bitpay.com/";
1111
const BitpayApiVersion = "2.0.0";
12-
const BitpayPluginInfo = "BitPay_PHP_Client_v7.3.0";
12+
const BitpayPluginInfo = "BitPay_PHP_Client_v7.3.1";
1313
const BitpayApiFrame = "std";
1414
const BitpayApiFrameVersion = "1.0.0";
1515
}

src/BitPaySDK/Model/Payout/PayoutBatch.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ private function _computeAndSetAmount()
8383
foreach ($this->_instructions as $instruction) {
8484
if ($instruction instanceof PayoutInstruction) {
8585
$amount += $instruction->getAmount();
86+
} elseif ($instruction instanceof \stdClass) {
87+
$amount += $instruction->amount;
8688
} else {
8789
$amount += $instruction['amount'];
8890
}
@@ -220,6 +222,9 @@ public function setEffectiveDate(string $effectiveDate)
220222
public function getInstructions()
221223
{
222224
$instructions = [];
225+
if (!$this->_instructions || !is_array($this->_instructions)) {
226+
return $instructions;
227+
}
223228

224229
foreach ($this->_instructions as $instruction) {
225230
if ($instruction instanceof PayoutInstruction) {

test/unit/BitPaySDK/ClientTest.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BitPaySDK\Test;
44

55
use BitPaySDK\Client;
6+
use BitPaySDK\Env;
67
use BitPaySDK\Exceptions\BillCreationException;
78
use BitPaySDK\Exceptions\BillDeliveryException;
89
use BitPaySDK\Exceptions\BillQueryException;
@@ -47,18 +48,19 @@
4748
use BitPaySDK\Model\Ledger\LedgerEntry;
4849
use BitPaySDK\Model\Payout\Payout;
4950
use BitPaySDK\Model\Payout\PayoutBatch;
51+
use BitPaySDK\Model\Payout\PayoutInstruction;
5052
use BitPaySDK\Model\Payout\PayoutRecipient;
5153
use BitPaySDK\Model\Payout\PayoutRecipients;
54+
use BitPaySDK\Model\Payout\RecipientReferenceMethod;
5255
use BitPaySDK\Model\Rate\Rate;
5356
use BitPaySDK\Model\Rate\Rates;
5457
use BitPaySDK\Model\Settlement\Settlement;
5558
use BitPaySDK\Model\Subscription\Subscription;
5659
use BitPaySDK\Model\Wallet\Wallet;
60+
use BitPaySDK\Tokens;
5761
use BitPaySDK\Util\RESTcli\RESTcli;
5862
use Exception;
5963
use PHPUnit\Framework\TestCase;
60-
use BitPaySDK\Env;
61-
use BitPaySDK\Tokens;
6264

6365

6466
class ClientTest extends TestCase
@@ -2334,15 +2336,22 @@ public function testGetPayoutBatch($testedObject)
23342336
$examplePayoutBatchId = 'test';
23352337
$params['token'] = $this->getPayoutTokenFromFile();
23362338
$restCliMock = $this->getRestCliMock();
2337-
$restCliMock->expects($this->once())->method('get')->with("payoutBatches/" . $examplePayoutBatchId, $params)->willReturn(self::CORRECT_JSON_STRING);
2339+
$restCliMock->expects(self::once())->method('get')
2340+
->with("payoutBatches/" . $examplePayoutBatchId, $params)
2341+
->willReturn(file_get_contents('json/getPayoutBatchResponse.json', true));
23382342
$setRestCli = function () use ($restCliMock) {
23392343
$this->_RESTcli = $restCliMock;
23402344
};
23412345
$doSetRestCli = $setRestCli->bindTo($testedObject, get_class($testedObject));
23422346
$doSetRestCli();
23432347

23442348
$result = $testedObject->getPayoutBatch($examplePayoutBatchId);
2345-
$this->assertInstanceOf(PayoutBatch::class, $result);
2349+
self::assertEquals('8dJGJ65BhrKJmcgfChfg65dn6fuENkKzhgf3hg4hglkj675hn', $result->getToken());
2350+
self::assertEquals(24.0, $result->getAmount());
2351+
self::assertEquals('USD', $result->getLedgerCurrency());
2352+
self::assertEquals('1-855-4-BITPAY', $result->getSupportPhone());
2353+
self::assertEquals('LRNmxyjRN1e2JUc6fhgfd', $result->getId());
2354+
self::assertEquals(12.0, $result->getInstructions()[0]->amount);
23462355
}
23472356

23482357
/**
@@ -3046,27 +3055,32 @@ public function testRequestPayoutBatchNotificationShouldCatchJsonEncodeException
30463055
*/
30473056
public function testSubmitPayoutBatch($testedObject)
30483057
{
3049-
$payoutBatchMock = $this->createMock(PayoutBatch::class);
3050-
$exampleCurrency = Currency::USD;
3051-
$payoutBatchToArray = [
3052-
'token' => $this->getPayoutTokenFromFile(),
3053-
'currency' => $exampleCurrency
3054-
];
3058+
$payoutBatchMock = new PayoutBatch();
3059+
$payoutInstruction = new PayoutInstruction(12, RecipientReferenceMethod::EMAIL, 'any@email.com');
3060+
$payoutInstruction2 = new PayoutInstruction(12, RecipientReferenceMethod::EMAIL, 'any2@email.com');
30553061

3056-
$payoutBatchMock->method('getCurrency')->willReturn(Currency::USD);
3057-
$payoutBatchMock->method('toArray')->willReturn($payoutBatchToArray);
3058-
$restCliMock = $this->getRestCliMock();
3059-
$restCliMock->expects($this->once())->method('post')->with("payoutBatches", $payoutBatchMock->toArray())
3060-
->willReturn(json_encode($payoutBatchMock->toArray()));
3062+
$payoutBatchMock->setToken($this->getPayoutTokenFromFile());
3063+
$payoutBatchMock->setCurrency(Currency::USD);
3064+
$payoutBatchMock->setLedgerCurrency(Currency::USD);
3065+
$payoutBatchMock->setInstructions([$payoutInstruction, $payoutInstruction2]);
30613066

3067+
$restCliMock = $this->getRestCliMock();
3068+
$restCliMock->expects(self::once())->method('post')->with("payoutBatches", $payoutBatchMock->toArray())
3069+
->willReturn(file_get_contents('json/getPayoutBatchResponse.json', true));
30623070
$setRestCli = function () use ($restCliMock) {
30633071
$this->_RESTcli = $restCliMock;
30643072
};
3073+
30653074
$doSetRestCli = $setRestCli->bindTo($testedObject, get_class($testedObject));
30663075
$doSetRestCli();
30673076

30683077
$result = $testedObject->submitPayoutBatch($payoutBatchMock);
3069-
$this->assertInstanceOf(PayoutBatch::class, $result);
3078+
self::assertEquals('8dJGJ65BhrKJmcgfChfg65dn6fuENkKzhgf3hg4hglkj675hn', $result->getToken());
3079+
self::assertEquals(24.0, $result->getAmount());
3080+
self::assertEquals('USD', $result->getLedgerCurrency());
3081+
self::assertEquals('1-855-4-BITPAY', $result->getSupportPhone());
3082+
self::assertEquals('LRNmxyjRN1e2JUc6fhgfd', $result->getId());
3083+
self::assertEquals(12.0, $result->getInstructions()[0]->amount);
30703084
}
30713085

30723086
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"id": "LRNmxyjRN1e2JUc6fhgfd",
3+
"account": "6iyfXz1dU54Ntrdtr4ez3ch",
4+
"status": "new",
5+
"amount": 12,
6+
"currency": "USD",
7+
"ledgerCurrency": "USD",
8+
"requestDate": "2023-05-04T11:10:26.032Z",
9+
"effectiveDate": "2023-05-04T09:00:00.000Z",
10+
"ignoreEmails": false,
11+
"supportPhone": "1-855-4-BITPAY",
12+
"instructions": [
13+
{
14+
"id": "XB85mxDdaYkj24hrgs",
15+
"payoutId": "Mfdgu5S589birhug24hj23",
16+
"amount": 12,
17+
"email": "any@email.com",
18+
"recipientId": "VeZgfxdgfc687tg34jk",
19+
"shopperId": "2toTAVjhkh2dgf65x",
20+
"transactions": []
21+
},
22+
{
23+
"id": "XB85mxDdaghj24hrgs",
24+
"payoutId": "Mfdgu5S589bifghg24hj23",
25+
"amount": 12,
26+
"email": "any2@email.com",
27+
"recipientId": "VeZgfxdgddfg4jk",
28+
"shopperId": "2toTAVjhksdfdgf65x",
29+
"transactions": []
30+
}
31+
],
32+
"token": "8dJGJ65BhrKJmcgfChfg65dn6fuENkKzhgf3hg4hglkj675hn"
33+
}

0 commit comments

Comments
 (0)