diff --git a/composer.json b/composer.json index 63bc3b70..7274d5d2 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "php": "^7.3", "guzzlehttp/guzzle": "^6.3", "ext-json": "^1.7", - "ext-reflection": "*" + "ext-reflection": "*", + "symfony/yaml": "^4.3" }, "authors": [ { diff --git a/composer.lock b/composer.lock index 062facba..e610009b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0efa77722be3ca1e98fddce9ad31564d", + "content-hash": "4d81feb158a59e0107dea94d5dc2a486", "packages": [ { "name": "bitpay/key-utils", @@ -323,6 +323,123 @@ ], "description": "A polyfill for getallheaders.", "time": "2019-03-08T08:55:37+00:00" + }, + { + "name": "symfony/polyfill-ctype", + "version": "v1.12.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "550ebaac289296ce228a706d0867afc34687e3f4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/550ebaac289296ce228a706d0867afc34687e3f4", + "reference": "550ebaac289296ce228a706d0867afc34687e3f4", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.12-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "time": "2019-08-06T08:03:45+00:00" + }, + { + "name": "symfony/yaml", + "version": "v4.3.4", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "5a0b7c32dc3ec56fd4abae8a4a71b0cf05013686" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/5a0b7c32dc3ec56fd4abae8a4a71b0cf05013686", + "reference": "5a0b7c32dc3ec56fd4abae8a4a71b0cf05013686", + "shasum": "" + }, + "require": { + "php": "^7.1.3", + "symfony/polyfill-ctype": "~1.8" + }, + "conflict": { + "symfony/console": "<3.4" + }, + "require-dev": { + "symfony/console": "~3.4|~4.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2019-08-20T14:27:59+00:00" } ], "packages-dev": [ @@ -468,18 +585,18 @@ "authors": [ { "name": "Arne Blankerts", - "role": "Developer", - "email": "arne@blankerts.de" + "email": "arne@blankerts.de", + "role": "Developer" }, { "name": "Sebastian Heuer", - "role": "Developer", - "email": "sebastian@phpeople.de" + "email": "sebastian@phpeople.de", + "role": "Developer" }, { "name": "Sebastian Bergmann", - "role": "Developer", - "email": "sebastian@phpunit.de" + "email": "sebastian@phpunit.de", + "role": "Developer" } ], "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", @@ -534,35 +651,33 @@ }, { "name": "phpdocumentor/reflection-common", - "version": "1.0.1", + "version": "2.0.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionCommon.git", - "reference": "21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6" + "reference": "63a995caa1ca9e5590304cd845c15ad6d482a62a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6", - "reference": "21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/63a995caa1ca9e5590304cd845c15ad6d482a62a", + "reference": "63a995caa1ca9e5590304cd845c15ad6d482a62a", "shasum": "" }, "require": { - "php": ">=5.5" + "php": ">=7.1" }, "require-dev": { - "phpunit/phpunit": "^4.6" + "phpunit/phpunit": "~6" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "2.x-dev" } }, "autoload": { "psr-4": { - "phpDocumentor\\Reflection\\": [ - "src" - ] + "phpDocumentor\\Reflection\\": "src/" } }, "notification-url": "https://packagist.org/downloads/", @@ -584,30 +699,30 @@ "reflection", "static analysis" ], - "time": "2017-09-11T18:02:19+00:00" + "time": "2018-08-07T13:53:10+00:00" }, { "name": "phpdocumentor/reflection-docblock", - "version": "4.3.1", + "version": "4.3.2", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "bdd9f737ebc2a01c06ea7ff4308ec6697db9b53c" + "reference": "b83ff7cfcfee7827e1e78b637a5904fe6a96698e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/bdd9f737ebc2a01c06ea7ff4308ec6697db9b53c", - "reference": "bdd9f737ebc2a01c06ea7ff4308ec6697db9b53c", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/b83ff7cfcfee7827e1e78b637a5904fe6a96698e", + "reference": "b83ff7cfcfee7827e1e78b637a5904fe6a96698e", "shasum": "" }, "require": { "php": "^7.0", - "phpdocumentor/reflection-common": "^1.0.0", - "phpdocumentor/type-resolver": "^0.4.0", + "phpdocumentor/reflection-common": "^1.0.0 || ^2.0.0", + "phpdocumentor/type-resolver": "~0.4 || ^1.0.0", "webmozart/assert": "^1.0" }, "require-dev": { - "doctrine/instantiator": "~1.0.5", + "doctrine/instantiator": "^1.0.5", "mockery/mockery": "^1.0", "phpunit/phpunit": "^6.4" }, @@ -635,41 +750,40 @@ } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2019-04-30T17:48:53+00:00" + "time": "2019-09-12T14:27:41+00:00" }, { "name": "phpdocumentor/type-resolver", - "version": "0.4.0", + "version": "1.0.1", "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "9c977708995954784726e25d0cd1dddf4e65b0f7" + "reference": "2e32a6d48972b2c1976ed5d8967145b6cec4a4a9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/9c977708995954784726e25d0cd1dddf4e65b0f7", - "reference": "9c977708995954784726e25d0cd1dddf4e65b0f7", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/2e32a6d48972b2c1976ed5d8967145b6cec4a4a9", + "reference": "2e32a6d48972b2c1976ed5d8967145b6cec4a4a9", "shasum": "" }, "require": { - "php": "^5.5 || ^7.0", - "phpdocumentor/reflection-common": "^1.0" + "php": "^7.1", + "phpdocumentor/reflection-common": "^2.0" }, "require-dev": { - "mockery/mockery": "^0.9.4", - "phpunit/phpunit": "^5.2||^4.8.24" + "ext-tokenizer": "^7.1", + "mockery/mockery": "~1", + "phpunit/phpunit": "^7.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.x-dev" } }, "autoload": { "psr-4": { - "phpDocumentor\\Reflection\\": [ - "src/" - ] + "phpDocumentor\\Reflection\\": "src" } }, "notification-url": "https://packagist.org/downloads/", @@ -682,7 +796,8 @@ "email": "me@mikevanriel.com" } ], - "time": "2017-07-14T14:27:02+00:00" + "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", + "time": "2019-08-22T18:11:29+00:00" }, { "name": "phpspec/prophecy", @@ -749,16 +864,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "7.0.7", + "version": "7.0.8", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "7743bbcfff2a907e9ee4a25be13d0f8ec5e73800" + "reference": "aa0d179a13284c7420fc281fc32750e6cc7c9e2f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/7743bbcfff2a907e9ee4a25be13d0f8ec5e73800", - "reference": "7743bbcfff2a907e9ee4a25be13d0f8ec5e73800", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/aa0d179a13284c7420fc281fc32750e6cc7c9e2f", + "reference": "aa0d179a13284c7420fc281fc32750e6cc7c9e2f", "shasum": "" }, "require": { @@ -767,7 +882,7 @@ "php": "^7.2", "phpunit/php-file-iterator": "^2.0.2", "phpunit/php-text-template": "^1.2.1", - "phpunit/php-token-stream": "^3.1.0", + "phpunit/php-token-stream": "^3.1.1", "sebastian/code-unit-reverse-lookup": "^1.0.1", "sebastian/environment": "^4.2.2", "sebastian/version": "^2.0.1", @@ -808,7 +923,7 @@ "testing", "xunit" ], - "time": "2019-07-25T05:31:54+00:00" + "time": "2019-09-17T06:24:36+00:00" }, { "name": "phpunit/php-file-iterator", @@ -952,16 +1067,16 @@ }, { "name": "phpunit/php-token-stream", - "version": "3.1.0", + "version": "3.1.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "e899757bb3df5ff6e95089132f32cd59aac2220a" + "reference": "995192df77f63a59e47f025390d2d1fdf8f425ff" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/e899757bb3df5ff6e95089132f32cd59aac2220a", - "reference": "e899757bb3df5ff6e95089132f32cd59aac2220a", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/995192df77f63a59e47f025390d2d1fdf8f425ff", + "reference": "995192df77f63a59e47f025390d2d1fdf8f425ff", "shasum": "" }, "require": { @@ -997,20 +1112,20 @@ "keywords": [ "tokenizer" ], - "time": "2019-07-25T05:29:42+00:00" + "time": "2019-09-17T06:23:10+00:00" }, { "name": "phpunit/phpunit", - "version": "8.3.4", + "version": "8.3.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "e31cce0cf4499c0ccdbbb211a3280d36ab341e36" + "reference": "302faed7059fde575cf3403a78c730c5e3a62750" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/e31cce0cf4499c0ccdbbb211a3280d36ab341e36", - "reference": "e31cce0cf4499c0ccdbbb211a3280d36ab341e36", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/302faed7059fde575cf3403a78c730c5e3a62750", + "reference": "302faed7059fde575cf3403a78c730c5e3a62750", "shasum": "" }, "require": { @@ -1033,7 +1148,7 @@ "sebastian/comparator": "^3.0.2", "sebastian/diff": "^3.0.2", "sebastian/environment": "^4.2.2", - "sebastian/exporter": "^3.1.0", + "sebastian/exporter": "^3.1.1", "sebastian/global-state": "^3.0.0", "sebastian/object-enumerator": "^3.0.3", "sebastian/resource-operations": "^2.0.1", @@ -1080,7 +1195,7 @@ "testing", "xunit" ], - "time": "2019-08-11T06:56:55+00:00" + "time": "2019-09-14T09:12:03+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", @@ -1302,16 +1417,16 @@ }, { "name": "sebastian/exporter", - "version": "3.1.1", + "version": "3.1.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "06a9a5947f47b3029d76118eb5c22802e5869687" + "reference": "68609e1261d215ea5b21b7987539cbfbe156ec3e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/06a9a5947f47b3029d76118eb5c22802e5869687", - "reference": "06a9a5947f47b3029d76118eb5c22802e5869687", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/68609e1261d215ea5b21b7987539cbfbe156ec3e", + "reference": "68609e1261d215ea5b21b7987539cbfbe156ec3e", "shasum": "" }, "require": { @@ -1365,7 +1480,7 @@ "export", "exporter" ], - "time": "2019-08-11T12:43:14+00:00" + "time": "2019-09-14T09:02:43+00:00" }, { "name": "sebastian/global-state", @@ -1697,64 +1812,6 @@ "homepage": "https://github.com/sebastianbergmann/version", "time": "2016-10-03T07:35:21+00:00" }, - { - "name": "symfony/polyfill-ctype", - "version": "v1.12.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "550ebaac289296ce228a706d0867afc34687e3f4" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/550ebaac289296ce228a706d0867afc34687e3f4", - "reference": "550ebaac289296ce228a706d0867afc34687e3f4", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "suggest": { - "ext-ctype": "For best performance" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.12-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Ctype\\": "" - }, - "files": [ - "bootstrap.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Gert de Pagter", - "email": "BackEndTea@gmail.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for ctype functions", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "ctype", - "polyfill", - "portable" - ], - "time": "2019-08-06T08:03:45+00:00" - }, { "name": "theseer/tokenizer", "version": "1.1.3", @@ -1788,8 +1845,8 @@ "authors": [ { "name": "Arne Blankerts", - "role": "Developer", - "email": "arne@blankerts.de" + "email": "arne@blankerts.de", + "role": "Developer" } ], "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", @@ -1853,7 +1910,8 @@ "prefer-lowest": false, "platform": { "php": "^7.3", - "ext-json": "^1.7" + "ext-json": "^1.7", + "ext-reflection": "*" }, "platform-dev": [] } diff --git a/examples/BitPay.config.json b/examples/BitPay.config.json new file mode 100644 index 00000000..f04ec579 --- /dev/null +++ b/examples/BitPay.config.json @@ -0,0 +1,15 @@ +{ + "BitPayConfiguration": { + "Environment": "", + "EnvConfig": { + "Test": { + "PrivateKeyPath": "", + "PrivateKeySecret": "", + "ApiTokens": { + "merchant": "", + "payroll": "" + } + } + } + } +} \ No newline at end of file diff --git a/examples/BitPay.config.yml b/examples/BitPay.config.yml new file mode 100644 index 00000000..bf5b80cd --- /dev/null +++ b/examples/BitPay.config.yml @@ -0,0 +1,14 @@ +BitPayConfiguration: + Environment: null + EnvConfig: + Test: + PrivateKeyPath: null + PrivateKeySecret: null + ApiTokens: + merchant: null + payroll: null + Prod: + PrivateKeyPath: null + ApiTokens: + merchant: null + payroll: null diff --git a/examples/bitpay_private_test.key b/examples/bitpay_private_test.key new file mode 100644 index 00000000..0596a7a1 --- /dev/null +++ b/examples/bitpay_private_test.key @@ -0,0 +1 @@ +447a06764339c7746ba6f549f708b38a6fd7c3e489c3c39027d6d2b7ae6ac4d2de9ed6ff88badfe2963fbf8b9138e1f8f4dd96b5267e0bb04b0eb8c21323bcae6dc4106dc308c85c947d67c8c06c1062efccd91947378377593948abc84023e7b68687edc9472af44dad206ccb3e0f8aa7c4f9ad85739d920f3b8bd59bd78cc528c0428c0b7796f72b218a789a2a78f2af716d550f96a37f56011d0c2e065423569f6e50d93ef6b88b0e2ab181d7e6e69fc3bf24e32a8cdd4485a8fd64959ebcbcb8d5313bffe704c3922d3a2337086fdd7448b30e7c4653e03b900f62a0ae3ce4ceb88f92ab4b585a618bcdd9dedffaf199dc706b0ac3905d4803740934d25648595eacd5e491913cf2299f6558dbca13a91e756bc66a027f44c1a12a8415b82e87c64f0cb8a6f58ed56f362bf12818d2e5092d3d9e44f7299acc8295650550a8aeb56b19fe58951e60da5027a977cd232c88d6a8fa5e5c6209460795b6dd0c \ No newline at end of file diff --git a/src/BitPay/Client.php b/src/BitPay/Client.php index a51613e6..ce2a5220 100644 --- a/src/BitPay/Client.php +++ b/src/BitPay/Client.php @@ -4,12 +4,14 @@ namespace Bitpay; +use BitPay\Exceptions\BillCreationException; use BitPay\Exceptions\BitPayException; use BitPay\Exceptions\InvoiceCreationException; use BitPay\Exceptions\InvoiceQueryException; use BitPay\Exceptions\PayoutCancellationException; use BitPay\Exceptions\PayoutCreationException; use BitPay\Exceptions\PayoutQueryException; +use Bitpay\Model\Bill\Bill; use Bitpay\Model\Facade; use Bitpay\Model\Invoice\Invoice; use Bitpay\Model\Payout\PayoutBatch; @@ -19,6 +21,7 @@ use BitPayKeyUtils\Util\Util; use GuzzleHttp; use GuzzleHttp\RequestOptions; +use Symfony\Component\Yaml\Yaml; /** * Class Client @@ -84,6 +87,27 @@ public function withData($environment, $privateKeyPath, Tokens $tokens, $private } } + /** + * Constructor for use if the keys and SIN are managed by this library. + * + * @param $configFilePath String The path to the configuration file. + * @return Client + * @throws BitPayException BitPayException class + */ + public function withFile($configFilePath) + { + try { + $this->_configFilePath = $configFilePath; + $this->GetConfig(); + $this->initKeys(); + $this->init(); + + return $this; + } catch (\Exception $e) { + throw new BitPayException("failed to initialize BitPay Client (Config) : ".$e->getMessage()); + } + } + /** * Create a BitPay invoice. * @@ -204,6 +228,41 @@ public function getInvoices(string $dateStart, string $dateEnd): array return $invoices; } + /** + * Create a BitPay Bill. + * + * @param $bill string A Bill object with request parameters defined. + * @param $facade string The facade used to create it. + * @param $signRequest bool Signed request. + * @return Bill A BitPay generated Bill object. + * @throws BitPayException BitPayException class + */ + public function createBill(Bill $bill, string $facade, bool $signRequest): Bill { + try { + $bill->setToken($this->_tokenCache->getTokenByFacade($facade)); + + $response = $this->post("bills", $bill->toArray(), $signRequest); + + $jsonString = $this->responseToJsonString($response); + } catch (\Exception $e) { + throw new BillCreationException("failed to serialize Bill object : ".$e->getMessage()); + } + + try { + $mapper = new JsonMapper(); + $bill = $mapper->map( + json_decode($jsonString), + new Bill() + ); + + } catch (\Exception $e) { + throw new BillCreationException( + "failed to deserialize BitPay server response (Bill) : ".$e->getMessage()); + } + + return $bill; + } + /** * Submit a BitPay Payout batch. * @@ -384,6 +443,44 @@ private function BuildConfig($privateKeyPath, $tokens, $privateKeySecret = null) } } + /** + * Loads the configuration file (JSON) + * + * @throws BitPayException BitPayException class + */ + public function GetConfig() + { + try { + $this->_configuration = new Config(); + if (!file_exists($this->_configFilePath)) { + throw new BitPayException("Configuration file not found"); + } + $configData = json_decode(file_get_contents($this->_configFilePath), true); + + if (!$configData) { + $configData = Yaml::parseFile($this->_configFilePath); + } + $this->_configuration->setEnvironment($configData["BitPayConfiguration"]["Environment"]); + $this->_env = $this->_configuration->getEnvironment(); + + + $tokens = new Tokens(); + $tokens->loadFromArray($configData["BitPayConfiguration"]["EnvConfig"][$this->_env]["ApiTokens"]); + $privateKeyPath = $configData["BitPayConfiguration"]["EnvConfig"][$this->_env]["PrivateKeyPath"]; + $privateKeySecret = $configData["BitPayConfiguration"]["EnvConfig"][$this->_env]["PrivateKeySecret"]; + + $envConfig[$this->_env] = [ + "PrivateKeyPath" => $privateKeyPath, + "PrivateKeySecret" => $privateKeySecret, + "ApiTokens" => $tokens, + ]; + + $this->_configuration->setEnvConfig($envConfig); + } catch (\Exception $e) { + throw new BitPayException("failed to initialize BitPay Client (Config) : ".$e->getMessage()); + } + } + /** * Initialize the public/private key pair by either loading the existing one or by creating a new one. * @@ -460,48 +557,6 @@ private function clearAccessTokenCache() $this->_tokenCache = new Tokens(); } - /** - * Constructor for use if the keys and SIN are managed by this library. - * - * @param $configFilePath String The path to the configuration file. - * @return Client - * @throws BitPayException BitPayException class - */ - public function withFile($configFilePath) - { - try { - $this->_configFilePath = $configFilePath; - $this->GetConfig(); - $this->initKeys(); - $this->init(); - - return $this; - } catch (\Exception $e) { - throw new BitPayException("failed to initialize BitPay Client (Config) : ".$e->getMessage()); - } - } - - /** - * Loads the configuration file (JSON) - * - * @throws BitPayException BitPayException class - */ - public function GetConfig() - { - try { - $this->_configuration = new Config(); - if (!file_exists($this->_configFilePath)) { - throw new BitPayException("Configuration file not found"); - } - $jsonData = json_decode(file_get_contents($this->_configFilePath), true); - $this->_configuration->setEnvironment($jsonData["BitPayConfiguration"]["Environment"]); - $this->_env = $this->_configuration->getEnvironment(); - $this->_configuration->setEnvConfig($jsonData["BitPayConfiguration"]["EnvConfig"][$this->_env]); - } catch (\Exception $e) { - throw new BitPayException("failed to initialize BitPay Client (Config) : ".$e->getMessage()); - } - } - public function post($uri, array $json = [], $signatureRequired = true) { try { diff --git a/src/BitPay/Tokens.php b/src/BitPay/Tokens.php index a4f791cc..7779bd48 100644 --- a/src/BitPay/Tokens.php +++ b/src/BitPay/Tokens.php @@ -32,6 +32,12 @@ public function __construct($merchant = null, $payroll = null) $this->payroll = $payroll; } + public function loadFromArray(array $tokens) { + foreach($tokens as $facade => $token){ + $this->{$facade} = $token; + } + } + /** * @param $facade * @return string diff --git a/tests/BitPay/BitPayTest.php b/tests/BitPay/BitPayTest.php index 4e3933cb..996dfbc7 100644 --- a/tests/BitPay/BitPayTest.php +++ b/tests/BitPay/BitPayTest.php @@ -12,6 +12,8 @@ class BitPayTest extends TestCase { protected $client; + protected $client1; + protected $client2; protected $clientMock; protected function setUp(): void @@ -19,7 +21,7 @@ protected function setUp(): void $this->clientMock = $this->createMock(Bitpay\Client::class); $this->clientMock->withData( Bitpay\Env::Test, - __DIR__."/../../bitpay_private_test.key", + __DIR__."/../../examples/bitpay_private_test.key", new Bitpay\Tokens( "7UeQtMcsHamehE4gDZojUQbNRbSuSdggbH17sawtobGJ", "5j48K7pUrX5k59DLhRVYkCupgw2CtoEt8DBFrHo2vW47" @@ -29,13 +31,20 @@ protected function setUp(): void $this->client = Bitpay\Client::create()->withData( Bitpay\Env::Test, - __DIR__."/../../bitpay_private_test.key", + __DIR__."/../../examples/bitpay_private_test.key", new Bitpay\Tokens( "7UeQtMcsHamehE4gDZojUQbNRbSuSdggbH17sawtobGJ", "5j48K7pUrX5k59DLhRVYkCupgw2CtoEt8DBFrHo2vW47" ), "YourMasterPassword"); + + $this->client1 = Bitpay\Client::create()->withFile(__DIR__."/../../examples/BitPay.config.json"); + $this->client2 = Bitpay\Client::create()->withFile(__DIR__."/../../examples/BitPay.config.yml"); + + $this->assertNotNull($this->client); + $this->assertNotNull($this->client1); + $this->assertNotNull($this->client2); } public function testShouldGetInvoiceId() @@ -95,6 +104,42 @@ public function testShouldGetInvoices() { $this->assertTrue(count($invoices) > 0); } + public function TestShouldCreateBillUSD() { + $items = []; + $item = new Bitpay\Model\Bill\Item(); + + $item->setPrice(30.0); + $item->setQuantity(9); + $item->setDescription("product-a"); + array_push($items, $item); + + $item->setPrice(14.0); + $item->setQuantity(16); + $item->setDescription("product-b"); + array_push($items, $item); + + $item->setPrice(3.90); + $item->setQuantity(42); + $item->setDescription("product-c"); + array_push($items, $item); + + $item->setPrice(6.99); + $item->setQuantity(12); + $item->setDescription("product-d"); + array_push($items, $item); + + $bill = new Bitpay\Model\Bill\Bill("7", Currency::USD, "agallardo@bitpay.com", $items); + $basicBill = null; + try { + $basicBill = $this->client->createBill($bill); + } catch (\Exception $e) { + $e->getTraceAsString(); + self::fail($e->getMessage()); + } + + $this->assertNotNull($basicBill->getId()); + } + public function testShouldSubmitPayoutBatch() { $date = new \DateTime(); $threeDaysFromNow = $date->modify('+3 day');