0

https://github.com/MukhtarSayedSaleh/saudi-zakat-qr-generator I can include this library in opencart 2 easily since it is pure php but how can I include it in twig files, or any php library.

how could I use the following code in admin/view/template/sale/order_invoice.twig

require __DIR__ . '/vendor/autoload.php';

use MuktarSayedSaleh\ZakatTlv\Encoder;

$encoder = new Encoder();
$qr_signature = $encoder->encode(
    "Sparehub",
    "1234567890",
    null,
    10000,
    150
);

var_dump($qr_signature); 

Edit: I'm trying to create it as a library in system/library/ZakatTlv/Encoder.php

<?php
namespace ZakatTlv;
use DateTime;
use ZakatTlv\Tag;

class Encoder {

public function __construct($registry) {
    $this->config = $registry->get('config');
    $this->db = $registry->get('db');
    $this->request = $registry->get('request');
    $this->session = $registry->get('session');
    $this->registry = $registry;
}

public function encode(
    string $seller_name,
    string $seller_vat_number,
    string $timestamp=null,
    float $total_amount,
    float $vat_amount
) {
    $now = new DateTime("now");
    $timestamp = $timestamp ?? $now->format('Y-m-dTH:i:sP');

    $components = [];

    $index=0;
    foreach([
        $seller_name,
        $seller_vat_number,
        $timestamp,
        $total_amount,
        $vat_amount
    ] as $value){
        $components[] = strval(
            new Tag(++$index, $value)
        );
    }
    //return $this->registry->get($components);

    return base64_encode(
        implode($components)
    );
}
}

When I try to call it in the controller like this

$this->registry->set('ZakatTlv/Encoder', new Encoder($this->registry));
$Encoder->encode("Sparehub", "1234567890", null, 10000, 150);

I get this error Fatal error: Uncaught Error: Class 'Encoder' not found

So how could I register a library in opencart3

  • A Twig template might not be the preferred place to load a library ...and `autoload.php` might also be useless, unless the library's PSR-4 namespace would actually be mapped by it. – Martin Zeitler Dec 13 '21 at 15:45
  • You don't, you extend twig and chain the code you've added here in there – DarkBee Dec 13 '21 at 15:58
  • Does this answer your question? [How to call php function from twig \_Symfony3](https://stackoverflow.com/questions/48659619/how-to-call-php-function-from-twig-symfony3) – DarkBee Dec 13 '21 at 15:59
  • Passing `string $qr_signature` into the twig template would rather be MVC conform. – Martin Zeitler Dec 13 '21 at 16:12
  • Does this answer your question? [OpenCart pass variable to twig from controller](https://stackoverflow.com/questions/52465396/opencart-pass-variable-to-twig-from-controller) – Martin Zeitler Dec 13 '21 at 16:14

0 Answers0