Michael Posted April 3, 2014 Report Posted April 3, 2014 Hello guys I'm a bit confused with this, it only happens on InterWorx was working fine on cPanel just wondering if I need to install anything. On licensecart.com we use the new API the guys did for the community to make our lifes easier however I get this in the error log: [Thu Apr 03 15:22:07 2014] [error] [client MYIP] PHP Warning: Invalid argument supplied for foreach() in /chroot/home/username/licensecart.com/html/blesta.php on line 48 The code on that line is important: foreach ($package->pricing as $price) { // Get monthly term if ($price->period == $period && $price->term == $term) { return $price; } } It's part of this: 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; } And I have: InterWorx 5.0.14 (Beta Tier) Blesta 3.1.3 MySQL 5.5.36 PHP 5.4.26 Apache 2.2.26 Thanks guys. Flynnnan 1 Quote
Tyson Posted April 3, 2014 Report Posted April 3, 2014 You're making the assumption that $package always exists and contains pricing, which may not be so. This may fix it, but it sounds like the package you're trying to get pricing for does not exist. function getPackagePrice($api, $package_id, $period, $term) { $package = $api->get("packages", "get", array('package_id' => $package_id))->response(); if (!$package) return false; $package_price = null; foreach ($package->pricing as $price) { // Get monthly term if ($price->period == $period && $price->term == $term) { return $price; } } return false; } Quote
Michael Posted April 3, 2014 Author Report Posted April 3, 2014 You're making the assumption that $package always exists and contains pricing, which may not be so. This may fix it, but it sounds like the package you're trying to get pricing for does not exist. function getPackagePrice($api, $package_id, $period, $term) { $package = $api->get("packages", "get", array('package_id' => $package_id))->response(); if (!$package) return false; $package_price = null; foreach ($package->pricing as $price) { // Get monthly term if ($price->period == $period && $price->term == $term) { return $price; } } return false; } That shows a white price still mate and in the error_log it shows: [Thu Apr 03 18:24:05 2014] [error] [client MYIP] PHP Warning: Invalid argument supplied for foreach() in /chroot/home/username/licensecart.com/html/html/blesta.php on line 48, referer: http://licensecart.com/products The full code is: <?php require_once "/chroot/home/username/licensecart.com/html/billing/blesta_api.php"; $user = "Licensecart_user"; $key = "9d383a*******************1b6a6e8"; $url = "https://billing.licensecart.com/api/"; $api = new BlestaApi($url, $user, $key); $company_id = 2; // 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, 17, "month", 1); $price2 = getPackagePrice($api, 18, "month", 1); $price3 = getPackagePrice($api, 19, "onetime", 0); $price4 = getPackagePrice($api, 20, "onetime", 0); $price5 = getPackagePrice($api, 23, "onetime", 0); $price6 = getPackagePrice($api, 24, "onetime", 0); $price7 = getPackagePrice($api, 24, "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); $price5_amount = formatCurrency($api, $price5->price, $price5->currency, $selected_currency, $company_id); $price6_amount = formatCurrency($api, $price6->price, $price6->currency, $selected_currency, $company_id); $price7_amount = formatCurrency($api, $price7->price, $price7->currency, $selected_currency, $company_id); function getPackagePrice($api, $package_id, $period, $term) { $package = $api->get("packages", "get", array('package_id' => $package_id))->response(); if (!$package) return false; $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(); } ?> Quote
Tyson Posted April 3, 2014 Report Posted April 3, 2014 That shows a white price still mate and in the error_log it shows: [Thu Apr 03 18:24:05 2014] [error] [client MYIP] PHP Warning: Invalid argument supplied for foreach() in /chroot/home/username/licensecart.com/html/html/blesta.php on line 48, referer: http://licensecart.com/products What's a "white price"? You shouldn't continue to experience the same error, so double check whether that is the case. But like I mentioned above, the reason you receive an error at all is because there is no package found with the given package ID, which is one of: 17, 18, 19, 20, 23, 24. If you want to debug, take a look at the response from the API when fetching a package: function getPackagePrice($api, $package_id, $period, $term) { $package = $api->get("packages", "get", array('package_id' => $package_id))->response(); var_dump($package); if (!$package) { echo "missing package id: " . $package_id; return false; } $package_price = null; foreach ($package->pricing as $price) { // Get monthly term if ($price->period == $period && $price->term == $term) { return $price; } } return false; } Quote
Michael Posted April 3, 2014 Author Report Posted April 3, 2014 Thanks mate I'll have a look at that, I might of missed something on the import :s By white price I mean if you go to: http://licensecart.com/blesta there's no prices That's weird mate haha. NULL missing package id: 17NULL missing package id: 18NULL missing package id: 19NULL missing package id: 20NULL missing package id: 23NULL missing package id: 24NULL missing package id: 24 But I'm sure 17 has a month and year because of the ID on the url for the package: Is there a debug code to see if the API connection is correct or not working mate? Quote
Tyson Posted April 3, 2014 Report Posted April 3, 2014 Thanks mate I'll have a look at that, I might of missed something on the import :s By white price I mean if you go to: http://licensecart.com/blesta there's no prices That's weird mate haha. NULL missing package id: 17NULL missing package id: 18NULL missing package id: 19NULL missing package id: 20NULL missing package id: 23NULL missing package id: 24NULL missing package id: 24 Is there a debug code to see if the API connection is correct or not working mate? Sounds like there is a connection problem then. You can try checking whether any errors are being set from the response: function getPackagePrice($api, $package_id, $period, $term) { $package = $api->get("packages", "get", array('package_id' => $package_id)); var_dump($package->response()); var_dump($package->errors()); $package = $package->response(); if (!$package) { echo "missing package id: " . $package_id; return false; } $package_price = null; foreach ($package->pricing as $price) { // Get monthly term if ($price->period == $period && $price->term == $term) { return $price; } } return false; } Quote
Michael Posted April 3, 2014 Author Report Posted April 3, 2014 With that code mate I get this: NULL object(stdClass)#3 (1) { ["error"]=> NULL } missing package id: 17NULL object(stdClass)#3 (1) { ["error"]=> NULL } missing package id: 18NULL object(stdClass)#3 (1) { ["error"]=> NULL } missing package id: 19NULL object(stdClass)#3 (1) { ["error"]=> NULL } missing package id: 20NULL object(stdClass)#3 (1) { ["error"]=> NULL } missing package id: 23NULL object(stdClass)#3 (1) { ["error"]=> NULL } missing package id: 24NULL object(stdClass)#3 (1) { ["error"]=> NULL } missing package id: 24 Quote
Tyson Posted April 3, 2014 Report Posted April 3, 2014 So no response and no errors. Something else must be preventing it from connecting. What changes have you made recently that caused that to stop working? Quote
Michael Posted April 4, 2014 Author Report Posted April 4, 2014 So no response and no errors. Something else must be preventing it from connecting. What changes have you made recently that caused that to stop working? Moved to InterWorx from cPanel mate and I removed company 1 from the new billing area as CubicWebs is closing. Would it be because it's not got a SSL? because on cubicwebs we had https://billing.cubicwebs.com however we didn't have one on the billing.licensecart.com hostname for company 2. Quote
Tyson Posted April 4, 2014 Report Posted April 4, 2014 Could be. If the https://billing.licensecart.com/api/ doesn't resolve, then that is a problem. I see that URL returns 404 as opposed to https://billing.cubicwebs.com/api Quote
Michael Posted April 4, 2014 Author Report Posted April 4, 2014 Could be. If the https://billing.licensecart.com/api/ doesn't resolve, then that is a problem. I see that URL returns 404 as opposed to https://billing.cubicwebs.com/api Ah mate I can't find out why that doesn't work on licensecart.com there's no folder or anythng to cause any issues. I wonder is it related to incorrect url to the file? require_once "/chroot/home/username/licensecart.com/html/billing/blesta_api.php"; Does it need to be 0777 chmod'd or something? Quote
flangefrog Posted April 4, 2014 Report Posted April 4, 2014 The reason your url https://billing.licensecart.com/api/ can't be reached is because your rewriting is not set up. You probably just need to rename your .htaccess file if you're using Apache. You can see this is the problem by changing the url in your file to http://billing.licensecart.com/index.php/api/ and it should work. The second problem is that when accessed over https, Apache is looking for the files in the wrong directory. You need to adjust the apache.conf or virtualhost conf file to point to the same directory as port 80. You can see this problem by going to https://billing.licensecart.com/ and it's pointing to a different directory than http://billing.licensecart.com/ Michael 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.