From ce93eccd4c27a416eefe9223ae378e4c83cceb25 Mon Sep 17 00:00:00 2001 From: Antonio Buedo Date: Wed, 11 Dec 2019 12:20:19 +0100 Subject: [PATCH] #v3.4.1912.0-rc - Model updates and ERC_20 codes (#30) * - Payout batch amount round * - Added ERC_20 support --- src/BitPaySDK/Client.php | 42 ++++++++++- src/BitPaySDK/Env.php | 2 +- src/BitPaySDK/Model/Invoice/MinerFees.php | 45 +++++++++++- src/BitPaySDK/Model/Invoice/PaymentCode.php | 28 ++++++- src/BitPaySDK/Model/Invoice/PaymentCodes.php | 73 ++++++++++++++----- src/BitPaySDK/Model/Invoice/PaymentTotal.php | 70 +++++++++++++----- .../SupportedTransactionCurrencies.php | 73 ++++++++++++++----- src/BitPaySDK/Model/Payout/PayoutBatch.php | 6 +- 8 files changed, 279 insertions(+), 60 deletions(-) diff --git a/src/BitPaySDK/Client.php b/src/BitPaySDK/Client.php index d75b9b70..c719fd1b 100644 --- a/src/BitPaySDK/Client.php +++ b/src/BitPaySDK/Client.php @@ -44,9 +44,9 @@ * Class Client * @package Bitpay * @author Antonio Buedo - * @version 3.3.1911 + * @version 3.4.1912 * See bitpay.com/api for more information. - * date 15.11.2019 + * date 10.12.2019 */ class Client { @@ -80,6 +80,11 @@ class Client */ protected $_RESTcli = null; + /** + * @var RESTcli + */ + public static $_currenciesInfo = null; + /** * Client constructor. */ @@ -1228,6 +1233,7 @@ private function init() try { $this->_RESTcli = new RESTcli($this->_env, $this->_ecKey); $this->loadAccessTokens(); + $this->loadCurrencies(); } catch (Exception $e) { throw new BitPayException("failed to build configuration : ".$e->getMessage()); } @@ -1253,4 +1259,36 @@ private function clearAccessTokenCache() { $this->_tokenCache = new Tokens(); } + + /** + * Load currencies info. + * + * @throws BitPayException BitPayException class + */ + private function loadCurrencies() + { + try { + self::$_currenciesInfo = json_decode($this->_RESTcli->get("currencies/", null, false), false); + } catch (Exception $e) { + throw new BitPayException("When loading currencies info : ".$e->getMessage()); + } + } + + /** + * Gets info for specific currency. + * + * @param $currencyCode String Currency code for which the info will be retrieved. + * + * @return object|null + */ + public static function getCurrencyInfo(string $currencyCode) + { + foreach (self::$_currenciesInfo as $currencyInfo) { + if ($currencyInfo->code == $currencyCode) { + return $currencyInfo; + } + } + + return null; + } } \ No newline at end of file diff --git a/src/BitPaySDK/Env.php b/src/BitPaySDK/Env.php index 07fafccc..536dabcc 100644 --- a/src/BitPaySDK/Env.php +++ b/src/BitPaySDK/Env.php @@ -11,5 +11,5 @@ interface Env const TestUrl = "https://test.bitpay.com/"; const ProdUrl = "https://bitpay.com/"; const BitpayApiVersion = "2.0.0"; - const BitpayPluginInfo = "BitPay_PHP_Client_v3.3.1911"; + const BitpayPluginInfo = "BitPay_PHP_Client_v3.4.1912"; } \ No newline at end of file diff --git a/src/BitPaySDK/Model/Invoice/MinerFees.php b/src/BitPaySDK/Model/Invoice/MinerFees.php index b7344d94..b9329379 100644 --- a/src/BitPaySDK/Model/Invoice/MinerFees.php +++ b/src/BitPaySDK/Model/Invoice/MinerFees.php @@ -9,12 +9,18 @@ class MinerFees protected $_btc; protected $_bch; protected $_eth; + protected $_usdc; + protected $_gusd; + protected $_pax; public function __construct() { $this->_btc = new MinerFeesItem(); $this->_bch = new MinerFeesItem(); $this->_eth = new MinerFeesItem(); + $this->_usdc = new MinerFeesItem(); + $this->_gusd = new MinerFeesItem(); + $this->_pax = new MinerFeesItem(); } public function getBTH() @@ -47,12 +53,45 @@ public function setETH(MinerFeesItem $eth) $this->_eth = $eth; } + public function getUSDC() + { + return $this->_usdc; + } + + public function setUSDC(MinerFeesItem $usdc) + { + $this->_usdc = $usdc; + } + + public function getGUSD() + { + return $this->_gusd; + } + + public function setGUSD(MinerFeesItem $gusd) + { + $this->_gusd = $gusd; + } + + public function getPAX() + { + return $this->_pax; + } + + public function setPAX(MinerFeesItem $pax) + { + $this->_pax = $pax; + } + public function toArray() { $elements = [ - 'btc' => $this->getBTH()->toArray(), - 'bch' => $this->getBCH()->toArray(), - 'eth' => $this->getETH()->toArray(), + 'btc' => $this->getBTH()->toArray(), + 'bch' => $this->getBCH()->toArray(), + 'eth' => $this->getETH()->toArray(), + 'usdc' => $this->getUSDC()->toArray(), + 'gusd' => $this->getGUSD()->toArray(), + 'pax' => $this->getPAX()->toArray(), ]; foreach ($elements as $key => $value) { diff --git a/src/BitPaySDK/Model/Invoice/PaymentCode.php b/src/BitPaySDK/Model/Invoice/PaymentCode.php index c62e70bf..e6613ff1 100644 --- a/src/BitPaySDK/Model/Invoice/PaymentCode.php +++ b/src/BitPaySDK/Model/Invoice/PaymentCode.php @@ -8,6 +8,8 @@ class PaymentCode { protected $_bip72b; protected $_bip73; + protected $_eip681; + protected $_eip681b; public function __construct() { @@ -16,8 +18,10 @@ public function __construct() public function toArray() { $elements = [ - 'bip72b' => $this->getBip72b(), - 'bip73' => $this->getBip73(), + 'bip72b' => $this->getBip72b(), + 'bip73' => $this->getBip73(), + 'eip681' => $this->getEip681(), + 'eip681b' => $this->getEip681b(), ]; foreach ($elements as $key => $value) { @@ -48,4 +52,24 @@ public function setBip73(string $bip73) { $this->_bip73 = $bip73; } + + public function getEip681() + { + return $this->_eip681; + } + + public function setEip681(string $eip681) + { + $this->_eip681 = $eip681; + } + + public function getEip681b() + { + return $this->_eip681b; + } + + public function setEip681b(string $eip681b) + { + $this->_eip681b = $eip681b; + } } \ No newline at end of file diff --git a/src/BitPaySDK/Model/Invoice/PaymentCodes.php b/src/BitPaySDK/Model/Invoice/PaymentCodes.php index 42db6936..13b81c4c 100644 --- a/src/BitPaySDK/Model/Invoice/PaymentCodes.php +++ b/src/BitPaySDK/Model/Invoice/PaymentCodes.php @@ -9,29 +9,18 @@ class PaymentCodes protected $_btc; protected $_bch; protected $_eth; + protected $_usdc; + protected $_gusd; + protected $_pax; public function __construct() { $this->_btc = new PaymentCode(); $this->_bch = new PaymentCode(); $this->_eth = new PaymentCode(); - } - - public function toArray() - { - $elements = [ - 'BTC' => $this->getBTC()->toArray(), - 'BCH' => $this->getBCH()->toArray(), - 'ETH' => $this->getETH()->toArray(), - ]; - - foreach ($elements as $key => $value) { - if (empty($value)) { - unset($elements[$key]); - } - } - - return $elements; + $this->_usdc = new PaymentCode(); + $this->_gusd = new PaymentCode(); + $this->_pax = new PaymentCode(); } public function getBTC() @@ -63,4 +52,54 @@ public function setETH(PaymentCode $eth) { $this->_eth = $eth; } + + public function getUSDC() + { + return $this->_usdc; + } + + public function setUSDC(PaymentCode $usdc) + { + $this->_usdc = $usdc; + } + + public function getGUSD() + { + return $this->_gusd; + } + + public function setGUSD(PaymentCode $gusd) + { + $this->_gusd = $gusd; + } + + public function getPAX() + { + return $this->_pax; + } + + public function setPAX(PaymentCode $pax) + { + $this->_pax = $pax; + } + + public function toArray() + { + $elements = [ + 'BTC' => $this->getBTC()->toArray(), + 'BCH' => $this->getBCH()->toArray(), + 'ETH' => $this->getETH()->toArray(), + 'USDC' => $this->getUSDC()->toArray(), + 'GUSD' => $this->getGUSD()->toArray(), + 'PAX' => $this->getPAX()->toArray(), + ]; + + foreach ($elements as $key => $value) { + if (empty($value)) { + unset($elements[$key]); + } + } + + return $elements; + } } \ No newline at end of file diff --git a/src/BitPaySDK/Model/Invoice/PaymentTotal.php b/src/BitPaySDK/Model/Invoice/PaymentTotal.php index 7c402a0c..db800b96 100644 --- a/src/BitPaySDK/Model/Invoice/PaymentTotal.php +++ b/src/BitPaySDK/Model/Invoice/PaymentTotal.php @@ -9,28 +9,14 @@ class PaymentTotal protected $_btc; protected $_bch; protected $_eth; + protected $_usdc; + protected $_gusd; + protected $_pax; public function __construct() { } - public function toArray() - { - $elements = [ - 'BTC' => $this->getBTC(), - 'BCH' => $this->getBCH(), - 'ETH' => $this->getETH(), - ]; - - foreach ($elements as $key => $value) { - if (empty($value)) { - unset($elements[$key]); - } - } - - return $elements; - } - public function getBTC() { return $this->_btc; @@ -60,4 +46,54 @@ public function setETH(float $eth) { $this->_eth = $eth; } + + public function getUSDC() + { + return $this->_usdc; + } + + public function setUSDC(float $usdc) + { + $this->_usdc = $usdc; + } + + public function getGUSD() + { + return $this->_gusd; + } + + public function setGUSD(float $gusd) + { + $this->_gusd = $gusd; + } + + public function getPAX() + { + return $this->_pax; + } + + public function setPAX(float $pax) + { + $this->_pax = $pax; + } + + public function toArray() + { + $elements = [ + 'BTC' => $this->getBTC(), + 'BCH' => $this->getBCH(), + 'ETH' => $this->getETH(), + 'USDC' => $this->getUSDC(), + 'GUSD' => $this->getGUSD(), + 'PAX' => $this->getPAX(), + ]; + + foreach ($elements as $key => $value) { + if (empty($value)) { + unset($elements[$key]); + } + } + + return $elements; + } } \ No newline at end of file diff --git a/src/BitPaySDK/Model/Invoice/SupportedTransactionCurrencies.php b/src/BitPaySDK/Model/Invoice/SupportedTransactionCurrencies.php index 1c77413a..4e5da13b 100644 --- a/src/BitPaySDK/Model/Invoice/SupportedTransactionCurrencies.php +++ b/src/BitPaySDK/Model/Invoice/SupportedTransactionCurrencies.php @@ -9,29 +9,18 @@ class SupportedTransactionCurrencies protected $_btc; protected $_bch; protected $_eth; + protected $_usdc; + protected $_gusd; + protected $_pax; public function __construct() { $this->_btc = new SupportedTransactionCurrency(); $this->_bch = new SupportedTransactionCurrency(); $this->_eth = new SupportedTransactionCurrency(); - } - - public function toArray() - { - $elements = [ - 'btc' => $this->getBTC()->toArray(), - 'bch' => $this->getBCH()->toArray(), - 'eth' => $this->getETH()->toArray(), - ]; - - foreach ($elements as $key => $value) { - if (empty($value)) { - unset($elements[$key]); - } - } - - return $elements; + $this->_usdc = new SupportedTransactionCurrency(); + $this->_gusd = new SupportedTransactionCurrency(); + $this->_pax = new SupportedTransactionCurrency(); } public function getBTC() @@ -63,4 +52,54 @@ public function setETH(SupportedTransactionCurrency $eth) { $this->_eth = $eth; } + + public function getUSDC() + { + return $this->_usdc; + } + + public function setUSDC(SupportedTransactionCurrency $usdc) + { + $this->_usdc = $usdc; + } + + public function getGUSD() + { + return $this->_gusd; + } + + public function setGUSD(SupportedTransactionCurrency $gusd) + { + $this->_gusd = $gusd; + } + + public function getPAX() + { + return $this->_pax; + } + + public function setPAX(SupportedTransactionCurrency $pax) + { + $this->_pax = $pax; + } + + public function toArray() + { + $elements = [ + 'btc' => $this->getBTC()->toArray(), + 'bch' => $this->getBCH()->toArray(), + 'eth' => $this->getETH()->toArray(), + 'usdc' => $this->getUSDC()->toArray(), + 'gusd' => $this->getGUSD()->toArray(), + 'pax' => $this->getPAX()->toArray(), + ]; + + foreach ($elements as $key => $value) { + if (empty($value)) { + unset($elements[$key]); + } + } + + return $elements; + } } \ No newline at end of file diff --git a/src/BitPaySDK/Model/Payout/PayoutBatch.php b/src/BitPaySDK/Model/Payout/PayoutBatch.php index 40a1de67..3c95e311 100644 --- a/src/BitPaySDK/Model/Payout/PayoutBatch.php +++ b/src/BitPaySDK/Model/Payout/PayoutBatch.php @@ -4,6 +4,7 @@ namespace BitPaySDK\Model\Payout; +use BitPaySDK; use BitPaySDK\Exceptions\BitPayException; use BitPaySDK\Model\Currency; @@ -63,6 +64,9 @@ public function __construct(string $currency = "USD", string $effectiveDate = nu private function _computeAndSetAmount() { + $currencyInfo = BitPaySDK\Client::getCurrencyInfo($this->_currency); + $precision = is_null($currencyInfo) ? 2 : $currencyInfo->precision; + $amount = 0.0; if ($this->_instructions) { foreach ($this->_instructions as $instruction) { @@ -73,7 +77,7 @@ private function _computeAndSetAmount() } } } - $this->_amount = $amount; + $this->_amount = round($amount, $precision); } // API fields