Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
v1.0.4
Browse files Browse the repository at this point in the history
- Use config base_url for choosing nano.to api url
- Clean Up LaravelNanoTo Facade to not use fake response
- LaravelNanoTo Facade Test utilize Guzzle Client Mock + fixes as required
- Removed Nano.to GET based function & tests
- isNanoToDown() helper function
  • Loading branch information
Niush committed Dec 3, 2021
1 parent 757d6bd commit 9bc9a35
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 167 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ NanoToApi::getCheckoutUrlAsJson("https://nano.to/checkout/xxx");

// 10) Check if nanocrawler is down or unreachable. Returns boolean true if down.
NanoToApi::isNanoCrawlerDown();

// 11) Check if Nano.to base_url is down or unreachable. Returns boolean true if down.
NanoToApi::isNanoToDown();
```

### Translation
Expand Down
5 changes: 5 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
* You can place your custom package configuration in here.
*/
return [
/**
* Nano.to Base URL of choice (without trailing slash)
*/
'base_url' => "https://nano.to",

/**
* Secret Key for Webhook verification
*/
Expand Down
147 changes: 19 additions & 128 deletions src/LaravelNanoTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@

use Error;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;

class LaravelNanoTo
{
public $base_url = 'https://nano.to';
public $title = "";
public $description = "";
public $amount;
public $suggest;
public $business;
public $background;
public $color;
public $checkout_url;
public $webhook_secret;
public $symbol = 'nano';
public $metadata = null;
public $raw = false;
public $image = null;
protected $base_url;
protected $title = "";
protected $description = "";
protected $amount;
protected $suggest;
protected $business;
protected $background;
protected $color;
protected $checkout_url;
protected $webhook_secret;
protected $symbol = 'nano';
protected $metadata = null;
protected $raw = false;
protected $image = null;

public function __construct()
{
$this->base_url = config('laravel-nano-to.base_url', 'https://nano.to');
$this->title = config('laravel-nano-to.title');
$this->description = config('laravel-nano-to.description');
$this->webhook_secret = config('laravel-nano-to.webhook_secret');
Expand Down Expand Up @@ -224,18 +224,11 @@ public function create($unique_id = null, $callback = null)
}

try {
$client = new Client();
$client = app(Client::class);

// Fake Nano.to response if testing.
if (App::environment() == 'testing') {
$response = new Response(200, [
'Content-Type' => 'application/json; charset=utf-8',
], '{"id":"test_id","url":"https://example.com/1","exp":"2021-10-10T01:51:23.853Z"}');
} else {
$response = $client->post($url, [
'json' => $body,
]);
}
$response = $client->post($url, [
'json' => $body,
]);
// var_dump($response->getBody()->getContents());
$this->checkout_url = json_decode($response->getBody()->getContents(), true)["url"];

Expand All @@ -257,108 +250,6 @@ public function create($unique_id = null, $callback = null)
return $this;
}
} catch (\Exception$e) {
if (App::environment() == 'testing') {dd($e);}
return $this->throw_checkout_page_not_loaded($e);
}
} else {
if (\Lang::has('nano-to.no-receiver')) {
throw new Error(__("nano-to.no-receiver"));
} else {
throw new Error("Receiver Account was not available.");
}
}
}

/**
* Get the Nano.to Gateway URL.
* Uses GET method (Default and favorable option is POST)
*
* @deprecated Use create function that uses POST action instead. And, has more features.
*
* @params integer|string $unique_id
* @params function $callback
* @return Niush\LaravelNanoTo\LaravelNanoTo || Illuminate\Http\RedirectResponse
*/
public function createWithGetRequest($unique_id = null, $callback = null)
{
$accounts = config('laravel-nano-to.accounts.' . $this->symbol, []);
$success_url = Route::has(config('laravel-nano-to.success_url'))
? route(config('laravel-nano-to.success_url'), $unique_id)
: config('laravel-nano-to.success_url');

$cancel_url = Route::has(config('laravel-nano-to.cancel_url'))
? route(config('laravel-nano-to.cancel_url'), $unique_id)
: config('laravel-nano-to.cancel_url');

$webhook_url = Route::has(config('laravel-nano-to.webhook_url'))
? route(config('laravel-nano-to.webhook_url'), $unique_id)
: config('laravel-nano-to.webhook_url');

// If Local use local_webhook_url from config instead.
if (!App::environment(['production', 'prod']) && config('laravel-nano-to.local_webhook_url')) {
$webhook_url = config('laravel-nano-to.local_webhook_url');
}

if ($accounts && sizeof($accounts) > 0) {
$address = $accounts[array_rand($accounts)];
$url = $this->base_url .
'/' . $address .
'?title=' . $this->title .
'&description=' . $this->description .
'&success_url=' . $success_url .
'&cancel_url=' . $cancel_url .
'&webhook_url=' . $webhook_url .
'&webhook_secret=' . $this->webhook_secret .
'&raw=' . ($this->raw ? 'true' : 'false');

if ($this->amount) {
$url .= '&price=' . $this->amount;
} elseif ($this->suggest) {
$parameters = array_map(function ($s) {
return $s["name"] . ":" . $s["price"];
}, $this->suggest);

$this->suggest = implode(",", $parameters);
$url .= '&suggest=' . $this->suggest;
}

try {
$client = new Client(['allow_redirects' => ['track_redirects' => true]]);

// Fake Nano.to response if testing.
if (App::environment() == 'testing') {
$response = new Response(200, [
'Content-Type' => 'text/html; charset=utf-8',
'X-Guzzle-Redirect-History' => [
'https://example.com/1',
'https://example.com/2',
],
'X-Guzzle-Redirect-Status-History' => [
"301",
"302",
],
]);
} else {
$response = $client->get($url);
}
// var_dump($response->getBody()->getContents());
$this->checkout_url = last($response->getHeader(\GuzzleHttp\RedirectMiddleware::HISTORY_HEADER));

if (!$this->checkout_url) {
return $this->throw_checkout_page_not_loaded();
} else {
if ($callback) {
$callback($this->checkout_url, $url);
} else {
if (!$unique_id) {
return $this->send();
}
}

return $this;
}
} catch (\Exception$e) {
if (App::environment() == 'testing') {dd($e);}
return $this->throw_checkout_page_not_loaded($e);
}
} else {
Expand Down
52 changes: 40 additions & 12 deletions src/NanoToApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,53 @@

class NanoToApi
{
private static $api_base_url = 'https://api.nano.to';
/**
* Get Nano.to API Base URL
*
* @return string
*/
public static function getBaseUrl()
{
return config('laravel-nano-to.base_url', 'https://nano.to');
}

/**
* Get CoinMarketCap conversion rate
*
* @param string $symbol
* @param string $currency
* @return object;
* @return object
*/
public static function getPrice($symbol = "NANO", $currency = "USD")
{
$client = new Client();
$response = $client->get(self::$api_base_url . '/price?symbol=' . $symbol . '&currency=' . $currency . '&json=true');
$response = $client->get(self::getBaseUrl() . '/price?symbol=' . $symbol . '&currency=' . $currency . '&json=true');
return (object) json_decode($response->getBody()->getContents(), true);
}

/**
* Get Nano.to Custom Username alias information
*
* @param string $username
* @return object;
* @return object
*/
public static function getUsername($username)
{
$client = new Client();
$response = $client->get(self::$api_base_url . '/' . $username . '/username?json=true');
$response = $client->get(self::getBaseUrl() . '/' . $username . '/username?json=true');
return (object) json_decode($response->getBody()->getContents(), true);
}

/**
* Get Nano Address Information
*
* @param string $address
* @return object;
* @return object
*/
public static function getNanoAddressInfo($address)
{
$client = new Client();
$response = $client->get(self::$api_base_url . '/' . $address . '/account?json=true');
$response = $client->get(self::getBaseUrl() . '/' . $address . '/account?json=true');
return (object) json_decode($response->getBody()->getContents(), true);
}

Expand All @@ -66,7 +74,7 @@ public static function getTotalNanoBalance()
];
$client = new Client();
foreach ($accounts as $account) {
$response = json_decode($client->get(self::$api_base_url . '/' . $account . '/account?json=true')->getBody()->getContents(), true);
$response = json_decode($client->get(self::getBaseUrl() . '/' . $account . '/account?json=true')->getBody()->getContents(), true);
$result['balance'] += $response["balance"] ?? 0;
$result['pending'] += !empty($response["pending"]) ? $response["pending"] : 0;
$result['balance_raw'] += $response["balance_raw"] ?? 0;
Expand All @@ -84,7 +92,7 @@ public static function getTotalNanoBalance()
public static function getPendingNanoBlocks($address)
{
$client = new Client();
$response = $client->get(self::$api_base_url . '/' . $address . '/pending?json=true');
$response = $client->get(self::getBaseUrl() . '/' . $address . '/pending?json=true');
return collect(json_decode($response->getBody()->getContents(), true));
}

Expand All @@ -97,7 +105,7 @@ public static function getPendingNanoBlocks($address)
public static function getNanoAddressHistory($address)
{
$client = new Client();
$response = $client->get(self::$api_base_url . '/' . $address . '/history?json=true');
$response = $client->get(self::getBaseUrl() . '/' . $address . '/history?json=true');
return collect(json_decode($response->getBody()->getContents(), true));
}

Expand All @@ -112,7 +120,7 @@ public static function getNanoAddressHistory($address)
public static function getNanoTransactionByAmount($address, $amount)
{
$client = new Client();
$response = $client->get(self::$api_base_url . '/payment/' . $address . '/' . $amount . '?json=true');
$response = $client->get(self::getBaseUrl() . '/payment/' . $address . '/' . $amount . '?json=true');
try {
return (object) json_decode($response->getBody()->getContents(), true);
} catch (Exception $e) {
Expand All @@ -129,7 +137,7 @@ public static function getNanoTransactionByAmount($address, $amount)
public static function getNanoTransactionByHash($hash)
{
$client = new Client();
$response = $client->get(self::$api_base_url . '/hash/' . $hash . '?json=true');
$response = $client->get(self::getBaseUrl() . '/hash/' . $hash . '?json=true');
try {
return (object) json_decode($response->getBody()->getContents(), true);
} catch (Exception $e) {
Expand Down Expand Up @@ -175,4 +183,24 @@ public static function isNanoCrawlerDown()
return true;
}
}

/**
* Check if Nano.to API is down or unreachable.
*
* @return boolean
*/
public static function isNanoToDown()
{
$client = new Client();
try {
$response = $client->get(self::getBaseUrl() . '/status?json=true');
$res = (object) json_decode($response->getBody()->getContents(), true);
if ($res->status === true) {
return false;
}
return true;
} catch (Exception $e) {
return true;
}
}
}
Loading

0 comments on commit 9bc9a35

Please sign in to comment.