Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nicwek's avatar

How do i fetch an external php class to javascript

I have some javascript code within my view in laraveland i am trying to fetch an external php class which creates an encryption endpoint then executes the Javascript. How do I do this in Laravel, how do i call a URL of the class?

<!-- Include the mula Express checkout library -->
<!-- Initialize the "Pay with mula" button -->
<script type="text/javascript">
// End point to your service that handles encryption
const merchantURL = " **THE PHP CLASS URL COME IN HERE**";

</script>
0 likes
1 reply
nicwek's avatar

External PHP Class:

<?php
namespace App\Classes;

class EncryptParameters{
    /**
    * Merchant's IV key
    */
   private $iv;
   /**
    * Merchant's secret key
    */
   private $key;
   /**
    * EncryptParameters constructor.
    */
   public function __construct()
   {
       $this->iv = '2BfjH469WxmcJdPM';
       $this->key = 'QpJXdbYv6tHLhCTK';
//       $this->request = !empty($_POST)? $_POST:json_decode(file_get_contents('php://input'), true);
   }
   /**
    * Encrypt the string of customer details with the IV and secret key.
    *
    * @param $payload Pass in the array of parameters to be pass to express checkout.
    * @return string
    */
   public function encryptData($payload = [])
   {
        //The encryption method to be used
        $encrypt_method = "AES-256-CBC";
        // Hash the secret key
        $key = hash('sha256', $this->key);
        // Hash the iv - encrypt method AES-256-CBC expects 16 bytes
        $iv = substr(hash('sha256', $this->iv), 0, 16);
        $encrypted = openssl_encrypt(
            json_encode($payload, true),
            $encrypt_method,
            $key,
            0,
            $iv
        );
        //Base 64 Encode the encrypted payload
        $encrypted = base64_encode($encrypted);

        //echo json_encode($iv);

        $result = array(
            'params' => $encrypted,
            'accessKey' => $payload['a$I.qb6TCaxdv5stvnZuvWGuRrTOvflWUo.EtSxFOmnLXd5P1Yegwim'],
            'countryCode' => $payload['KE']
        );

        echo json_encode($result);
    }
}

$class = new EncryptParameters;
$request = !empty($_POST)? $_POST:json_decode(file_get_contents('php://input'), true);

$class->encryptData($request);

Please or to participate in this conversation.