> For the complete documentation index, see [llms.txt](https://beycanpress.gitbook.io/cryptopay-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://beycanpress.gitbook.io/cryptopay-docs/for-developers/examples/currency-converters.md).

# Currency converters

You can use the structure below to add support for a currency converter.

```php
<?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();
    }
});

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://beycanpress.gitbook.io/cryptopay-docs/for-developers/examples/currency-converters.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
