Jump to content

Use Multi-Currency On Your Site Using GeoIP


Michael Kinder

Recommended Posts

Hi everyone! I just wanted to share this with you because I think it would be useful to those who aren't necessarily familiar with PHP and want to display different currencies based upon the user's IP address. Big thanks to @Blesta.Store for sharing a big portion of this code already on the forum. This will pull prices from Blesta's API and allow you to use multiple currencies.

 

Quote

Ok firstly you need to download the API which Cody has developed to make it easier for developers from:
 
https://github.com/phillipsdata/blesta_sdk (Click on download zip)
 
Put the files into the root of the Blesta install.
 
 
Then you need to make a API user and get the hash for the connection to your Blesta install.
 
 
After you've done that you can do the following, you need to put this on the php file you wish the prices to be displayed on:
 
If you aren't using a SSL on your installation change https://blestainstallationurl.com/api to http://blestainstallationurl.com/api

After you've uploaded the API SDK, you're going to want to download this file: https://www.geoplugin.com/_media/webservices/geoplugin.class.phps

Upload that file into your site's directory, then take the contents below and place it at the top of your site files (or the pages where you will be displaying the prices).

 

<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
// create a variable for the country code
$var_country_code = $geoplugin->countryCode;
// redirect based on country code:
if ($var_country_code == "AU") {
$selected_currency = "AUD";
}
else if ($var_country_code == "CA") {
$selected_currency = "CAD";
}
else if ($var_country_code == "AT") {
$selected_currency = "EUR";
}
else if ($var_country_code == "BE") {
$selected_currency = "EUR";
}
else if ($var_country_code == "BG") {
$selected_currency = "EUR";
}
else if ($var_country_code == "CY") {
$selected_currency = "EUR";
}
else if ($var_country_code == "CZ") {
$selected_currency = "EUR";
}
else if ($var_country_code == "DK") {
$selected_currency = "EUR";
}
else if ($var_country_code == "EE") {
$selected_currency = "EUR";
}
else if ($var_country_code == "FI") {
$selected_currency = "EUR";
}
else if ($var_country_code == "DE") {
$selected_currency = "EUR";
}
else if ($var_country_code == "GR") {
$selected_currency = "EUR";
}
else if ($var_country_code == "HU") {
$selected_currency = "EUR";
}
else if ($var_country_code == "IE") {
$selected_currency = "EUR";
}
else if ($var_country_code == "IT") {
$selected_currency = "EUR";
}
else if ($var_country_code == "LV") {
$selected_currency = "EUR";
}
else if ($var_country_code == "LU") {
$selected_currency = "EUR";
}
else if ($var_country_code == "MT") {
$selected_currency = "EUR";
}
else if ($var_country_code == "NL") {
$selected_currency = "EUR";
}
else if ($var_country_code == "PL") {
$selected_currency = "EUR";
}
else if ($var_country_code == "PT") {
$selected_currency = "EUR";
}
else if ($var_country_code == "RO") {
$selected_currency = "EUR";
}
else if ($var_country_code == "SK") {
$selected_currency = "EUR";
}
else if ($var_country_code == "SI") {
$selected_currency = "EUR";
}
else if ($var_country_code == "ES") {
$selected_currency = "EUR";
}
else if ($var_country_code == "SE") {
$selected_currency = "EUR";
}
else if ($var_country_code == "GB") {
$selected_currency = "GBP";
}
else if ($var_country_code == "IN") {
$selected_currency = "INR";
}
else if ($var_country_code == "MX") {
$selected_currency = "MXN";
}
else {
$selected_currency = "USD";
}

require_once "/home/username/public_html/blesta/api/blesta_api.php";

$user = "username";
$key = "key";
$url = "url";
$api = new BlestaApi($url, $user, $key);

$company_id = 1;

// Set acceptable currencies
$valid_currencies = array("USD", "GBP", "EUR", "INR", "MXN", "AUD");
// Set default currency


// Set another currency if given
if (isset($_GET['currency']) && in_array(strtoupper($_GET['currency']), $valid_currencies))
    $selected_currency = strtoupper($_GET['currency']);

$price1 = getPackagePrice($api, 4, "month", 1);
$price2 = getPackagePrice($api, 57, "year", 1);
$price3 = getPackagePrice($api, 29, "month", 1);
$price4 = getPackagePrice($api, 50, "month", 1);
$price5 = getPackagePrice($api, 298, "year", 1);


$price1_amount = formatCurrency($api, $price1->price, $price1->currency, $selected_currency, $company_id);
$price2_amount = formatCurrency($api, $price2->price, $price2->currency, $selected_currency, $company_id);
$price3_amount = formatCurrency($api, $price3->price, $price3->currency, $selected_currency, $company_id);
$price4_amount = formatCurrency($api, $price4->price, $price4->currency, $selected_currency, $company_id);
$price5_amount = formatCurrency($api, $price5->price, $price5->currency, $selected_currency, $company_id);


function getPackagePrice($api, $package_id, $period, $term) {

    $package = $api->get("packages", "get", array('package_id' => $package_id))->response();

    $package_price = null;
    foreach ($package->pricing as $price) {
        // Get monthly term
        if ($price->period == $period && $price->term == $term) {
            return $price;
        }
    }
    return false;
}


function formatCurrency($api, $amount, $from_currency, $to_currency, $company_id) {
    // Do the currency conversion
    $amount = $api->get("currencies", "convert", array('amount' => $amount, 'from_currency' => $from_currency, 'to_currency' => $to_currency, 'company_id' => $company_id))->response();

    // Format the currency
    return $api->get("currencies", "toCurrency", array('value' => $amount, 'currency' => $to_currency, 'company_id' => $company_id))->response();
}
?>

 

Next, you're going to want to create your API credentials in Blesta and fill them in appropriately.

After you've done this, simply place the following code (changing the numbers appropriately) where you would like to display the prices.
 

<?php echo $price1_amount; ?>

 

Of course you can make modifications as you desire, but I wanted to share this with you in case anyone wanted to do the same.

Link to comment
Share on other sites

Looks good!

One thing you could do in the future to improve on this is to create a plugin for Blesta that accepts an IP address and uses the GeoIP2 database already supported by Blesta to determine the country (i.e. replaces the geoplugin.com file), then your plugin can do the mapping and return the appropriate currency for the country.

Link to comment
Share on other sites

1 hour ago, Tyson said:

Looks good!

One thing you could do in the future to improve on this is to create a plugin for Blesta that accepts an IP address and uses the GeoIP2 database already supported by Blesta to determine the country (i.e. replaces the geoplugin.com file), then your plugin can do the mapping and return the appropriate currency for the country.

I'll definitely look into that! I'm just learning Blesta and can't wait to experiment further with it! ?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...