🔌Currency converters
You can use the structure below to add support for a currency converter.
<?php
declare(strict_types=1);
namespace BeycanPress\CryptoPay;
// @phpcs:disable PSR1.Files.SideEffects
// @phpcs:disable PSR12.Files.FileHeader
// @phpcs:disable Generic.Files.LineLength
/**
* Plugin Name: Example Currency Converter
* Version: 1.0.0
* Plugin URI: https://beycanpress.com/cryptopay/
* Update URI: https://beycanpress.com/cryptopay/
* Description: Extra currency converter API for CryptoPay
* Author: BeycanPress LLC
* Author URI: https://beycanpress.com
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: cryptopay
* Tags: Cryptopay, Cryptocurrency, WooCommerce, WordPress, MetaMask, Trust, Binance, Wallet, Ethereum, Bitcoin, Binance smart chain, Payment, Plugin, Gateway, Converter, API
* Requires at least: 5.0
* Tested up to: 6.4.2
* Requires PHP: 8.1
*/
use BeycanPress\CryptoPay\Helpers;
use BeycanPress\CryptoPay\PluginHero\Hook;
use BeycanPress\CryptoPay\Settings\Settings;
use BeycanPress\CryptoPay\Types\Data\PaymentDataType;
add_action('plugins_loaded', function (): void {
if (class_exists(Loader::class)) {
class ExampleCurrenyConverter
{
/**
* @var string
*/
private string $key = 'example';
/**
* exampleconstructor.
*/
public function __construct()
{
// this is required for all addons
Helpers::registerAddon('example', __FILE__);
Hook::addFilter("converters", function ($converters) {
$converters[$this->key] = $this->key;
return $converters;
});
Hook::addFilter("api_options", function ($options) {
$options[] = [
'id' => 'exampleApiKey',
'type' => 'text',
'title' => esc_html__('Exmaple API Key', 'cryptopay'),
'help' => esc_html__('Exmaple API key is required for Exmaple currency converter.', 'cryptopay'),
'dependency' => ['converter', '==', $this->key],
'sanitize' => function ($val) {
return sanitize_text_field($val);
},
'validate' => function ($val) {
$converter = Settings::live('converter');
if (!is_null($converter) && $converter == $this->key && !$val) {
return esc_html__('Exmaple API key is required.', 'cryptopay');
}
}
];
return $options;
});
Hook::addFilter(
"currency_converter_" . $this->key,
function (?float $paymentPrice, PaymentDataType $data): ?float {
try {
$order = $data->getOrder();
$amount = $order->getAmount();
$network = $data->getNetwork();
$orderCurrency = $order->getCurrency();
$paymentCurrency = $order->getPaymentCurrency();
// Your convert process
return $paymentPrice;
} catch (\Exception $e) {
Helpers::debug($e->getMessage(), 'ERROR', $e);
return $paymentPrice;
}
},
10,
5
);
}
}
new ExampleCurrenyConverter();
}
});
Last updated