Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions packages/payment_qr_codes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@apify/payment_qr_codes",
"version": "0.0.1",
"description": "Tools for creating payment QR codes.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"keywords": [
"apify"
],
"author": {
"name": "Apify",
"email": "support@apify.com",
"url": "https://apify.com"
},
"contributors": [
"Matej Hamas <matej.hamas@apify.com>"
],
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "git+https://github.com/apify/apify-shared-js"
},
"bugs": {
"url": "https://github.com/apify/apify-shared-js/issues"
},
"homepage": "https://apify.com",
"scripts": {
"build": "npm run clean && npm run compile && npm run copy",
"clean": "rimraf ./dist",
"compile": "tsc -p tsconfig.build.json",
"copy": "ts-node -T ../../scripts/copy.ts"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"qrcode": "^1.4.4"
},
"devDependencies": {
"@types/qrcode": "^1.4.1"
}
}
27 changes: 27 additions & 0 deletions packages/payment_qr_codes/src/czk_payment_qr_codes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import qrcode from 'qrcode';

interface CzkQRCodeInputData {
iban: string;
amount: number;
currencyCode: string; // 3 letter currency code
message: string; // e.g. invoice number
beneficiaryName: string;
}
/*
* This function encodes provided data into a single string complying with CZK QR code payment protocol.
* The protocol is issued by Czech Bank Association and can be found
* at https://cbaonline.cz/upload/1645-standard-qr-v1-2-cerven-2021.pdf
*/
export function encodeInputDataToRawQrCodeInputString(data: CzkQRCodeInputData): string {
const { iban, amount, currencyCode, message, beneficiaryName } = data;
return `SPD*1.0*RN:${beneficiaryName}*ACC:${iban}*AM:${amount}*CC:${currencyCode.toUpperCase()}*MSG:${message}`;
}

/**
* This function generates CZK QR code that can be used for domestic QR code payment.
* It returns the QR code encoded as data URL that can be directly viewed in browser or used in <img> tag.
*/
export async function generateCzkPaymentQrCodeDataUrl(data: CzkQRCodeInputData): Promise<string> {
const code = qrcode.create(encodeInputDataToRawQrCodeInputString(data), {});
return qrcode.toDataURL(code.segments);
}
1 change: 1 addition & 0 deletions packages/payment_qr_codes/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './czk_payment_qr_codes';
7 changes: 7 additions & 0 deletions packages/payment_qr_codes/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src/**/*"]
}
4 changes: 4 additions & 0 deletions packages/payment_qr_codes/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*"]
}
3 changes: 3 additions & 0 deletions test/__snapshots__/czk_payment_qr_codes.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CZK payment QR code generation test 1`] = `"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAAC0CAYAAAA9zQYyAAAAAklEQVR4AewaftIAAAeJSURBVO3BQW4ky5LAQDKg+1+Z00tfJZCokub9gJvZP6x1icNaFzmsdZHDWhc5rHWRw1oXOax1kcNaFzmsdZHDWhc5rHWRw1oXOax1kcNaFzmsdZHDWhf54UMqf6niicpUMalMFZPKJyomlaniicpU8UTlmyomlb9U8YnDWhc5rHWRw1oX+eHLKr5J5YnKE5VPVEwqv6niicqTikllqphU3qj4JpVvOqx1kcNaFzmsdZEffpnKGxVvVEwqU8WkMqk8UZkq3qiYVKaKNyomlUnljYpPqLxR8ZsOa13ksNZFDmtd5IfLqUwVk8pfqphUPlExqbyhMlX8LzusdZHDWhc5rHWRH/7HqTypmFSmik+oTBWTyicqnqh8ouImh7UucljrIoe1LvLDL6v4TRVvVE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a6yGGti/wfUZ3njolYwk8AAAAASUVORK5CYII="`;
35 changes: 35 additions & 0 deletions test/czk_payment_qr_codes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
encodeInputDataToRawQrCodeInputString,
generateCzkPaymentQrCodeDataUrl,
} from '@apify/payment_qr_codes';

describe('CZK payment QR code generation', () => {
it('can encode payment data to QR cod estring', () => {
expect(encodeInputDataToRawQrCodeInputString({
iban: 'CZ6508000000192000145399', // testing IBAN from https://www.cnb.cz/cs/platebni-styk/iban/iban-mezinarodni-format-cisla-uctu/
amount: 123.45,
currencyCode: 'czk',
message: 'Test message',
beneficiaryName: 'John Doe',
})).toBe('SPD*1.0*RN:John Doe*ACC:CZ6508000000192000145399*AM:123.45*CC:CZK*MSG:Test message');

expect(encodeInputDataToRawQrCodeInputString({
iban: 'CZ6508000000192000145399', // testing IBAN from https://www.cnb.cz/cs/platebni-styk/iban/iban-mezinarodni-format-cisla-uctu/
amount: 123.45,
currencyCode: 'EUR',
message: 'Test message',
beneficiaryName: 'John Doe',
})).toBe('SPD*1.0*RN:John Doe*ACC:CZ6508000000192000145399*AM:123.45*CC:EUR*MSG:Test message');
});

it('test', async () => {
const code = await generateCzkPaymentQrCodeDataUrl({
iban: 'CZ6508000000192000145399', // testing IBAN from https://www.cnb.cz/cs/platebni-styk/iban/iban-mezinarodni-format-cisla-uctu/
amount: 123.45,
currencyCode: 'czk',
message: 'Test message',
beneficiaryName: 'John Doe',
});
expect(code).toMatchSnapshot();
});
});