Skip to content

get value from API

I have module create and i can get value from API but i’m failing to use Math.ceil to calculate to get correct interest based on product price.

here my code below

Helpers file

<?php

namespace VendorModuleHelper;

class Data extends MagentoFrameworkAppHelperAbstractHelper
{

    /**
     * @param MagentoFrameworkAppHelperContext $context
     */
    public function __construct(
        MagentoFrameworkAppHelperContext $context
    ) {
        parent::__construct($context);
    }

    public function getDetails()
    {

        $api_url = 'https://mobicred.co.za/api/get-interest';
        $json_data = file_get_contents($api_url);

        echo $json_data;
    }
    
}

Block file

<?php

namespace VendorModuleBlock;

use MagentoCatalogModelProduct;

class ShowResult extends MagentoFrameworkViewElementTemplate
{
   /**
     * @var Product
     */
    protected $_product = null;

    /**
     * Core registry
     *
     * @var MagentoFrameworkRegistry
     */
    protected $_coreRegistry = null;

    /**
     *
     * @var MagentoFrameworkAppObjectManager
     */
    private $objectManager;

    /**
     * @param MagentoFrameworkViewElementTemplateContext $context
     * @param MagentoFrameworkRegistry $registry
     * @param array $data
     **/

    public function __construct(
        MagentoFrameworkViewElementTemplateContext $context,
        MagentoFrameworkRegistry $registry,
        array $data = []
    ) {
        $this->_coreRegistry = $registry;
        parent::__construct($context, $data);

        $objectManager = MagentoFrameworkAppObjectManager::getInstance();
        $this->_interest = $objectManager->get("VendorModuleHelperData");
    }

    public function getResults()
    {
        $this->_interest->getDetails();
    }
    /**
     * @return Product
     */
    public function getProduct()
    {
        if (!$this->_product) {
            $this->_product = $this->_coreRegistry->registry('product');
        }
        return $this->_product;
    }
}

Template my phtml

<?php
/** @var VendorModuleBlockShowResult $block */
?>
<?php

    $block->getResults();
   $amount = $product->getprice();
    
// Calculation
$term = 12;
$rate = $block->getResults();;
$apr = $rate / 1200;
//$apr = 0.01458333333;
$formula = $apr * -$amount * Math.pow((1 + $apr), $term) / (1 - Math.pow((1 + $apr), $term));
echo $instalmentValue = Math.ceil($formula);
?>

What did i go wrong. please help