nicwek
1 month ago
286
8
Laravel

How can i fetch a php class to a laravel project

Posted 1 month ago by nicwek

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));
?>

Please sign in or create an account to participate in this conversation.