Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

15 changes: 0 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1 @@
# NOWPayments API Client

## Installing

```bash
composer require onetoweb/nowpayments
```

## Usage

See example.php


## Example webhook authentication

See webhook.php
18 changes: 7 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
{
"name" : "onetoweb/nowpayments",
"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.1.0",
"php" : "^8.2",
"guzzlehttp/guzzle": "^7.3"
},
"require-dev" : {
"symfony/error-handler" : "^5.1"
},
"autoload" : {
"psr-4" : {
"Onetoweb\\NOWPayments\\" : "src/"
"NP\\" : "src/"
}
}
}
66 changes: 0 additions & 66 deletions example.php

This file was deleted.

10 changes: 5 additions & 5 deletions src/Authentication.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Onetoweb\NOWPayments;
namespace NP;

/**
* Authentication.
*
* @author Jonathan van 't Ende <jvantende@onetoweb.nl>
* @copyright Onetoweb B.V.
* @author Nikolai Shcherbin <support@wzm.me>
* @copyright Nikolai Shcherbin
*/
class Authentication
{
Expand All @@ -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);

Expand Down
33 changes: 25 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?php

namespace Onetoweb\NOWPayments;
namespace NP;

use Onetoweb\NOWPayments\Endpoint;
use NP\Endpoint;

/**
* Client.
*
* @author Jonathan van 't Ende <jvantende@onetoweb.nl>
* @copyright Onetoweb B.V.
* @author Nikolai Shcherbin <support@wzm.me>
* @copyright Nikolai Shcherbin
*/
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
Expand All @@ -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();
}
Expand All @@ -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);
}
}
}
38 changes: 23 additions & 15 deletions src/Endpoint/AbstractEndpoint.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
<?php

namespace Onetoweb\NOWPayments\Endpoint;
namespace NP\Endpoint;

use Onetoweb\NOWPayments\Client;
use NP\Client;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Exception\RequestException;

/**
* Abstract Endpoint.
*
* @author Jonathan van 't Ende <jvantende@onetoweb.nl>
* @copyright Onetoweb B.V.
* @author Nikolai Shcherbin <support@wzm.me>
* @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
Expand Down Expand Up @@ -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 [
Expand Down
30 changes: 30 additions & 0 deletions src/Endpoint/Auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace NP\Endpoint;

/**
* Authentication endpoint.
*
* @author Nikolai Shcherbin <support@wzm.me>
* @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);
}
}
6 changes: 3 additions & 3 deletions src/Endpoint/Currency.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Onetoweb\NOWPayments\Endpoint;
namespace NP\Endpoint;

/**
* Currency endpoint.
*
* @author Jonathan van 't Ende <jvantende@onetoweb.nl>
* @copyright Onetoweb B.V.
* @author Nikolai Shcherbin <support@wzm.me>
* @copyright Nikolai Shcherbin
*/
class Currency extends AbstractEndpoint
{
Expand Down
6 changes: 3 additions & 3 deletions src/Endpoint/EndpointInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Onetoweb\NOWPayments\Endpoint;
namespace NP\Endpoint;

/**
* Endpoint interface.
*
* @author Jonathan van 't Ende <jvantende@onetoweb.nl>
* @copyright Onetoweb B.V.
* @author Nikolai Shcherbin <support@wzm.me>
* @copyright Nikolai Shcherbin
*/
interface EndpointInterface
{
Expand Down
6 changes: 3 additions & 3 deletions src/Endpoint/Estimate.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Onetoweb\NOWPayments\Endpoint;
namespace NP\Endpoint;

/**
* Estimate endpoint
*
* @author Jonathan van 't Ende <jvantende@onetoweb.nl>
* @copyright Onetoweb B.V.
* @author Nikolai Shcherbin <support@wzm.me>
* @copyright Nikolai Shcherbin
*/
class Estimate extends AbstractEndpoint
{
Expand Down
Loading