Created
January 9, 2021 16:31
-
-
Save con322/b957250aa8edba6b9d372a2eda66f17f to your computer and use it in GitHub Desktop.
Convert any currency that's not EUR to EUR for Nash Link payment method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// this filter needs adding in nash woocomerce plugin, please. | |
$invoiceData = $api->createinvoice( apply_filters( 'nash_link_invoice_params', $params ) ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function nash_link_convert( $amount, $from_currency, $to_currency ){ | |
$apikey = get_option( 'crypto_warzone_option_currencyconverterapi_key' ); | |
// If no apikey return early with current param(s). | |
if ( !$apikey ) { | |
return $params; | |
} | |
$from_Currency = urlencode( $from_currency ); | |
$to_Currency = urlencode( $to_currency ); | |
$query = "{$from_Currency}_{$to_Currency}"; | |
// @TODO error handling here, maybe? | |
$json = file_get_contents("https://free.currconv.com/api/v7/convert?q={$query}&compact=ultra&apiKey={$apikey}"); | |
$obj = json_decode( $json, true ); | |
$val = floatval( $obj["$query"] ); | |
$total = $val * $amount; | |
return number_format( $total, 2, '.', '' ); | |
} | |
function nash_currency_conversion( $params ) { | |
// Currently Nash Link only accepts EUR, so we return early if the currency param is "EUR". | |
if ( 'EUR' === $params->currency ) { | |
return $params; | |
} | |
// Convert from passed currency to EUR. | |
$converted_currency = nash_link_convert( $params->price, $params->currency, 'EUR' ); | |
$params->price = $converted_currency; | |
// Set the currency param to EUR.. | |
$params->currency = 'EUR'; | |
// Return new params. | |
return $params; | |
} | |
add_filter( 'nash_link_invoice_params', 'nash_currency_conversion', 10, 1 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function crypto_warzone_register_settings() { | |
add_option( 'crypto_warzone_option_currencyconverterapi_key' ); | |
register_setting( 'crypto_warzone_options_group', 'crypto_warzone_option_currencyconverterapi_key', 'crypto_warzone_callback' ); | |
} | |
add_action( 'admin_init', 'crypto_warzone_register_settings' ); | |
function crypto_warzone_register_options_page() { | |
add_options_page('Crypto Warzone', 'Crypto Warzone', 'manage_options', 'crypto-warzone-settings', 'crypto_warzone_options_page'); | |
} | |
add_action('admin_menu', 'crypto_warzone_register_options_page'); | |
function crypto_warzone_options_page() | |
{ | |
?> | |
<div> | |
<h2>Crypto Warzone options</h2> | |
<form method="post" action="options.php"> | |
<?php settings_fields( 'crypto_warzone_options_group' ); ?> | |
<h3>Currency converter API</h3> | |
<p>The Free Currency Converter API, get api key <a href="https://free.currencyconverterapi.com/" target="_blank">here</a>.</p> | |
<table> | |
<tr valign="top"> | |
<th scope="row"><label for="crypto_warzone_option_currencyconverterapi_key">API key</label></th> | |
<td><input type="text" id="crypto_warzone_option_currencyconverterapi_key" name="crypto_warzone_option_currencyconverterapi_key" value="<?php echo get_option('crypto_warzone_option_currencyconverterapi_key'); ?>" /></td> | |
</tr> | |
</table> | |
<?php submit_button(); ?> | |
</form> | |
</div> | |
<?php | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment