Skip to content

Commit

Permalink
Merge pull request #240 from mwarzybok-sumoheavy/feature/SP-548
Browse files Browse the repository at this point in the history
SP-548 Issue with PayoutBatch
  • Loading branch information
bobbrodie committed May 5, 2023
2 parents 6555139 + 12a8b0e commit c9581b6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/BitPaySDK/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface Env
const TestUrl = "https://test.bitpay.com/";
const ProdUrl = "https://bitpay.com/";
const BitpayApiVersion = "2.0.0";
const BitpayPluginInfo = "BitPay_PHP_Client_v7.3.0";
const BitpayPluginInfo = "BitPay_PHP_Client_v7.3.1";
const BitpayApiFrame = "std";
const BitpayApiFrameVersion = "1.0.0";
}
5 changes: 5 additions & 0 deletions src/BitPaySDK/Model/Payout/PayoutBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ private function _computeAndSetAmount()
foreach ($this->_instructions as $instruction) {
if ($instruction instanceof PayoutInstruction) {
$amount += $instruction->getAmount();
} elseif ($instruction instanceof \stdClass) {
$amount += $instruction->amount;
} else {
$amount += $instruction['amount'];
}
Expand Down Expand Up @@ -220,6 +222,9 @@ public function setEffectiveDate(string $effectiveDate)
public function getInstructions()
{
$instructions = [];
if (!$this->_instructions || !is_array($this->_instructions)) {
return $instructions;
}

foreach ($this->_instructions as $instruction) {
if ($instruction instanceof PayoutInstruction) {
Expand Down
46 changes: 30 additions & 16 deletions test/unit/BitPaySDK/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BitPaySDK\Test;

use BitPaySDK\Client;
use BitPaySDK\Env;
use BitPaySDK\Exceptions\BillCreationException;
use BitPaySDK\Exceptions\BillDeliveryException;
use BitPaySDK\Exceptions\BillQueryException;
Expand Down Expand Up @@ -47,18 +48,19 @@
use BitPaySDK\Model\Ledger\LedgerEntry;
use BitPaySDK\Model\Payout\Payout;
use BitPaySDK\Model\Payout\PayoutBatch;
use BitPaySDK\Model\Payout\PayoutInstruction;
use BitPaySDK\Model\Payout\PayoutRecipient;
use BitPaySDK\Model\Payout\PayoutRecipients;
use BitPaySDK\Model\Payout\RecipientReferenceMethod;
use BitPaySDK\Model\Rate\Rate;
use BitPaySDK\Model\Rate\Rates;
use BitPaySDK\Model\Settlement\Settlement;
use BitPaySDK\Model\Subscription\Subscription;
use BitPaySDK\Model\Wallet\Wallet;
use BitPaySDK\Tokens;
use BitPaySDK\Util\RESTcli\RESTcli;
use Exception;
use PHPUnit\Framework\TestCase;
use BitPaySDK\Env;
use BitPaySDK\Tokens;


class ClientTest extends TestCase
Expand Down Expand Up @@ -2334,15 +2336,22 @@ public function testGetPayoutBatch($testedObject)
$examplePayoutBatchId = 'test';
$params['token'] = $this->getPayoutTokenFromFile();
$restCliMock = $this->getRestCliMock();
$restCliMock->expects($this->once())->method('get')->with("payoutBatches/" . $examplePayoutBatchId, $params)->willReturn(self::CORRECT_JSON_STRING);
$restCliMock->expects(self::once())->method('get')
->with("payoutBatches/" . $examplePayoutBatchId, $params)
->willReturn(file_get_contents('json/getPayoutBatchResponse.json', true));
$setRestCli = function () use ($restCliMock) {
$this->_RESTcli = $restCliMock;
};
$doSetRestCli = $setRestCli->bindTo($testedObject, get_class($testedObject));
$doSetRestCli();

$result = $testedObject->getPayoutBatch($examplePayoutBatchId);
$this->assertInstanceOf(PayoutBatch::class, $result);
self::assertEquals('8dJGJ65BhrKJmcgfChfg65dn6fuENkKzhgf3hg4hglkj675hn', $result->getToken());
self::assertEquals(24.0, $result->getAmount());
self::assertEquals('USD', $result->getLedgerCurrency());
self::assertEquals('1-855-4-BITPAY', $result->getSupportPhone());
self::assertEquals('LRNmxyjRN1e2JUc6fhgfd', $result->getId());
self::assertEquals(12.0, $result->getInstructions()[0]->amount);
}

/**
Expand Down Expand Up @@ -3046,27 +3055,32 @@ public function testRequestPayoutBatchNotificationShouldCatchJsonEncodeException
*/
public function testSubmitPayoutBatch($testedObject)
{
$payoutBatchMock = $this->createMock(PayoutBatch::class);
$exampleCurrency = Currency::USD;
$payoutBatchToArray = [
'token' => $this->getPayoutTokenFromFile(),
'currency' => $exampleCurrency
];
$payoutBatchMock = new PayoutBatch();
$payoutInstruction = new PayoutInstruction(12, RecipientReferenceMethod::EMAIL, 'any@email.com');
$payoutInstruction2 = new PayoutInstruction(12, RecipientReferenceMethod::EMAIL, 'any2@email.com');

$payoutBatchMock->method('getCurrency')->willReturn(Currency::USD);
$payoutBatchMock->method('toArray')->willReturn($payoutBatchToArray);
$restCliMock = $this->getRestCliMock();
$restCliMock->expects($this->once())->method('post')->with("payoutBatches", $payoutBatchMock->toArray())
->willReturn(json_encode($payoutBatchMock->toArray()));
$payoutBatchMock->setToken($this->getPayoutTokenFromFile());
$payoutBatchMock->setCurrency(Currency::USD);
$payoutBatchMock->setLedgerCurrency(Currency::USD);
$payoutBatchMock->setInstructions([$payoutInstruction, $payoutInstruction2]);

$restCliMock = $this->getRestCliMock();
$restCliMock->expects(self::once())->method('post')->with("payoutBatches", $payoutBatchMock->toArray())
->willReturn(file_get_contents('json/getPayoutBatchResponse.json', true));
$setRestCli = function () use ($restCliMock) {
$this->_RESTcli = $restCliMock;
};

$doSetRestCli = $setRestCli->bindTo($testedObject, get_class($testedObject));
$doSetRestCli();

$result = $testedObject->submitPayoutBatch($payoutBatchMock);
$this->assertInstanceOf(PayoutBatch::class, $result);
self::assertEquals('8dJGJ65BhrKJmcgfChfg65dn6fuENkKzhgf3hg4hglkj675hn', $result->getToken());
self::assertEquals(24.0, $result->getAmount());
self::assertEquals('USD', $result->getLedgerCurrency());
self::assertEquals('1-855-4-BITPAY', $result->getSupportPhone());
self::assertEquals('LRNmxyjRN1e2JUc6fhgfd', $result->getId());
self::assertEquals(12.0, $result->getInstructions()[0]->amount);
}

/**
Expand Down
33 changes: 33 additions & 0 deletions test/unit/BitPaySDK/json/getPayoutBatchResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"id": "LRNmxyjRN1e2JUc6fhgfd",
"account": "6iyfXz1dU54Ntrdtr4ez3ch",
"status": "new",
"amount": 12,
"currency": "USD",
"ledgerCurrency": "USD",
"requestDate": "2023-05-04T11:10:26.032Z",
"effectiveDate": "2023-05-04T09:00:00.000Z",
"ignoreEmails": false,
"supportPhone": "1-855-4-BITPAY",
"instructions": [
{
"id": "XB85mxDdaYkj24hrgs",
"payoutId": "Mfdgu5S589birhug24hj23",
"amount": 12,
"email": "any@email.com",
"recipientId": "VeZgfxdgfc687tg34jk",
"shopperId": "2toTAVjhkh2dgf65x",
"transactions": []
},
{
"id": "XB85mxDdaghj24hrgs",
"payoutId": "Mfdgu5S589bifghg24hj23",
"amount": 12,
"email": "any2@email.com",
"recipientId": "VeZgfxdgddfg4jk",
"shopperId": "2toTAVjhksdfdgf65x",
"transactions": []
}
],
"token": "8dJGJ65BhrKJmcgfChfg65dn6fuENkKzhgf3hg4hglkj675hn"
}

0 comments on commit c9581b6

Please sign in to comment.