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

nicwek's avatar

How can i fetch a php class to a laravel project

I am new to php and laravel I need to fetch a php class endpoint and insert into the javascript section of my php project, how is this achieved, what is the procedure? All the help will be higly appreciated.

My javascript:

document
.querySelector('.awesome-checkout-button')
.addEventListener('click', function() {
 //create the checkout request

	fetch('<THE URL IS INSERTED HERE >', {
   
 method: 'post',
    headers: {
        'Content-type': 'application/json; charset=UTF-8'
    },
    //checkout details as shown above
    body: payload
	 })

This is my php endpoint code:

<?php
class Checkout {
private $secret;
private $IV;

public function __construct($secret, $IV) {
    $this->secret = $secret;
    $this->IV = $IV;
}

public function encrypt($requestBody) {
    $secret = hash('sha256', $this->secret);
    $IV = substr(hash('sha256', $this->IV), 0, 16);

    $payload = json_encode($requestBody);
    $result = openssl_encrypt(
        $payload, 
        'AES-256-CBC', 
        $secret, 
        0, 
        $IV
    );

    return base64_encode($result);
 }
}

// Get the body of the post request made 
// after the customer clicked the checkout button
$checkoutRequestBody = file_get_contents('php://input');

// The Request body is read into a string,
// so we decode the JSON string into a PHP associative array
$checkoutPayload = json_decode($checkoutRequestBody);

$checkout = new Checkout(
	'6PZ7pTGbMHyRK8XN',
	'3MY6hpwJQy9b7cfn'
);
$params = $checkout->encrypt($checkoutPayload);
header('Content-Type: application/json');
echo json_encode(array("params" => $params));
?>
0 likes
8 replies
jlrdw's avatar

Have you looked over the chapter in the documentation on front end it explains how to set up assets. But anything PHP can be used in laravel since it's a PHP framework, also JavaScript and just about any JavaScript library you choose to use.

I noticed the word check out in your code is this some sort of a shopping cart. Anyway if that is the case whichever payment system you are using they will also have instructions on using their API.

nicwek's avatar

Thanks @jlrdw for the response. Honestly, i am implementing a payment gateway. However, the docs for the gateway are not informative. Also, i have tried the laravel docs and i seem not to be finding the answers i am looking for. Hence, if you won't mind, i just needed to know, how is the class implemented, do i have to create an api with a controller and model, and so on, or what is the actually procedure of doing this?

jlrdw's avatar

Which payment system are you using so others can get involved and maybe help.

nicwek's avatar

Okay, its a Kenyan payment gateway, Tingg

jlrdw's avatar

If no one else responds shortly, I suggest start a new topic, something like:

I need help using Kenyan payment gateway

Something like that.

Cronix's avatar

Is this URL you're trying to access on your sever or an external server? At the simplest, your frontend makes a request to the backend via ajax and return a json response that your frontend javascript can then consume. That typically involves at least a route, a controller to process it, a model (if interacting with the database), and a json response back. How many routes do you need for this? Hard to say about needing an API with what you've stated. If it's just the one route you mentioned, no, I wouldn't make an API just for that and use a regular route. Do others need to access this route and be authenticated, etc., that would probably actually require an API?

Cronix's avatar

Routes map the url to a controller. When that route is hit, it will call whatever controller method you defined for it. The controller method can get the input that was sent, if any, from the frontend, manipulate it, gather other data with models, etc., and then return a json response.

You should probably watch the introduction to laravel series. I think it will answer a lot of your questions and it does quite a good job. https://laracasts.com/series/laravel-6-from-scratch

Please or to participate in this conversation.