nicwek
1 month ago
413
1
Laravel

I need help using Kenyan payment gateway

Posted 1 month ago by nicwek

I am trying to integrate a Kenyan Payment Gateway, Tingg, to my laravel project but i am having some challenges. Being a newbie to Laravel and Php, I am having a hard time changing their code which is written in Php to Laravel. There is an encryption endpoint php class that seems to give errors everytime i click on a button link to redirect me to an express checkout page. Help me recreate the php code to laravel. All the help with this will be sincerely appreciated.

The Php Class:

<?php
class EncryptParameters{
    /**
    * Merchant's IV key
    */
   private $iv;
   /**
    * Merchant's secret key
    */
   private $key;
   /**
    * EncryptParameters constructor.
    */
   public function __construct()
   {
       $this->iv = 'QnP4mk3WDy8VFzKw';
       $this->key = 'KmQkqYrZ389D2zRJ';
//       $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);

        $result = array(
            'params' => $encrypted,
            'accessKey' => $payload['accessKey'],
            'countryCode' => $payload['countryCode']
        );

        echo json_encode($result);
    }
}

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

$class->encryptData($request);


The Javascript Code with Button:

<!-- The "Pay with mula" button needs to have the "mula-checkout-button" class -->
<a class="checkout-button"></a>

<!-- Include a polyfil for to support the old browsers -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/1.0.17/webcomponents-loader.js"></script>

<!-- Include the mula Express checkout library -->
<script id="mula-checkout-library" type="text/javascript" src="https://beep2.cellulant.com:9212/checkout/v2/tingg-checkout.js" charset="utf-8"></script>

<!-- 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 = "http://localhost/checkout/encrypt.php";

    const no = Math.floor((Math.random() * 50000000) + 10000000);

    const params = {
        merchantTransactionID: no,
        customerFirstName: 'TestName',
        customerLastName: "TestName",
        customerEmail: "[email protected]",
        amount: 12,
        accountNumber: no,
        currencyCode: 'KES',
        languageCode: 'en',
        serviceDescription: 'Payment for the AfterWork Data Science Program',
        transactionID: no,
        serviceCode: '',
        productCode: '',
        payerClientCode:"",
        MSISDN: '',
        countryCode: '',
        accessKey:"", // Input your access key here
        dueDate: '2020-05-22 19:30:005',
        successRedirectUrl:"http://127.0.0.1:8000/payment-success",
        failRedirectUrl: "http://127.0.0.1:8000/payment-failed",
        paymentWebhookUrl: "http://127.0.0.1:8000/webhook"
    }; // The params to be encrypted

    Tingg.renderPayButton({ className:'checkout-button', checkoutType:'redirect'});

    // Initialize the mula checkout modal/redirect
    //on button click, redirect to express checkout
    document.querySelector(".checkout-button").addEventListener("click", function () {

        function encrypt() {
            var request = new XMLHttpRequest();
            request.open('POST', merchantURL, true);
            request.setRequestHeader('Content-Type',
                'application/json; charset=UTF-8',
                'Access-Control-Allow-Origin', '*',
            );

            request.onreadystatechange = function() {

                if(this.readyState == XMLHttpRequest.DONE && this.status == 200) {
                    var jsonData = JSON.parse(request.responseText);

                    Tingg.renderCheckout({
                        checkoutType: "redirect",
                        merchantProperties: jsonData
                    });
                }
            }

            request.send(JSON.stringify(params));
        }
        //encrypt and load checkout
        encrypt();
    });
</script>

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