aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-util/src/qr.ts
blob: e22c81dc020e873922c685de67d1133005185a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 This file is part of GNU Taler
 (C) 2024 Taler Systems S.A.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

/**
 * Imports.
 */
import { Amounts } from "./index.js";
import { parsePaytoUri } from "./payto.js";

type EncodeResult = { type: "ok"; qrContent: string } | { type: "skip" };

/**
 * See "Schweizer Implementation Guidelines QR-Rechnung".
 */
function encodePaytoAsSwissQrBill(paytoUri: string): EncodeResult {
  const parsedPayto = parsePaytoUri(paytoUri);
  if (!parsedPayto) {
    throw Error("invalid payto URI");
  }
  if (parsedPayto.targetType !== "iban") {
    return { type: "skip" };
  }
  const amountStr = parsedPayto.params["amount"];
  const iban = parsedPayto.targetPath;
  const countryCode = iban.slice(0, 2);
  const lines = [
    "SPC", // QRType
    "0200", // Version
    "1", // Character set (1: UTF-8)
    iban, // Beneficiary IBAN
    // Group: Beneficiary
    "S", // Address type (S: structured)
    parsedPayto.params["receiver-name"], // Beneficiary name
    "", // street
    "", // apt. nr.
    "", // postal code
    "", // town
    countryCode, // Country
    // Group: Ultimate Debtor (not used in version 0200)
    "", // Ultimate Debtor Address type (S: structured)
    "", // Ultimate Debtor name
    "", // Ultimate Debtor street
    "", // Ultimate Debtor apt. nr
    "", // Ultimate Debtor postal code
    "", // Ultimate Debtor town
    "", // Ultimate Debtor country
    // Group: Amount
    Amounts.stringifyValue(amountStr, 2), // Amount
    Amounts.currencyOf(amountStr), // Currency
    // Group: Debtor
    "", // Address type (S: structured)
    "", // Debtor name
    "", // Debtor street
    "", // Debtor apt. nr
    "", // Debtor postal code
    "", // Debtor town
    "", // Debtor country
    // Group: Reference
    "NON", // reference type
    "", // Reference
    // Group: Additional Information
    parsedPayto.params["message"], // Unstructured data
    "EPD", // End of payment data
  ];

  return {
    type: "ok",
    qrContent: lines.join("\n"),
  };
}

/**
 * See "Quick Response Code - Guidelines to
 * Enable the Data Capture for the
 * Initiation of a SEPA Credit Transfer".
 */
function encodePaytoAsEpcQr(paytoUri: string): EncodeResult {
  const parsedPayto = parsePaytoUri(paytoUri);
  if (!parsedPayto) {
    throw Error("invalid payto URI");
  }
  if (parsedPayto.targetType !== "iban") {
    return { type: "skip" };
  }
  const amountStr = parsedPayto.params["amount"];
  Amounts.stringifyValue;
  const lines = [
    "BCD", // service tag
    "002", // version
    "1", // character set (1: UTF-8)
    "SCT", // Identification
    "", // optional BIC
    parsedPayto.params["receiver-name"], // Beneficiary name
    parsedPayto.targetPath, // Beneficiary IBAN
    `${Amounts.currencyOf(amountStr)}${Amounts.stringifyValue(amountStr, 2)}`, // Amount
    "", // AT-44 Purpose
    parsedPayto.params["message"], // AT-05 Unstructured remittance information
  ];

  return {
    type: "ok",
    qrContent: lines.join("\n"),
  };
}

/**
 * Specification of a QR code that includes payment information.
 */
export interface QrCodeSpec {
  /**
   * Type of the QR code.
   *
   * Depending on the type, different visual styles
   * might be applied.
   */
  type: string;

  /**
   * Content of the QR code that should be rendered.
   */
  qrContent: string;
}

/**
 * Get applicable QR code specifications for the given payto URI.
 */
export function getQrCodesForPayto(paytoUri: string): QrCodeSpec[] {
  const res: QrCodeSpec[] = [];
  {
    const qr = encodePaytoAsEpcQr(paytoUri);
    if (qr.type == "ok") {
      res.push({
        type: "epc-qr",
        qrContent: qr.qrContent,
      });
    }
  }
  {
    const qr = encodePaytoAsSwissQrBill(paytoUri);
    if (qr.type == "ok") {
      res.push({
        type: "epc-qr",
        qrContent: qr.qrContent,
      });
    }
  }
  return res;
}