From f39d7ba305d0a9181278d048e6e60c500d3657e5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Tue, 7 Mar 2023 20:54:17 +0100
Subject: [PATCH] Podpora QR pro platbu

---
 .gitlab-ci.yml                  |  2 +-
 Dockerfile                      |  2 ++
 lib/CF2022.pm                   |  1 +
 lib/CF2022/Controller/Orders.pm | 39 ++++++++++++++++++++++++++++++++-
 4 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 9fb5bb6..56064bc 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,7 +2,7 @@ image: docker:20.10.9
 
 variables:
   DOCKER_TLS_CERTDIR: "/certs"
-  IMAGE_VER: 0.12.1
+  IMAGE_VER: 0.13.0
 
 services:
   - docker:20.10.9-dind
diff --git a/Dockerfile b/Dockerfile
index 66885dd..87713c3 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -13,9 +13,11 @@ RUN apt-get update && apt-get install -y \
     libio-socket-ssl-perl \
     libjson-perl \
     libyaml-perl \
+    libpng-dev \
     libyaml-dev
 
 RUN cpanm \
+    Image::PNG::QRCode \
     Mojolicious \
     Mojo::Pg \
     Mojo::Redis \
diff --git a/lib/CF2022.pm b/lib/CF2022.pm
index 8c76ef8..463b55b 100644
--- a/lib/CF2022.pm
+++ b/lib/CF2022.pm
@@ -79,6 +79,7 @@ sub startup {
     # Router
     my $r = $self->routes;
     $r->get('/')->to(cb => sub { shift->redirect_to('/api.html');});
+    $r->get('/api/orders/:id/payment.png')->to('Orders#qr');
 
 }
 
diff --git a/lib/CF2022/Controller/Orders.pm b/lib/CF2022/Controller/Orders.pm
index efd7b52..56f9a11 100644
--- a/lib/CF2022/Controller/Orders.pm
+++ b/lib/CF2022/Controller/Orders.pm
@@ -2,12 +2,16 @@ package CF2022::Controller::Orders;
 use Mojo::Base 'Mojolicious::Controller';
 use Mojo::UserAgent;
 use Mojo::Asset::File;
+use Image::PNG::QRCode 'qrpng';
 
 use constant MAIN_PRODUCTS        => qr/^(1|2|3)$/; # typy akreditace
 use constant NONFREE_PRODUCTS     => qr/^(1)$/;     # akreditace vyzadujici token
 use constant SUBSIDY_PRODUCTS     => qr/^(58)$/;    # prispevkove polozky
 use constant FALLBACK_PRODUCT     => 2;             # fallback akreditace
 
+use constant TPL_VS      => '%d%07d';
+use constant TPL_PAYMENT => 'SPD*1.0*ACC:%s*AM:%f*MSG:%s, %s*X-ID:%s*X-VS:%d*X-SS:%d*X-KS:%d';
+
 use feature 'signatures';
 no warnings qw{ experimental::signatures };
 
@@ -167,6 +171,14 @@ sub create ($c ) {
         order_id => $rc->json->{code},
     });
 
+    $rc->json->{payment_request} => {
+        account => $c->cfg->{payment_account},
+        amount  => $order->response->{payments}[0]{amount},
+        vs      => sprintf(TPL_VS, $c->cfg->{payment_vs_prefix}, $order->id ),
+        ss      => $c->cfg->{payment_ss},
+        ks      => $c->cfg->{payment_ks},
+    };
+
     $c->render(
         status => 201,
         json   => $rc->json,
@@ -206,7 +218,7 @@ sub get ($c ) {
 
     return $c->error(404, 'NOT FOUND') if ! $order;
 
-    # datas z pretixu
+    # data z pretixu (kvuli stavu, jinak je to v databazi???)
 
     $c->app->log->error($order->api . $order->order_id);
     my $ua = Mojo::UserAgent->new;
@@ -222,6 +234,31 @@ sub get ($c ) {
     );
 }
 
+sub qr ($c) {
+    my $order = $c->schema->resultset('Order')->find({
+        id  => $c->stash->{id},
+        deleted  => undef,
+    });
+
+    return $c->error(404, 'NOT FOUND') if ! $order;
+
+    my $payment = sprintf(TPL_PAYMENT,
+        $c->cfg->{payment_iban},
+        $order->response->{payments}[0]{amount},
+        $c->cfg->{payment_msg},
+        $order->request->{positions}[0]{attendee_name},
+        $order->order_id,
+        sprintf(TPL_VS, $c->cfg->{payment_vs_prefix}, $order->id ),
+        $c->cfg->{payment_ss},
+        $c->cfg->{payment_ks},
+    );
+
+    my $png = qrpng (text => $payment, level => 4);
+
+    $c->res->headers->content_type('image/png');
+    $c->render( data => $png );
+}
+
 sub _upload_photo ($c, $username ) {
 
     my $ua = Mojo::UserAgent->new;
-- 
GitLab