Skip to content

Commit 30eb79f

Browse files
author
Antonio Buedo
authored
#v5.0.2011.0-beta.1 - New payouts infrastructure (#51)
* - Added payout recipients resource * - Fixed getInvoices * - Functional tests update * - Payout Batch model updated * - Version upgrade * - BitPay Api Frame Version upgrade * - Composer lock update * - Code refactor
1 parent 7c47450 commit 30eb79f

File tree

11 files changed

+772
-308
lines changed

11 files changed

+772
-308
lines changed

composer.lock

Lines changed: 174 additions & 247 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BitPaySDK/Client.php

Lines changed: 156 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use BitPaySDK\Exceptions\PayoutQueryException;
2222
use BitPaySDK\Exceptions\RateQueryException;
2323
use BitPaySDK\Exceptions\RefundCreationException;
24+
use BitPaySDK\Exceptions\RefundCancellationException;
2425
use BitPaySDK\Exceptions\RefundQueryException;
2526
use BitPaySDK\Exceptions\SettlementQueryException;
2627
use BitPaySDK\Exceptions\SubscriptionCreationException;
@@ -32,6 +33,8 @@
3233
use BitPaySDK\Model\Invoice\Refund;
3334
use BitPaySDK\Model\Ledger\Ledger;
3435
use BitPaySDK\Model\Payout\PayoutBatch;
36+
use BitPaySDK\Model\Payout\PayoutRecipient;
37+
use BitPaySDK\Model\Payout\PayoutRecipients;
3538
use BitPaySDK\Model\Rate\Rates;
3639
use BitPaySDK\Model\Settlement\Settlement;
3740
use BitPaySDK\Model\Subscription\Subscription;
@@ -44,9 +47,9 @@
4447
* Class Client
4548
* @package Bitpay
4649
* @author Antonio Buedo
47-
* @version 4.0.2006
50+
* @version 5.0.2011
4851
* See bitpay.com/api for more information.
49-
* date 19.06.2020
52+
* date 30.10.2020
5053
*/
5154
class Client
5255
{
@@ -253,13 +256,13 @@ public function getInvoices(
253256
if ($status) {
254257
$params["status"] = $status;
255258
}
256-
if ($status) {
259+
if ($orderId) {
257260
$params["orderId"] = $orderId;
258261
}
259-
if ($status) {
262+
if ($limit) {
260263
$params["limit"] = $limit;
261264
}
262-
if ($status) {
265+
if ($offset) {
263266
$params["offset"] = $offset;
264267
}
265268

@@ -402,7 +405,7 @@ public function getRefund(
402405
* @param $refund Refund The BitPay invoice having the associated refund to be canceled. Must have been obtained
403406
* using the merchant facade.
404407
* @return bool True if the refund was successfully canceled, false otherwise.
405-
* @throws BitPayException BitPayException class
408+
* @throws RefundCancellationException RefundCancellationException class
406409
*/
407410
public function cancelRefund(string $invoiceId, Refund $refund): bool
408411
{
@@ -412,14 +415,14 @@ public function cancelRefund(string $invoiceId, Refund $refund): bool
412415

413416
$responseJson = $this->_RESTcli->delete("invoices/".$invoiceId."/refunds/".$refund->getId(), $params);
414417
} catch (Exception $e) {
415-
throw new PayoutCancellationException("failed to serialize server object : ".$e->getMessage());
418+
throw new RefundCancellationException("failed to serialize server object : ".$e->getMessage());
416419
}
417420

418421
try {
419422
$result = strtolower(trim($responseJson, '"')) === "success";
420423

421424
} catch (Exception $e) {
422-
throw new PayoutCancellationException(
425+
throw new RefundCancellationException(
423426
"failed to deserialize BitPay server response (Refund) : ".$e->getMessage());
424427
}
425428

@@ -429,7 +432,7 @@ public function cancelRefund(string $invoiceId, Refund $refund): bool
429432
/**
430433
* Create a BitPay Bill.
431434
*
432-
* @param $bill string A Bill object with request parameters defined.
435+
* @param $bill Bill A Bill object with request parameters defined.
433436
* @param $facade string The facade used to create it.
434437
* @param $signRequest bool Signed request.
435438
* @return Bill A BitPay generated Bill object.
@@ -701,6 +704,148 @@ public function getLedgers(): array
701704
return $ledgers;
702705
}
703706

707+
/**
708+
* Submit BitPay Payout Recipients.
709+
*
710+
* @param $recipients PayoutRecipients A PayoutRecipients object with request parameters defined.
711+
* @return array A list of BitPay PayoutRecipients objects..
712+
* @throws PayoutCreationException BitPayException class
713+
*/
714+
public function submitPayoutRecipients(PayoutRecipients $recipients): array
715+
{
716+
try {
717+
$recipients->setToken($this->_tokenCache->getTokenByFacade(Facade::Payroll));
718+
$recipients->setGuid(Util::guid());
719+
720+
$responseJson = $this->_RESTcli->post("recipients", $recipients->toArray());
721+
} catch (Exception $e) {
722+
throw new PayoutCreationException("failed to serialize PayoutRecipients object : ".$e->getMessage());
723+
}
724+
725+
try {
726+
$mapper = new JsonMapper();
727+
$recipients = $mapper->mapArray(
728+
json_decode($responseJson),
729+
[],
730+
'BitPaySDK\Model\Payout\PayoutRecipient'
731+
);
732+
733+
} catch (Exception $e) {
734+
throw new PayoutCreationException(
735+
"failed to deserialize BitPay server response (PayoutRecipients) : ".$e->getMessage());
736+
}
737+
738+
return $recipients;
739+
}
740+
741+
/**
742+
* Retrieve a collection of BitPay Payout Recipients.
743+
*
744+
* @param $status string|null The recipient status you want to query on.
745+
* @param $limit int|null Maximum results that the query will return (useful for paging results).
746+
* result).
747+
* @return array A list of BitPayRecipient objects.
748+
* @throws BitPayException BitPayException class
749+
*/
750+
public function getPayoutRecipients(
751+
string $status = null,
752+
int $limit = null
753+
): array {
754+
try {
755+
$params = [];
756+
$params["token"] = $this->_tokenCache->getTokenByFacade(Facade::Payroll);
757+
if ($status) {
758+
$params["status"] = $status;
759+
}
760+
if ($limit) {
761+
$params["limit"] = $limit;
762+
}
763+
764+
$responseJson = $this->_RESTcli->get("recipients", $params);
765+
} catch (Exception $e) {
766+
throw new PayoutQueryException("failed to serialize PayoutRecipients object : ".$e->getMessage());
767+
}
768+
769+
try {
770+
$mapper = new JsonMapper();
771+
$recipients = $mapper->mapArray(
772+
json_decode($responseJson),
773+
[],
774+
'BitPaySDK\Model\Payout\PayoutRecipient'
775+
);
776+
777+
} catch (Exception $e) {
778+
throw new PayoutQueryException(
779+
"failed to deserialize BitPay server response (PayoutRecipients) : ".$e->getMessage());
780+
}
781+
782+
return $recipients;
783+
}
784+
785+
/**
786+
* Retrieve a BitPay payout recipient by batch id using. The client must have been previously authorized for the
787+
* payroll facade.
788+
*
789+
* @param $recipientId string The id of the recipient to retrieve.
790+
* @return PayoutRecipient A BitPay PayoutRecipient object.
791+
* @throws PayoutQueryException BitPayException class
792+
*/
793+
public function getPayoutRecipient(string $recipientId): PayoutRecipient
794+
{
795+
try {
796+
$params = [];
797+
$params["token"] = $this->_tokenCache->getTokenByFacade(Facade::Payroll);
798+
799+
$responseJson = $this->_RESTcli->get("recipients/".$recipientId, $params);
800+
} catch (Exception $e) {
801+
throw new PayoutQueryException("failed to serialize PayoutRecipient object : ".$e->getMessage());
802+
}
803+
804+
try {
805+
$mapper = new JsonMapper();
806+
$recipient = $mapper->map(
807+
json_decode($responseJson),
808+
new PayoutRecipient()
809+
);
810+
811+
} catch (Exception $e) {
812+
throw new PayoutQueryException(
813+
"failed to deserialize BitPay server response (PayoutRecipient) : ".$e->getMessage());
814+
}
815+
816+
return $recipient;
817+
}
818+
819+
// TODO Resource inopperative
820+
// /**
821+
// * Notify BitPay Payout Recipients.
822+
// *
823+
// * @param $recipientId string The id of the recipient to notify.
824+
// * @return bool True if the notification was successfully sent, false otherwise.
825+
// * @throws PayoutCreationException BitPayException class
826+
// */
827+
// public function notifyPayoutRecipient(string $recipientId): array
828+
// {
829+
// try {
830+
// $content = [];
831+
// $content["token"] = $this->_tokenCache->getTokenByFacade(Facade::Payroll);
832+
//
833+
// $responseJson = $this->_RESTcli->post("recipients/".$recipientId."/notifications", $content);
834+
// } catch (Exception $e) {
835+
// throw new PayoutCreationException("failed to serialize PayoutRecipients object : ".$e->getMessage());
836+
// }
837+
//
838+
// try {
839+
// $result = json_decode($responseJson)->success;
840+
//
841+
// } catch (Exception $e) {
842+
// throw new PayoutCreationException(
843+
// "failed to deserialize BitPay server response (PayoutRecipients) : ".$e->getMessage());
844+
// }
845+
//
846+
// return $result;
847+
// }
848+
704849
/**
705850
* Submit a BitPay Payout batch.
706851
*
@@ -902,7 +1047,7 @@ public function getSettlements(
9021047
/**
9031048
* Retrieves a summary of the specified settlement.
9041049
*
905-
* @param $settlementId Settlement Id.
1050+
* @param $settlementId string Settlement Id.
9061051
* @return Settlement A BitPay Settlement object.
9071052
* @throws BitPayException BitPayException class
9081053
*/
@@ -968,7 +1113,7 @@ public function getSettlementReconciliationReport(Settlement $settlement): Settl
9681113
/**
9691114
* Create a BitPay Subscription.
9701115
*
971-
* @param $subscription string A Subscription object with request parameters defined.
1116+
* @param $subscription Subscription A Subscription object with request parameters defined.
9721117
* @return Subscription A BitPay generated Subscription object.
9731118
* @throws BitPayException BitPayException class
9741119
*/

src/BitPaySDK/Env.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface Env
1111
const TestUrl = "https://test.bitpay.com/";
1212
const ProdUrl = "https://bitpay.com/";
1313
const BitpayApiVersion = "2.0.0";
14-
const BitpayPluginInfo = "BitPay_PHP_Client_v4.0.2006";
14+
const BitpayPluginInfo = "BitPay_PHP_Client_v5.0.2011";
1515
const BitpayApiFrame = "custom";
16-
const BitpayApiFrameVersion = "1.0.0";
16+
const BitpayApiFrameVersion = "2.0.0";
1717
}

src/BitPaySDK/Model/Payout/PayoutBatch.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
*/
1515
class PayoutBatch
1616
{
17-
const MethodManual2 = "manual_2";
18-
const MethodVwap24 = "vwap_24hr";
17+
const MethodVwap24 = "vwap_24hr";
1918

2019
protected $_guid = "";
2120
protected $_token = "";
@@ -45,10 +44,12 @@ class PayoutBatch
4544
/**
4645
* Constructor, create an instruction-full request PayoutBatch object.
4746
*
48-
* @param $currency string The three digit currency string for the PayoutBatch to use.
49-
* @param $effectiveDate string Date when request is effective. Note that the time of day will automatically be set
50-
* to 09:00:00.000 UTC time for the given day. Only requests submitted before 09:00:00.000
51-
* UTC are guaranteed to be processed on the same day.
47+
* @param $currency string The three digit currency string for the PayoutBatch to use.
48+
* @param $effectiveDate string Date when request is effective. Note that the time of day will
49+
* automatically be set to 09:00:00.000 UTC time for the given day. Only requests
50+
* submitted before 09:00:00.000 UTC are guaranteed to be processed on the same
51+
* day.
52+
* @param array|null $instructions
5253
*/
5354
public function __construct(string $currency = "USD", string $effectiveDate = null, array $instructions = null)
5455
{
@@ -318,16 +319,6 @@ public function setDateExecuted(string $dateExecuted)
318319
$this->_dateExecuted = $dateExecuted;
319320
}
320321

321-
public function setCurrencyInfo(array $currencyInfo)
322-
{
323-
$this->_currnecyinfo = $currencyInfo;
324-
}
325-
326-
public function getCurrencyInfo()
327-
{
328-
return $this->_currencyInfo;
329-
}
330-
331322
public function toArray()
332323
{
333324
$elements = [

0 commit comments

Comments
 (0)