Comment on page

Currency converter add-on example

Below you can see the structure of Moralis Currency converter API Add-on. This way you can develop your own custom converters.
Or you can contact us to get custom development service from us.
<?php
namespace BeycanPress\CryptoPay;
/**
* Plugin Name: CryptoPay Moralis Converter API
* Version: 1.0.0
* Plugin URI: https://beycanpress.com/
* Description: Extra currency converter API for CryptoPay
* Author: BeycanPress
* Author URI: https://beycanpress.com
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl-3.0.tr.html
* Tags: Cryptopay, Cryptocurrency, WooCommerce, WordPress, MetaMask, Trust, Binance, Wallet, Ethereum, Bitcoin, Binance smart chain, Payment, Plugin, Gateway, Moralis, Converter, API
* Requires at least: 5.0
* Tested up to: 6.2
* Requires PHP: 7.4
*/
use BeycanPress\CurrencyConverter;
use BeycanPress\CryptoPay\Settings;
use BeycanPress\CryptoPay\PluginHero\Hook;
add_action('plugins_loaded', function() {
if (class_exists(Loader::class)) {
class Moralis extends PluginHero\Plugin
{
private $key = 'Moralis';
public function __construct()
{
Hook::addFilter("converters", function($converters) {
$converters[$this->key] = $this->key;
return $converters;
});
Hook::addFilter("api_options", function($options) {
$options[] = [
'id' => 'moralisAPIKey',
'type' => 'text',
'title' => esc_html__('Moralis API Key', 'cryptopay'),
'dependency' => ['converter', '==', $this->key],
'desc' => '<a href="https://moralis.io/" target="_blank">Get API Key</a>',
'sanitize' => function($val) {
return sanitize_text_field($val);
}
];
return $options;
});
if (Settings::get('converter') == $this->key) {
Hook::addFilter("currency_converter",
function($paymentPrice, $fiatCurrency, $cryptoCurrency, $amount, $network) {
try {
$converter = new CurrencyConverter('CryptoCompare');
if (!isset($cryptoCurrency->address)) return null;
if ($converter->isStableCoin($fiatCurrency, $cryptoCurrency->symbol)) {
return floatval($amount);
}
if ($network->code == 'evmBased') {
$hexId = $network->hexId != '0x4' ? $network->hexId : '0x5';
$url = "https://deep-index.moralis.io/api/v2/erc20/$cryptoCurrency->address/price?chain=$hexId";
} elseif ($network->code == 'solana') {
$url = "https://solana-gateway.moralis.io/token/mainnet/$cryptoCurrency->address/price";
}
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = [
"accept: application/json",
"X-API-Key: " . Settings::get('moralisAPIKey'),
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = json_decode(curl_exec($curl));
curl_close($curl);
if ($response && isset($response->usdPrice)) {
if ($tokenPrice = $response->usdPrice) {
$orderUsdPrice = $converter->convert($fiatCurrency, 'USD', $amount);
return (float) ($orderUsdPrice / $tokenPrice);
}
}
return null;
} catch (\Exception $e) {
return null;
}
}, 10, 5);
}
}
}
new Moralis();
}
});