Thanks to Cody, Tyson and Josh (Serverbin.net) for the code, together they helped get this code working for us the community and here is a little tutorial on how to get this working. Ok firstly you need to download the brand new 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
<?php
require_once "/home/username/public_html/blesta_api.php";
$user = "API USERNAME";
$key = "API KEY";
$url = "https://blestainstallationurl.com/api/";
$api = new BlestaApi($url, $user, $key);
$company_id = 1;
// Set acceptable currencies
$valid_currencies = array("USD", "GBP", "EUR");
// Set default currency
$selected_currency = "USD";
// Set another currency if given
if (isset($_GET['currency']) && in_array(strtoupper($_GET['currency']), $valid_currencies))
$selected_currency = strtoupper($_GET['currency']);
$price1 = getPackagePrice($api, 1, "month", 1);
$price1_amount = formatCurrency($api, $price1->price, $price1->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();
}
?>
You can alter the code for more than one product like the example for mine:
<?php
require_once "/home/cwadminb/public_html/blesta_api.php";
$user = "CubicWebs_API";
$key = "dbf8a2e7bd87bea1c79d868dba5a56f3";
$url = "https://billing.cubicwebs.com/api/";
$api = new BlestaApi($url, $user, $key);
$company_id = 1;
// Set acceptable currencies
$valid_currencies = array("USD", "GBP", "EUR");
// Set default currency
$selected_currency = "USD";
// Set another currency if given
if (isset($_GET['currency']) && in_array(strtoupper($_GET['currency']), $valid_currencies))
$selected_currency = strtoupper($_GET['currency']);
$price1 = getPackagePrice($api, 1, "month", 1);
$price2 = getPackagePrice($api, 2, "month", 1);
$price3 = getPackagePrice($api, 3, "month", 1);
$price4 = getPackagePrice($api, 4, "month", 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);
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();
}
?>
Where you wish the price to be put the following php code:
<?php echo $price1_amount; ?>
Change the number to the product price you wish to display. Again thanks to the coders for helping the community.
If you used this code when I posted it please update it as it has a fix from Tyson to ensure the GBP prices don't just change the USD symbol.