Michael Posted July 18, 2013 Report Posted July 18, 2013 (edited) 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. Edited July 19, 2013 by CubicWebs MemoryX2 and Bit Bayou 2
Michael Posted July 18, 2013 Author Report Posted July 18, 2013 On 7/18/2013 at 10:10 PM, MemoryX2 said: Looks awesome!! Thanks! All the hard work was the developers I just did the tut haha But it works amazing.
Gareth-Host Red Dragon Posted July 19, 2013 Report Posted July 19, 2013 Nice one. This is definitely something that will be very useful, thanks.
Michael Posted July 19, 2013 Author Report Posted July 19, 2013 Another tip guys, if you get A£ etc and A€ you need to put this in the head section of your website pages: <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> If you get the following error when doing onetime prices: number_format() expects parameter 1 to be double, string given change them to: $price3 = getPackagePrice($api, 19, "onetime", 0); $price4 = getPackagePrice($api, 20, "onetime", 0);
MemoryX2 Posted July 20, 2013 Report Posted July 20, 2013 I'd like to just say that I have implemented this on one of my pages and it works great! Thanks again Mike!
Michael Posted July 20, 2013 Author Report Posted July 20, 2013 On 7/20/2013 at 4:09 PM, MemoryX2 said: I'd like to just say that I have implemented this on one of my pages and it works great! Thanks again Mike! I didn't do anything except from annoying Cody and Tyson haha.
MemoryX2 Posted July 22, 2013 Report Posted July 22, 2013 I need to figure out how to do this with page caching. It affects my page load times pretty drastically on any page that I'm accessing the api from. It would be nice if I had a script that would calculate these for me ever so often via cron tasks.. maybe once a week or something.
Michael Posted July 22, 2013 Author Report Posted July 22, 2013 On 7/22/2013 at 3:07 AM, MemoryX2 said: I need to figure out how to do this with page caching. It affects my page load times pretty drastically on any page that I'm accessing the api from. It would be nice if I had a script that would calculate these for me ever so often via cron tasks.. maybe once a week or something. really? are you using the require_once?
MemoryX2 Posted July 22, 2013 Report Posted July 22, 2013 Yeah. I mean it isn't terrible, but it's noticeable. Check out this page -> http://dev.memoryx2.com/ Notice how fast it loads then click on the web hosting tab. It is definately noticeable. Maybe because I put it in the same file so it reloads every time.. Just thought about this.
Michael Posted July 22, 2013 Author Report Posted July 22, 2013 On 7/22/2013 at 3:30 AM, MemoryX2 said: Yeah. I mean it isn't terrible, but it's noticeable. Check out this page -> http://dev.memoryx2.com/ Notice how fast it loads then click on the web hosting tab. It is definately noticeable. Maybe because I put it in the same file so it reloads every time.. Just thought about this. Oh I see haha Its only started happening on mine. http://licensecart.com/site/ http://cubicwebs.xxx/shared-hosting http://gtmetrix.com/reports/cubicwebs.xxx/1r5eQRsx
Michael Posted July 23, 2013 Author Report Posted July 23, 2013 On 7/22/2013 at 3:30 AM, MemoryX2 said: Yeah. I mean it isn't terrible, but it's noticeable. Check out this page -> http://dev.memoryx2.com/ Notice how fast it loads then click on the web hosting tab. It is definately noticeable. Maybe because I put it in the same file so it reloads every time.. Just thought about this. I don't get that now? http://tools.pingdom.com/fpt/#!/ddanDA/http://cubicwebs.xxx/ - Netherlands - EU http://tools.pingdom.com/fpt/#!/eyaMuJ/http://cubicwebs.xxx/ - Dallas - TX http://tools.pingdom.com/fpt/#!/ciRFwt/http://cubicwebs.xxx/ - New York - NY
votick Posted January 26, 2014 Report Posted January 26, 2014 I will give this a try as I did have live pricing from WHMCS when we where using it. I've also got a ticket in with support on finding out what I need to query to report back number of rows in a product id (web hosting, domains, vps's). E.g. How many web hosting packages are we hosting. So I can display it on our site.
votick Posted January 26, 2014 Report Posted January 26, 2014 Got the above working if anyone needs the code feel free to PM. Just working out how to work out the average ticket response time.
Michael Posted January 26, 2014 Author Report Posted January 26, 2014 On 1/26/2014 at 3:09 PM, votick said: Got the above working if anyone needs the code feel free to PM. Just working out how to work out the average ticket response time. Glad you got it working, average ticket time might be in 3.2 or 3.3, there's a feature request.
votick Posted February 7, 2014 Report Posted February 7, 2014 Been using your script to do pricing on our site. How can I remove GBP from appearing on the end? Also it seems to take a few seconds longer to load the page. Is there a quicker way of pulling pricing? Anyone got MySQL queries?
Michael Posted February 7, 2014 Author Report Posted February 7, 2014 On 2/7/2014 at 10:59 PM, votick said: Been using your script to do pricing on our site. How can I remove GBP from appearing on the end? Also it seems to take a few seconds longer to load the page. Is there a quicker way of pulling pricing? Anyone got MySQL queries? Do you have GBP on the end of your prices in the cart? If you do you can remove it from the currency screen in the admin area, and it should remove it from the site.
votick Posted February 8, 2014 Report Posted February 8, 2014 On 2/7/2014 at 11:08 PM, CubicWebs said: Do you have GBP on the end of your prices in the cart? If you do you can remove it from the currency screen in the admin area, and it should remove it from the site. Perfect yes that's worked. As for caching, If anyone could help with that that would be fab. Not quite sure how to go about it.
Michael Posted February 8, 2014 Author Report Posted February 8, 2014 On 2/8/2014 at 9:18 AM, votick said: Perfect yes that's worked. As for caching, If anyone could help with that that would be fab. Not quite sure how to go about it. I recommend this script on this post: http://www.blesta.com/forums/index.php?/topic/443-licensecart/?p=5362 . I still use it today.
votick Posted February 15, 2014 Report Posted February 15, 2014 I created my own caching system in the end using cron jobs. On another note I've finaly got around to doing our domains page. $COUK = getPackagePrice($api, 20, "year", 1); However this shows as £0.00 unless I set the billing to 1 Year instead of 2 Years. How can I get it to show the price when it's set to minimum of 2 Years? Cheers
Michael Posted February 15, 2014 Author Report Posted February 15, 2014 On 2/15/2014 at 3:04 PM, votick said: I created my own caching system in the end using cron jobs. On another note I've finaly got around to doing our domains page. $COUK = getPackagePrice($api, 20, "year", 1);However this shows as £0.00 unless I set the billing to 1 Year instead of 2 Years.How can I get it to show the price when it's set to minimum of 2 Years? Cheers Change the 1 to a 2 and it should work we do for SSL prices.
votick Posted February 15, 2014 Report Posted February 15, 2014 Perfect thanks. I don't know why but I thought that was to do with the company ID. Site is slowly getting there Michael 1
pcfacility Posted February 26, 2014 Report Posted February 26, 2014 Hello, I have a small problem with the price displayed by the api in € for ex : €4.99EUR The "€" is the problem. Also, how to add a space between the 4.99 end the € like 4.99 EUR? Thank you
Michael Posted February 26, 2014 Author Report Posted February 26, 2014 Make sure your website is encoded with UTF-8 eg: <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now