From b3479030777df4b2e1699c0063f395fea5c23427 Mon Sep 17 00:00:00 2001 From: Nikolai Shcherbin Date: Sat, 19 Mar 2022 19:52:26 +0300 Subject: [PATCH 1/3] Sandbox APi endpoint has been changed [Source](https://documenter.getpostman.com/view/7907941/T1LSCRHC#5c2b8f84-73dc-41ff-aa4b-8d08a4556102) --- src/Client.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Client.php b/src/Client.php index afccabd..fe5a95a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -16,7 +16,7 @@ class Client * API endpoint */ const API_ENDPOINT = 'https://api.nowpayments.io'; - const API_SANDBOX_ENDPOINT = 'https://api.sandbox.nowpayments.io'; + const API_SANDBOX_ENDPOINT = 'https://api-sandbox.nowpayments.io'; /** * API version @@ -77,4 +77,4 @@ private function initializeEndpoints() $this->invoice = new Endpoint\Invoice($this); $this->minAmount = new Endpoint\MinAmount($this); } -} \ No newline at end of file +} From 892cba71aa4ec7b4c7909508e3d0958ae8b6ed7b Mon Sep 17 00:00:00 2001 From: RiverVanRain Date: Sun, 19 Mar 2023 17:07:58 +0300 Subject: [PATCH 2/3] Vendor has been changed --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 3b583c3..fad41e9 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name" : "onetoweb/nowpayments", + "name" : "rivervanrain/nowpayments", "description" : "NOWPayments API Client", "keywords" : [ "api", @@ -15,7 +15,7 @@ } ], "require" : { - "php" : ">=7.1.0", + "php" : "^7.3", "guzzlehttp/guzzle": "^7.3" }, "require-dev" : { From 56ff193e032aff26fb71ff56f20ae7ca0c354d7d Mon Sep 17 00:00:00 2001 From: Nikolai Shcherbin Date: Mon, 2 Oct 2023 02:34:24 +0300 Subject: [PATCH 3/3] Release 3.0 --- .gitignore | 2 - README.md | 15 ------ composer.json | 16 +++--- example.php | 66 ----------------------- src/Authentication.php | 10 ++-- src/Client.php | 29 +++++++--- src/Endpoint/AbstractEndpoint.php | 38 +++++++------ src/Endpoint/Auth.php | 30 +++++++++++ src/Endpoint/Currency.php | 6 +-- src/Endpoint/EndpointInterface.php | 6 +-- src/Endpoint/Estimate.php | 6 +-- src/Endpoint/Invoice.php | 6 +-- src/Endpoint/MinAmount.php | 6 +-- src/Endpoint/Payment.php | 8 +-- src/Endpoint/Status.php | 6 +-- src/Endpoint/Subscriptions.php | 87 ++++++++++++++++++++++++++++++ webhook.php | 29 ---------- 17 files changed, 196 insertions(+), 170 deletions(-) delete mode 100644 .gitignore delete mode 100644 example.php create mode 100644 src/Endpoint/Auth.php create mode 100644 src/Endpoint/Subscriptions.php delete mode 100644 webhook.php diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 9462cac..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -composer.lock -/vendor/ \ No newline at end of file diff --git a/README.md b/README.md index 7955088..7ba90d2 100644 --- a/README.md +++ b/README.md @@ -1,16 +1 @@ # NOWPayments API Client - -## Installing - -```bash -composer require onetoweb/nowpayments -``` - -## Usage - -See example.php - - -## Example webhook authentication - -See webhook.php \ No newline at end of file diff --git a/composer.json b/composer.json index fad41e9..9af00ac 100644 --- a/composer.json +++ b/composer.json @@ -1,21 +1,17 @@ { "name" : "rivervanrain/nowpayments", "description" : "NOWPayments API Client", - "keywords" : [ - "api", - "php", - "nowpayments" - ], + "keywords" : ["nowpayments"], "type" : "library", - "homepage" : "https://github.com/onetoweb/nowpayments", + "homepage" : "https://github.com/rivervanrain/nowpayments", "license" : "MIT", "authors" : [{ - "name" : "Jonathan van 't Ende", - "email" : "jvantende@onetoweb.nl" + "name" : "Nikolai Shcherbin", + "email" : "support@wzm.me" } ], "require" : { - "php" : "^7.3", + "php" : "^8.2", "guzzlehttp/guzzle": "^7.3" }, "require-dev" : { @@ -23,7 +19,7 @@ }, "autoload" : { "psr-4" : { - "Onetoweb\\NOWPayments\\" : "src/" + "NP\\" : "src/" } } } diff --git a/example.php b/example.php deleted file mode 100644 index f5f9581..0000000 --- a/example.php +++ /dev/null @@ -1,66 +0,0 @@ -status->get(); - -// get currencies -$currencies = $nowPaymentsClient->currency->get(); - -// estimate -$estimate = $nowPaymentsClient->estimate->get([ - 'amount' => 1, - 'currency_from' => 'eur', - 'currency_to' => 'doge' -]); - -// create payment -$payment = $nowPaymentsClient->payment->create([ - 'price_amount' => 32, - 'price_currency' => 'eur', - 'pay_currency' => 'doge', - 'ipn_callback_url' => 'https://example.com/webhook.php', - 'order_id' => 'order 1', - 'order_description' => 'order_description' -]); - -// get payment -$paymentId = 42; -$payment = $nowPaymentsClient->payment->get($paymentId); - -// list payments -$payments = $nowPaymentsClient->payment->list([ - 'limit' => 10, - 'page' => 0, - 'sortBy' => 'created_at', - 'orderBy' => 'asc', - 'dateFrom' => '2020-01-01', - 'dateTo' => '2022-01-01' -]); - -// get min amount -$minAmount = $nowPaymentsClient->minAmount->get([ - 'currency_from' => 'btc', - 'currency_to' => 'doge', -]); - -// create invoice -$invoice = $nowPaymentsClient->invoice->create([ - 'price_amount' => 1000, - 'price_currency' => 'eur', - 'order_id' => 'order 1', - 'order_description' => 'order_description', - 'ipn_callback_url' => 'https://example.com/webhook.php', - 'success_url' => 'https://example.com/webhook.php', - 'cancel_url' => 'https://example.com/webhook.php' -]); \ No newline at end of file diff --git a/src/Authentication.php b/src/Authentication.php index 76d7dc6..43fe8e6 100644 --- a/src/Authentication.php +++ b/src/Authentication.php @@ -1,12 +1,12 @@ - * @copyright Onetoweb B.V. + * @author Nikolai Shcherbin + * @copyright Nikolai Shcherbin */ class Authentication { @@ -19,13 +19,13 @@ class Authentication */ public static function authenticate(string $content, string $receivedHmac, string $ipnSecret): bool { - if (!empty($content) and !empty($receivedHmac) and !empty($ipnSecret)) { + if (!empty($content) && !empty($receivedHmac) && !empty($ipnSecret)) { $data = json_decode($content, true); ksort($data); $json = json_encode($data); - if ($json !== false and !empty($json)) { + if ($json !== false && !empty($json)) { $hmac = hash_hmac('sha512', $json, $ipnSecret); diff --git a/src/Client.php b/src/Client.php index fe5a95a..410e1ce 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1,14 +1,14 @@ - * @copyright Onetoweb B.V. + * @author Nikolai Shcherbin + * @copyright Nikolai Shcherbin */ class Client { @@ -32,15 +32,22 @@ class Client * @var bool */ private $testModus; + + /** + * @var string + */ + private $jwt; /** * @param string $apiKey * @param bool $testModus = false + * @param string $jwt = null */ - public function __construct(string $apiKey, bool $testModus = false) + public function __construct(string $apiKey, bool $testModus = false, $jwt = null) { $this->apiKey = $apiKey; $this->testModus = $testModus; + $this->jwt = $jwt; $this->initializeEndpoints(); } @@ -64,17 +71,27 @@ public function getApiEndpoint(): string return self::API_ENDPOINT; } + + /** + * @return string + */ + public function getJwt() + { + return $this->jwt; + } /** * Initialize endpoints. */ private function initializeEndpoints() { - $this->status = new Endpoint\Status($this); + $this->auth = new Endpoint\Auth($this); + $this->status = new Endpoint\Status($this); $this->currency = new Endpoint\Currency($this); $this->payment = new Endpoint\Payment($this); $this->estimate = new Endpoint\Estimate($this); $this->invoice = new Endpoint\Invoice($this); $this->minAmount = new Endpoint\MinAmount($this); + $this->subscriptions = new Endpoint\Subscriptions($this); } } diff --git a/src/Endpoint/AbstractEndpoint.php b/src/Endpoint/AbstractEndpoint.php index 1604da8..eb2b3b0 100644 --- a/src/Endpoint/AbstractEndpoint.php +++ b/src/Endpoint/AbstractEndpoint.php @@ -1,8 +1,8 @@ - * @copyright Onetoweb B.V. + * @author Nikolai Shcherbin + * @copyright Nikolai Shcherbin */ abstract class AbstractEndpoint implements EndpointInterface { const METHOD_GET = 'GET'; const METHOD_POST = 'POST'; + const METHOD_PATCH = 'PATCH'; + const METHOD_DELETE = 'DELETE'; /** * @var Client @@ -56,30 +58,36 @@ private function getUrl(string $endpoint = null): string */ protected function request(string $method = self::METHOD_GET, string $endpoint = null, array $data = [], array $query = []): array { - $options = [ - RequestOptions::HEADERS => [ - 'x-api-key' => $this->client->getApiKey() - ], - RequestOptions::QUERY => $query - ]; + if (!is_null($this->client->getJwt())) { + $options = [ + RequestOptions::HEADERS => [ + 'x-api-key' => $this->client->getApiKey(), + 'Authorization' => 'Bearer ' . $this->client->getJwt(), + ], + RequestOptions::QUERY => $query + ]; + } else { + $options = [ + RequestOptions::HEADERS => [ + 'x-api-key' => $this->client->getApiKey() + ], + RequestOptions::QUERY => $query + ]; + } - if ($method == self::METHOD_POST) { + if ($method == self::METHOD_POST || $method == self::METHOD_PATCH) { $options[RequestOptions::JSON] = $data; } try { - $guzzleClient = new GuzzleClient(); $response = $guzzleClient->request($method, $this->getUrl($endpoint), $options); return json_decode($response->getBody()->getContents(), true); } catch (RequestException $requestException) { - if ($requestException->hasResponse()) { - return json_decode($requestException->getResponse()->getBody()->getContents(), true); - } return [ diff --git a/src/Endpoint/Auth.php b/src/Endpoint/Auth.php new file mode 100644 index 0000000..a3c8742 --- /dev/null +++ b/src/Endpoint/Auth.php @@ -0,0 +1,30 @@ + + * @copyright Nikolai Shcherbin + */ +class Auth extends AbstractEndpoint +{ + const RESOURCE = 'auth'; + + /** + * @return string + */ + public function getResource(): string + { + return self::RESOURCE; + } + + /** + * @return array + */ + public function token(array $data): array + { + return $this->request(parent::METHOD_POST, null, $data); + } +} \ No newline at end of file diff --git a/src/Endpoint/Currency.php b/src/Endpoint/Currency.php index f0e293b..8e85c1a 100644 --- a/src/Endpoint/Currency.php +++ b/src/Endpoint/Currency.php @@ -1,12 +1,12 @@ - * @copyright Onetoweb B.V. + * @author Nikolai Shcherbin + * @copyright Nikolai Shcherbin */ class Currency extends AbstractEndpoint { diff --git a/src/Endpoint/EndpointInterface.php b/src/Endpoint/EndpointInterface.php index e4e07b2..6cf2637 100644 --- a/src/Endpoint/EndpointInterface.php +++ b/src/Endpoint/EndpointInterface.php @@ -1,12 +1,12 @@ - * @copyright Onetoweb B.V. + * @author Nikolai Shcherbin + * @copyright Nikolai Shcherbin */ interface EndpointInterface { diff --git a/src/Endpoint/Estimate.php b/src/Endpoint/Estimate.php index b3d9909..2c0980d 100644 --- a/src/Endpoint/Estimate.php +++ b/src/Endpoint/Estimate.php @@ -1,12 +1,12 @@ - * @copyright Onetoweb B.V. + * @author Nikolai Shcherbin + * @copyright Nikolai Shcherbin */ class Estimate extends AbstractEndpoint { diff --git a/src/Endpoint/Invoice.php b/src/Endpoint/Invoice.php index ceb24c7..7d954f2 100644 --- a/src/Endpoint/Invoice.php +++ b/src/Endpoint/Invoice.php @@ -1,12 +1,12 @@ - * @copyright Onetoweb B.V. + * @author Nikolai Shcherbin + * @copyright Nikolai Shcherbin */ class Invoice extends AbstractEndpoint { diff --git a/src/Endpoint/MinAmount.php b/src/Endpoint/MinAmount.php index 6a8e154..456eed9 100644 --- a/src/Endpoint/MinAmount.php +++ b/src/Endpoint/MinAmount.php @@ -1,12 +1,12 @@ - * @copyright Onetoweb B.V. + * @author Nikolai Shcherbin + * @copyright Nikolai Shcherbin */ class MinAmount extends AbstractEndpoint { diff --git a/src/Endpoint/Payment.php b/src/Endpoint/Payment.php index 67f350f..4d7c555 100644 --- a/src/Endpoint/Payment.php +++ b/src/Endpoint/Payment.php @@ -1,12 +1,12 @@ - * @copyright Onetoweb B.V. + * @author Nikolai Shcherbin + * @copyright Nikolai Shcherbin */ class Payment extends AbstractEndpoint { @@ -42,6 +42,6 @@ public function get(int $paymentId): array */ public function list(array $query): array { - return $this->request(parent::METHOD_GET, null, [], $query); + return $this->request(parent::METHOD_GET, null, $query); } } \ No newline at end of file diff --git a/src/Endpoint/Status.php b/src/Endpoint/Status.php index b6d497b..1ebc424 100644 --- a/src/Endpoint/Status.php +++ b/src/Endpoint/Status.php @@ -1,12 +1,12 @@ - * @copyright Onetoweb B.V. + * @author Nikolai Shcherbin + * @copyright Nikolai Shcherbin */ class Status extends AbstractEndpoint { diff --git a/src/Endpoint/Subscriptions.php b/src/Endpoint/Subscriptions.php new file mode 100644 index 0000000..07231f9 --- /dev/null +++ b/src/Endpoint/Subscriptions.php @@ -0,0 +1,87 @@ + + * @copyright https://wzm.me + */ +class Subscriptions extends AbstractEndpoint +{ + const RESOURCE = 'subscriptions'; + + /** + * @return string + */ + public function getResource(): string + { + return self::RESOURCE; + } + + /** + * @param array $data + */ + public function createPlan(array $data): array + { + return $this->request(parent::METHOD_POST, 'plans', $data); + } + + /** + * @param int $planId + * @param array $data + */ + public function updatePlan(int $planId, array $data): array + { + return $this->request(parent::METHOD_PATCH, 'plans/' . $planId, $data); + } + + /** + * @param int $planId + */ + public function getPlan(int $planId): array + { + return $this->request(parent::METHOD_GET, 'plans/' . $planId); + } + + /** + * @param array $data + */ + public function getManyPlans(array $data): array + { + return $this->request(parent::METHOD_GET, 'plans', $data); + } + + /** + * @param array $data + */ + public function createSubscription(array $data): array + { + return $this->request(parent::METHOD_POST, null, $data); + } + + /** + * @param int $subId + */ + public function getSubscription(int $subId): array + { + return $this->request(parent::METHOD_GET, $subId); + } + + /** + * @param array $data + */ + public function getManySubscriptions(array $data): array + { + return $this->request(parent::METHOD_GET, null, $data); + } + + /** + * @param int $subId + */ + public function deleteSubscription(int $subId): array + { + return $this->request(parent::METHOD_DELETE, $subId); + } +} \ No newline at end of file diff --git a/webhook.php b/webhook.php deleted file mode 100644 index d69cfb1..0000000 --- a/webhook.php +++ /dev/null @@ -1,29 +0,0 @@ -getContent(), $request->headers->get('x-nowpayments-sig'), $ipnSecret)) { - - // webhook authenticated - -} - - -// alternatively example using basic php - -$contents = file_get_contents('php://input'); - -if(Authentication::authenticate($contents, $_SERVER['HTTP_X_NOWPAYMENTS_SIG'], $ipnSecret)) { - - // webhook authenticated - -} \ No newline at end of file